Exemplo n.º 1
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);
 }
Exemplo n.º 2
0
 /**
  * Inserts row in a table.
  * @param  array|\Traversable|Selection array($column => $value)|\Traversable|Selection for INSERT ... SELECT
  * @return ActiveRow|int|bool Returns IRow or number of affected rows for Selection or table without primary key
  */
 public function insert($data)
 {
     if ($data instanceof Selection) {
         $data = new SqlLiteral($data->getQuery(), $data->getParameters());
     } elseif ($data instanceof \Traversable) {
         $data = iterator_to_array($data);
     }
     $return = $this->connection->query($this->getSqlBuilder('insert')->getQuery(), $data);
     $this->loadRefCache();
     if ($data instanceof SqlLiteral || $this->primary === null) {
         unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
         return $return->getRowCount();
     }
     $primaryKey = $this->connection->getInsertId($this->getPrimarySequence());
     if ($primaryKey === false) {
         unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
         return $return->getRowCount();
     }
     if (is_array($this->getPrimary())) {
         $primaryKey = array();
         foreach ((array) $this->getPrimary() as $key) {
             if (!isset($data[$key])) {
                 return $data;
             }
             $primaryKey[$key] = $data[$key];
         }
         if (count($primaryKey) === 1) {
             $primaryKey = reset($primaryKey);
         }
     }
     $row = $this->createSelectionInstance()->wherePrimary($primaryKey)->fetch();
     if ($this->rows !== NULL) {
         if ($signature = $row->getSignature(false)) {
             $this->rows[$signature] = $row;
             $this->data[$signature] = $row;
         } else {
             $this->rows[] = $row;
             $this->data[] = $row;
         }
     }
     return $row;
 }
Exemplo n.º 3
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;
 }