/** * Process an "insert get ID" query. * * @param \Nova\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; }
/** * Add a "WHERE" clause to the given query. * * @param \Nova\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); } }
/** * Begin a fluent query against a database table. * * @param string $table * @return \Nova\Database\Query\Builder */ public function table($table) { $processor = $this->getPostProcessor(); $query = new Query\Builder($this, $this->getQueryGrammar(), $processor); return $query->from($table); }
/** * Process an "insert get ID" query. * * @param \Nova\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; }
/** * Set a model instance for the model being queried. * * @param \Nova\Database\ORM\Model $model * @return \Nova\Database\ORM\Builder */ public function setModel(Model $model) { $this->model = $model; $this->query->from($model->getTable()); return $this; }
/** * Compile the "join" portions of the query. * * @param \Nova\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); }
/** * Compile the "join" portions of the query. * * @param \Nova\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); // 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); }