10 defined(
'JPATH_PLATFORM') or die;
39 protected $nameQuote =
"'";
48 protected $nullDate =
'0000-00-00 00:00:00';
62 protected $executed =
false;
71 public function __construct($options)
74 $options[
'driver'] = (isset($options[
'driver'])) ? $options[
'driver'] :
'odbc';
75 $options[
'dsn'] = (isset($options[
'dsn'])) ? $options[
'dsn'] :
'';
76 $options[
'host'] = (isset($options[
'host'])) ? $options[
'host'] :
'localhost';
77 $options[
'database'] = (isset($options[
'database'])) ? $options[
'database'] :
'';
78 $options[
'user'] = (isset($options[
'user'])) ? $options[
'user'] :
'';
79 $options[
'password'] = (isset($options[
'password'])) ? $options[
'password'] :
'';
80 $options[
'driverOptions'] = (isset($options[
'driverOptions'])) ? $options[
'driverOptions'] : array();
83 parent::__construct($options);
91 public function __destruct()
104 public function connect()
106 if ($this->connection)
112 if (!self::isSupported())
114 throw new RuntimeException(
'PDO Extension is not available.', 1);
121 switch ($this->options[
'driver'])
124 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 33000;
126 $format =
'cubrid:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
128 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#');
129 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database']);
134 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 1433;
136 $format =
'dblib:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
138 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#');
139 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database']);
144 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 3050;
146 $format =
'firebird:dbname=#DBNAME#';
148 $replace = array(
'#DBNAME#');
149 $with = array($this->options[
'database']);
154 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 56789;
156 if (!empty($this->options[
'dsn']))
158 $format =
'ibm:DSN=#DSN#';
160 $replace = array(
'#DSN#');
161 $with = array($this->options[
'dsn']);
165 $format =
'ibm:hostname=#HOST#;port=#PORT#;database=#DBNAME#';
167 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#');
168 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database']);
174 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 1526;
175 $this->options[
'protocol'] = (isset($this->options[
'protocol'])) ? $this->options[
'protocol'] :
'onsoctcp';
177 if (!empty($this->options[
'dsn']))
179 $format =
'informix:DSN=#DSN#';
181 $replace = array(
'#DSN#');
182 $with = array($this->options[
'dsn']);
186 $format =
'informix:host=#HOST#;service=#PORT#;database=#DBNAME#;server=#SERVER#;protocol=#PROTOCOL#';
188 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#',
'#SERVER#',
'#PROTOCOL#');
189 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database'], $this->options[
'server'], $this->options[
'protocol']);
195 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 1433;
197 $format =
'mssql:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
199 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#');
200 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database']);
205 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 3306;
207 $format =
'mysql:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
209 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#');
210 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database']);
215 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 1521;
216 $this->options[
'charset'] = (isset($this->options[
'charset'])) ? $this->options[
'charset'] :
'AL32UTF8';
218 if (!empty($this->options[
'dsn']))
220 $format =
'oci:dbname=#DSN#';
222 $replace = array(
'#DSN#');
223 $with = array($this->options[
'dsn']);
227 $format =
'oci:dbname=//#HOST#:#PORT#/#DBNAME#';
229 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#');
230 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database']);
233 $format .=
';charset=' . $this->options[
'charset'];
238 $format =
'odbc:DSN=#DSN#;UID:#USER#;PWD=#PASSWORD#';
240 $replace = array(
'#DSN#',
'#USER#',
'#PASSWORD#');
241 $with = array($this->options[
'dsn'], $this->options[
'user'], $this->options[
'password']);
246 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 5432;
248 $format =
'pgsql:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
250 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#');
251 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database']);
257 if (isset($this->options[
'version']) && $this->options[
'version'] == 2)
259 $format =
'sqlite2:#DBNAME#';
263 $format =
'sqlite:#DBNAME#';
266 $replace = array(
'#DBNAME#');
267 $with = array($this->options[
'database']);
272 $this->options[
'port'] = (isset($this->options[
'port'])) ? $this->options[
'port'] : 1433;
274 $format =
'mssql:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
276 $replace = array(
'#HOST#',
'#PORT#',
'#DBNAME#');
277 $with = array($this->options[
'host'], $this->options[
'port'], $this->options[
'database']);
283 $connectionString = str_replace($replace, $with, $format);
287 $this->connection =
new PDO(
289 $this->options[
'user'],
290 $this->options[
'password'],
291 $this->options[
'driverOptions']
294 catch (PDOException $e)
296 throw new RuntimeException(
'Could not connect to PDO: ' . $e->getMessage(), 2, $e);
307 public function disconnect()
309 foreach ($this->disconnectHandlers as $h)
311 call_user_func_array($h, array( &$this));
315 unset($this->connection);
339 public function escape($text, $extra =
false)
341 if (is_int($text) || is_float($text))
346 $text = str_replace(
"'",
"''", $text);
348 return addcslashes($text,
"\000\n\r\\\032");
360 public function execute()
364 if (!is_object($this->connection))
367 throw new RuntimeException($this->errorMsg, $this->errorNum);
371 $query = $this->replacePrefix((
string) $this->sql);
373 if (!($this->sql instanceof
JDatabaseQuery) && ($this->limit > 0 || $this->offset > 0))
376 $query .=
' LIMIT ' . $this->offset .
', ' . $this->limit;
384 $this->errorMsg =
'';
390 $this->log[] = $query;
394 $this->timings[] = microtime(
true);
398 $this->executed =
false;
399 if ($this->prepared instanceof PDOStatement)
404 $bounded =& $this->sql->getBounded();
405 foreach ($bounded as $key => $obj)
407 $this->prepared->bindParam($key, $obj->value, $obj->dataType, $obj->length, $obj->driverOptions);
411 $this->executed = $this->prepared->execute();
416 $this->timings[] = microtime(
true);
417 if (defined(
'DEBUG_BACKTRACE_IGNORE_ARGS'))
419 $this->callStacks[] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
423 $this->callStacks[] = debug_backtrace();
428 if (!$this->executed)
431 $errorNum = (int) $this->connection->errorCode();
432 $errorMsg = (string)
'SQL: ' . implode(
", ", $this->connection->errorInfo());
435 if (!$this->connected())
440 $this->connection = null;
444 catch (RuntimeException $e)
447 $this->errorNum = (int) $this->connection->errorCode();
448 $this->errorMsg = (string)
'SQL: ' . implode(
", ", $this->connection->errorInfo());
452 throw new RuntimeException($this->errorMsg, $this->errorNum);
456 return $this->execute();
462 $this->errorNum = $errorNum;
463 $this->errorMsg = $errorMsg;
467 throw new RuntimeException($this->errorMsg, $this->errorNum);
471 return $this->prepared;
486 public function getOption($key)
490 return $this->connection->getAttribute($key);
500 public function getConnectedQuery()
520 public function setOption($key, $value)
524 return $this->connection->setAttribute($key, $value);
535 public static function isSupported()
537 return defined(
'PDO::ATTR_DRIVER_NAME');
547 public function connected()
550 static $checkingConnected =
false;
552 if ($checkingConnected)
555 $checkingConnected =
true;
556 die(
'Recursion trying to check if connected.');
561 $limit = $this->limit;
562 $offset = $this->offset;
563 $prepared = $this->prepared;
568 $checkingConnected =
true;
571 $this->setQuery($this->getConnectedQuery());
572 $status = (bool) $this->loadResult();
582 $this->limit = $limit;
583 $this->offset = $offset;
584 $this->prepared = $prepared;
585 $checkingConnected =
false;
598 public function getAffectedRows()
602 if ($this->prepared instanceof PDOStatement)
604 return $this->prepared->rowCount();
621 public function getNumRows($cursor = null)
625 if ($cursor instanceof PDOStatement)
627 return $cursor->rowCount();
629 elseif ($this->prepared instanceof PDOStatement)
631 return $this->prepared->rowCount();
646 public function insertid()
651 return @$this->connection->lastInsertId();
664 public function select($database)
683 public function setQuery($query, $offset = null, $limit = null, $driverOptions = array())
689 if (is_string($query))
692 $query = $this->getQuery(
true)->setQuery($query);
697 $query->setLimit($limit, $offset);
700 $query = $this->replacePrefix((
string) $query);
702 $this->prepared = $this->connection->prepare($query, $driverOptions);
705 parent::setQuery($query, $offset, $limit);
717 public function setUTF()
732 public function transactionCommit($toSavepoint =
false)
736 if (!$toSavepoint || $this->transactionDepth == 1)
738 $this->connection->commit();
741 $this->transactionDepth--;
754 public function transactionRollback($toSavepoint =
false)
758 if (!$toSavepoint || $this->transactionDepth == 1)
760 $this->connection->rollBack();
763 $this->transactionDepth--;
776 public function transactionStart($asSavepoint =
false)
780 if (!$asSavepoint || !$this->transactionDepth)
782 $this->connection->beginTransaction();
785 $this->transactionDepth++;
797 protected function fetchArray($cursor = null)
799 if (!empty($cursor) && $cursor instanceof PDOStatement)
801 return $cursor->fetch(PDO::FETCH_NUM);
803 if ($this->prepared instanceof PDOStatement)
805 return $this->prepared->fetch(PDO::FETCH_NUM);
818 protected function fetchAssoc($cursor = null)
820 if (!empty($cursor) && $cursor instanceof PDOStatement)
822 return $cursor->fetch(PDO::FETCH_ASSOC);
824 if ($this->prepared instanceof PDOStatement)
826 return $this->prepared->fetch(PDO::FETCH_ASSOC);
840 protected function fetchObject($cursor = null, $class =
'stdClass')
842 if (!empty($cursor) && $cursor instanceof PDOStatement)
844 return $cursor->fetchObject($class);
846 if ($this->prepared instanceof PDOStatement)
848 return $this->prepared->fetchObject($class);
861 protected function freeResult($cursor = null)
863 $this->executed =
false;
865 if ($cursor instanceof PDOStatement)
867 $cursor->closeCursor();
870 if ($this->prepared instanceof PDOStatement)
872 $this->prepared->closeCursor();
873 $this->prepared = null;
887 public function loadNextObject($class =
'stdClass')
892 if (!$this->executed)
894 if (!($this->execute()))
896 return $this->errorNum ? null :
false;
901 if ($row = $this->fetchObject(null, $class))
920 public function loadNextAssoc()
925 if (!$this->executed)
927 if (!($this->execute()))
929 return $this->errorNum ? null :
false;
934 if ($row = $this->fetchAssoc())
953 public function loadNextRow()
958 if (!$this->executed)
960 if (!($this->execute()))
962 return $this->errorNum ? null :
false;
967 if ($row = $this->fetchArray())
985 public function __sleep()
987 $serializedProperties = array();
989 $reflect =
new ReflectionClass($this);
992 $properties = $reflect->getProperties();
994 foreach ($properties as $property)
997 if ($property->isStatic() ==
false && !($this->{$property->name} instanceof PDO))
999 array_push($serializedProperties, $property->name);
1003 return $serializedProperties;
1013 public function __wakeup()
1016 $this->__construct($this->options);