# # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ---------------------------------------- # # Usage: # object dbx_execute ( object link_identifier, string query, array args [, long flags]) # string dbx_make_query ( object link_identifier, string query, array args) # string dbx_quote_arg ( object link_identifier, string arg) # # Examples: # $link = dbx_connect(...); # $results = dbx_execute($link, # "DELETE FROM foo WHERE a = ? AND b = ? AND c = ? AND d = ?", # array("foo", $b, 5, '5')); # $real_query = dbx_make_query($link, # "DELETE FROM foo WHERE a = ? AND b = ? AND c = ? AND d = ?", # array("foo", "foo\"'\\\x00bar", 5, '5')); class _dbx_execute_wrapper { var $link; var $_args; var $_argnum; function _dbx_execute_wrapper($link) { $this->link = $link; } function execute($query, $args=null, $flags=null) { $q = $this->make_query($query, $args); if (is_null($flags)) { return dbx_query($this->link, $q); } else { return dbx_query($this->link, $q, $flags); } } function make_query($query, $args) { $this->_argnum = 0; $this->_args = $args; return preg_replace_callback("|(\\?)|", array($this, '_cb'), $query); } function quote_arg($arg) { if (is_int($arg)) { return strval(intval($arg)); } else if (is_null($arg)) { return "NULL"; } return "'" . dbx_escape_string($this->link, $arg) . "'"; } function _cb($matches) { $arg = $this->_args[$this->_argnum]; $this->_argnum++; return $this->quote_arg($arg); } } function dbx_execute($link, $query, $args=null, $flags=null) { $w = new _dbx_execute_wrapper($link); return $w->execute($query, $args, $flags); } function dbx_make_query($link, $query, $args=null) { $w = new _dbx_execute_wrapper($link); return $w->make_query($query, $args); } function dbx_quote_arg($link, $arg) { $w = new _dbx_execute_wrapper($link); return $w->quote_arg($arg); } # vim:set ts=4 sw=4 expandtab: