59 var
$string_quoting = array(
'start' =>
"'",
'end' =>
"'",
'escape' =>
'\\',
'escape_pattern' =>
'\\');
64 array(
'start' =>
'-- ',
'end' =>
"\n",
'escape' =>
false),
65 array(
'start' =>
'#',
'end' =>
"\n",
'escape' =>
false),
66 array(
'start' =>
'/*',
'end' =>
'*/',
'escape' =>
false),
82 $this->phptype =
'mysqli';
83 $this->dbsyntax =
'mysql';
85 $this->supported[
'sequences'] =
'emulated';
86 $this->supported[
'indexes'] =
true;
87 $this->supported[
'affected_rows'] =
true;
88 $this->supported[
'transactions'] =
false;
89 $this->supported[
'savepoints'] =
false;
90 $this->supported[
'summary_functions'] =
true;
91 $this->supported[
'order_by_text'] =
true;
92 $this->supported[
'current_id'] =
'emulated';
93 $this->supported[
'limit_queries'] =
true;
94 $this->supported[
'LOBs'] =
true;
95 $this->supported[
'replace'] =
true;
96 $this->supported[
'sub_selects'] =
'emulated';
97 $this->supported[
'auto_increment'] =
true;
98 $this->supported[
'primary_key'] =
true;
99 $this->supported[
'result_introspection'] =
true;
100 $this->supported[
'prepared_statements'] =
'emulated';
101 $this->supported[
'identifier_quoting'] =
true;
102 $this->supported[
'pattern_escaping'] =
true;
103 $this->supported[
'new_link'] =
true;
105 $this->options[
'default_table_type'] =
'';
106 $this->options[
'multi_query'] =
false;
121 if ($this->connection) {
122 $native_code = @mysqli_errno($this->connection);
123 $native_msg = @mysqli_error($this->connection);
125 $native_code = @mysqli_connect_errno();
126 $native_msg = @mysqli_connect_error();
128 if (is_null($error)) {
130 if (empty($ecode_map)) {
170 if (isset($ecode_map[$native_code])) {
171 $error = $ecode_map[$native_code];
174 return array($error, $native_code, $native_msg);
191 function escape($text, $escape_wildcards =
false)
193 if ($escape_wildcards) {
200 $text = @mysqli_real_escape_string(
$connection, $text);
217 $this->
debug(
'Starting transaction/savepoint', __FUNCTION__, array(
'is_manip' =>
true,
'savepoint' => $savepoint));
219 if (!is_null($savepoint)) {
220 if (!$this->
supports(
'savepoints')) {
222 'savepoints are not supported', __FUNCTION__);
224 if (!$this->in_transaction) {
226 'savepoint cannot be released when changes are auto committed', __FUNCTION__);
228 $query =
'SAVEPOINT '.$savepoint;
230 } elseif ($this->in_transaction) {
233 $query = $this->start_transaction ?
'START TRANSACTION' :
'SET AUTOCOMMIT = 1';
238 $this->in_transaction =
true;
258 $this->
debug(
'Committing transaction/savepoint', __FUNCTION__, array(
'is_manip' =>
true,
'savepoint' => $savepoint));
259 if (!$this->in_transaction) {
261 'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);
263 if (!is_null($savepoint)) {
264 if (!$this->
supports(
'savepoints')) {
266 'savepoints are not supported', __FUNCTION__);
269 if (version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'5.0.3',
'<')) {
272 $query =
'RELEASE SAVEPOINT '.$savepoint;
276 if (!$this->
supports(
'transactions')) {
278 'transactions are not supported', __FUNCTION__);
285 if (!$this->start_transaction) {
286 $query =
'SET AUTOCOMMIT = 0';
292 $this->in_transaction =
false;
312 $this->
debug(
'Rolling back transaction/savepoint', __FUNCTION__, array(
'is_manip' =>
true,
'savepoint' => $savepoint));
313 if (!$this->in_transaction) {
315 'rollback cannot be done changes are auto committed', __FUNCTION__);
317 if (!is_null($savepoint)) {
318 if (!$this->
supports(
'savepoints')) {
320 'savepoints are not supported', __FUNCTION__);
322 $query =
'ROLLBACK TO SAVEPOINT '.$savepoint;
331 if (!$this->start_transaction) {
332 $query =
'SET AUTOCOMMIT = 0';
338 $this->in_transaction =
false;
360 $this->
debug(
'Setting transaction isolation level', __FUNCTION__, array(
'is_manip' =>
true));
361 if (!$this->
supports(
'transactions')) {
363 'transactions are not supported', __FUNCTION__);
365 switch ($isolation) {
366 case 'READ UNCOMMITTED':
367 case 'READ COMMITTED':
368 case 'REPEATABLE READ':
373 'isolation level is not supported: '.$isolation, __FUNCTION__);
376 $query =
"SET SESSION TRANSACTION ISOLATION LEVEL $isolation";
390 if (is_object($this->connection)) {
391 if (count(array_diff($this->connected_dsn, $this->dsn)) == 0) {
394 $this->connection = 0;
399 'extension '.$this->phptype.
' is not compiled into PHP', __FUNCTION__);
402 if ($this->options[
'ssl']) {
403 $init = @mysqli_init();
406 empty($this->dsn[
'key']) ? null : $this->dsn[
'key'],
407 empty($this->dsn[
'cert']) ? null : $this->dsn[
'cert'],
408 empty($this->dsn[
'ca']) ? null : $this->dsn[
'ca'],
409 empty($this->dsn[
'capath']) ? null : $this->dsn[
'capath'],
410 empty($this->dsn[
'cipher']) ? null : $this->dsn[
'cipher']
414 $this->dsn[
'hostspec'],
415 $this->dsn[
'username'],
416 $this->dsn[
'password'],
417 $this->database_name,
419 $this->dsn[
'socket']))
426 if(!is_string($this->dsn[
'socket'])) {
427 $this->dsn[
'socket'] =
"";
429 if(!is_string($this->dsn[
'port'])) {
430 $this->dsn[
'port'] = 0;
435 $this->dsn[
'hostspec'],
436 $this->dsn[
'username'],
437 $this->dsn[
'password'],
438 $this->database_name,
452 if (($err = @mysqli_connect_error()) !=
'') {
454 null, null, $err, __FUNCTION__);
457 'unable to establish a connection', __FUNCTION__);
461 if (!empty($this->dsn[
'charset'])) {
471 $this->dbsyntax = $this->dsn[
'dbsyntax'] ? $this->dsn[
'dbsyntax'] :
$this->phptype;
473 $this->supported[
'transactions'] = $this->options[
'use_transactions'];
474 if ($this->options[
'default_table_type']) {
475 switch (strtoupper($this->options[
'default_table_type'])) {
487 $this->supported[
'transactions'] =
false;
488 $this->warnings[] = $this->options[
'default_table_type'] .
489 ' is not a supported default table type';
536 if (is_object($this->connection)) {
537 if ($this->in_transaction) {
540 $persistent = $this->options[
'persistent'];
547 $this->options[
'persistent'] = $persistent;
551 @mysqli_close($this->connection);
571 $this->last_query =
$query;
572 $result = $this->
debug(
$query,
'query', array(
'is_manip' => $is_manip,
'when' =>
'pre'));
579 if ($this->options[
'disable_query']) {
580 $result = $is_manip ? 0 : null;
605 if ($this->options[
'multi_query']) {
608 $resultmode = $this->options[
'result_buffering'] ? MYSQLI_USE_RESULT : MYSQLI_USE_RESULT;
614 'Could not execute statement', __FUNCTION__);
618 if ($this->options[
'multi_query']) {
619 if ($this->options[
'result_buffering']) {
622 'Could not get the first result from a multi query', __FUNCTION__);
627 'Could not get the first result from a multi query', __FUNCTION__);
632 $this->
debug(
$query,
'query', array(
'is_manip' => $is_manip,
'when' =>
'post',
'result' =>
$result));
676 if (preg_match(
'/^\s*DELETE\s+FROM\s+(\S+)\s*$/i',
$query)) {
677 $query = preg_replace(
'/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
678 'DELETE FROM \1 WHERE 1=1',
$query);
682 && !preg_match(
'/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i',
$query)
685 if (substr(
$query, -1) ==
';') {
692 if (preg_match(
'/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims',
$query, $matches)) {
693 $after = $matches[0];
694 $query = preg_replace(
'/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims',
'',
$query);
695 } elseif (preg_match(
'/(\s+FOR\s+UPDATE\s*)$/i',
$query, $matches)) {
696 $after = $matches[0];
697 $query = preg_replace(
'/(\s+FOR\s+UPDATE\s*)$/im',
'',
$query);
698 } elseif (preg_match(
'/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im',
$query, $matches)) {
699 $after = $matches[0];
700 $query = preg_replace(
'/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im',
'',
$query);
704 return $query .
" LIMIT $limit";
706 return $query .
" LIMIT $offset, $limit";
728 if ($this->connected_server_info) {
731 $server_info = @mysqli_get_server_info(
$connection);
735 'Could not get server information', __FUNCTION__);
738 $this->connected_server_info = $server_info;
740 $tmp = explode(
'.', $server_info, 3);
741 if (isset($tmp[2]) && strpos($tmp[2],
'-')) {
742 $tmp2 = explode(
'-', @$tmp[2], 2);
744 $tmp2[0] = isset($tmp[2]) ? $tmp[2] : null;
747 $server_info = array(
748 'major' => isset($tmp[0]) ? $tmp[0] : null,
749 'minor' => isset($tmp[1]) ? $tmp[1] : null,
752 'native' => $server_info,
769 static $already_checked =
false;
770 if (!$already_checked) {
771 $already_checked =
true;
774 $this->supported[
'sub_selects'] =
'emulated';
775 $this->supported[
'prepared_statements'] =
'emulated';
776 $this->start_transaction =
false;
777 $this->varchar_max_length = 255;
780 if (is_array($server_info)) {
781 if (!version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'4.1.0',
'<')) {
782 $this->supported[
'sub_selects'] =
true;
783 $this->supported[
'prepared_statements'] =
true;
786 if (!version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'4.0.14',
'<')
787 || !version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'4.1.1',
'<')
789 $this->supported[
'savepoints'] =
true;
792 if (!version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'4.0.11',
'<')) {
793 $this->start_transaction =
true;
796 if (!version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'5.0.3',
'<')) {
797 $this->varchar_max_length = 65532;
820 $found = strpos(strrev(substr(
$query, 0, $position)),
'@');
821 if ($found ===
false) {
824 $pos = strlen(
$query) - strlen(substr(
$query, $position)) - $found - 1;
825 $substring = substr(
$query, $pos, $position - $pos + 2);
826 if (preg_match(
'/^@\w+:=$/', $substring)) {
827 return $position + 1;
855 function &
prepare(
$query, $types = null, $result_types = null, $lobs = array())
857 if ($this->options[
'emulate_prepared']
858 || $this->supported[
'prepared_statements'] !==
true
866 $this->offset = $this->limit = 0;
868 $result = $this->
debug(
$query, __FUNCTION__, array(
'is_manip' => $is_manip,
'when' =>
'pre'));
875 $placeholder_type_guess = $placeholder_type = null;
878 $positions = array();
880 while ($position < strlen(
$query)) {
881 $q_position = strpos(
$query, $question, $position);
882 $c_position = strpos(
$query, $colon, $position);
883 if ($q_position && $c_position) {
884 $p_position = min($q_position, $c_position);
885 } elseif ($q_position) {
886 $p_position = $q_position;
887 } elseif ($c_position) {
888 $p_position = $c_position;
892 if (is_null($placeholder_type)) {
893 $placeholder_type_guess =
$query[$p_position];
900 if ($new_pos != $position) {
901 $position = $new_pos;
905 if (
$query[$position] == $placeholder_type_guess) {
906 if (is_null($placeholder_type)) {
907 $placeholder_type =
$query[$p_position];
908 $question = $colon = $placeholder_type;
910 if ($placeholder_type ==
':') {
913 if ($new_pos != $position) {
914 $position = $new_pos;
917 $parameter = preg_replace(
'/^.{'.($position+1).
'}([a-z0-9_]+).*$/si',
'\\1',
$query);
918 if ($parameter ===
'') {
920 'named parameter with an empty name', __FUNCTION__);
923 $positions[$p_position] = $parameter;
924 $query = substr_replace(
$query,
'?', $position, strlen($parameter)+1);
926 $positions[$p_position] = count($positions);
928 $position = $p_position + 1;
930 $position = $p_position;
939 $statement_name = sprintf($this->options[
'statement_format'], $this->phptype, md5(time() + rand()));
940 $query =
"PREPARE $statement_name FROM ".$this->quote(
$query,
'text');
946 $statement = $statement_name;
951 'Unable to create prepared statement handle', __FUNCTION__);
956 $class_name =
'MDB2_Statement_'.$this->phptype;
957 $obj =&
new $class_name($this, $statement, $positions,
$query, $types, $result_types, $is_manip,
$limit,
$offset);
958 $this->
debug(
$query, __FUNCTION__, array(
'is_manip' => $is_manip,
'when' =>
'post',
'result' => $obj));
1031 $count = count($fields);
1033 $keys = $colnum = 0;
1034 for (reset($fields); $colnum < $count; next($fields), $colnum++) {
1035 $name = key($fields);
1041 if (isset($fields[$name][
'null']) && $fields[$name][
'null']) {
1044 $type = isset($fields[$name][
'type']) ? $fields[$name][
'type'] : null;
1045 $value = $this->
quote($fields[$name][
'value'], $type);
1048 if (isset($fields[$name][
'key']) && $fields[$name][
'key']) {
1049 if ($value ===
'NULL') {
1051 'key value '.$name.
' may not be NULL', __FUNCTION__);
1058 'not specified which fields are keys', __FUNCTION__);
1066 $query =
"REPLACE INTO $table ($query) VALUES ($values)";
1091 $seqcol_name = $this->
quoteIdentifier($this->options[
'seqcol_name'],
true);
1092 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
1099 $result = $this->manager->createSequence($seq_name);
1102 'on demand sequence '.$seq_name.
' could not be created', __FUNCTION__);
1104 return $this->
nextID($seq_name,
false);
1110 if (is_numeric($value)) {
1111 $query =
"DELETE FROM $sequence_name WHERE $seqcol_name < $value";
1114 $this->warnings[] =
'nextID: could not delete previous sequence table values from '.$seq_name;
1135 return $this->
queryOne(
'SELECT LAST_INSERT_ID()');
1151 $seqcol_name = $this->
quoteIdentifier($this->options[
'seqcol_name'],
true);
1152 $query =
"SELECT MAX($seqcol_name) FROM $sequence_name";
1186 $fetchmode = $this->db->fetchmode;
1189 $row = @mysqli_fetch_assoc($this->result);
1193 $row = array_change_key_case(
$row, $this->db->options[
'field_case']);
1196 $row = @mysqli_fetch_row($this->result);
1200 if ($this->result ===
false) {
1202 'resultset has already been freed', __FUNCTION__);
1210 $this->db->_fixResultArrayValues(
$row, $mode);
1212 if (!empty($this->types)) {
1213 $row = $this->db->datatype->convertResultRow($this->types,
$row,
false);
1215 if (!empty($this->values)) {
1219 $object_class = $this->db->options[
'fetch_class'];
1220 if ($object_class ==
'stdClass') {
1249 for ($column = 0; $column < $numcols; $column++) {
1250 $column_info = @mysqli_fetch_field_direct($this->result, $column);
1251 $columns[$column_info->name] = $column;
1254 $columns = array_change_key_case(
$columns, $this->db->options[
'field_case']);
1271 $cols = @mysqli_num_fields($this->result);
1272 if (is_null($cols)) {
1273 if ($this->result ===
false) {
1275 'resultset has already been freed', __FUNCTION__);
1276 } elseif (is_null($this->result)) {
1277 return count($this->types);
1279 return $this->db->raiseError(null, null, null,
1280 'Could not get column count', __FUNCTION__);
1296 $connection = $this->db->getConnection();
1301 if (!@mysqli_more_results($connection)) {
1304 if (!@mysqli_next_result($connection)) {
1307 if (!($this->result = @mysqli_use_result($connection))) {
1324 if (is_object($this->result) && $this->db->connection) {
1325 $free = @mysqli_free_result($this->result);
1326 if ($free ===
false) {
1327 return $this->db->raiseError(null, null, null,
1328 'Could not free result', __FUNCTION__);
1331 $this->result =
false;
1357 if ($this->rownum != (
$rownum - 1) && !@mysqli_data_seek($this->result,
$rownum)) {
1358 if ($this->result ===
false) {
1360 'resultset has already been freed', __FUNCTION__);
1361 } elseif (is_null($this->result)) {
1365 'tried to seek to an invalid row number ('.
$rownum.
')', __FUNCTION__);
1386 return $this->rownum < ($numrows - 1);
1400 $rows = @mysqli_num_rows($this->result);
1401 if (is_null($rows)) {
1402 if ($this->result ===
false) {
1404 'resultset has already been freed', __FUNCTION__);
1405 } elseif (is_null($this->result)) {
1408 return $this->db->raiseError(null, null, null,
1409 'Could not get row count', __FUNCTION__);
1426 $connection = $this->db->getConnection();
1431 if (!@mysqli_more_results($connection)) {
1434 if (!@mysqli_next_result($connection)) {
1437 if (!($this->result = @mysqli_store_result($connection))) {
1463 function &
_execute($result_class =
true, $result_wrap_class =
false)
1465 if (is_null($this->statement)) {
1470 $this->db->debug($this->query,
'execute', array(
'is_manip' => $this->is_manip,
'when' =>
'pre',
'parameters' => $this->values));
1471 if ($this->db->getOption(
'disable_query')) {
1472 $result = $this->is_manip ? 0 : null;
1476 $connection = $this->db->getConnection();
1481 if (!is_object($this->statement)) {
1482 $query =
'EXECUTE '.$this->statement;
1484 if (!empty($this->positions)) {
1485 $parameters = array(0 => $this->statement, 1 =>
'');
1488 foreach ($this->positions as $parameter) {
1489 if (!array_key_exists($parameter, $this->values)) {
1491 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
1493 $value = $this->values[$parameter];
1494 $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null;
1495 if (!is_object($this->statement)) {
1496 if (is_resource($value) || $type ==
'clob' || $type ==
'blob') {
1497 if (!is_resource($value) && preg_match(
'/^(\w+:\/\/)(.*)$/', $value, $match)) {
1498 if ($match[1] ==
'file://') {
1501 $value = @fopen($value,
'r');
1504 if (is_resource($value)) {
1506 while (!@feof($value)) {
1507 $data.= @fread($value, $this->db->options[
'lob_buffer_length']);
1515 $quoted = $this->db->quote($value, $type);
1519 $param_query =
'SET @'.$parameter.
' = '.$quoted;
1520 $result = $this->db->_doQuery($param_query,
true, $connection);
1525 if (is_resource($value) || $type ==
'clob' || $type ==
'blob') {
1526 $parameters[] = null;
1527 $parameters[1].=
'b';
1528 $lobs[$i] = $parameter;
1530 $parameters[] = $this->db->quote($value, $type,
false);
1531 $parameters[1].= $this->db->datatype->mapPrepareDatatype($type);
1537 if (!is_object($this->statement)) {
1538 $query.=
' USING @'.implode(
', @', array_values($this->positions));
1542 $stmt_params = array();
1543 foreach ($parameters as $k => &$value) {
1544 $stmt_params[$k] = &$value;
1547 $result = @call_user_func_array(
'mysqli_stmt_bind_param', $stmt_params);
1549 $err =& $this->db->raiseError(null, null, null,
1550 'Unable to bind parameters', __FUNCTION__);
1554 foreach ($lobs as $i => $parameter) {
1555 $value = $this->values[$parameter];
1557 if (!is_resource($value)) {
1559 if (preg_match(
'/^(\w+:\/\/)(.*)$/', $value, $match)) {
1560 if ($match[1] ==
'file://') {
1563 $value = @fopen($value,
'r');
1566 @fwrite($fp, $value);
1571 while (!@feof($value)) {
1572 $data = @fread($value, $this->db->options[
'lob_buffer_length']);
1573 @mysqli_stmt_send_long_data($this->statement, $i, $data);
1582 if (!is_object($this->statement)) {
1583 $result = $this->db->_doQuery(
$query, $this->is_manip, $connection);
1588 if ($this->is_manip) {
1589 $affected_rows = $this->db->_affectedRows($connection,
$result);
1590 return $affected_rows;
1594 $result_class, $result_wrap_class, $this->limit, $this->offset);
1596 if (!@mysqli_stmt_execute($this->statement)) {
1597 $err =& $this->db->raiseError(null, null, null,
1598 'Unable to execute statement', __FUNCTION__);
1602 if ($this->is_manip) {
1603 $affected_rows = @mysqli_stmt_affected_rows($this->statement);
1604 return $affected_rows;
1607 if ($this->db->options[
'result_buffering']) {
1608 @mysqli_stmt_store_result($this->statement);
1611 $result =& $this->db->_wrapResult($this->statement, $this->result_types,
1612 $result_class, $result_wrap_class, $this->limit, $this->offset);
1615 $this->db->debug($this->query,
'execute', array(
'is_manip' => $this->is_manip,
'when' =>
'post',
'result' =>
$result));
1630 if (is_null($this->positions)) {
1631 return $this->db->raiseError(
MDB2_ERROR, null, null,
1632 'Prepared statement has already been freed', __FUNCTION__);
1636 if (is_object($this->statement)) {
1637 if (!@mysqli_stmt_close($this->statement)) {
1638 $result = $this->db->raiseError(null, null, null,
1639 'Could not free statement', __FUNCTION__);
1641 } elseif (!is_null($this->statement)) {
1642 $connection = $this->db->getConnection();
1647 $query =
'DEALLOCATE PREPARE '.$this->statement;