Skip to content

Commit

Permalink
moved autoprepare(), autoexecute() and buildManipSql() to the pear wr…
Browse files Browse the repository at this point in the history
…apper, 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
  • Loading branch information
Lukas Smith committed Aug 28, 2002
1 parent 4e2bbe4 commit 0fc6451
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 204 deletions.
102 changes: 0 additions & 102 deletions MDB/Common.php
Expand Up @@ -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()

Expand Down
58 changes: 58 additions & 0 deletions MDB/peardb_wrapper.php
Expand Up @@ -239,6 +239,9 @@ function quoteString($string)

function quote($string)
{
if ($string === NULL) {
return 'NULL';
}
return $this->MDB_object->_quote($string);
}

Expand Down Expand Up @@ -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);
Expand Down
102 changes: 0 additions & 102 deletions common.php
Expand Up @@ -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()

Expand Down
58 changes: 58 additions & 0 deletions pear_wrapper.php
Expand Up @@ -239,6 +239,9 @@ function quoteString($string)

function quote($string)
{
if ($string === NULL) {
return 'NULL';
}
return $this->MDB_object->_quote($string);
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0fc6451

Please sign in to comment.