示例#1
0
 /**
  * @covers Cradle\Sql\SqlException::forUnknownPDO
  */
 public function testForUnknownPDO()
 {
     $message = null;
     try {
         throw SqlException::forUnknownPDO('foo');
     } catch (SqlException $e) {
         $message = $e->getMessage();
     }
     $this->assertEquals('Could not match an SQL handler with foo', $message);
 }
示例#2
0
 public static function load(PDO $connection)
 {
     $name = $connection->getAttribute(PDO::ATTR_DRIVER_NAME);
     switch ($name) {
         case 'mysql':
             return MySql::loadPDO($connection);
         case 'pgsql':
             return PostGreSql::loadPDO($connection);
         case 'sqlite':
             return Sqlite::loadPDO($connection);
         default:
             throw SqlException::forUnknownPDO($name);
     }
 }
示例#3
0
 /**
  * Updates model to database
  *
  * @param string|null       $table    Table name
  * @param SqlInterface|null  $database Dabase object
  * @param string|array|null $primary  The primary column if you know it
  *
  * @return Model
  */
 public function update($table = null, SqlInterface $database = null, $primary = null)
 {
     //if no table
     if (is_null($table)) {
         //if no default table either
         if (!$this->table) {
             //throw error
             throw SqlException::forTableNotSet();
         }
         $table = $this->table;
     }
     //if no database
     if (is_null($database)) {
         //and no default database
         if (!$this->database) {
             throw SqlException::forDatabaseNotSet();
         }
         $database = $this->database;
     }
     //get the meta data, the valid column values and whether is primary is set
     $meta = $this->getMeta($table, $database);
     $data = $this->getValidColumns(array_keys($meta[self::COLUMNS]));
     //update original data
     $this->original = $this->data;
     //from here it means that this table has primary
     //columns and all primary values are set
     if (is_null($primary)) {
         $primary = $meta[self::PRIMARY];
     }
     if (is_string($primary)) {
         $primary = [$primary];
     }
     $filter = [];
     //for each primary key
     foreach ($primary as $column) {
         //add the condition to the filter
         $filter[] = [$column . '=%s', $data[$column]];
     }
     //we update it
     $database->updateRows($table, $data, $filter);
     return $this;
 }
示例#4
0
 /**
  * Queries the database
  *
  * @param *string       $query The query to ran
  * @param array         $binds List of binded values
  * @param callable|null $fetch Whether to fetch all the rows
  *
  * @return array
  */
 public function query($query, array $binds = [], $fetch = null)
 {
     $request = new StdClass();
     $request->query = $query;
     $request->binds = $binds;
     $connection = $this->getConnection();
     $query = (string) $request->query;
     $stmt = $connection->prepare($query);
     //bind some more values
     foreach ($request->binds as $key => $value) {
         $stmt->bindValue($key, $value);
     }
     //PDO Execute
     if (!$stmt->execute()) {
         $error = $stmt->errorInfo();
         //unpack binds for the report
         foreach ($binds as $key => $value) {
             $query = str_replace($key, "'{$value}'", $query);
         }
         //throw Exception
         throw SqlException::forQueryError($query, $error[2]);
     }
     //clear binds
     $this->binds = [];
     if (!is_callable($fetch)) {
         $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
         //log query
         $this->log(['query' => $query, 'binds' => $binds, 'results' => $results]);
         return $results;
     }
     if ($fetch instanceof Closure) {
         $fetch = $fetch->bindTo($this, get_class($this));
     }
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         if (call_user_func($fetch, $row, $this) === false) {
             break;
         }
     }
     return $this;
 }