You are here

function coder_upgrade_parse_sql_conditions in Coder 7

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

Parses sql conditions into conditional object strings.

Parameters

string $string: String of sql conditions.

array $values:

Return value

string String of conditions in DBTNG syntax.

2 calls to coder_upgrade_parse_sql_conditions()
coder_upgrade_parse_delete_query_string in coder_upgrade/conversions/db.inc
Replaces D6 database API call with D7 equivalent.
coder_upgrade_parse_update_query_string in coder_upgrade/conversions/db.inc
Replaces D6 database API call with D7 equivalent.

File

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

Code

function coder_upgrade_parse_sql_conditions($string, $values) {

  // Check for inner conditionals or function calls.  For now, we'll just use
  // where() instead of conditionals, given the complexity of inner conditionals.
  if (preg_match("/\\((.*)\\)/", $string, $matches)) {
    return "\t\t->where('{$string}', " . var_dump($values) . ")";
  }

  // TODO Handle other conjunctions besides AND.
  $conditions = preg_split('/AND|OR|XOR|&&|\\|\\|/', $string);
  if (!is_array($conditions)) {
    $conditions = array(
      $string,
    );
  }
  foreach ($conditions as $condition) {
    list($condition, $operator, $value) = preg_split('/([!=><]+|LIKE)/', $condition, -1, PREG_SPLIT_DELIM_CAPTURE);
    if (preg_match('/%[sbdf]/', trim($value))) {
      $value = coder_upgrade_next_replacement_value($values);
    }

    // Trim the condition for whitespace.
    $condition = trim($condition);
    $value = trim($value);

    // TODO handle IN and BETWEEN conditions
    // TODO Handle function conditions such as LIKE, MIN, SUM
    switch ($operator) {
      case '=':
        $parsed_conditions[] = "\t->condition('{$condition}', {$value})";
        break;
      default:
        $parsed_conditions[] = "\t->condition('{$condition}', {$value}, '{$operator}')";
        break;
    }
  }
  return implode("\n", $parsed_conditions);
}