diff -up -r coder_cvs/coder_upgrade//conversions/coder_upgrade.db.inc coder/coder_upgrade//conversions/coder_upgrade.db.inc
--- coder_cvs/coder_upgrade//conversions/coder_upgrade.db.inc	2010-06-30 13:31:39.745987149 -0500
+++ coder/coder_upgrade//conversions/coder_upgrade.db.inc	2010-06-30 16:30:50.786718408 -0500
@@ -18,6 +18,7 @@
  *
  * Database
  * http://drupal.org/node/224333#dbtng (NOT FINISHED)
+ * http://drupal.org/node/224333#db_rewrite_sql (NOT FINISHED)
  */
 
 /**
@@ -25,11 +26,12 @@
  *
  * @param PGPFunctionCall $item
  */
-function coder_upgrade_upgrade_call_db_query_alter(&$item, &$reader) {
+function coder_upgrade_upgrade_call_db_query_alter(&$item, &$reader) { // NEEDS WORK
   // Disable this routine until it is improved.
+//  return;
 
   // Create helper objects.
-  $editor = PGPEditor::getInstance();
+//  $editor = PGPEditor::getInstance();
 
   /*
    * Use cases
@@ -39,24 +41,61 @@ function coder_upgrade_upgrade_call_db_q
    */
 
   // Process function call.
-  $name = &$item->name; // DONE (NEEDS WORK)
   $p0 = $item->getParameter(0);
   if ($p0->count() == 1) {
     $operand0 = $p0->getElement();
     if (is_array($operand0) && $operand0['type'] == T_CONSTANT_ENCAPSED_STRING) {
       // Convert values to strings.
       // Parse the string.
-      coder_upgrade_parse_query_string($item, $operand0, $reader);
+      coder_upgrade_parse_query_string($item, $operand0);
     }
   }
 }
 
 /**
- * Implements hook_upgrade_call_db_query_alter().
+ * Implements hook_upgrade_call_db_rewrite_sql_alter().
+ *
+ * @todo db_rewrite_sql() replaced with hook_query_alter()
+ * @see http://drupal.org/node/224333#db_rewrite_sql
+ *
+ * @param PGPFunctionCall $item
+ */
+function coder_upgrade_upgrade_call_db_rewrite_sql_alter(&$item, &$reader) {
+  // Create helper objects.
+//  $editor = PGPEditor::getInstance();
+
+  /*
+   * Use cases
+   * - query is a string: parse directly
+   * - query is a variable; find the variable and see if it is a string
+   * - query is an expression
+   */
+
+  // Process function call.
+  $name = &$item->name; // NOT DONE - Copied from db_query above
+  $p0 = $item->getParameter(0);
+  if ($p0->count() == 1) {
+    $operand0 = $p0->getElement();
+    if (is_array($operand0) && $operand0['type'] == T_CONSTANT_ENCAPSED_STRING) {
+      // Convert values to strings.
+      // Parse the string.
+      coder_upgrade_parse_query_string($item, $operand0);
+      // Add $query->addTag('node_access' /* TODO Please indicate the appropriate tag */);
+    }
+  }
+}
+
+/**
+ * Replaces D6 database API call with D7 equivalent.
+ *
+ * @todo Fill in this with unhandled items.
  *
  * @param PGPFunctionCall $item
+ *   The function call object to be replaced.
+ * @param PGPOperand $operand
+ *   The query string to be parsed (first parameter to $item).
  */
-function coder_upgrade_parse_query_string(&$item, &$operand, &$reader) {
+function coder_upgrade_parse_query_string(&$item, &$operand) {
   // Create helper objects.
   $editor = PGPEditor::getInstance();
 
@@ -64,10 +103,11 @@ function coder_upgrade_parse_query_strin
   // TODO The parameter could be an array variable or expression
   // See http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test?revision=1.80&view=markup
   $values = array();
-  for ($i = 1; $i < $item->parameters->count(); $i++) {
-    $values[] = $item->printParameter($i);
+  for ($i = 1; $i < $item->parameterCount(); $i++) {
+    $item->setParameter($i, $item->getParameter($i)->stripComments());
+    $values[] = $item->printParameter($i); // $values[] = $item->getParameter($i)->stripComments()->toString(); //
   }
-  cdp($values);
+  cdp($values, '$values');
 
   /*
    * TODO Run the table names through a conversion routine.
@@ -77,41 +117,45 @@ function coder_upgrade_parse_query_strin
   // TODO trim won't work here, since it'll trim off the last ' if the string ends in '%s'
   $sql = substr($operand['value'], 1, -1);
   if (strpos($sql, 'SELECT') === 0) {
-    $find = '@^(SELECT.*?WHERE\s+)(.*?)$@m';
+    $find = '@^(SELECT.*?WHERE\s+)(.*?)$@ms';
     preg_match($find, $sql, $matches);
-    cdp($matches);
+    cdp($matches, '$matches');
     // Convert conditions from field = '%s' to field = :field
     // TODO handle more complex conditions
     if ($matches[2]) {
       preg_match_all('/
-        (\S*)\s+([!=<>]+)\s+[\'"]?%[sdfb][\'"]? # Match field = %d
+        (\S*)\s*([!=<>]+)\s*[\'"]?%[sdfb][\'"]? # Match field = %d
         |
-        (\S*)\s+IN\s?\([\'"]%s[\'"]?\) # Match field IN(%s)
+        (\S*)\s*([!=<>]+)\s*([\'"].*?[\'"]|\S*) # Match field literals
         |
-        (\S*)\s+([!=<>]+)\s+(\S*) # Match field literals
-      /x', $matches[2], $conditions); 
+        (\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) {
-        // field = %d condition
         if ($conditions[1][$key]) {
+          // field = %d condition
           $condition_keys[] = ':'. $conditions[1][$key];
           $operand['value'] = str_replace($condition, $conditions[1][$key] . ' ' . $conditions[2][$key] . ' :' . $conditions[1][$key], $operand['value']);
         }
-        // filed IN (%s) condition
         else if ($conditions[3][$key]) {
+          // field = literal condition
           $condition_keys[] = ':'. $conditions[3][$key];
-          $operand['value'] = str_replace($condition, $conditions[3][$key] . ' IN (:' . $conditions[3][$key] . ')', $operand['value']);
+          $operand['value'] = str_replace($condition, $conditions[3][$key] . ' ' . $conditions[4][$key] . ' :' . $conditions[3][$key], $operand['value']);
+          $item->insertParameter($key + 1, $editor->expressionToStatement($conditions[5][$key]));
+        }
+        else if ($conditions[6][$key]) {
+          // filed IN (%s) condition
+          $condition_keys[] = ':'. $conditions[6][$key];
+          $operand['value'] = str_replace($condition, $conditions[6][$key] . ' IN (:' . $conditions[6][$key] . ')', $operand['value']);
           // D6 convention was to use implode(",", $array).  Now we can just pass in the array.
-          $param = $item->getParameter($key + 1)->getElement()->getParameter(1);  
+          $param = $item->getParameter($key + 1)->getElement()->getParameter(1);
           $item->setParameter($key + 1, $param);
         }
-        else if ($conditions[4][$key]) {
-          $condition_keys[] = ':'. $conditions[4][$key];
-          $operand['value'] = str_replace($condition, $conditions[4][$key] . ' ' . $conditions[5][$key] . ' :' . $conditions[4][$key], $operand['value']);
-          $item->insertParameter($key + 1, $editor->expressionToStatement($conditions[6][$key]));
-        }
       }
       $editor->setParameter($item, 0, $operand['value']);
       $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);
@@ -130,7 +174,7 @@ function coder_upgrade_parse_query_strin
     $new[] = "\$id = db_insert('{$matches[1]}')";
     $new[] = "\t->fields(array(";
     foreach ($fields as $field => $value) {
-      
+
       // Value is a placeholder
       if (preg_match('/%[sdbf]/', $value)) {
         $value = array_shift($values);
@@ -188,44 +232,17 @@ function coder_upgrade_parse_query_strin
   $container = &$parent->container;
   // Insert a statement.
   $statement = $editor->textToStatements($new)->getElement(0);
-  $container->insertAfter($parent, $statement, 'function_call');
-}
-
-/**
- * Implements hook_upgrade_call_db_rewrite_sql_alter().
- *
- * @param PGPFunctionCall $item
- */
-function coder_upgrade_upgrade_call_db_rewrite_sql_alter(&$item, &$reader) {
-  // Create helper objects.
-  $editor = PGPEditor::getInstance();
-
-  /*
-   * Use cases
-   * - query is a string: parse directly
-   * - query is a variable; find the variable and see if it is a string
-   * - query is an expression
-   */
-
-  // Process function call.
-  $name = &$item->name; // NOT DONE - Copied from db_query above
-  $p0 = $item->getParameter(0);
-  if ($p0->count() == 1) {
-    $operand0 = $p0->getElement();
-    if (is_array($operand0) && $operand0['type'] == T_CONSTANT_ENCAPSED_STRING) {
-      // Convert values to strings.
-      // Parse the string.
-      coder_upgrade_parse_query_string($item, $operand0, $reader);
-      // Add $query->addTag('node_access' /* TODO Please indicate the appropriate tag */);
-    }
-  }
+  $container->insertAfter($parent, $statement, 'function_call'); // TODO Have not used 'function_call' as type
 }
 
 /**
- * Parse sql conditions into conditional object strings.
+ * Parses sql conditions into conditional object strings.
  *
  * @param string $conditions
  * @param array $values
+ *
+ * @return string
+ *   String of conditions in DBTNG syntax.
  */
 function coder_upgrade_parse_sql_conditions($conditions, $values) {
   // Check for inner conditionals or function calls.  For now, we'll just use
