? DB.diff Index: DB.php =================================================================== RCS file: /repository/pear/DB/DB.php,v retrieving revision 1.49 diff -u -r1.49 DB.php --- DB.php 6 Feb 2004 17:29:43 -0000 1.49 +++ DB.php 7 Feb 2004 18:17:07 -0000 @@ -167,7 +167,7 @@ */ define('DB_AUTOQUERY_INSERT', 1); define('DB_AUTOQUERY_UPDATE', 2); - +define('DB_AUTOQUERY_SELECT', 3); // }}} // {{{ portability modes Index: DB/common.php =================================================================== RCS file: /repository/pear/DB/DB/common.php,v retrieving revision 1.80 diff -u -r1.80 common.php --- DB/common.php 6 Feb 2004 04:23:17 -0000 1.80 +++ DB/common.php 7 Feb 2004 18:17:10 -0000 @@ -879,6 +879,75 @@ } // }}} + // {{{ buildSelectSQL() + + /** + * Make automatically an sql query + * + * Example : buildSelectSQL('table_sql', array('field1', 'field2', 'field3')) + * will return the string : SELECT field1,field2,field3 FROM table_sql + * NB : - This belongs more to a SQL Builder class, but this is a simple facility + * + * @param string $table name of the table + * @param array $table_fields ordered array containing the fields names + * @param string $where in case of update queries, this string will be put after the sql WHERE statement + * @return string sql SELECT query for query(), getAll(), getAssoc(), etc. + * @access public + */ + function buildSelectSQL($table, $table_fields, $where = false) + { + if (count($table_fields) == 0) { + $this->raiseError(DB_ERROR_NEED_MORE_DATA); + } + $first = true; + $names = ''; + foreach ($table_fields as $value) { + if ($first) { + $first = false; + } else { + $names .= ','; + } + $names .= $value; + } + $sql = "SELECT $names FROM $table"; + if ($where) { + $sql .= " WHERE $where"; + } + return $sql; + } + + // }}} + // {{{ buildSQL() + + /** + * Make automatically an sql query + * + * Example : buildSQL('table_sql', array('field1', 'field2', 'field3'), DB_AUTOQUERY_SELECT) + * will return the string : SELECT field1,field2,field3 FROM table_sql + * NB : - This belongs more to a SQL Builder class, but this is a simple facility + * + * @param string $table name of the table + * @param array $table_fields ordered array containing the fields names + * @param int $mode type of query to make (DB_AUTOQUERY_SELECT, DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE) + * @param string $where in case of update queries, this string will be put after the sql WHERE statement + * @return string sql query for query() or for prepare() + * @see DB_common::buildSelectSQL(), DB_common::buildManipSQL() + * @access public + */ + function buildSQL($table, $table_fields, $mode, $where = false) + { + switch ($mode) { + case DB_AUTOQUERY_SELECT: + return $this->buildSelectSQL($table, $table_fields, $where); + case DB_AUTOQUERY_INSERT: + case DB_AUTOQUERY_UPDATE: + return $this->buildManipSQL($table, $table_fields, $mode, $where); + default: + $this->raiseError(DB_ERROR_SYNTAX); + } + } + + // }}} // {{{ execute() /**