You are here

function DB_common::buildManipSQL in Flickr API 5

Produces an SQL query string for autoPrepare()

Example: <pre> buildManipSQL('table_sql', array('field1', 'field2', 'field3'), DB_AUTOQUERY_INSERT); </pre>

That returns <samp> INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?) </samp>

NOTES:

  • This belongs more to a SQL Builder class, but this is a simple facility.
  • Be carefull! If you don't give a $where param with an UPDATE query, all the records of the table will be updated!

Parameters

string $table the table name:

array $table_fields the array of field names:

int $mode a type of query to make:: DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE

string $where for update queries: the WHERE clause to: append to the SQL statement. Don't include the "WHERE" keyword.

Return value

string the sql query for autoPrepare()

1 call to DB_common::buildManipSQL()
DB_common::autoPrepare in phpFlickr/PEAR/DB/common.php
Automaticaly generates an insert or update query and pass it to prepare()

File

phpFlickr/PEAR/DB/common.php, line 878

Class

DB_common
DB_common is the base class from which each database driver class extends

Code

function buildManipSQL($table, $table_fields, $mode, $where = false) {
  if (count($table_fields) == 0) {
    return $this
      ->raiseError(DB_ERROR_NEED_MORE_DATA);
  }
  $first = true;
  switch ($mode) {
    case DB_AUTOQUERY_INSERT:
      $values = '';
      $names = '';
      foreach ($table_fields as $value) {
        if ($first) {
          $first = false;
        }
        else {
          $names .= ',';
          $values .= ',';
        }
        $names .= $value;
        $values .= '?';
      }
      return "INSERT INTO {$table} ({$names}) VALUES ({$values})";
    case DB_AUTOQUERY_UPDATE:
      $set = '';
      foreach ($table_fields as $value) {
        if ($first) {
          $first = false;
        }
        else {
          $set .= ',';
        }
        $set .= "{$value} = ?";
      }
      $sql = "UPDATE {$table} SET {$set}";
      if ($where) {
        $sql .= " WHERE {$where}";
      }
      return $sql;
    default:
      return $this
        ->raiseError(DB_ERROR_SYNTAX);
  }
}