55 require_once
'PEAR.php';
67 define(
'MDB2_OK',
true);
68 define(
'MDB2_ERROR', -1);
69 define(
'MDB2_ERROR_SYNTAX', -2);
70 define(
'MDB2_ERROR_CONSTRAINT', -3);
71 define(
'MDB2_ERROR_NOT_FOUND', -4);
72 define(
'MDB2_ERROR_ALREADY_EXISTS', -5);
73 define(
'MDB2_ERROR_UNSUPPORTED', -6);
74 define(
'MDB2_ERROR_MISMATCH', -7);
75 define(
'MDB2_ERROR_INVALID', -8);
76 define(
'MDB2_ERROR_NOT_CAPABLE', -9);
77 define(
'MDB2_ERROR_TRUNCATED', -10);
78 define(
'MDB2_ERROR_INVALID_NUMBER', -11);
79 define(
'MDB2_ERROR_INVALID_DATE', -12);
80 define(
'MDB2_ERROR_DIVZERO', -13);
81 define(
'MDB2_ERROR_NODBSELECTED', -14);
82 define(
'MDB2_ERROR_CANNOT_CREATE', -15);
83 define(
'MDB2_ERROR_CANNOT_DELETE', -16);
84 define(
'MDB2_ERROR_CANNOT_DROP', -17);
85 define(
'MDB2_ERROR_NOSUCHTABLE', -18);
86 define(
'MDB2_ERROR_NOSUCHFIELD', -19);
87 define(
'MDB2_ERROR_NEED_MORE_DATA', -20);
88 define(
'MDB2_ERROR_NOT_LOCKED', -21);
89 define(
'MDB2_ERROR_VALUE_COUNT_ON_ROW', -22);
90 define(
'MDB2_ERROR_INVALID_DSN', -23);
91 define(
'MDB2_ERROR_CONNECT_FAILED', -24);
92 define(
'MDB2_ERROR_EXTENSION_NOT_FOUND',-25);
93 define(
'MDB2_ERROR_NOSUCHDB', -26);
94 define(
'MDB2_ERROR_ACCESS_VIOLATION', -27);
95 define(
'MDB2_ERROR_CANNOT_REPLACE', -28);
96 define(
'MDB2_ERROR_CONSTRAINT_NOT_NULL',-29);
97 define(
'MDB2_ERROR_DEADLOCK', -30);
98 define(
'MDB2_ERROR_CANNOT_ALTER', -31);
99 define(
'MDB2_ERROR_MANAGER', -32);
100 define(
'MDB2_ERROR_MANAGER_PARSE', -33);
101 define(
'MDB2_ERROR_LOADMODULE', -34);
102 define(
'MDB2_ERROR_INSUFFICIENT_DATA', -35);
109 define(
'MDB2_PREPARE_MANIP',
false);
110 define(
'MDB2_PREPARE_RESULT', null);
119 define(
'MDB2_FETCHMODE_DEFAULT', 0);
124 define(
'MDB2_FETCHMODE_ORDERED', 1);
129 define(
'MDB2_FETCHMODE_ASSOC', 2);
134 define(
'MDB2_FETCHMODE_OBJECT', 3);
142 define(
'MDB2_FETCHMODE_FLIPPED', 4);
151 define(
'MDB2_PORTABILITY_NONE', 0);
158 define(
'MDB2_PORTABILITY_FIX_CASE', 1);
164 define(
'MDB2_PORTABILITY_RTRIM', 2);
170 define(
'MDB2_PORTABILITY_DELETE_COUNT', 4);
176 define(
'MDB2_PORTABILITY_NUMROWS', 8);
191 define(
'MDB2_PORTABILITY_ERRORS', 16);
198 define(
'MDB2_PORTABILITY_EMPTY_TO_NULL', 32);
204 define(
'MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES', 64);
210 define(
'MDB2_PORTABILITY_ALL', 127);
281 if (is_array($options)) {
282 foreach ($options as $option => $value) {
283 $test = $db->setOption($option, $value);
306 if (version_compare(phpversion(),
"5.0",
">=")) {
307 return class_exists($classname,
false);
309 return class_exists($classname);
328 $file_name = str_replace(
'_', DIRECTORY_SEPARATOR, $class_name).
'.php';
336 $msg =
"unable to find package '$class_name' file '$file_name'";
338 $msg =
"unable to load class '$class_name' from file '$file_name'";
377 if (empty($dsninfo[
'phptype'])) {
379 null, null,
'no RDBMS driver specified');
382 $class_name =
'MDB2_Driver_'.$dsninfo[
'phptype'];
384 $debug = (!empty($options[
'debug']));
390 $db =&
new $class_name();
391 $db->setDSN($dsninfo);
438 $err = $db->connect();
440 $dsn = $db->getDSN(
'string',
'xxx');
442 $err->addUserInfo($dsn);
485 $dsninfo = array_merge(
$GLOBALS[
'_MDB2_dsninfo_default'], $dsninfo);
486 $keys = array_keys(
$GLOBALS[
'_MDB2_databases']);
487 for ($i=0, $j=count($keys); $i<$j; ++$i) {
488 if (isset(
$GLOBALS[
'_MDB2_databases'][$keys[$i]])) {
489 $tmp_dsn =
$GLOBALS[
'_MDB2_databases'][$keys[$i]]->getDSN(
'array');
490 if (count(array_diff_assoc($tmp_dsn, $dsninfo)) == 0) {
492 return $GLOBALS[
'_MDB2_databases'][$keys[$i]];
496 } elseif (is_array(
$GLOBALS[
'_MDB2_databases']) && reset(
$GLOBALS[
'_MDB2_databases'])) {
518 $file_name =
'MDB2'.DIRECTORY_SEPARATOR.$file.
'.php';
572 function &
raiseError($code = null, $mode = null, $options = null, $userinfo = null)
574 $err =&
PEAR::raiseError(null, $code, $mode, $options, $userinfo,
'MDB2_Error',
true);
596 if (is_a(
$data,
'MDB2_Error')) {
597 if (is_null($code)) {
599 } elseif (is_string($code)) {
600 return $data->getMessage() === $code;
602 $code = (array)$code;
603 return in_array(
$data->getCode(), $code);
623 return is_a($value,
'MDB2_Driver_Common');
640 return is_a($value,
'MDB2_Result');
657 return is_a($value,
'MDB2_Result_Common');
674 return is_a($value,
'MDB2_Statement');
694 static $errorMessages;
696 if (is_array($value)) {
697 $errorMessages = $value;
701 if (!isset($errorMessages)) {
702 $errorMessages = array(
739 if (is_null($value)) {
740 return $errorMessages;
744 $value = $value->getCode();
747 return isset($errorMessages[$value]) ?
748 $errorMessages[$value] : $errorMessages[
MDB2_ERROR];
793 $parsed =
$GLOBALS[
'_MDB2_dsninfo_default'];
795 if (is_array($dsn)) {
796 $dsn = array_merge($parsed, $dsn);
797 if (!$dsn[
'dbsyntax']) {
798 $dsn[
'dbsyntax'] = $dsn[
'phptype'];
804 if ((
$pos = strpos($dsn,
'://')) !==
false) {
805 $str = substr($dsn, 0,
$pos);
806 $dsn = substr($dsn,
$pos + 3);
814 if (preg_match(
'|^(.+?)\((.*?)\)$|', $str, $arr)) {
815 $parsed[
'phptype'] = $arr[1];
816 $parsed[
'dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
818 $parsed[
'phptype'] = $str;
819 $parsed[
'dbsyntax'] = $str;
828 if (($at = strrpos($dsn,
'@')) !==
false) {
829 $str = substr($dsn, 0, $at);
830 $dsn = substr($dsn, $at + 1);
831 if ((
$pos = strpos($str,
':')) !==
false) {
832 $parsed[
'username'] = rawurldecode(substr($str, 0,
$pos));
833 $parsed[
'password'] = rawurldecode(substr($str,
$pos + 1));
835 $parsed[
'username'] = rawurldecode($str);
842 if (preg_match(
'|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
844 $proto_opts = $match[2] ? $match[2] :
false;
849 if (strpos($dsn,
'+') !==
false) {
850 list($proto, $dsn) = explode(
'+', $dsn, 2);
852 if ( strpos($dsn,
'//') === 0
853 && strpos($dsn,
'/', 2) !==
false
854 && $parsed[
'phptype'] ==
'oci8'
861 } elseif (strpos($dsn,
'/') !==
false) {
862 list($proto_opts, $dsn) = explode(
'/', $dsn, 2);
870 $parsed[
'protocol'] = (!empty($proto)) ? $proto :
'tcp';
871 $proto_opts = rawurldecode($proto_opts);
872 if (strpos($proto_opts,
':') !==
false) {
873 list($proto_opts, $parsed[
'port']) = explode(
':', $proto_opts);
875 if ($parsed[
'protocol'] ==
'tcp') {
876 $parsed[
'hostspec'] = $proto_opts;
877 } elseif ($parsed[
'protocol'] ==
'unix') {
878 $parsed[
'socket'] = $proto_opts;
885 if ((
$pos = strpos($dsn,
'?')) ===
false) {
886 $parsed[
'database'] = $dsn;
889 $parsed[
'database'] = substr($dsn, 0,
$pos);
890 $dsn = substr($dsn,
$pos + 1);
891 if (strpos($dsn,
'&') !==
false) {
892 $opts = explode(
'&', $dsn);
896 foreach ($opts as $opt) {
897 list($key, $value) = explode(
'=', $opt);
898 if (!isset($parsed[$key])) {
900 $parsed[$key] = rawurldecode($value);
924 if (!@ini_get(
'safe_mode')) {
925 $dirs = explode(PATH_SEPARATOR, ini_get(
'include_path'));
927 if (@is_readable($dir . DIRECTORY_SEPARATOR .
$file)) {
932 $fp = @fopen(
$file,
'r',
true);
933 if (is_resource($fp)) {
967 $level = E_USER_NOTICE, $debuginfo = null)
969 if (is_null(
$code)) {
1055 'sequences' =>
false,
1057 'affected_rows' =>
false,
1058 'summary_functions' =>
false,
1059 'order_by_text' =>
false,
1060 'transactions' =>
false,
1061 'savepoints' =>
false,
1062 'current_id' =>
false,
1063 'limit_queries' =>
false,
1066 'sub_selects' =>
false,
1067 'auto_increment' =>
false,
1068 'primary_key' =>
false,
1069 'result_introspection' =>
false,
1070 'prepared_statements' =>
false,
1071 'identifier_quoting' =>
false,
1072 'pattern_escaping' =>
false,
1073 'new_link' =>
false,
1124 'field_case' => CASE_LOWER,
1125 'disable_query' =>
false,
1126 'result_class' =>
'MDB2_Result_%s',
1127 'buffered_result_class' =>
'MDB2_BufferedResult_%s',
1128 'result_wrap_class' =>
false,
1129 'result_buffering' =>
true,
1130 'fetch_class' =>
'stdClass',
1131 'persistent' =>
false,
1133 'debug_handler' =>
'MDB2_defaultDebugOutput',
1134 'debug_expanded_output' =>
false,
1135 'default_text_field_length' => 4096,
1136 'lob_buffer_length' => 8192,
1137 'log_line_break' =>
"\n",
1138 'idxname_format' =>
'%s_idx',
1139 'seqname_format' =>
'%s_seq',
1140 'savepoint_format' =>
'MDB2_SAVEPOINT_%s',
1141 'statement_format' =>
'MDB2_STATEMENT_%1$s_%2$s',
1142 'seqcol_name' =>
'sequence',
1143 'quote_identifier' =>
false,
1144 'use_transactions' =>
true,
1145 'decimal_places' => 2,
1155 'emulate_prepared' =>
false,
1156 'datatype_map' => array(),
1157 'datatype_map_callback' => array(),
1158 'nativetype_map_callback' => array(),
1166 var
$string_quoting = array(
'start' =>
"'",
'end' =>
"'",
'escape' =>
false,
'escape_pattern' =>
false);
1181 array(
'start' =>
'--',
'end' =>
"\n",
'escape' =>
false),
1182 array(
'start' =>
'/*',
'end' =>
'*/',
'escape' =>
false),
1312 $this->destructor_registered =
false;
1339 unset(
$GLOBALS[
'_MDB2_databases'][$this->db_index]);
1340 unset($this->db_index);
1356 $info = get_class($this);
1357 $info.=
': (phptype = '.$this->phptype.
', dbsyntax = '.$this->dbsyntax.
')';
1358 if ($this->connection) {
1359 $info.=
' [connected]';
1378 return array(
$error, null, null);
1409 $userinfo =
"[Error message: $userinfo]\n";
1414 if (is_null($mode) && !empty($this->_default_error_mode)) {
1418 if (is_null($userinfo)) {
1419 $userinfo = $code->getUserinfo();
1421 $code = $code->getCode();
1425 } elseif (isset($this->connection)) {
1426 if (!empty($this->last_query)) {
1427 $userinfo.=
"[Last executed query: {$this->last_query}]\n";
1429 $native_errno = $native_msg = null;
1430 list($code, $native_errno, $native_msg) = $this->
errorInfo($code);
1431 if (!is_null($native_errno) && $native_errno !==
'') {
1432 $userinfo.=
"[Native code: $native_errno]\n";
1434 if (!is_null($native_msg) && $native_msg !==
'') {
1435 $userinfo.=
"[Native message: ". strip_tags($native_msg) .
"]\n";
1437 if (!is_null($method)) {
1438 $userinfo = $method.
': '.$userinfo;
1445 $this->has_transaction_error =& $err;
1462 $this->warnings = array();
1479 return array_reverse($this->warnings);
1508 $this->options[
'fetch_class'] = $object_class;
1515 'invalid fetchmode mode', __FUNCTION__);
1536 if (array_key_exists($option, $this->options)) {
1537 $this->options[$option] = $value;
1541 "unknown option $option", __FUNCTION__);
1558 if (array_key_exists($option, $this->options)) {
1559 return $this->options[$option];
1562 "unknown option $option", __FUNCTION__);
1582 function debug($message, $scope =
'', $context = array())
1584 if ($this->options[
'debug'] && $this->options[
'debug_handler']) {
1585 if (!$this->options[
'debug_expanded_output']) {
1586 if (!empty($context[
'when']) && $context[
'when'] !==
'pre') {
1589 $context = empty($context[
'is_manip']) ?
false : $context[
'is_manip'];
1591 return call_user_func_array($this->options[
'debug_handler'], array(&$this, $scope, $message, $context));
1625 function escape($text, $escape_wildcards =
false)
1627 if ($escape_wildcards) {
1631 $text = str_replace($this->string_quoting[
'end'], $this->string_quoting[
'escape'] . $this->string_quoting[
'end'], $text);
1654 if ($this->string_quoting[
'escape_pattern']) {
1655 $text = str_replace($this->string_quoting[
'escape_pattern'], $this->string_quoting[
'escape_pattern'] . $this->string_quoting[
'escape_pattern'], $text);
1656 foreach ($this->wildcards as $wildcard) {
1657 $text = str_replace($wildcard, $this->string_quoting[
'escape_pattern'] . $wildcard, $text);
1702 if ($check_option && !$this->options[
'quote_identifier']) {
1705 $str = str_replace($this->identifier_quoting[
'end'], $this->identifier_quoting[
'escape'] . $this->identifier_quoting[
'end'], $str);
1706 return $this->identifier_quoting[
'start'] . $str . $this->identifier_quoting[
'end'];
1759 foreach (
$row as $key => $value) {
1760 if ($value ===
'') {
1766 foreach (
$row as $key => $value) {
1767 if (is_string($value)) {
1768 $row[$key] = rtrim($value);
1774 foreach (
$row as $key => $value) {
1775 $tmp_row[preg_replace(
'/^(?:.*\.)?([^.]+)$/',
'\\1', $key)] = $value;
1780 foreach (
$row as $key => $value) {
1781 if ($value ===
'') {
1783 } elseif (is_string($value)) {
1784 $row[$key] = rtrim($value);
1790 foreach (
$row as $key => $value) {
1791 if (is_string($value)) {
1792 $value = rtrim($value);
1794 $tmp_row[preg_replace(
'/^(?:.*\.)?([^.]+)$/',
'\\1', $key)] = $value;
1800 foreach (
$row as $key => $value) {
1801 if ($value ===
'') {
1804 $tmp_row[preg_replace(
'/^(?:.*\.)?([^.]+)$/',
'\\1', $key)] = $value;
1810 foreach (
$row as $key => $value) {
1811 if ($value ===
'') {
1813 } elseif (is_string($value)) {
1814 $value = rtrim($value);
1816 $tmp_row[preg_replace(
'/^(?:.*\.)?([^.]+)$/',
'\\1', $key)] = $value;
1840 function &
loadModule($module, $property = null, $phptype_specific = null)
1843 $property = strtolower($module);
1846 if (!isset($this->{$property})) {
1847 $version = $phptype_specific;
1848 if ($phptype_specific !==
false) {
1851 $file_name = str_replace(
'_', DIRECTORY_SEPARATOR, $class_name).
'.php';
1853 if ($phptype_specific ===
false
1857 $class_name =
'MDB2_'.$module;
1858 $file_name = str_replace(
'_', DIRECTORY_SEPARATOR, $class_name).
'.php';
1868 if (method_exists($class_name,
'getClassName')) {
1869 $class_name_new = call_user_func(array($class_name,
'getClassName'), $this->db_index);
1870 if ($class_name != $class_name_new) {
1871 $class_name = $class_name_new;
1882 "unable to load module '$module' into property '$property'", __FUNCTION__);
1885 $this->{$property} =&
new $class_name($this->db_index);
1886 $this->modules[$module] =& $this->{$property};
1891 $this->loaded_version_modules[] = $property;
1895 return $this->{$property};
1912 if (preg_match(
'/^([a-z]+)([A-Z])(.*)$/', $method, $match)
1913 && isset($this->options[
'modules'][$match[1]])
1915 $module = $this->options[
'modules'][$match[1]];
1916 $method = strtolower($match[2]).$match[3];
1917 if (!isset($this->modules[$module]) || !is_object($this->modules[$module])) {
1924 foreach ($this->modules as $key => $foo) {
1925 if (is_object($this->modules[$key])
1926 && method_exists($this->modules[$key], $method)
1933 if (!is_null($module)) {
1934 return call_user_func_array(array(&$this->modules[$module], $method), $params);
1936 trigger_error(sprintf(
'Call to undefined function: %s::%s().', get_class($this), $method), E_USER_ERROR);
1952 $this->
debug(
'Starting transaction', __FUNCTION__, array(
'is_manip' =>
true,
'savepoint' => $savepoint));
1954 'transactions are not supported', __FUNCTION__);
1973 $this->
debug(
'Committing transaction/savepoint', __FUNCTION__, array(
'is_manip' =>
true,
'savepoint' => $savepoint));
1975 'commiting transactions is not supported', __FUNCTION__);
1994 $this->
debug(
'Rolling back transaction/savepoint', __FUNCTION__, array(
'is_manip' =>
true,
'savepoint' => $savepoint));
1996 'rolling back transactions is not supported', __FUNCTION__);
2015 if (!$ignore_nested && isset($this->nested_transaction_counter)) {
2042 $this->
debug(
'Setting transaction isolation level', __FUNCTION__, array(
'is_manip' =>
true));
2044 'isolation level setting is not supported', __FUNCTION__);
2065 if ($this->in_transaction) {
2067 $savepoint = sprintf($this->options[
'savepoint_format'], $this->nested_transaction_counter);
2068 if ($this->
supports(
'savepoints') && $savepoint) {
2073 $this->has_transaction_error =
false;
2075 $this->nested_transaction_counter = 1;
2101 if ($this->nested_transaction_counter > 1) {
2102 $savepoint = sprintf($this->options[
'savepoint_format'], $this->nested_transaction_counter);
2103 if ($this->
supports(
'savepoints') && $savepoint) {
2104 if ($force_rollback || $this->has_transaction_error) {
2105 $result = $this->
rollback($savepoint);
2108 $this->has_transaction_error =
false;
2111 $result = $this->
commit($savepoint);
2120 $this->nested_transaction_counter = null;
2124 if ($this->in_transaction) {
2125 if ($force_rollback || $this->has_transaction_error) {
2131 $result = $this->
commit();
2134 $this->has_transaction_error =
false;
2159 $error = $this->has_transaction_error ? $this->has_transaction_error :
true;
2163 $this->has_transaction_error =
$error;
2164 if (!$immediately) {
2202 'method not implemented', __FUNCTION__);
2219 'method not implemented', __FUNCTION__);
2238 $this->connection = 0;
2239 $this->connected_dsn = array();
2240 $this->connected_database_name =
'';
2241 $this->opened_persistent = null;
2242 $this->connected_server_info =
'';
2243 $this->in_transaction = null;
2244 $this->nested_transaction_counter = null;
2262 $previous_database_name = (isset($this->database_name)) ? $this->database_name :
'';
2263 $this->database_name =
$name;
2265 return $previous_database_name;
2297 $dsn_default =
$GLOBALS[
'_MDB2_dsninfo_default'];
2299 if (array_key_exists(
'database',
$dsn)) {
2300 $this->database_name =
$dsn[
'database'];
2301 unset(
$dsn[
'database']);
2303 $this->dsn = array_merge($dsn_default,
$dsn);
2322 $dsn = array_merge(
$GLOBALS[
'_MDB2_dsninfo_default'], $this->dsn);
2326 $dsn[
'password'] = $hidepw;
2332 (
$dsn[
'dbsyntax'] ? (
'('.$dsn[
'dbsyntax'].
')') :
'').
2333 '://'.
$dsn[
'username'].
':'.
2334 $dsn[
'password'].
'@'.
$dsn[
'hostspec'].
2335 (
$dsn[
'port'] ? (
':'.
$dsn[
'port']) :
'').
2336 '/'.
$dsn[
'database'];
2364 $this->offset = $this->limit = 0;
2379 return $affected_rows;
2421 $this->last_query =
$query;
2422 $result = $this->
debug(
$query,
'query', array(
'is_manip' => $is_manip,
'when' =>
'pre'));
2430 'method not implemented', __FUNCTION__);
2450 'method not implemented', __FUNCTION__);
2469 $this->offset = $this->limit = 0;
2483 return $affectedRows;
2502 function &
query(
$query, $types = null, $result_class =
true, $result_wrap_class =
false)
2506 $this->offset = $this->limit = 0;
2541 function &
_wrapResult($result, $types = array(), $result_class =
true,
2544 if ($types ===
true) {
2545 if ($this->
supports(
'result_introspection')) {
2547 $tableInfo = $this->reverse->tableInfo($result);
2552 foreach ($tableInfo as $field) {
2553 $types[] = $field[
'mdb2type'];
2560 if ($result_class ===
true) {
2561 $result_class = $this->options[
'result_buffering']
2562 ? $this->options[
'buffered_result_class'] : $this->options[
'result_class'];
2565 if ($result_class) {
2566 $class_name = sprintf($result_class, $this->phptype);
2569 'result class does not exist '.$class_name, __FUNCTION__);
2572 $result =&
new $class_name($this, $result,
$limit,
$offset);
2575 'result class is not extended from MDB2_Result_Common', __FUNCTION__);
2578 if (!empty($types)) {
2579 $err = $result->setResultTypes($types);
2586 if ($result_wrap_class ===
true) {
2587 $result_wrap_class = $this->options[
'result_wrap_class'];
2589 if ($result_wrap_class) {
2592 'result wrap class does not exist '.$result_wrap_class, __FUNCTION__);
2595 $result =&
new $result_wrap_class($result, $this->fetchmode);
2615 'method not implemented', __FUNCTION__);
2633 if (!$this->
supports(
'limit_queries')) {
2635 'limit is not supported by this driver', __FUNCTION__);
2640 'it was not specified a valid selected range row limit', __FUNCTION__);
2647 'it was not specified a valid first selected range row', __FUNCTION__);
2671 if ($this->
supports(
'sub_selects') ===
true) {
2675 if (!$this->
supports(
'sub_selects')) {
2677 'method not implemented', __FUNCTION__);
2684 if (!is_array($col) || count($col) == 0) {
2689 return $this->datatype->implodeArray($col,
$type);
2691 return implode(
', ', $col);
2764 'replace query is not supported', __FUNCTION__);
2766 $count = count($fields);
2767 $condition = $values = array();
2768 for ($colnum = 0, reset($fields); $colnum < $count; next($fields), $colnum++) {
2769 $name = key($fields);
2770 if (isset($fields[
$name][
'null']) && $fields[$name][
'null']) {
2773 $type = isset($fields[$name][
'type']) ? $fields[
$name][
'type'] : null;
2774 $value = $this->
quote($fields[$name][
'value'],
$type);
2776 $values[
$name] = $value;
2777 if (isset($fields[$name][
'key']) && $fields[$name][
'key']) {
2778 if ($value ===
'NULL') {
2780 'key value '.$name.
' may not be NULL', __FUNCTION__);
2782 $condition[] = $name .
'=' . $value;
2785 if (empty($condition)) {
2787 'not specified which fields are keys', __FUNCTION__);
2801 $condition =
' WHERE '.implode(
' AND ', $condition);
2802 $query =
"DELETE FROM $table$condition";
2806 $insert = implode(
', ', array_keys($values));
2807 $values = implode(
', ', $values);
2808 $query =
"INSERT INTO $table ($insert) VALUES ($values)";
2819 $result = $this->
commit();
2827 return $affected_rows;
2860 $this->offset = $this->limit = 0;
2861 $result = $this->
debug(
$query, __FUNCTION__, array(
'is_manip' => $is_manip,
'when' =>
'pre'));
2868 $placeholder_type_guess = $placeholder_type = null;
2871 $positions = array();
2876 while ($position < strlen(
$query)) {
2877 $q_position = strpos(
$query, $question, $position);
2878 $c_position = strpos(
$query, $colon, $position);
2879 if ($q_position && $c_position) {
2880 $p_position = min($q_position, $c_position);
2881 } elseif ($q_position) {
2882 $p_position = $q_position;
2883 } elseif ($c_position) {
2884 $p_position = $c_position;
2888 if (is_null($placeholder_type)) {
2889 $placeholder_type_guess =
$query[$p_position];
2896 if ($new_pos != $position) {
2897 $position = $new_pos;
2901 if (
$query[$position] == $placeholder_type_guess) {
2902 if (is_null($placeholder_type)) {
2903 $placeholder_type =
$query[$p_position];
2904 $question = $colon = $placeholder_type;
2905 if (!empty($types) && is_array($types)) {
2906 if ($placeholder_type ==
':') {
2907 if (is_int(key($types))) {
2908 $types_tmp = $types;
2913 $types = array_values($types);
2917 if ($placeholder_type ==
':') {
2918 $parameter = preg_replace(
'/^.{'.($position+1).
'}([a-z0-9_]+).*$/si',
'\\1',
$query);
2919 if ($parameter ===
'') {
2921 'named parameter with an empty name', __FUNCTION__);
2924 $positions[$p_position] = $parameter;
2925 $query = substr_replace(
$query,
'?', $position, strlen($parameter)+1);
2927 if (isset($count) && isset($types_tmp[++$count])) {
2928 $types[$parameter] = $types_tmp[$count];
2931 $positions[$p_position] = count($positions);
2933 $position = $p_position + 1;
2935 $position = $p_position;
2938 $class_name =
'MDB2_Statement_'.$this->phptype;
2940 $obj =&
new $class_name($this, $statement, $positions,
$query, $types, $result_types, $is_manip,
$limit,
$offset);
2941 $this->
debug(
$query, __FUNCTION__, array(
'is_manip' => $is_manip,
'when' =>
'post',
'result' => $obj));
2969 foreach ($ignores as $ignore) {
2970 if (!empty($ignore[
'start'])) {
2971 if (is_int($start_quote = strpos(
$query, $ignore[
'start'], $position)) && $start_quote < $p_position) {
2972 $end_quote = $start_quote;
2974 if (!is_int($end_quote = strpos(
$query, $ignore[
'end'], $end_quote + 1))) {
2975 if ($ignore[
'end'] ===
"\n") {
2976 $end_quote = strlen(
$query) - 1;
2979 'query with an unterminated text string specified', __FUNCTION__);
2983 }
while ($ignore[
'escape'] &&
$query[($end_quote - 1)] == $ignore[
'escape']);
2984 $position = $end_quote + 1;
3009 function quote($value,
$type = null, $quote =
true, $escape_wildcards =
false)
3011 $result = $this->
loadModule(
'Datatype', null,
true);
3016 return $this->datatype->quote($value,
$type, $quote, $escape_wildcards);
3037 $result = $this->
loadModule(
'Datatype', null,
true);
3041 return $this->datatype->getDeclaration(
$type,
$name, $field);
3059 $result = $this->
loadModule(
'Datatype', null,
true);
3063 return $this->datatype->compareDefinition($current, $previous);
3083 if (array_key_exists($feature, $this->supported)) {
3084 return $this->supported[$feature];
3087 "unknown support feature $feature", __FUNCTION__);
3104 return sprintf($this->options[
'seqname_format'],
3105 preg_replace(
'/[^a-z0-9_\$.]/i',
'_', $sqn));
3122 return sprintf($this->options[
'idxname_format'],
3123 preg_replace(
'/[^a-z0-9_\$]/i',
'_', $idx));
3142 'method not implemented', __FUNCTION__);
3162 'method not implemented', __FUNCTION__);
3179 $this->warnings[] =
'database does not support getting current
3180 sequence value, the sequence value was incremented';
3181 return $this->
nextID($seq_name);
3210 $one = $result->fetchOne($colnum);
3271 $col = $result->fetchCol($colnum);
3304 $rekey =
false, $force_array =
false, $group =
false)
3311 $all = $result->fetchAll(
$fetchmode, $rekey, $force_array, $group);
3368 $this->limit = max(0,
$limit - 1);
3408 $load = $this->db->loadModule(
'Datatype', null,
true);
3412 $types = $this->db->datatype->checkResultTypes(
$types);
3435 if ($this->rownum > $target_rownum) {
3437 'seeking to previous rows not implemented', __FUNCTION__);
3439 while ($this->rownum < $target_rownum) {
3461 'method not implemented', __FUNCTION__);
3485 if (!array_key_exists($colnum,
$row)) {
3487 'column is not defined in the result set: '.$colnum, __FUNCTION__);
3489 return $row[$colnum];
3509 if (is_array(
$row)) {
3510 if (!array_key_exists($colnum,
$row)) {
3512 'column is not defined in the result set: '.$colnum, __FUNCTION__);
3515 $column[] =
$row[$colnum];
3516 }
while (is_array(
$row = $this->
fetchRow($fetchmode)));
3551 $force_array =
false, $group =
false)
3561 $shift_array = $rekey ?
false : null;
3562 if (!is_null($shift_array)) {
3563 if (is_object(
$row)) {
3564 $colnum = count(get_object_vars(
$row));
3566 $colnum = count(
$row);
3570 'rekey feature requires atleast 2 column', __FUNCTION__);
3572 $shift_array = (!$force_array && $colnum == 2);
3577 if (is_object(
$row)) {
3578 $arr = get_object_vars(
$row);
3580 unset(
$row->{$key});
3586 $key = array_shift(
$row);
3593 $all[$key][] =
$row;
3600 foreach (
$row as $key => $val) {
3601 $all[$key][] = $val;
3623 return $this->rownum + 1;
3639 'method not implemented', __FUNCTION__);
3652 function nextResult()
3655 'method not implemented', __FUNCTION__);
3674 function getColumnNames($flip =
false)
3676 if (!isset($this->column_names)) {
3677 $result = $this->_getColumnNames();
3681 $this->column_names = $result;
3684 return array_flip($this->column_names);
3686 return $this->column_names;
3702 function _getColumnNames()
3705 'method not implemented', __FUNCTION__);
3722 'method not implemented', __FUNCTION__);
3735 function getResource()
3737 return $this->result;
3754 function bindColumn($column, &$value,
$type = null)
3756 if (!is_numeric($column)) {
3757 $column_names = $this->getColumnNames();
3759 if ($this->db->options[
'field_case'] == CASE_LOWER) {
3760 $column = strtolower($column);
3762 $column = strtoupper($column);
3765 $column = $column_names[$column];
3767 $this->values[$column] =& $value;
3768 if (!is_null(
$type)) {
3769 $this->types[$column] =
$type;
3789 foreach (
$row as $column => $value) {
3790 if (array_key_exists($column, $this->values)) {
3791 $this->values[$column] = $value;
3809 $this->result =
false;
3837 foreach (
$row as $key => $value) {
3838 $this->$key = &
$row[$key];
3892 $this->positions = $positions;
3894 $this->types = (array)
$types;
3930 if (!is_numeric($parameter)) {
3931 $parameter = preg_replace(
'/^:(.*)$/',
'\\1', $parameter);
3933 if (!in_array($parameter, $this->positions)) {
3935 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
3937 $this->values[$parameter] = $value;
3938 if (!is_null(
$type)) {
3939 $this->types[$parameter] =
$type;
3963 $parameters = array_keys(
$values);
3964 foreach ($parameters as $key => $parameter) {
3991 if (!is_numeric($parameter)) {
3992 $parameter = preg_replace(
'/^:(.*)$/',
'\\1', $parameter);
3994 if (!in_array($parameter, $this->positions)) {
3996 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
3998 $this->values[$parameter] =& $value;
3999 if (!is_null(
$type)) {
4000 $this->types[$parameter] =
$type;
4024 $parameters = array_keys(
$values);
4025 foreach ($parameters as $key => $parameter) {
4052 if (is_null($this->positions)) {
4053 return $this->db->raiseError(
MDB2_ERROR, null, null,
4054 'Prepared statement has already been freed', __FUNCTION__);
4061 return $this->db->raiseError(
MDB2_ERROR, null, null,
4062 'Binding Values failed with message: ' . $err->getMessage(), __FUNCTION__);
4065 $result =& $this->
_execute($result_class, $result_wrap_class);
4082 function &
_execute($result_class =
true, $result_wrap_class =
false)
4087 foreach ($this->positions as $current_position => $parameter) {
4088 if (!array_key_exists($parameter, $this->values)) {
4090 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
4092 $value = $this->values[$parameter];
4093 $query.= substr($this->query, $last_position, $current_position - $last_position);
4094 if (!isset($value)) {
4095 $value_quoted =
'NULL';
4097 $type = !empty($this->types[$parameter]) ? $this->types[$parameter] : null;
4098 $value_quoted = $this->db->quote($value,
$type);
4100 return $value_quoted;
4104 $last_position = $current_position + 1;
4106 $query.= substr($this->query, $last_position);
4110 if ($this->is_manip) {
4111 $result = $this->db->exec(
$query);
4113 $result =& $this->db->query(
$query, $this->result_types, $result_class, $result_wrap_class);
4130 if (is_null($this->positions)) {
4131 return $this->db->raiseError(
MDB2_ERROR, null, null,
4132 'Prepared statement has already been freed', __FUNCTION__);
4135 $this->statement = null;
4136 $this->positions = null;
4137 $this->query = null;
4138 $this->types = null;
4139 $this->result_types = null;
4140 $this->limit = null;
4141 $this->is_manip = null;
4142 $this->offset = null;
4143 $this->values = null;
4208 if (isset(
$GLOBALS[
'_MDB2_databases'][$this->db_index])) {
4212 'could not find MDB2 instance');
4233 reset(
$GLOBALS[
'_MDB2_databases']);
4234 while (next(
$GLOBALS[
'_MDB2_databases'])) {
4235 $key = key(
$GLOBALS[
'_MDB2_databases']);
4236 if (
$GLOBALS[
'_MDB2_databases'][$key]->opened_persistent
4237 &&
$GLOBALS[
'_MDB2_databases'][$key]->in_transaction
4239 $GLOBALS[
'_MDB2_databases'][$key]->rollback();
4265 $db->debug_output.= $scope.
'('.$db->db_index.
'): ';
4266 $db->debug_output.= $message.$db->getOption(
'log_line_break');