10 defined(
'JPATH_PLATFORM') or die;
28 public $name =
'mysql';
37 public function __construct($options)
40 $options[
'host'] = (isset($options[
'host'])) ? $options[
'host'] :
'localhost';
41 $options[
'user'] = (isset($options[
'user'])) ? $options[
'user'] :
'root';
42 $options[
'password'] = (isset($options[
'password'])) ? $options[
'password'] :
'';
43 $options[
'database'] = (isset($options[
'database'])) ? $options[
'database'] :
'';
44 $options[
'select'] = (isset($options[
'select'])) ? (
bool) $options[
'select'] :
true;
47 parent::__construct($options);
55 public function __destruct()
68 public function connect()
70 if ($this->connection)
76 if (!function_exists(
'mysql_connect'))
78 throw new RuntimeException(
'Could not connect to MySQL.');
82 if (!($this->connection = @ mysql_connect($this->options[
'host'], $this->options[
'user'], $this->options[
'password'],
true)))
84 throw new RuntimeException(
'Could not connect to MySQL.');
88 mysql_query(
"SET @@SESSION.sql_mode = '';", $this->connection);
91 if ($this->options[
'select'] && !empty($this->options[
'database']))
93 $this->select($this->options[
'database']);
100 if ($this->debug && $this->hasProfiling())
102 mysql_query(
"SET profiling = 1;", $this->connection);
113 public function disconnect()
116 if (is_resource($this->connection))
118 foreach ($this->disconnectHandlers as $h)
120 call_user_func_array($h, array( &$this));
123 mysql_close($this->connection);
126 $this->connection = null;
139 public function escape($text, $extra =
false)
143 $result = mysql_real_escape_string($text, $this->getConnection());
147 $result = addcslashes($result,
'%_');
160 public static function isSupported()
162 return (function_exists(
'mysql_connect'));
172 public function connected()
174 if (is_resource($this->connection))
176 return @mysql_ping($this->connection);
189 public function getAffectedRows()
193 return mysql_affected_rows($this->connection);
205 public function getNumRows($cursor = null)
209 return mysql_num_rows($cursor ? $cursor : $this->cursor);
219 public function getVersion()
223 return mysql_get_server_info($this->connection);
233 public function insertid()
237 return mysql_insert_id($this->connection);
248 public function execute()
252 if (!is_resource($this->connection))
255 throw new RuntimeException($this->errorMsg, $this->errorNum);
259 $query = $this->replacePrefix((
string) $this->sql);
261 if (!($this->sql instanceof
JDatabaseQuery) && ($this->limit > 0 || $this->offset > 0))
263 $query .=
' LIMIT ' . $this->offset .
', ' . $this->limit;
271 $this->errorMsg =
'';
277 $this->log[] = $query;
281 $this->timings[] = microtime(
true);
285 $this->cursor = @mysql_query($query, $this->connection);
289 $this->timings[] = microtime(
true);
291 if (defined(
'DEBUG_BACKTRACE_IGNORE_ARGS'))
293 $this->callStacks[] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
297 $this->callStacks[] = debug_backtrace();
305 if (!$this->connected())
310 $this->connection = null;
314 catch (RuntimeException $e)
317 $this->errorNum = (int) mysql_errno($this->connection);
318 $this->errorMsg = (string) mysql_error($this->connection) .
' SQL=' . $query;
322 throw new RuntimeException($this->errorMsg, $this->errorNum);
326 return $this->execute();
332 $this->errorNum = (int) mysql_errno($this->connection);
333 $this->errorMsg = (string) mysql_error($this->connection) .
' SQL=' . $query;
337 throw new RuntimeException($this->errorMsg, $this->errorNum);
341 return $this->cursor;
354 public function select($database)
363 if (!mysql_select_db($database, $this->connection))
365 throw new RuntimeException(
'Could not connect to database');
378 public function setUTF()
382 return mysql_set_charset(
'utf8', $this->connection);
394 protected function fetchArray($cursor = null)
396 return mysql_fetch_row($cursor ? $cursor : $this->cursor);
408 protected function fetchAssoc($cursor = null)
410 return mysql_fetch_assoc($cursor ? $cursor : $this->cursor);
423 protected function fetchObject($cursor = null, $class =
'stdClass')
425 return mysql_fetch_object($cursor ? $cursor : $this->cursor, $class);
437 protected function freeResult($cursor = null)
439 mysql_free_result($cursor ? $cursor : $this->cursor);
449 private function hasProfiling()
453 $res = mysql_query(
"SHOW VARIABLES LIKE 'have_profiling'", $this->connection);
454 $row = mysql_fetch_assoc($res);