Esempio n. 1
0
 /**
  * Executes statement.
  * @param  array
  * @return self
  */
 public function execute($params = array())
 {
     static $types = array('boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT, 'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL);
     foreach ($params as $param => $value) {
         $type = gettype($value);
         $this->bindValue(is_int($param) ? $param + 1 : $param, $value, isset($types[$type]) ? $types[$type] : PDO::PARAM_STR);
     }
     $time = microtime(TRUE);
     try {
         parent::execute();
     } catch (PDOException $e) {
         $e->queryString = $this->queryString;
         $this->connection->onError($this, $params, $e);
         throw $e;
     }
     $this->time = microtime(TRUE) - $time;
     $this->connection->onQuery($this, $params);
     // $this->connection->onQuery() in PHP 5.3
     return $this;
 }
Esempio n. 2
0
 protected function reloadForeignKeys($table)
 {
     foreach ($this->connection->getSupplementalDriver()->getForeignKeys($table) as $row) {
         $this->structure['belongsTo'][strtolower($table)][$row['column']] = $row['ref_table'];
         $this->structure['hasMany'][strtolower($row['ref_table'])][$row['column'] . $table] = array($row['column'], $table);
         $this->structure['foreignKeys'][$table][$row['ref_table']] = array($row['ref_table'], $row['column']);
     }
     if (isset($this->structure['belongsTo'][$table])) {
         uksort($this->structure['belongsTo'][$table], function ($a, $b) {
             return strlen($a) - strlen($b);
         });
     }
 }
Esempio n. 3
0
 /**
  * Updates all rows in result set.
  * Joins in UPDATE are supported only in MySQL
  * @param  array|\Traversable ($column => $value)
  * @return int number of affected rows
  */
 public function update($data)
 {
     if ($data instanceof \Traversable) {
         $data = iterator_to_array($data);
     } elseif (!is_array($data)) {
         throw new InvalidArgumentException();
     }
     if (!$data) {
         return 0;
     }
     $builder = clone $this->getSqlBuilder('UPDATE');
     $builder->set(null);
     $builder->set($data);
     return $this->connection->queryArgs($builder->getQuery(), $builder->getParameters())->getRowCount();
 }
Esempio n. 4
0
 /**
  * Returns metadata for all foreign keys in a table.
  */
 public function getForeignKeys($table)
 {
     $keys = array();
     $query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE ' . 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
     foreach ($this->connection->query($query) as $id => $row) {
         $keys[$id]['name'] = $row['CONSTRAINT_NAME'];
         // foreign key name
         $keys[$id]['column'] = $row['COLUMN_NAME'];
         // local columns
         $keys[$id]['ref_table'] = $row['REFERENCED_TABLE_NAME'];
         // referenced table
         $keys[$id]['ref_column'] = $row['REFERENCED_COLUMN_NAME'];
         // referenced columns
     }
     return array_values($keys);
 }
Esempio n. 5
0
 public function quote($str)
 {
     return $this->connection->getPdo()->quote($str);
 }
Esempio n. 6
0
 /**
  * Import SQL dump from file - extreme fast.
  * @return int  count of commands
  */
 public static function loadFromFile(Connection $connection, $file)
 {
     @set_time_limit(0);
     // intentionally @
     $handle = @fopen($file, 'r');
     // intentionally @
     if (!$handle) {
         throw new \Exception("Cannot open file '{$file}'.");
     }
     //save listeners
     $listeners = $connection->onQuery;
     $connection->onQuery = array();
     $count = 0;
     $sql = '';
     while (!feof($handle)) {
         $s = fgets($handle);
         $sql .= $s;
         if (substr(rtrim($s), -1) === ';') {
             $connection->query($sql);
             // native query without logging
             $sql = '';
             $count++;
         }
     }
     if (trim($sql) !== '') {
         $connection->query($sql);
         $count++;
     }
     //reload listeners
     $connection->onQuery = $listeners;
     fclose($handle);
     return $count;
 }