cvs: pear /DB/DB common.php ibase.php mssql.php mysql.php mysql4.php oci8.php odbc.php pgsql.php sybase.php
| From: | Daniel Convissor | Date: | Wed, 31 Dec 2003 15:58:44 +0000 |
| Subject: | cvs: pear /DB/DB common.php ibase.php mssql.php mysql.php mysql4.php oci8.php odbc.php pgsql.php sybase.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-15238@lists.php.net to get a copy of this message | ||
danielc Wed Dec 31 10:58:44 2003 EDT
Modified files:
/pear/DB/DB common.php ibase.php mssql.php mysql.php mysql4.php
oci8.php odbc.php pgsql.php sybase.php
Log:
Improve DBMS specific quote() methods.
Index: pear/DB/DB/common.php diff -u pear/DB/DB/common.php:1.32 pear/DB/DB/common.php:1.33 --- pear/DB/DB/common.php:1.32 Wed Dec 31 00:05:51 2003 +++ pear/DB/DB/common.php Wed Dec 31 10:58:44 2003 @@ -17,7 +17,7 @@ // | Tomas V.V.Cox <cox@idecnet.com> | // +----------------------------------------------------------------------+ // -// $Id: common.php,v 1.32 2003/12/31 05:05:51 danielc Exp $ +// $Id: common.php,v 1.33 2003/12/31 15:58:44 danielc Exp $ // // Base class for DB implementations. // @@ -29,7 +29,7 @@ * inherited by all such. * * @package DB - * @version $Id: common.php,v 1.32 2003/12/31 05:05:51 danielc Exp $ + * @version $Id: common.php,v 1.33 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Stig Bakken <ssb@php.net> * @author Tomas V.V.Cox <cox@idecnet.com> @@ -170,9 +170,14 @@ } /** - * Quotes a string so it can be safely used in a query. It will return - * the string with single quotes around. Other backend quote styles - * should override this method. + * Quotes a string so it can be safely used in a query. + * + * It will return the string with single quotes around. + * + * This method gets overridden if the DBMS being used has a quote() + * method in its sub-class. Those overriding methods contain notes + * about which data types should be used to handle BOOLEAN data in + * each DBMS. * * @param string $string the input string to quote * Index: pear/DB/DB/ibase.php diff -u pear/DB/DB/ibase.php:1.18 pear/DB/DB/ibase.php:1.19 --- pear/DB/DB/ibase.php:1.18 Wed Dec 31 00:05:51 2003 +++ pear/DB/DB/ibase.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: ibase.php,v 1.18 2003/12/31 05:05:51 danielc Exp $ +// $Id: ibase.php,v 1.19 2003/12/31 15:58:44 danielc Exp $ // // Bugs: @@ -29,7 +29,7 @@ * extension. * * @package DB - * @version $Id: ibase.php,v 1.18 2003/12/31 05:05:51 danielc Exp $ + * @version $Id: ibase.php,v 1.19 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Sterling Hughes <sterling@php.net> */ @@ -261,6 +261,37 @@ } // }}} + // {{{ quote() + + /** + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = the string <samp>TRUE</samp> or + * <samp>FALSE</samp> + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ + function quote($str = null) + { + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 'TRUE' : 'FALSE'; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; + } + } + + // }}} // {{{ numCols() function numCols($result) Index: pear/DB/DB/mssql.php diff -u pear/DB/DB/mssql.php:1.19 pear/DB/DB/mssql.php:1.20 --- pear/DB/DB/mssql.php:1.19 Fri Dec 5 17:00:14 2003 +++ pear/DB/DB/mssql.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: mssql.php,v 1.19 2003/12/05 22:00:14 lsmith Exp $ +// $Id: mssql.php,v 1.20 2003/12/31 15:58:44 danielc Exp $ // require_once 'DB/common.php'; @@ -26,7 +26,7 @@ * extension. * * @package DB - * @version $Id: mssql.php,v 1.19 2003/12/05 22:00:14 lsmith Exp $ + * @version $Id: mssql.php,v 1.20 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Sterling Hughes <sterling@php.net> */ @@ -203,6 +203,38 @@ } // }}} + // {{{ quote() + + /** + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. Use the BIT + * data type because MS SQL doesn't have BOOLEAN. + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ + function quote($str = null) + { + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; + } + } + + // }}} // {{{ numCols() function numCols($result) Index: pear/DB/DB/mysql.php diff -u pear/DB/DB/mysql.php:1.25 pear/DB/DB/mysql.php:1.26 --- pear/DB/DB/mysql.php:1.25 Thu Sep 18 15:29:46 2003 +++ pear/DB/DB/mysql.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: Stig Bakken <ssb@php.net> | // +----------------------------------------------------------------------+ // -// $Id: mysql.php,v 1.25 2003/09/18 19:29:46 cox Exp $ +// $Id: mysql.php,v 1.26 2003/12/31 15:58:44 danielc Exp $ // // @@ -33,7 +33,7 @@ * extension. * * @package DB - * @version $Id: mysql.php,v 1.25 2003/09/18 19:29:46 cox Exp $ + * @version $Id: mysql.php,v 1.26 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Stig Bakken <ssb@php.net> */ @@ -627,26 +627,34 @@ // {{{ quote() /** - * Quote the given string so it can be safely used within string delimiters - * in a query. - * @param $string mixed Data to be quoted - * @return mixed "NULL" string, quoted string or original data - */ + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. + * 1 and 0 used because MySQL maps BOOL to TINYINT(1). + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data escaped according to MySQL's settings + * then encapsulated between single quotes + */ function quote($str = null) { - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'integer': - case 'double': - return $str; - case 'string': - default: - if(function_exists('mysql_real_escape_string')) { - return "'".mysql_real_escape_string($str, $this->connection)."'"; - } else { - return "'".mysql_escape_string($str)."'"; - } + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } elseif (is_null($str)) { + return 'NULL'; + } else { + if(function_exists('mysql_real_escape_string')) { + return "'".mysql_real_escape_string($str, $this->connection)."'"; + } else { + return "'".mysql_escape_string($str)."'"; + } } } Index: pear/DB/DB/mysql4.php diff -u pear/DB/DB/mysql4.php:1.9 pear/DB/DB/mysql4.php:1.10 --- pear/DB/DB/mysql4.php:1.9 Thu Sep 18 15:29:46 2003 +++ pear/DB/DB/mysql4.php Wed Dec 31 10:58:44 2003 @@ -28,7 +28,7 @@ * Note that pconnect is GONE. * * @package DB - * @version $Id: mysql4.php,v 1.9 2003/09/18 19:29:46 cox Exp $ + * @version $Id: mysql4.php,v 1.10 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Chaillan Nicolas <nicos@php.net> */ @@ -614,31 +614,37 @@ } return true; } - // }}} + // }}} // {{{ quote() /** - * Quote the given string so it can be safely used within string delimiters - * in a query. - * @param $string mixed Data to be quoted - * @return mixed "NULL" string, quoted string or original data - */ + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. + * 1 and 0 used because MySQL maps BOOLEAN to TINYINT(1). + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data escaped according to MySQL's settings + * then encapsulated between single quotes + */ function quote($str = null) { - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'integer': - case 'double': - return $str; - case 'string': - default: - if(function_exists('mysql_real_escape_string')) { - return "'".mysql_real_escape_string($str, $this->connection)."'"; - } else { - return "'".mysql_escape_string($str)."'"; - } + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } else { + if(function_exists('mysql_real_escape_string')) { + return "'".mysql_real_escape_string($str, $this->connection)."'"; + } else { + return "'".mysql_escape_string($str)."'"; + } } } Index: pear/DB/DB/oci8.php diff -u pear/DB/DB/oci8.php:1.18 pear/DB/DB/oci8.php:1.19 --- pear/DB/DB/oci8.php:1.18 Wed Dec 31 00:05:51 2003 +++ pear/DB/DB/oci8.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: James L. Pine <jlp@valinux.com> | // +----------------------------------------------------------------------+ // -// $Id: oci8.php,v 1.18 2003/12/31 05:05:51 danielc Exp $ +// $Id: oci8.php,v 1.19 2003/12/31 15:58:44 danielc Exp $ // // Database independent query interface definition for PHP's Oracle 8 // call-interface extension. @@ -249,6 +249,38 @@ } // }}} + // {{{ quote() + + /** + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. Use the NUMBER(1) + * data type because Oracle doesn't have BOOLEAN. + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ + function quote($str = null) + { + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; + } + } + + // }}} // {{{ numRows() function numRows($result) Index: pear/DB/DB/odbc.php diff -u pear/DB/DB/odbc.php:1.9 pear/DB/DB/odbc.php:1.10 --- pear/DB/DB/odbc.php:1.9 Thu Sep 18 09:32:58 2003 +++ pear/DB/DB/odbc.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: Stig Bakken <ssb@php.net> | // +----------------------------------------------------------------------+ // -// $Id: odbc.php,v 1.9 2003/09/18 13:32:58 cox Exp $ +// $Id: odbc.php,v 1.10 2003/12/31 15:58:44 danielc Exp $ // // Database independent query interface definition for PHP's ODBC // extension. @@ -297,27 +297,34 @@ return $nrows; } + // }}} // {{{ quote() + /** - * Quote the given string so it can be safely used within string delimiters - * in a query. - * @param $string mixed Data to be quoted - * @return mixed "NULL" string, quoted string or original data - */ + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = the string <samp>TRUE</samp> or + * <samp>FALSE</samp> + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ function quote($str = null) { - if (is_numeric($str)) { + if (is_int($str) || is_double($str)) { return $str; - } - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'boolean': - return $str ? 'TRUE' : 'FALSE'; - case 'string': - default: - $str = str_replace("'", "''", $str); - return "'$str'"; + } elseif (is_bool($str)) { + return $str ? 'TRUE' : 'FALSE'; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; } } Index: pear/DB/DB/pgsql.php diff -u pear/DB/DB/pgsql.php:1.32 pear/DB/DB/pgsql.php:1.33 --- pear/DB/DB/pgsql.php:1.32 Sat Dec 20 11:37:50 2003 +++ pear/DB/DB/pgsql.php Wed Dec 31 10:58:44 2003 @@ -17,7 +17,7 @@ // | Stig Bakken <ssb@php.net> | // +----------------------------------------------------------------------+ // -// $Id: pgsql.php,v 1.32 2003/12/20 16:37:50 chagenbu Exp $ +// $Id: pgsql.php,v 1.33 2003/12/31 15:58:44 danielc Exp $ // require_once 'DB/common.php'; @@ -27,7 +27,7 @@ * extension. * * @package DB - * @version $Id: pgsql.php,v 1.32 2003/12/20 16:37:50 chagenbu Exp $ + * @version $Id: pgsql.php,v 1.33 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Rui Hirokawa <rui_hirokawa@ybb.ne.jp> * @author Stig Bakken <ssb@php.net> @@ -323,30 +323,38 @@ // }}} // {{{ quote() + /** - * Quote the given string so it can be safely used within string delimiters - * in a query. - * @param $string mixed Data to be quoted - * @return mixed "NULL" string, quoted string or original data - */ + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = the string <samp>TRUE</samp> or + * <samp>FALSE</samp> + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes, backslashes are escaped by preceeding + * backslashes, then the whole string is encapsulated + * between single quotes + */ function quote($str = null) { - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'integer': - case 'double' : - return $str; - case 'boolean': - return $str ? 'TRUE' : 'FALSE'; - case 'string': - default: - $str = str_replace("'", "''", $str); - //PostgreSQL treats a backslash as an escape character. - $str = str_replace('\\', '\\\\', $str); - return "'$str'"; + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 'TRUE' : 'FALSE'; + } elseif (is_null($str)) { + return 'NULL'; + } else { + //PostgreSQL treats a backslash as an escape character. + $str = str_replace('\\', '\\\\', $str); + return "'" . str_replace("'", "''", $str) . "'"; } } + // }}} // {{{ numCols() Index: pear/DB/DB/sybase.php diff -u pear/DB/DB/sybase.php:1.16 pear/DB/DB/sybase.php:1.17 --- pear/DB/DB/sybase.php:1.16 Wed Dec 31 00:13:20 2003 +++ pear/DB/DB/sybase.php Wed Dec 31 10:58:44 2003 @@ -17,7 +17,7 @@ // | Antônio Carlos Venâncio Júnior <floripa@php.net> | // +----------------------------------------------------------------------+ // -// $Id: sybase.php,v 1.16 2003/12/31 05:13:20 danielc Exp $ +// $Id: sybase.php,v 1.17 2003/12/31 15:58:44 danielc Exp $ // // // TODO @@ -31,7 +31,7 @@ * extension. * * @package DB - * @version $Id: sybase.php,v 1.16 2003/12/31 05:13:20 danielc Exp $ + * @version $Id: sybase.php,v 1.17 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Sterling Hughes <sterling@php.net> * @author Antônio Carlos Venâncio Júnior <floripa@php.net> @@ -326,6 +326,38 @@ } // }}} + // {{{ quote() + + /** + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + integer or double = the unquoted number + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. Use the TINYINT(1) data type. + * Sybase doesn't have BOOLEAN and BIT can't be null. + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ + function quote($str = null) + { + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; + } + } + + // }}} // {{{ numCols() /**
Index: pear/DB/DB/common.php diff -u pear/DB/DB/common.php:1.32 pear/DB/DB/common.php:1.33 --- pear/DB/DB/common.php:1.32 Wed Dec 31 00:05:51 2003 +++ pear/DB/DB/common.php Wed Dec 31 10:58:44 2003 @@ -17,7 +17,7 @@ // | Tomas V.V.Cox <cox@idecnet.com> | // +----------------------------------------------------------------------+ // -// $Id: common.php,v 1.32 2003/12/31 05:05:51 danielc Exp $ +// $Id: common.php,v 1.33 2003/12/31 15:58:44 danielc Exp $ // // Base class for DB implementations. // @@ -29,7 +29,7 @@ * inherited by all such. * * @package DB - * @version $Id: common.php,v 1.32 2003/12/31 05:05:51 danielc Exp $ + * @version $Id: common.php,v 1.33 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Stig Bakken <ssb@php.net> * @author Tomas V.V.Cox <cox@idecnet.com> @@ -170,9 +170,14 @@ } /** - * Quotes a string so it can be safely used in a query. It will return - * the string with single quotes around. Other backend quote styles - * should override this method. + * Quotes a string so it can be safely used in a query. + * + * It will return the string with single quotes around. + * + * This method gets overridden if the DBMS being used has a quote() + * method in its sub-class. Those overriding methods contain notes + * about which data types should be used to handle BOOLEAN data in + * each DBMS. * * @param string $string the input string to quote * Index: pear/DB/DB/ibase.php diff -u pear/DB/DB/ibase.php:1.18 pear/DB/DB/ibase.php:1.19 --- pear/DB/DB/ibase.php:1.18 Wed Dec 31 00:05:51 2003 +++ pear/DB/DB/ibase.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: ibase.php,v 1.18 2003/12/31 05:05:51 danielc Exp $ +// $Id: ibase.php,v 1.19 2003/12/31 15:58:44 danielc Exp $ // // Bugs: @@ -29,7 +29,7 @@ * extension. * * @package DB - * @version $Id: ibase.php,v 1.18 2003/12/31 05:05:51 danielc Exp $ + * @version $Id: ibase.php,v 1.19 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Sterling Hughes <sterling@php.net> */ @@ -261,6 +261,37 @@ } // }}} + // {{{ quote() + + /** + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = the string <samp>TRUE</samp> or + * <samp>FALSE</samp> + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ + function quote($str = null) + { + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 'TRUE' : 'FALSE'; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; + } + } + + // }}} // {{{ numCols() function numCols($result) Index: pear/DB/DB/mssql.php diff -u pear/DB/DB/mssql.php:1.19 pear/DB/DB/mssql.php:1.20 --- pear/DB/DB/mssql.php:1.19 Fri Dec 5 17:00:14 2003 +++ pear/DB/DB/mssql.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: mssql.php,v 1.19 2003/12/05 22:00:14 lsmith Exp $ +// $Id: mssql.php,v 1.20 2003/12/31 15:58:44 danielc Exp $ // require_once 'DB/common.php'; @@ -26,7 +26,7 @@ * extension. * * @package DB - * @version $Id: mssql.php,v 1.19 2003/12/05 22:00:14 lsmith Exp $ + * @version $Id: mssql.php,v 1.20 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Sterling Hughes <sterling@php.net> */ @@ -203,6 +203,38 @@ } // }}} + // {{{ quote() + + /** + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. Use the BIT + * data type because MS SQL doesn't have BOOLEAN. + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ + function quote($str = null) + { + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; + } + } + + // }}} // {{{ numCols() function numCols($result) Index: pear/DB/DB/mysql.php diff -u pear/DB/DB/mysql.php:1.25 pear/DB/DB/mysql.php:1.26 --- pear/DB/DB/mysql.php:1.25 Thu Sep 18 15:29:46 2003 +++ pear/DB/DB/mysql.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: Stig Bakken <ssb@php.net> | // +----------------------------------------------------------------------+ // -// $Id: mysql.php,v 1.25 2003/09/18 19:29:46 cox Exp $ +// $Id: mysql.php,v 1.26 2003/12/31 15:58:44 danielc Exp $ // // @@ -33,7 +33,7 @@ * extension. * * @package DB - * @version $Id: mysql.php,v 1.25 2003/09/18 19:29:46 cox Exp $ + * @version $Id: mysql.php,v 1.26 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Stig Bakken <ssb@php.net> */ @@ -627,26 +627,34 @@ // {{{ quote() /** - * Quote the given string so it can be safely used within string delimiters - * in a query. - * @param $string mixed Data to be quoted - * @return mixed "NULL" string, quoted string or original data - */ + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. + * 1 and 0 used because MySQL maps BOOL to TINYINT(1). + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data escaped according to MySQL's settings + * then encapsulated between single quotes + */ function quote($str = null) { - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'integer': - case 'double': - return $str; - case 'string': - default: - if(function_exists('mysql_real_escape_string')) { - return "'".mysql_real_escape_string($str, $this->connection)."'"; - } else { - return "'".mysql_escape_string($str)."'"; - } + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } elseif (is_null($str)) { + return 'NULL'; + } else { + if(function_exists('mysql_real_escape_string')) { + return "'".mysql_real_escape_string($str, $this->connection)."'"; + } else { + return "'".mysql_escape_string($str)."'"; + } } } Index: pear/DB/DB/mysql4.php diff -u pear/DB/DB/mysql4.php:1.9 pear/DB/DB/mysql4.php:1.10 --- pear/DB/DB/mysql4.php:1.9 Thu Sep 18 15:29:46 2003 +++ pear/DB/DB/mysql4.php Wed Dec 31 10:58:44 2003 @@ -28,7 +28,7 @@ * Note that pconnect is GONE. * * @package DB - * @version $Id: mysql4.php,v 1.9 2003/09/18 19:29:46 cox Exp $ + * @version $Id: mysql4.php,v 1.10 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Chaillan Nicolas <nicos@php.net> */ @@ -614,31 +614,37 @@ } return true; } - // }}} + // }}} // {{{ quote() /** - * Quote the given string so it can be safely used within string delimiters - * in a query. - * @param $string mixed Data to be quoted - * @return mixed "NULL" string, quoted string or original data - */ + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. + * 1 and 0 used because MySQL maps BOOLEAN to TINYINT(1). + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data escaped according to MySQL's settings + * then encapsulated between single quotes + */ function quote($str = null) { - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'integer': - case 'double': - return $str; - case 'string': - default: - if(function_exists('mysql_real_escape_string')) { - return "'".mysql_real_escape_string($str, $this->connection)."'"; - } else { - return "'".mysql_escape_string($str)."'"; - } + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } else { + if(function_exists('mysql_real_escape_string')) { + return "'".mysql_real_escape_string($str, $this->connection)."'"; + } else { + return "'".mysql_escape_string($str)."'"; + } } } Index: pear/DB/DB/oci8.php diff -u pear/DB/DB/oci8.php:1.18 pear/DB/DB/oci8.php:1.19 --- pear/DB/DB/oci8.php:1.18 Wed Dec 31 00:05:51 2003 +++ pear/DB/DB/oci8.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: James L. Pine <jlp@valinux.com> | // +----------------------------------------------------------------------+ // -// $Id: oci8.php,v 1.18 2003/12/31 05:05:51 danielc Exp $ +// $Id: oci8.php,v 1.19 2003/12/31 15:58:44 danielc Exp $ // // Database independent query interface definition for PHP's Oracle 8 // call-interface extension. @@ -249,6 +249,38 @@ } // }}} + // {{{ quote() + + /** + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. Use the NUMBER(1) + * data type because Oracle doesn't have BOOLEAN. + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ + function quote($str = null) + { + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; + } + } + + // }}} // {{{ numRows() function numRows($result) Index: pear/DB/DB/odbc.php diff -u pear/DB/DB/odbc.php:1.9 pear/DB/DB/odbc.php:1.10 --- pear/DB/DB/odbc.php:1.9 Thu Sep 18 09:32:58 2003 +++ pear/DB/DB/odbc.php Wed Dec 31 10:58:44 2003 @@ -16,7 +16,7 @@ // | Author: Stig Bakken <ssb@php.net> | // +----------------------------------------------------------------------+ // -// $Id: odbc.php,v 1.9 2003/09/18 13:32:58 cox Exp $ +// $Id: odbc.php,v 1.10 2003/12/31 15:58:44 danielc Exp $ // // Database independent query interface definition for PHP's ODBC // extension. @@ -297,27 +297,34 @@ return $nrows; } + // }}} // {{{ quote() + /** - * Quote the given string so it can be safely used within string delimiters - * in a query. - * @param $string mixed Data to be quoted - * @return mixed "NULL" string, quoted string or original data - */ + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = the string <samp>TRUE</samp> or + * <samp>FALSE</samp> + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ function quote($str = null) { - if (is_numeric($str)) { + if (is_int($str) || is_double($str)) { return $str; - } - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'boolean': - return $str ? 'TRUE' : 'FALSE'; - case 'string': - default: - $str = str_replace("'", "''", $str); - return "'$str'"; + } elseif (is_bool($str)) { + return $str ? 'TRUE' : 'FALSE'; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; } } Index: pear/DB/DB/pgsql.php diff -u pear/DB/DB/pgsql.php:1.32 pear/DB/DB/pgsql.php:1.33 --- pear/DB/DB/pgsql.php:1.32 Sat Dec 20 11:37:50 2003 +++ pear/DB/DB/pgsql.php Wed Dec 31 10:58:44 2003 @@ -17,7 +17,7 @@ // | Stig Bakken <ssb@php.net> | // +----------------------------------------------------------------------+ // -// $Id: pgsql.php,v 1.32 2003/12/20 16:37:50 chagenbu Exp $ +// $Id: pgsql.php,v 1.33 2003/12/31 15:58:44 danielc Exp $ // require_once 'DB/common.php'; @@ -27,7 +27,7 @@ * extension. * * @package DB - * @version $Id: pgsql.php,v 1.32 2003/12/20 16:37:50 chagenbu Exp $ + * @version $Id: pgsql.php,v 1.33 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Rui Hirokawa <rui_hirokawa@ybb.ne.jp> * @author Stig Bakken <ssb@php.net> @@ -323,30 +323,38 @@ // }}} // {{{ quote() + /** - * Quote the given string so it can be safely used within string delimiters - * in a query. - * @param $string mixed Data to be quoted - * @return mixed "NULL" string, quoted string or original data - */ + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + boolean = the string <samp>TRUE</samp> or + * <samp>FALSE</samp> + * + integer or double = the unquoted number + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes, backslashes are escaped by preceeding + * backslashes, then the whole string is encapsulated + * between single quotes + */ function quote($str = null) { - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'integer': - case 'double' : - return $str; - case 'boolean': - return $str ? 'TRUE' : 'FALSE'; - case 'string': - default: - $str = str_replace("'", "''", $str); - //PostgreSQL treats a backslash as an escape character. - $str = str_replace('\\', '\\\\', $str); - return "'$str'"; + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 'TRUE' : 'FALSE'; + } elseif (is_null($str)) { + return 'NULL'; + } else { + //PostgreSQL treats a backslash as an escape character. + $str = str_replace('\\', '\\\\', $str); + return "'" . str_replace("'", "''", $str) . "'"; } } + // }}} // {{{ numCols() Index: pear/DB/DB/sybase.php diff -u pear/DB/DB/sybase.php:1.16 pear/DB/DB/sybase.php:1.17 --- pear/DB/DB/sybase.php:1.16 Wed Dec 31 00:13:20 2003 +++ pear/DB/DB/sybase.php Wed Dec 31 10:58:44 2003 @@ -17,7 +17,7 @@ // | Antônio Carlos Venâncio Júnior <floripa@php.net> | // +----------------------------------------------------------------------+ // -// $Id: sybase.php,v 1.16 2003/12/31 05:13:20 danielc Exp $ +// $Id: sybase.php,v 1.17 2003/12/31 15:58:44 danielc Exp $ // // // TODO @@ -31,7 +31,7 @@ * extension. * * @package DB - * @version $Id: sybase.php,v 1.16 2003/12/31 05:13:20 danielc Exp $ + * @version $Id: sybase.php,v 1.17 2003/12/31 15:58:44 danielc Exp $ * @category Database * @author Sterling Hughes <sterling@php.net> * @author Antônio Carlos Venâncio Júnior <floripa@php.net> @@ -326,6 +326,38 @@ } // }}} + // {{{ quote() + + /** + * Quote the given string so it can be safely used in a query. + * + * @param $str mixed data to be quoted + * + * @return mixed Submitted variable's type = returned value: + * + null = the string <samp>NULL</samp> + * + integer or double = the unquoted number + * + boolean = <samp>1</samp> if true or + * <samp>0</samp> if false. Use the TINYINT(1) data type. + * Sybase doesn't have BOOLEAN and BIT can't be null. + * + other (including strings and numeric strings) = + * the data with single quotes escaped by preceeding + * single quotes then the whole string is encapsulated + * between single quotes + */ + function quote($str = null) + { + if (is_int($str) || is_double($str)) { + return $str; + } elseif (is_bool($str)) { + return $str ? 1 : 0; + } elseif (is_null($str)) { + return 'NULL'; + } else { + return "'" . str_replace("'", "''", $str) . "'"; + } + } + + // }}} // {{{ numCols() /**