You are here

function coder_upgrade_parse_select_query_string in Coder 7.2

Same name and namespace in other branches
  1. 7 coder_upgrade/conversions/db.inc \coder_upgrade_parse_select_query_string()

Replaces D6 database API call with D7 equivalent.

@todo Fill in this with unhandled items.

Parameters

PGPNode $node: The node containing the function call object to be replaced.

string $sql: The SQL query string to parse.

array $values: List of replacement values passed to db_query().

1 call to coder_upgrade_parse_select_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 271
Provides conversion routines applied to database API function calls and hooks.

Code

function coder_upgrade_parse_select_query_string($node, $sql, $values, $delimiter) {

  // Create helper objects.
  $editor = PGPEditor::getInstance();

  // Get the function call object.
  $item =& $node->data;
  $find = '@^(SELECT.*?WHERE\\s+)(.*?)$@s';
  preg_match($find, $sql, $matches);
  cdp($matches, '$matches');
  if (empty($matches)) {
    cdp($sql, '$sql');
    clp('ERROR: could not parse sql SELECT statement.');
    return array(
      'NULL',
    );

    // $new;
  }

  // Convert conditions to use named placeholders (i.e., from field = '%s' to
  // field = :field).
  // TODO handle more complex conditions
  //  if (isset($matches[2]) && $matches[2]) {
  preg_match_all('/
      (\\S*)\\s*([!=<>]+)\\s*[\'"]?%[sdfb][\'"]? # Match field = %d
      |
      (\\S*)\\s*([!=<>]+)\\s*([\'"].*?[\'"]|\\S*) # Match field literals
      |
      (\\S*)\\s+IN\\s?\\([\'"]%s[\'"]?\\) # Match field IN(%s)
    /x', $matches[2], $conditions);
  cdp($conditions, '$conditions');
  $condition_keys = array();
  foreach ($conditions[0] as $key => $condition) {
    if ($conditions[1][$key]) {

      // field = %d condition
      $condition_keys[] = ':' . $conditions[1][$key];
      $sql = str_replace($condition, $conditions[1][$key] . ' ' . $conditions[2][$key] . ' :' . $conditions[1][$key], $sql);
    }
    elseif ($conditions[3][$key]) {

      // field = literal condition
      $condition_keys[] = ':' . $conditions[3][$key];
      $sql = str_replace($condition, $conditions[3][$key] . ' ' . $conditions[4][$key] . ' :' . $conditions[3][$key], $sql);
      $item
        ->insertParameter($key + 1, $editor
        ->expressionToStatement($conditions[5][$key]));
    }
    elseif ($conditions[6][$key]) {

      // field IN (%s) condition
      $condition_keys[] = ':' . $conditions[6][$key];
      $sql = str_replace($condition, $conditions[6][$key] . ' IN (:' . $conditions[6][$key] . ')', $sql);

      // D6 convention was to use implode(",", $array).  Now we can just pass in the array.
      $param = $item
        ->getParameter($key + 1)
        ->getElement()
        ->getParameter(1);
      $item
        ->setParameter($key + 1, $param);
    }
  }

  // Replace the $sql parameter.
  $editor
    ->setParameter($item, 0, $delimiter . $sql . $delimiter);
  if (empty($condition_keys)) {

    // Parsing of conditions failed.
    clp("ERROR: Could not parse the conditions to select query '{$sql}' in " . __FUNCTION__);
    $item
      ->insertStatementBefore($editor
      ->commentToStatement('// TODO Please convert this statement to the D7 database API syntax.'));
    return;
  }

  // Arrayitize the values array.
  $string = $editor
    ->arrayitize($item, 1, $condition_keys, array_fill(0, count($condition_keys), "'XXX_YYY'"));
  cdp($string, '$string');
  $temp = $editor
    ->expressionToStatement($string);
  $temp
    ->getElement(0)->multiline = 0;
  $item
    ->setParameter(1, $temp);

  //  }
}