Get the current query value bindings in a flattened array.
public getBindings ( ) : array | ||
Результат | array |
/** * Is used for quick selecting of big number of records from database without creating ORM objects * @param \Illuminate\Database\Query\Builder $query - Query Builder object * @return array $results - results of query */ private static function getArrayFromQuery($query) { $sql = $query->toSql(); foreach ($query->getBindings() as $binding) { $value = is_numeric($binding) ? $binding : "'" . $binding . "'"; $sql = preg_replace('/\\?/', $value, $sql, 1); } $results = \DB::select($sql); return $results; }
/** * Render query SQL string. * * @author Morten Rugaard <*****@*****.**> * * @param \Illuminate\Database\Query\Builder $query * @return string */ function render_sql(\Illuminate\Database\Query\Builder $query) { return vsprintf($query->toSql(), $query->getBindings()); }
public static function toSql(Builder $query) { $sql = $query->toSql(); $connection = $query->getConnection(); $pdo = $connection->getPdo(); $bindings = $query->getBindings(); $bindings = $connection->prepareBindings($bindings); if (!empty($bindings)) { foreach ($bindings as $binding) { $sql = preg_replace('/\\?/', $pdo->quote($binding), $sql, 1); } } return $sql; }
/** * Get the current query value bindings in a flattened array. * * @return array * @static */ public static function getBindings() { return \Illuminate\Database\Query\Builder::getBindings(); }
/** * Add a external sub-select to the query. * * @param string $column * @param \Illuminate\Database\Query\Builder|static $query * @param string $boolean * @param bool $not * @return $this */ protected function whereInExistingQuery($column, $query, $boolean, $not) { $type = $not ? 'NotInSub' : 'InSub'; $this->wheres[] = compact('type', 'column', 'query', 'boolean'); $this->addBinding($query->getBindings(), 'where'); return $this; }
/** * Add an exists clause to the query. * * @param \Illuminate\Database\Query\Builder $query * @param string $boolean * @param bool $not * @return $this */ public function addWhereExistsQuery(Builder $query, $boolean = 'and', $not = false) { $type = $not ? 'NotExists' : 'Exists'; $this->wheres[] = compact('type', 'operator', 'query', 'boolean'); $this->addBinding($query->getBindings(), 'where'); return $this; }
/** * Add another query builder as a nested where to the query builder. * * @param \Illuminate\Database\Query\Builder|static $query * @param string $boolean * @return $this */ public function addNestedWhereQuery($query, $boolean = 'and') { if (count($query->wheres)) { $type = 'Nested'; $this->wheres[] = compact('type', 'query', 'boolean'); $this->addBinding($query->getBindings(), 'where'); } return $this; }
/** * Add a sub query count clause to the query. * * @param \Illuminate\Database\Query\Builder $query * @param string $operator * @param int $count * @param string $boolean * @return $this */ protected function whereCountQuery(QueryBuilder $query, $operator = '>=', $count = 1, $boolean = 'and') { if (is_numeric($count)) { $count = new Expression($count); } $this->query->addBinding($query->getBindings(), 'where'); return $this->where(new Expression('(' . $query->toSql() . ')'), $operator, $count, $boolean); }
/** * Performs the count query and returns info about the pages * * @param \Illuminate\Database\Query\Builder $countQuery * @param string $querySql * @param int $page * * @return array */ public function performCountQuery(QueryBuilder $countQuery, $querySql, $page) { //grab the model instance $model = $this->config->getDataModel(); //then wrap the inner table and perform the count $sql = "SELECT COUNT({$model->getKeyName()}) AS aggregate FROM ({$querySql}) AS agg"; //then perform the count query $results = $countQuery->getConnection()->select($sql, $countQuery->getBindings()); $numRows = $results[0]->aggregate; $page = (int) $page; $last = (int) ceil($numRows / $this->rowsPerPage); return array('page' => $page > $last ? $last : $page, 'last' => $last, 'total' => $numRows); }
protected function getQueryKey(Builder $builder, $other = null) { $name = $builder->getConnection()->getName(); return md5($name . $builder->toSql() . serialize($builder->getBindings()) . $other); }
/** * Add a full sub-select to the query. * * @param string $column * @param \Illuminate\Database\Query\Builder $query * @param string $boolean * @return $this */ public function whereSubQuery($column, $query, $boolean = 'and') { list($type, $operator) = ['Sub', 'in']; $this->wheres[] = compact('type', 'column', 'operator', 'query', 'boolean'); $this->addBinding($query->getBindings(), 'where'); return $this; }
/** * Extracts a raw WHERE clause string from a QueryBuilder instance. * Note that this is practically identical to the original Listify. * * @param QueryBuilder $query A Query Builder instance * @return string */ protected function getConditionStringFromQueryBuilder(QueryBuilder $query) { $initialQueryChunks = explode('where ', $query->toSql()); if (count($initialQueryChunks) == 1) { throw new InvalidArgumentException('The query builder instance must have a where clause to build a condition string from'); } $queryChunks = explode('?', $initialQueryChunks[1]); $chunkCount = count($queryChunks); $bindings = $query->getBindings(); $whereString = ''; for ($i = 0; $i < $chunkCount; $i++) { $whereString .= $queryChunks[$i]; if (isset($bindings[$i])) { if (gettype($bindings[$i]) === 'string') { $whereString .= '"' . $bindings[$i] . '"'; } } } return $whereString; }
/** * Return sql statement * * @param Builder $queryBuilder * @return string */ public static function toSql(Builder $queryBuilder) { $params = $queryBuilder->getBindings(); $pdoStatement = $queryBuilder->toSql(); foreach ($params as $param) { $param = is_int($param) ? $param : "'{$param}'"; $pdoStatement = str_replace_first('?', $param, $pdoStatement); } return str_replace('`', '', $pdoStatement); }
/** * @param Builder $qb * @param array $attributes */ private function generateQueryHashFromQueryBuilder(Builder $qb, array $attributes) { $this->actualQueryHash = sha1(implode('_', [$this->cacheHashKeyPrefix, $qb->toSql(), serialize($qb->getBindings()), serialize($attributes)])); }
/** * @param EloquentBuilder|QueryBuilder|Model $query * @param string $key * @param bool $returnExpression * * @return string */ public function scopeToSubQuery($query, $key, $returnExpression = false) { $index = 0; $bindings = array(); if (!Str::contains($key, '.')) { /** @var Model|BetterEloquentTrait $model */ $model = $query->getModel(); $key = $model->getField($key); } $sql = $query->select(array($key))->toSql(); foreach ($query->getBindings() as $binding) { $bindings[] = is_array($binding) ? array_merge($bindings, $binding) : $binding; } while (Str::contains($sql, '?')) { $sql = $this->replaceFirst('?', $bindings[$index++], $sql); } $sql = "({$sql})"; return $returnExpression ? new Expression($sql) : $sql; }