From 0fc6451b341dfe8ea278f1a4813870e0bd1a20fb Mon Sep 17 00:00:00 2001 From: Lukas Smith Date: Wed, 28 Aug 2002 13:15:12 +0000 Subject: [PATCH] moved autoprepare(), autoexecute() and buildManipSql() to the pear wrapper, because I dont think that they belong in the MDB core git-svn-id: http://svn.php.net/repository/pear/packages/MDB/trunk@93929 c90b9560-bf6c-de11-be94-00142212c4b1 --- MDB/Common.php | 102 ----------------------------------------- MDB/peardb_wrapper.php | 58 +++++++++++++++++++++++ common.php | 102 ----------------------------------------- pear_wrapper.php | 58 +++++++++++++++++++++++ 4 files changed, 116 insertions(+), 204 deletions(-) diff --git a/MDB/Common.php b/MDB/Common.php index ec214ff..6406982 100644 --- a/MDB/Common.php +++ b/MDB/Common.php @@ -1878,108 +1878,6 @@ function executeMultiple($prepared_query, $types = NULL, $params, $param_types = return (DB_OK); } - // }}} - // {{{ autoPrepare() - - /** - * Make automaticaly an insert or update query and call prepare() with it - * - * @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_INSERT or DB_AUTOQUERY_UPDATE) - * @param string $where in case of update queries, this string will be put after the sql WHERE statement - * @return resource handle for the query - * @access public - * @see buildManipSQL - */ - function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT, $where = FALSE) - { - $query = $this->buildManipSQL($table, $table_fields, $mode, $where); - return $this->prepare($query); - } - // {{{ - // }}} autoExecute() - /** - * Make automaticaly an insert or update query and call prepare() and execute() with it - * - * @param string $table name of the table - * @param array $fields_values assoc ($key=>$value) where $key is a field name and $value its value - * @param int $mode type of query to make (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 mixed a new DB_Result or a DB_Error when fail - * @access public - * @see buildManipSQL - * @see autoPrepare - */ - function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, $where = FALSE) - { - $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, $where); - return $this->execute($sth, array_values($fields_values)); - } - // {{{ - // }}} buildManipSQL() - /** - * Make automaticaly an sql query for prepare() - * - * Example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), - * DB_AUTOQUERY_INSERT) - * will return the string : - * INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?) - * NB : - 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 ! - * - * @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_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 prepare() - * @access public - */ - function buildManipSQL($table, $table_fields, $mode, $where = FALSE) - { - if (count($table_fields) == 0) { - $this->raiseError(DB_ERROR_NEED_MORE_DATA); - } - $first = TRUE; - switch ($mode) { - case DB_AUTOQUERY_INSERT: - $values = ''; - $names = ''; - while (list(, $value) = each($table_fields)) { - if ($first) { - $first = FALSE; - } else { - $names .= ','; - $values .= ','; - } - $names .= $value; - $values .= '?'; - } - return "INSERT INTO $table ($names) VALUES ($values)"; - break; - case DB_AUTOQUERY_UPDATE: - $set = ''; - while (list(, $value) = each($table_fields)) { - if ($first) { - $first = FALSE; - } else { - $set .= ','; - } - $set .= "$value = ?"; - } - $sql = "UPDATE $table SET $set"; - if ($where) { - $sql .= " WHERE $where"; - } - return $sql; - break; - default: - $this->raiseError(DB_ERROR_SYNTAX); - } - } - // }}} // {{{ setParam() diff --git a/MDB/peardb_wrapper.php b/MDB/peardb_wrapper.php index dd84ee5..20b18e2 100644 --- a/MDB/peardb_wrapper.php +++ b/MDB/peardb_wrapper.php @@ -239,6 +239,9 @@ function quoteString($string) function quote($string) { + if ($string === NULL) { + return 'NULL'; + } return $this->MDB_object->_quote($string); } @@ -283,6 +286,61 @@ function prepare($query) return $this->MDB_object->prepareQuery($query); } + function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT, $where = false) + { + $query = $this->buildManipSQL($table, $table_fields, $mode, $where); + return $this->prepare($query); + } + + function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, $where = false) + { + $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, $where); + return $this->execute($sth, array_values($fields_values)); + } + + function buildManipSQL($table, $table_fields, $mode, $where = false) + { + if (count($table_fields) == 0) { + $this->raiseError(DB_ERROR_NEED_MORE_DATA); + } + $first = true; + switch ($mode) { + case DB_AUTOQUERY_INSERT: + $values = ''; + $names = ''; + while (list(, $value) = each($table_fields)) { + if ($first) { + $first = false; + } else { + $names .= ','; + $values .= ','; + } + $names .= $value; + $values .= '?'; + } + return "INSERT INTO $table ($names) VALUES ($values)"; + break; + case DB_AUTOQUERY_UPDATE: + $set = ''; + while (list(, $value) = each($table_fields)) { + if ($first) { + $first = false; + } else { + $set .= ','; + } + $set .= "$value = ?"; + } + $sql = "UPDATE $table SET $set"; + if ($where) { + $sql .= " WHERE $where"; + } + return $sql; + break; + default: + $this->raiseError(DB_ERROR_SYNTAX); + } + } + function execute($stmt, $data = FALSE) { $result = $this->MDB_object->execute($stmt, NULL, $data); diff --git a/common.php b/common.php index ec214ff..6406982 100644 --- a/common.php +++ b/common.php @@ -1878,108 +1878,6 @@ function executeMultiple($prepared_query, $types = NULL, $params, $param_types = return (DB_OK); } - // }}} - // {{{ autoPrepare() - - /** - * Make automaticaly an insert or update query and call prepare() with it - * - * @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_INSERT or DB_AUTOQUERY_UPDATE) - * @param string $where in case of update queries, this string will be put after the sql WHERE statement - * @return resource handle for the query - * @access public - * @see buildManipSQL - */ - function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT, $where = FALSE) - { - $query = $this->buildManipSQL($table, $table_fields, $mode, $where); - return $this->prepare($query); - } - // {{{ - // }}} autoExecute() - /** - * Make automaticaly an insert or update query and call prepare() and execute() with it - * - * @param string $table name of the table - * @param array $fields_values assoc ($key=>$value) where $key is a field name and $value its value - * @param int $mode type of query to make (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 mixed a new DB_Result or a DB_Error when fail - * @access public - * @see buildManipSQL - * @see autoPrepare - */ - function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, $where = FALSE) - { - $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, $where); - return $this->execute($sth, array_values($fields_values)); - } - // {{{ - // }}} buildManipSQL() - /** - * Make automaticaly an sql query for prepare() - * - * Example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), - * DB_AUTOQUERY_INSERT) - * will return the string : - * INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?) - * NB : - 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 ! - * - * @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_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 prepare() - * @access public - */ - function buildManipSQL($table, $table_fields, $mode, $where = FALSE) - { - if (count($table_fields) == 0) { - $this->raiseError(DB_ERROR_NEED_MORE_DATA); - } - $first = TRUE; - switch ($mode) { - case DB_AUTOQUERY_INSERT: - $values = ''; - $names = ''; - while (list(, $value) = each($table_fields)) { - if ($first) { - $first = FALSE; - } else { - $names .= ','; - $values .= ','; - } - $names .= $value; - $values .= '?'; - } - return "INSERT INTO $table ($names) VALUES ($values)"; - break; - case DB_AUTOQUERY_UPDATE: - $set = ''; - while (list(, $value) = each($table_fields)) { - if ($first) { - $first = FALSE; - } else { - $set .= ','; - } - $set .= "$value = ?"; - } - $sql = "UPDATE $table SET $set"; - if ($where) { - $sql .= " WHERE $where"; - } - return $sql; - break; - default: - $this->raiseError(DB_ERROR_SYNTAX); - } - } - // }}} // {{{ setParam() diff --git a/pear_wrapper.php b/pear_wrapper.php index dd84ee5..20b18e2 100644 --- a/pear_wrapper.php +++ b/pear_wrapper.php @@ -239,6 +239,9 @@ function quoteString($string) function quote($string) { + if ($string === NULL) { + return 'NULL'; + } return $this->MDB_object->_quote($string); } @@ -283,6 +286,61 @@ function prepare($query) return $this->MDB_object->prepareQuery($query); } + function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT, $where = false) + { + $query = $this->buildManipSQL($table, $table_fields, $mode, $where); + return $this->prepare($query); + } + + function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, $where = false) + { + $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, $where); + return $this->execute($sth, array_values($fields_values)); + } + + function buildManipSQL($table, $table_fields, $mode, $where = false) + { + if (count($table_fields) == 0) { + $this->raiseError(DB_ERROR_NEED_MORE_DATA); + } + $first = true; + switch ($mode) { + case DB_AUTOQUERY_INSERT: + $values = ''; + $names = ''; + while (list(, $value) = each($table_fields)) { + if ($first) { + $first = false; + } else { + $names .= ','; + $values .= ','; + } + $names .= $value; + $values .= '?'; + } + return "INSERT INTO $table ($names) VALUES ($values)"; + break; + case DB_AUTOQUERY_UPDATE: + $set = ''; + while (list(, $value) = each($table_fields)) { + if ($first) { + $first = false; + } else { + $set .= ','; + } + $set .= "$value = ?"; + } + $sql = "UPDATE $table SET $set"; + if ($where) { + $sql .= " WHERE $where"; + } + return $sql; + break; + default: + $this->raiseError(DB_ERROR_SYNTAX); + } + } + function execute($stmt, $data = FALSE) { $result = $this->MDB_object->execute($stmt, NULL, $data);