function coder_upgrade_parse_update_query_string in Coder 7
Same name and namespace in other branches
- 7.2 coder_upgrade/conversions/db.inc \coder_upgrade_parse_update_query_string()
 
Replaces D6 database API call with D7 equivalent.
@todo Fill in this with unhandled items.
Parameters
string $sql: The SQL query string to parse.
array $values: List of replacement values passed to db_query().
1 call to coder_upgrade_parse_update_query_string()
- coder_upgrade_parse_query_string in coder_upgrade/
conversions/ db.inc  - Replaces D6 database API call with D7 equivalent.
 
File
- coder_upgrade/
conversions/ db.inc, line 407  - Provides conversion routines applied to database API function calls and hooks.
 
Code
function coder_upgrade_parse_update_query_string($sql, $values) {
  cdp(__FUNCTION__);
  global $_coder_upgrade_replacement_values_is_array;
  // Regex needs to handle multi-line strings. The parsing and exploding of
  // fields and conditions fails on this.
  // Also, if there are multiple values to be replaced and only a single value
  // as parameter to drupal_query, then assume it is an array and apply it in
  // order by index to the substitutions.
  // db_update('quotes_blocks')->fields(array('name' => $vals[0], 'block_type' => $vals[1], ...)
  $new = array();
  // Look for query with both SET and WHERE clauses.
  // UPDATE {node} SET title='%s', status=%d WHERE uid=%d
  $find = '@^UPDATE\\s+{(\\w+)}\\s+SET\\s+(.*?)\\s+WHERE\\s+(.*?)$@s';
  // TODO Check for WHERE and use a different regex.
  preg_match($find, $sql, $matches);
  cdp($matches, '$matches');
  if (empty($matches)) {
    // Look for query with only a SET clause.
    $find = '@^UPDATE\\s+{(\\w+)}\\s+SET\\s+(.*?)$@s';
    preg_match($find, $sql, $matches);
    cdp($matches, '$matches');
    if (empty($matches)) {
      cdp($sql, '$sql');
      clp('ERROR: could not parse sql UPDATE statement.');
      return array(
        'NULL',
      );
      // $new;
    }
  }
  // Parse fields to be updated.
  // TODO This will fail on a literal string with a comma.
  $fields = preg_split("@,\\s*@", $matches[2]);
  // $fields = explode(', ', $matches[2]);
  cdp($fields, '$fields');
  $condition_count = isset($matches[3]) && $matches[3] ? coder_upgrade_count_sql_conditions($matches[3]) : 0;
  $replacement_count = coder_upgrade_count_sql_fields($fields) + $condition_count;
  cdp($replacement_count, '$replacement_count');
  if (count($values) != $replacement_count) {
    clp('ERROR: number of replacement values to db_query does not equal number of items needing replacement.');
    clp($sql);
  }
  $_coder_upgrade_replacement_values_is_array = count($values) == 1 && $replacement_count > 1;
  // Build DBTNG syntax string.
  $new[] = "db_update('{$matches[1]}')";
  $new[] = "\t->fields(array(";
  foreach ($fields as $field) {
    list($field, $value) = explode("=", $field);
    // Value is a placeholder
    if (preg_match('/%[sdbf]/', $value)) {
      $value = coder_upgrade_next_replacement_value($values);
    }
    // Trim the field for whitespace.
    $field = trim($field);
    $new[] = "\t\t'{$field}' => {$value},";
  }
  $new[] = "\t))";
  if (isset($matches[3]) && $matches[3]) {
    $new[] = coder_upgrade_parse_sql_conditions($matches[3], $values);
  }
  $new[] = "\t->execute();";
  cdp($new, '$new');
  return $new;
}