Question mark operator in a query

In my laravel 5 application, I am using the postgreSQL jsonb data type and it has a statement ?.

But I can't get it to work in my model, because laravel uses question marks as bindings.

In particular, in the whereRaw () method:

$query->whereRaw("jsonb_column ? 'a_key'")

How can I use a question mark in my queries?

+4
source share
3 answers

you can use function call instead of operator.

First you have to figure out which function? The statement uses the following query in your PostgresSQL database:

SELECT oprname, oprcode FROM pg_operator WHERE oprname = '?'

jsonb_exists, :

$query->whereRaw("jsonb_exists(jsonb_column, 'a_key')")

, , .

+4

2 :

  • , Laravel Query Builder whereRaw, , .
  • Laravel (.. Query Builder, PostgresQL), , , .

[1.]:

"Illuminate\Database\Query" :

Laravel 5.0:


:

whereRaw Builder.php():

/**
 * Add a raw where clause to the query.
 *
 * @param  string  $sql
 * @param  array   $bindings
 * @param  string  $boolean
 * @return $this
 */
public function whereRaw($sql, array $bindings = array(), $boolean = 'and')
{
    $type = 'raw';
    $this->wheres[] = compact('type', 'sql', 'boolean');
    $this->addBinding($bindings, 'where');
    return $this;
}

compileWheres Grammar.php():

/**
 * Compile the "where" portions of the query.
 *
 * @param  \Illuminate\Database\Query\Builder  $query
 * @return string
 */
protected function compileWheres(Builder $query)
{
    $sql = array();
    if (is_null($query->wheres)) return '';
    // Each type of where clauses has its own compiler function which is responsible
    // for actually creating the where clauses SQL. This helps keep the code nice
    // and maintainable since each clause has a very small method that it uses.
    foreach ($query->wheres as $where)
    {
        $method = "where{$where['type']}";
        $sql[] = $where['boolean'].' '.$this->$method($query, $where);
    }
    // If we actually have some where clauses, we will strip off the first boolean
    // operator, which is added by the query builders for convenience so we can
    // avoid checking for the first clauses in each of the compilers methods.
    if (count($sql) > 0)
    {
        $sql = implode(' ', $sql);
        return 'where '.$this->removeLeadingBoolean($sql);
    }
    return '';
}

$operator array PostgresGrammar.php():

/**
 * All of the available clause operators.
 *
 * @var array
 */
protected $operators = array(
    '=', '<', '>', '<=', '>=', '<>', '!=',
    'like', 'not like', 'between', 'ilike',
    '&', '|', '#', '<<', '>>',
);

, ? ; -)

, PostgreSQL PostgresGrammar.php():

/**
 * Compile the additional where clauses for updates with joins.
 *
 * @param  \Illuminate\Database\Query\Builder  $query
 * @return string
 */
protected function compileUpdateWheres(Builder $query)
{
    $baseWhere = $this->compileWheres($query);
    if ( ! isset($query->joins)) return $baseWhere;
    // Once we compile the join constraints, we will either use them as the where
    // clause or append them to the existing base where clauses. If we need to
    // strip the leading boolean we will do so when using as the only where.
    $joinWhere = $this->compileUpdateJoinWheres($query);
    if (trim($baseWhere) == '')
    {
        return 'where '.$this->removeLeadingBoolean($joinWhere);
    }
    return $baseWhere.' '.$joinWhere;
}
/**
 * Compile the "join" clauses for an update.
 *
 * @param  \Illuminate\Database\Query\Builder  $query
 * @return string
 */
protected function compileUpdateJoinWheres(Builder $query)
{
    $joinWheres = array();
    // Here we will just loop through all of the join constraints and compile them
    // all out then implode them. This should give us "where" like syntax after
    // everything has been built and then we will join it to the real wheres.
    foreach ($query->joins as $join)
    {
        foreach ($join->clauses as $clause)
        {
            $joinWheres[] = $this->compileJoinConstraint($clause);
        }
    }
    return implode(' ', $joinWheres);
}

compileWheres, , , ( 2!!!) (Illuminate\Database\Query\Grammars\Grammar).


(SOFTonSOFA).

:

, : Laravel - , , , ..). .


, Buar Builder Builder PostgreSQL, ? whereRaw.

, /, .

+3

, ,

SELECT * FROM table WHERE id = \?;
-2

All Articles