| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- class Core_Pdo extends PDO {
- protected $_database_name;
- protected $_zasob_id;
- // public PDO::__construct ( string $dsn [, string $username [, string $password [, array $options ]]] )
- public function __construct($dsn, $username, $password, $options = array()) {
- $this->_database_name = $options['database'];
- $this->_zasob_id = $options['zasob_id'];
- unset($options['database']);
- unset($options['zasob_id']);
- parent::__construct($dsn, $username, $password, $options);
- $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- }
- public function getDatabaseName() {
- return $this->_database_name;
- }
- public function getZasobId() {
- return $this->_zasob_id;
- }
- public function fetchAll($sql) {
- $sth = $this->prepare($sql);
- $sth->execute();
- return $sth->fetchAll();
- }
- public function bindValues($sth, $values) {
- foreach ($values as $name => $value) {
- $val = $value;
- $type = PDO::PARAM_STR;
- if (is_array($value)) {
- $val = $value[0];
- if (count($value) > 1) {
- $type = $value[1];
- }
- }
- $sth->bindValue($name, $val, $type);
- if (!isset($sth->bindedValues)) $sth->bindedValues = array();
- $sth->bindedValues[$name] = array($val, $type);
- }
- }
- public function getRawSql($sth, $values = array()) {
- $sql = $sth->queryString;
- $params = array();
- if (!empty($sth->bindedValues)) {
- foreach ($sth->bindedValues as $name => $value) {
- $params[$name] = array($value[0], $value[1]);
- }
- }
- foreach ($values as $name => $value) {
- $val = $value;
- $type = PDO::PARAM_STR;
- if (is_array($value)) {
- $val = $value[0];
- if (count($value) > 1) {
- $type = $value[1];
- }
- }
- $params[$name] = array($val, $type);
- }
- if (!empty($params)) {
- foreach ($params as $name => $val) {
- $outValue = $val[0];
- if (PDO::PARAM_STR == $val[1]) $outValue = "'{$outValue}'";
- $sql = str_replace(":{$name}", $outValue, $sql);
- }
- }
- return $sql;
- }
- }
|