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']))
425 $this->dsn[
'hostspec'],
426 $this->dsn[
'username'],
427 $this->dsn[
'password'],
428 $this->database_name,
435 if (($err = @mysqli_connect_error()) !=
'') {
437 null, null, $err, __FUNCTION__);
440 'unable to establish a connection', __FUNCTION__);
444 if (!empty($this->dsn[
'charset'])) {
454 $this->dbsyntax = $this->dsn[
'dbsyntax'] ? $this->dsn[
'dbsyntax'] :
$this->phptype;
456 $this->supported[
'transactions'] = $this->options[
'use_transactions'];
457 if ($this->options[
'default_table_type']) {
458 switch (strtoupper($this->options[
'default_table_type'])) {
470 $this->supported[
'transactions'] =
false;
471 $this->warnings[] = $this->options[
'default_table_type'] .
472 ' is not a supported default table type';
519 if (is_object($this->connection)) {
520 if ($this->in_transaction) {
523 $persistent = $this->options[
'persistent'];
530 $this->options[
'persistent'] = $persistent;
534 @mysqli_close($this->connection);
554 $this->last_query =
$query;
555 $result = $this->
debug(
$query,
'query', array(
'is_manip' => $is_manip,
'when' =>
'pre'));
562 if ($this->options[
'disable_query']) {
563 $result = $is_manip ? 0 : null;
588 if ($this->options[
'multi_query']) {
591 $resultmode = $this->options[
'result_buffering'] ? MYSQLI_USE_RESULT : MYSQLI_USE_RESULT;
597 'Could not execute statement', __FUNCTION__);
601 if ($this->options[
'multi_query']) {
602 if ($this->options[
'result_buffering']) {
605 'Could not get the first result from a multi query', __FUNCTION__);
610 'Could not get the first result from a multi query', __FUNCTION__);
615 $this->
debug(
$query,
'query', array(
'is_manip' => $is_manip,
'when' =>
'post',
'result' =>
$result));
659 if (preg_match(
'/^\s*DELETE\s+FROM\s+(\S+)\s*$/i',
$query)) {
660 $query = preg_replace(
'/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
661 'DELETE FROM \1 WHERE 1=1',
$query);
665 && !preg_match(
'/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i',
$query)
668 if (substr(
$query, -1) ==
';') {
675 if (preg_match(
'/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims',
$query, $matches)) {
676 $after = $matches[0];
677 $query = preg_replace(
'/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims',
'',
$query);
678 } elseif (preg_match(
'/(\s+FOR\s+UPDATE\s*)$/i',
$query, $matches)) {
679 $after = $matches[0];
680 $query = preg_replace(
'/(\s+FOR\s+UPDATE\s*)$/im',
'',
$query);
681 } elseif (preg_match(
'/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im',
$query, $matches)) {
682 $after = $matches[0];
683 $query = preg_replace(
'/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im',
'',
$query);
687 return $query .
" LIMIT $limit";
689 return $query .
" LIMIT $offset, $limit";
711 if ($this->connected_server_info) {
714 $server_info = @mysqli_get_server_info(
$connection);
718 'Could not get server information', __FUNCTION__);
721 $this->connected_server_info = $server_info;
723 $tmp = explode(
'.', $server_info, 3);
724 if (isset($tmp[2]) && strpos($tmp[2],
'-')) {
725 $tmp2 = explode(
'-', @$tmp[2], 2);
727 $tmp2[0] = isset($tmp[2]) ? $tmp[2] : null;
730 $server_info = array(
731 'major' => isset($tmp[0]) ? $tmp[0] : null,
732 'minor' => isset($tmp[1]) ? $tmp[1] : null,
735 'native' => $server_info,
752 static $already_checked =
false;
753 if (!$already_checked) {
754 $already_checked =
true;
757 $this->supported[
'sub_selects'] =
'emulated';
758 $this->supported[
'prepared_statements'] =
'emulated';
759 $this->start_transaction =
false;
760 $this->varchar_max_length = 255;
763 if (is_array($server_info)) {
764 if (!version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'4.1.0',
'<')) {
765 $this->supported[
'sub_selects'] =
true;
766 $this->supported[
'prepared_statements'] =
true;
769 if (!version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'4.0.14',
'<')
770 || !version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'4.1.1',
'<')
772 $this->supported[
'savepoints'] =
true;
775 if (!version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'4.0.11',
'<')) {
776 $this->start_transaction =
true;
779 if (!version_compare($server_info[
'major'].
'.'.$server_info[
'minor'].
'.'.$server_info[
'patch'],
'5.0.3',
'<')) {
780 $this->varchar_max_length = 65532;
803 $found = strpos(strrev(substr(
$query, 0, $position)),
'@');
804 if ($found ===
false) {
807 $pos = strlen(
$query) - strlen(substr(
$query, $position)) - $found - 1;
808 $substring = substr(
$query, $pos, $position - $pos + 2);
809 if (preg_match(
'/^@\w+:=$/', $substring)) {
810 return $position + 1;
838 function &
prepare(
$query, $types = null, $result_types = null, $lobs = array())
840 if ($this->options[
'emulate_prepared']
841 || $this->supported[
'prepared_statements'] !==
true
849 $this->offset = $this->limit = 0;
851 $result = $this->
debug(
$query, __FUNCTION__, array(
'is_manip' => $is_manip,
'when' =>
'pre'));
858 $placeholder_type_guess = $placeholder_type = null;
861 $positions = array();
863 while ($position < strlen(
$query)) {
864 $q_position = strpos(
$query, $question, $position);
865 $c_position = strpos(
$query, $colon, $position);
866 if ($q_position && $c_position) {
867 $p_position = min($q_position, $c_position);
868 } elseif ($q_position) {
869 $p_position = $q_position;
870 } elseif ($c_position) {
871 $p_position = $c_position;
875 if (is_null($placeholder_type)) {
876 $placeholder_type_guess =
$query[$p_position];
883 if ($new_pos != $position) {
884 $position = $new_pos;
888 if (
$query[$position] == $placeholder_type_guess) {
889 if (is_null($placeholder_type)) {
890 $placeholder_type =
$query[$p_position];
891 $question = $colon = $placeholder_type;
893 if ($placeholder_type ==
':') {
896 if ($new_pos != $position) {
897 $position = $new_pos;
900 $parameter = preg_replace(
'/^.{'.($position+1).
'}([a-z0-9_]+).*$/si',
'\\1',
$query);
901 if ($parameter ===
'') {
903 'named parameter with an empty name', __FUNCTION__);
906 $positions[$p_position] = $parameter;
907 $query = substr_replace(
$query,
'?', $position, strlen($parameter)+1);
909 $positions[$p_position] = count($positions);
911 $position = $p_position + 1;
913 $position = $p_position;
922 $statement_name = sprintf($this->options[
'statement_format'], $this->phptype, md5(time() + rand()));
923 $query =
"PREPARE $statement_name FROM ".$this->quote(
$query,
'text');
929 $statement = $statement_name;
934 'Unable to create prepared statement handle', __FUNCTION__);
939 $class_name =
'MDB2_Statement_'.$this->phptype;
940 $obj =&
new $class_name($this, $statement, $positions,
$query, $types, $result_types, $is_manip,
$limit,
$offset);
941 $this->
debug(
$query, __FUNCTION__, array(
'is_manip' => $is_manip,
'when' =>
'post',
'result' => $obj));
1014 $count = count($fields);
1016 $keys = $colnum = 0;
1017 for (reset($fields); $colnum < $count; next($fields), $colnum++) {
1018 $name = key($fields);
1024 if (isset($fields[$name][
'null']) && $fields[$name][
'null']) {
1027 $type = isset($fields[$name][
'type']) ? $fields[$name][
'type'] : null;
1028 $value = $this->
quote($fields[$name][
'value'], $type);
1031 if (isset($fields[$name][
'key']) && $fields[$name][
'key']) {
1032 if ($value ===
'NULL') {
1034 'key value '.$name.
' may not be NULL', __FUNCTION__);
1041 'not specified which fields are keys', __FUNCTION__);
1049 $query =
"REPLACE INTO $table ($query) VALUES ($values)";
1074 $seqcol_name = $this->
quoteIdentifier($this->options[
'seqcol_name'],
true);
1075 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
1082 $result = $this->manager->createSequence($seq_name);
1085 'on demand sequence '.$seq_name.
' could not be created', __FUNCTION__);
1087 return $this->
nextID($seq_name,
false);
1093 if (is_numeric($value)) {
1094 $query =
"DELETE FROM $sequence_name WHERE $seqcol_name < $value";
1097 $this->warnings[] =
'nextID: could not delete previous sequence table values from '.$seq_name;
1118 return $this->
queryOne(
'SELECT LAST_INSERT_ID()');
1134 $seqcol_name = $this->
quoteIdentifier($this->options[
'seqcol_name'],
true);
1135 $query =
"SELECT MAX($seqcol_name) FROM $sequence_name";
1169 $fetchmode = $this->db->fetchmode;
1172 $row = @mysqli_fetch_assoc($this->result);
1176 $row = array_change_key_case(
$row, $this->db->options[
'field_case']);
1179 $row = @mysqli_fetch_row($this->result);
1183 if ($this->result ===
false) {
1185 'resultset has already been freed', __FUNCTION__);
1193 $this->db->_fixResultArrayValues(
$row, $mode);
1195 if (!empty($this->types)) {
1196 $row = $this->db->datatype->convertResultRow($this->types,
$row,
false);
1198 if (!empty($this->values)) {
1202 $object_class = $this->db->options[
'fetch_class'];
1203 if ($object_class ==
'stdClass') {
1232 for ($column = 0; $column < $numcols; $column++) {
1233 $column_info = @mysqli_fetch_field_direct($this->result, $column);
1234 $columns[$column_info->name] = $column;
1237 $columns = array_change_key_case(
$columns, $this->db->options[
'field_case']);
1254 $cols = @mysqli_num_fields($this->result);
1255 if (is_null($cols)) {
1256 if ($this->result ===
false) {
1258 'resultset has already been freed', __FUNCTION__);
1259 } elseif (is_null($this->result)) {
1260 return count($this->types);
1262 return $this->db->raiseError(null, null, null,
1263 'Could not get column count', __FUNCTION__);
1279 $connection = $this->db->getConnection();
1284 if (!@mysqli_more_results($connection)) {
1287 if (!@mysqli_next_result($connection)) {
1290 if (!($this->result = @mysqli_use_result($connection))) {
1307 if (is_object($this->result) && $this->db->connection) {
1308 $free = @mysqli_free_result($this->result);
1309 if ($free ===
false) {
1310 return $this->db->raiseError(null, null, null,
1311 'Could not free result', __FUNCTION__);
1314 $this->result =
false;
1340 if ($this->rownum != (
$rownum - 1) && !@mysqli_data_seek($this->result,
$rownum)) {
1341 if ($this->result ===
false) {
1343 'resultset has already been freed', __FUNCTION__);
1344 } elseif (is_null($this->result)) {
1348 'tried to seek to an invalid row number ('.
$rownum.
')', __FUNCTION__);
1369 return $this->rownum < ($numrows - 1);
1383 $rows = @mysqli_num_rows($this->result);
1384 if (is_null($rows)) {
1385 if ($this->result ===
false) {
1387 'resultset has already been freed', __FUNCTION__);
1388 } elseif (is_null($this->result)) {
1391 return $this->db->raiseError(null, null, null,
1392 'Could not get row count', __FUNCTION__);
1409 $connection = $this->db->getConnection();
1414 if (!@mysqli_more_results($connection)) {
1417 if (!@mysqli_next_result($connection)) {
1420 if (!($this->result = @mysqli_store_result($connection))) {
1446 function &
_execute($result_class =
true, $result_wrap_class =
false)
1448 if (is_null($this->statement)) {
1453 $this->db->debug($this->query,
'execute', array(
'is_manip' => $this->is_manip,
'when' =>
'pre',
'parameters' => $this->values));
1454 if ($this->db->getOption(
'disable_query')) {
1455 $result = $this->is_manip ? 0 : null;
1459 $connection = $this->db->getConnection();
1464 if (!is_object($this->statement)) {
1465 $query =
'EXECUTE '.$this->statement;
1467 if (!empty($this->positions)) {
1468 $parameters = array(0 => $this->statement, 1 =>
'');
1471 foreach ($this->positions as $parameter) {
1472 if (!array_key_exists($parameter, $this->values)) {
1474 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
1476 $value = $this->values[$parameter];
1477 $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null;
1478 if (!is_object($this->statement)) {
1479 if (is_resource($value) || $type ==
'clob' || $type ==
'blob') {
1480 if (!is_resource($value) && preg_match(
'/^(\w+:\/\/)(.*)$/', $value, $match)) {
1481 if ($match[1] ==
'file://') {
1484 $value = @fopen($value,
'r');
1487 if (is_resource($value)) {
1489 while (!@feof($value)) {
1490 $data.= @fread($value, $this->db->options[
'lob_buffer_length']);
1498 $quoted = $this->db->quote($value, $type);
1502 $param_query =
'SET @'.$parameter.
' = '.$quoted;
1503 $result = $this->db->_doQuery($param_query,
true, $connection);
1508 if (is_resource($value) || $type ==
'clob' || $type ==
'blob') {
1509 $parameters[] = null;
1510 $parameters[1].=
'b';
1511 $lobs[$i] = $parameter;
1513 $parameters[] = $this->db->quote($value, $type,
false);
1514 $parameters[1].= $this->db->datatype->mapPrepareDatatype($type);
1520 if (!is_object($this->statement)) {
1521 $query.=
' USING @'.implode(
', @', array_values($this->positions));
1525 $stmt_params = array();
1526 foreach ($parameters as $k => &$value) {
1527 $stmt_params[$k] = &$value;
1530 $result = @call_user_func_array(
'mysqli_stmt_bind_param', $stmt_params);
1532 $err =& $this->db->raiseError(null, null, null,
1533 'Unable to bind parameters', __FUNCTION__);
1537 foreach ($lobs as $i => $parameter) {
1538 $value = $this->values[$parameter];
1540 if (!is_resource($value)) {
1542 if (preg_match(
'/^(\w+:\/\/)(.*)$/', $value, $match)) {
1543 if ($match[1] ==
'file://') {
1546 $value = @fopen($value,
'r');
1549 @fwrite($fp, $value);
1554 while (!@feof($value)) {
1555 $data = @fread($value, $this->db->options[
'lob_buffer_length']);
1556 @mysqli_stmt_send_long_data($this->statement, $i,
$data);
1565 if (!is_object($this->statement)) {
1566 $result = $this->db->_doQuery(
$query, $this->is_manip, $connection);
1571 if ($this->is_manip) {
1572 $affected_rows = $this->db->_affectedRows($connection,
$result);
1573 return $affected_rows;
1577 $result_class, $result_wrap_class, $this->limit, $this->offset);
1579 if (!@mysqli_stmt_execute($this->statement)) {
1580 $err =& $this->db->raiseError(null, null, null,
1581 'Unable to execute statement', __FUNCTION__);
1585 if ($this->is_manip) {
1586 $affected_rows = @mysqli_stmt_affected_rows($this->statement);
1587 return $affected_rows;
1590 if ($this->db->options[
'result_buffering']) {
1591 @mysqli_stmt_store_result($this->statement);
1594 $result =& $this->db->_wrapResult($this->statement, $this->result_types,
1595 $result_class, $result_wrap_class, $this->limit, $this->offset);
1598 $this->db->debug($this->query,
'execute', array(
'is_manip' => $this->is_manip,
'when' =>
'post',
'result' =>
$result));
1613 if (is_null($this->positions)) {
1614 return $this->db->raiseError(
MDB2_ERROR, null, null,
1615 'Prepared statement has already been freed', __FUNCTION__);
1619 if (is_object($this->statement)) {
1620 if (!@mysqli_stmt_close($this->statement)) {
1621 $result = $this->db->raiseError(null, null, null,
1622 'Could not free statement', __FUNCTION__);
1624 } elseif (!is_null($this->statement)) {
1625 $connection = $this->db->getConnection();
1630 $query =
'DEALLOCATE PREPARE '.$this->statement;