10 defined(
'JPATH_PLATFORM') or die;
28 public $name =
'mysqli';
39 protected $nameQuote =
'`';
48 protected $nullDate =
'0000-00-00 00:00:00';
54 protected static $dbMinimum =
'5.0.4';
63 public function __construct($options)
66 $options[
'host'] = (isset($options[
'host'])) ? $options[
'host'] :
'localhost';
67 $options[
'user'] = (isset($options[
'user'])) ? $options[
'user'] :
'root';
68 $options[
'password'] = (isset($options[
'password'])) ? $options[
'password'] :
'';
69 $options[
'database'] = (isset($options[
'database'])) ? $options[
'database'] :
'';
70 $options[
'select'] = (isset($options[
'select'])) ? (
bool) $options[
'select'] :
true;
71 $options[
'port'] = null;
72 $options[
'socket'] = null;
75 parent::__construct($options);
83 public function __destruct()
96 public function connect()
98 if ($this->connection)
107 $port = isset($this->options[
'port']) ? $this->options[
'port'] : 3306;
109 if (preg_match(
'/^(?P<host>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:(?P<port>.+))?$/', $this->options[
'host'], $matches))
112 $this->options[
'host'] = $matches[
'host'];
114 if (!empty($matches[
'port']))
116 $port = $matches[
'port'];
119 elseif (preg_match(
'/^(?P<host>\[.*\])(:(?P<port>.+))?$/', $this->options[
'host'], $matches))
122 $this->options[
'host'] = $matches[
'host'];
124 if (!empty($matches[
'port']))
126 $port = $matches[
'port'];
129 elseif (preg_match(
'/^(?P<host>(\w+:\/{2,3})?[a-z0-9\.\-]+)(:(?P<port>[^:]+))?$/i', $this->options[
'host'], $matches))
132 $this->options[
'host'] = $matches[
'host'];
134 if (!empty($matches[
'port']))
136 $port = $matches[
'port'];
139 elseif (preg_match(
'/^:(?P<port>[^:]+)$/', $this->options[
'host'], $matches))
142 $this->options[
'host'] =
'localhost';
143 $port = $matches[
'port'];
148 if (is_numeric($port))
150 $this->options[
'port'] = (int) $port;
154 $this->options[
'socket'] = $port;
158 if (!function_exists(
'mysqli_connect'))
160 throw new RuntimeException(
'The MySQL adapter mysqli is not available');
163 $this->connection = @mysqli_connect(
164 $this->options[
'host'], $this->options[
'user'], $this->options[
'password'], null, $this->options[
'port'], $this->options[
'socket']
168 if (!$this->connection)
170 throw new RuntimeException(
'Could not connect to MySQL.');
174 mysqli_query($this->connection,
"SET @@SESSION.sql_mode = '';");
177 if ($this->options[
'select'] && !empty($this->options[
'database']))
179 $this->select($this->options[
'database']);
186 if ($this->debug && $this->hasProfiling())
188 mysqli_query($this->connection,
"SET profiling_history_size = 100;");
189 mysqli_query($this->connection,
"SET profiling = 1;");
200 public function disconnect()
203 if ($this->connection)
205 foreach ($this->disconnectHandlers as $h)
207 call_user_func_array($h, array( &$this));
210 mysqli_close($this->connection);
213 $this->connection = null;
226 public function escape($text, $extra =
false)
230 $result = mysqli_real_escape_string($this->getConnection(), $text);
234 $result = addcslashes($result,
'%_');
247 public static function isSupported()
249 return (function_exists(
'mysqli_connect'));
259 public function connected()
261 if (is_object($this->connection))
263 return mysqli_ping($this->connection);
280 public function dropTable($tableName, $ifExists =
true)
284 $query = $this->getQuery(
true);
286 $this->setQuery(
'DROP TABLE ' . ($ifExists ?
'IF EXISTS ' :
'') . $query->quoteName($tableName));
300 public function getAffectedRows()
304 return mysqli_affected_rows($this->connection);
315 public function getCollation()
319 $tables = $this->getTableList();
321 $this->setQuery(
'SHOW FULL COLUMNS FROM ' . $tables[0]);
322 $array = $this->loadAssocList();
324 foreach ($array as $field)
326 if (!is_null($field[
'Collation']))
328 return $field[
'Collation'];
344 public function getNumRows($cursor = null)
346 return mysqli_num_rows($cursor ? $cursor : $this->cursor);
359 public function getTableCreate($tables)
366 settype($tables,
'array');
367 foreach ($tables as $table)
370 $this->setQuery(
'SHOW CREATE table ' . $this->quoteName($this->escape($table)));
371 $row = $this->loadRow();
374 $result[$table] = $row[1];
391 public function getTableColumns($table, $typeOnly =
true)
398 $this->setQuery(
'SHOW FULL COLUMNS FROM ' . $this->quoteName($this->escape($table)));
399 $fields = $this->loadObjectList();
404 foreach ($fields as $field)
406 $result[$field->Field] = preg_replace(
"/[(0-9)]/",
'', $field->Type);
412 foreach ($fields as $field)
414 $result[$field->Field] = $field;
431 public function getTableKeys($table)
436 $this->setQuery(
'SHOW KEYS FROM ' . $this->quoteName($table));
437 $keys = $this->loadObjectList();
450 public function getTableList()
455 $this->setQuery(
'SHOW TABLES');
456 $tables = $this->loadColumn();
468 public function getVersion()
472 return mysqli_get_server_info($this->connection);
483 public function insertid()
487 return mysqli_insert_id($this->connection);
500 public function lockTable($table)
502 $this->setQuery(
'LOCK TABLES ' . $this->quoteName($table) .
' WRITE')->execute();
515 public function execute()
519 if (!is_object($this->connection))
522 throw new RuntimeException($this->errorMsg, $this->errorNum);
526 $query = $this->replacePrefix((
string) $this->sql);
528 if (!($this->sql instanceof
JDatabaseQuery) && ($this->limit > 0 || $this->offset > 0))
530 $query .=
' LIMIT ' . $this->offset .
', ' . $this->limit;
538 $this->errorMsg =
'';
539 $memoryBefore = null;
545 $this->log[] = $query;
549 $this->timings[] = microtime(
true);
551 if (is_object($this->cursor))
554 @$this->freeResult();
556 $memoryBefore = memory_get_usage();
560 $this->cursor = @mysqli_query($this->connection, $query);
564 $this->timings[] = microtime(
true);
565 if (defined(
'DEBUG_BACKTRACE_IGNORE_ARGS'))
567 $this->callStacks[] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
571 $this->callStacks[] = debug_backtrace();
573 $this->callStacks[count($this->callStacks) - 1][0][
'memory'] = array($memoryBefore, memory_get_usage(), is_object($this->cursor) ? $this->getNumRows() : null);
579 $this->errorNum = (int) mysqli_errno($this->connection);
580 $this->errorMsg = (string) mysqli_error($this->connection) .
' SQL=' . $query;
583 if (!$this->connected())
588 $this->connection = null;
592 catch (RuntimeException $e)
595 throw new RuntimeException($this->errorMsg, $this->errorNum);
599 return $this->execute();
605 throw new RuntimeException($this->errorMsg, $this->errorNum);
609 return $this->cursor;
625 public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
627 $this->setQuery(
'RENAME TABLE ' . $oldTable .
' TO ' . $newTable)->execute();
642 public function select($database)
651 if (!mysqli_select_db($this->connection, $database))
653 throw new RuntimeException(
'Could not connect to database.');
666 public function setUTF()
670 return $this->connection->set_charset(
'utf8');
683 public function transactionCommit($toSavepoint =
false)
687 if (!$toSavepoint || $this->transactionDepth <= 1)
689 if ($this->setQuery(
'COMMIT')->execute())
691 $this->transactionDepth = 0;
697 $this->transactionDepth--;
710 public function transactionRollback($toSavepoint =
false)
714 if (!$toSavepoint || $this->transactionDepth <= 1)
716 if ($this->setQuery(
'ROLLBACK')->execute())
718 $this->transactionDepth = 0;
724 $savepoint =
'SP_' . ($this->transactionDepth - 1);
725 $this->setQuery(
'ROLLBACK TO SAVEPOINT ' . $this->quoteName($savepoint));
727 if ($this->execute())
729 $this->transactionDepth--;
743 public function transactionStart($asSavepoint =
false)
747 if (!$asSavepoint || !$this->transactionDepth)
749 if ($this->setQuery(
'START TRANSACTION')->execute())
751 $this->transactionDepth = 1;
757 $savepoint =
'SP_' . $this->transactionDepth;
758 $this->setQuery(
'SAVEPOINT ' . $this->quoteName($savepoint));
760 if ($this->execute())
762 $this->transactionDepth++;
775 protected function fetchArray($cursor = null)
777 return mysqli_fetch_row($cursor ? $cursor : $this->cursor);
789 protected function fetchAssoc($cursor = null)
791 return mysqli_fetch_assoc($cursor ? $cursor : $this->cursor);
804 protected function fetchObject($cursor = null, $class =
'stdClass')
806 return mysqli_fetch_object($cursor ? $cursor : $this->cursor, $class);
818 protected function freeResult($cursor = null)
820 mysqli_free_result($cursor ? $cursor : $this->cursor);
821 if ((! $cursor) || ($cursor === $this->cursor))
823 $this->cursor = null;
835 public function unlockTables()
837 $this->setQuery(
'UNLOCK TABLES')->execute();
849 private function hasProfiling()
853 $res = mysqli_query($this->connection,
"SHOW VARIABLES LIKE 'have_profiling'");
854 $row = mysqli_fetch_assoc($res);