You are here

function coder_upgrade_locate_query_string in Coder 7.2

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

Returns the query string if found.

Parameters

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

PGPOperand $operand: The query string to be parsed (first parameter to $item).

Return value

string The query string or FALSE.

2 calls to coder_upgrade_locate_query_string()
coder_upgrade_upgrade_call_db_query_alter in coder_upgrade/conversions/db.inc
Implements hook_upgrade_call_db_query_alter().
coder_upgrade_upgrade_call_db_query_range_alter in coder_upgrade/conversions/db.inc
Implements hook_upgrade_call_db_query_range_alter().

File

coder_upgrade/conversions/db.inc, line 129
Provides conversion routines applied to database API function calls and hooks.

Code

function coder_upgrade_locate_query_string(&$node) {

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

  // Get the function call object.
  $item =& $node->data;

  /*
   * Use cases
   * - query is a string: parse directly
   * - query is a variable: find the variable and see if it is a string (assigned only once or always from strings, no expressions)
   * - query is an expression (leave this alone)
   */

  // Find the query string. (TODO Are there any API functions in which the query parameter is not in slot zero?)
  $found = FALSE;
  $p0 = $item
    ->getParameter(0)
    ->stripComments();
  if ($p0
    ->count() == 1) {

    // The parameter is a single item expression.
    //    $operand0 = $p0->getElement();
    if ($p0
      ->isType(T_CONSTANT_ENCAPSED_STRING)) {

      // The parameter is a string containing the query expression.
      $found = TRUE;
      return $p0
        ->toString();
    }
    elseif ($p0
      ->isType(T_VARIABLE)) {

      // The parameter is a variable containing the query expression.
      cdp('inside T_VARIABLE');

      //      $operand0 = $p0->getElement();
      // Parameter is a variable.
      $variable = $p0
        ->getElement()
        ->findNode('value');

      // $variable = $operand0->findNode('value');
      cdp($variable, '$variable');

      // Get the parent = statement (i.e. node) this function call is part of.
      //      $parent = $item->parent; // $parent = &$item->parent;
      // Find the assignment in the statement list the parent is part of.
      $statement = $item
        ->searchBackward('PGPAssignment', 'values', 0, $variable);

      // $statement = $parent->container->searchBackward('PGPAssignment', 'values', 0, $variable, $parent);
      cdp($statement, '$statement');
      if ($statement && $statement->values
        ->getElement()
        ->countType('operand') == 2) {
        cdp('inside $statement->values->countType == 2');

        // This assignment could look like $foo = 'bar'.
        $operand2 =& $statement->values
          ->getElement()
          ->findNode('operand', 'backward');

        // TODO A pattern here - this is the same code as above but executed on a different object.
        if (is_array($operand2) && $operand2['type'] == T_CONSTANT_ENCAPSED_STRING) {

          // The parameter variable is assigned a string containing the query expression.
          $found = TRUE;
          return $operand2['value'];
        }
      }
    }
  }
  clp("ERROR: Could not find a SQL string to parse in " . __FUNCTION__);
  $item
    ->insertStatementBefore($editor
    ->commentToStatement('// TODO Please convert this statement to the D7 database API syntax.'));
  return FALSE;
}