public function testExecuteQuery()
 {
     $builder = new Builder(new Grammar());
     $builder->select('name')->from('users')->where('id', 3);
     $data = $this->getConnection()->execute($builder);
     $this->assertEquals([['name' => 'jack']], $data);
 }
Beispiel #2
0
 /**
  * Insert a new record into the database, with an update if it exists
  *
  * @param Traversable $values
  * @param array $updateValues an array of column => bindings pairs to update
  * @return int
  */
 public function insertUpdate(Traversable $values, array $updateValues)
 {
     $upserts = 0;
     $this->buffer($values, function (array $buffer) use($updateValues, &$upserts) {
         $upserts += $this->builder->insertUpdate($buffer, $updateValues)->rowCount();
     });
     return $upserts;
 }
 /**
  * Process an "insert get ID" query.
  *
  * @param  \Database\Query\Builder  $query
  * @param  string  $sql
  * @param  array   $values
  * @param  string  $sequence
  * @return int
  */
 public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
 {
     $results = $query->getConnection()->selectFromWriteConnection($sql, $values);
     $sequence = $sequence ?: 'id';
     $result = (array) $results[0];
     $id = $result[$sequence];
     return is_numeric($id) ? (int) $id : $id;
 }
Beispiel #4
0
 public function execute(Builder $query)
 {
     $data = $query->toSql();
     if ($query->command == 'select') {
         return $this->queryAll($data[0], $data[1]);
     }
     return $this->exec($data[0], $data[1]);
 }
 /**
  * Add a "WHERE" clause to the given query.
  *
  * @param  \Database\Query\Builder  $query
  * @param  string  $key
  * @param  string  $extraValue
  * @return void
  */
 protected function addWhere(QueryBuilder $query, $key, $extraValue)
 {
     if ($extraValue === 'NULL') {
         $query->whereNull($key);
     } elseif ($extraValue === 'NOT_NULL') {
         $query->whereNotNull($key);
     } else {
         $query->where($key, $extraValue);
     }
 }
Beispiel #6
0
 public function __construct(Connection $connection, Grammar $grammar = null)
 {
     $this->connection = $connection;
     parent::__construct($grammar);
 }
 /**
  * Set a model instance for the model being queried.
  *
  * @param  \Database\ORM\Model  $model
  * @return $this
  */
 public function setModel(Model $model)
 {
     $this->model = $model;
     $this->query->from($model->getTable());
     return $this;
 }
 /**
  * Begin a fluent query against a database table.
  *
  * @param  string  $table
  * @return \Database\Query\Builder
  */
 public function table($table)
 {
     $processor = $this->getPostProcessor();
     $query = new Query\Builder($this, $this->getQueryGrammar(), $processor);
     return $query->from($table);
 }
Beispiel #9
0
 /**
  * Compile the "join" portions of the query.
  *
  * @param  \Database\Query\Builder $query
  * @param  array $joins
  * @return string
  */
 protected function compileJoins(Builder $query, $joins)
 {
     $sql = array();
     foreach ($joins as $join) {
         $table = $this->wrapTable($join->table);
         // First we need to build all of the "on" clauses for the join. There may be many
         // of these clauses so we will need to iterate through each one and build them
         // separately, then we'll join them up into a single string when we're done.
         $clauses = array();
         foreach ($join->clauses as $clause) {
             $clauses[] = $this->compileJoinConstraint($clause);
         }
         foreach ($join->bindings as $binding) {
             $query->addBinding($binding, 'join');
         }
         // Once we have constructed the clauses, we'll need to take the boolean connector
         // off of the first clause as it obviously will not be required on that clause
         // because it leads the rest of the clauses, thus not requiring any boolean.
         $clauses[0] = $this->removeLeadingBoolean($clauses[0]);
         $clauses = implode(' ', $clauses);
         $type = $join->type;
         // Once we have everything ready to go, we will just concatenate all the parts to
         // build the final join statement SQL for the query and we can then return the
         // final clause back to the callers as a single, stringified join statement.
         $sql[] = "{$type} join {$table} on {$clauses}";
     }
     return implode(' ', $sql);
 }
Beispiel #10
0
 /**
  * Begin a fluent query against a database table.
  *
  * @param  string $table
  * @return \Database\Query\Builder
  */
 public function table($table)
 {
     $query = new Query\Builder($this, $this->getQueryGrammar());
     return $query->from($table);
 }
Beispiel #11
0
 /**
  * @param $table Table name
  * @return Builder
  */
 public function table($table)
 {
     $builder = new Builder($this->getGrammar());
     return $builder->table($table);
 }
 /**
  * Compile the "join" portions of the query.
  *
  * @param  \Database\Query\Builder  $query
  * @param  array  $joins
  * @return string
  */
 protected function compileJoins(Builder $query, $joins)
 {
     $sql = array();
     $query->setBindings(array(), 'join');
     foreach ($joins as $join) {
         $table = $this->wrapTable($join->table);
         $clauses = array();
         foreach ($join->clauses as $clause) {
             $clauses[] = $this->compileJoinConstraint($clause);
         }
         foreach ($join->bindings as $binding) {
             $query->addBinding($binding, 'join');
         }
         $clauses[0] = $this->removeLeadingBoolean($clauses[0]);
         $clauses = implode(' ', $clauses);
         $type = $join->type;
         //
         $sql[] = "{$type} join {$table} on {$clauses}";
     }
     return implode(' ', $sql);
 }
 /**
  * Process an  "insert get ID" query.
  *
  * @param  \Database\Query\Builder  $query
  * @param  string  $sql
  * @param  array   $values
  * @param  string  $sequence
  * @return int
  */
 public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
 {
     $query->getConnection()->insert($sql, $values);
     $id = $query->getConnection()->getPdo()->lastInsertId($sequence);
     return is_numeric($id) ? (int) $id : $id;
 }