php - Question mark operator in query -
in laravel 5 app, i'm using postgresql's jsonb data type , has ?
operator.
but can't work in model, because laravel uses question marks bindings.
specifically, in whereraw() method:
$query->whereraw("jsonb_column ? 'a_key'")
how can use question mark in queries?
basically have 2 options:
- getting hand dirty extending current way laravel's query builder implement
whereraw
, otherwise stated doing hard way. - send feature request laravel team (ie asking them extend query builder component dealing more postgresql specifics) , cross fingers address if patient enough.
here takes [1.] option:
the namespace "illuminate\database\query" you:
you need particularly delve following laravel 5.0 source codes of interest:
code fragments of interest:
whereraw in builder.php (excerpt):
/** * add raw clause 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 in grammar.php (excerpt):
/** * compile "where" portions of 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 clauses has own compiler function responsible // creating clauses sql. helps keep code nice // , maintainable since each clause has small method uses. foreach ($query->wheres $where) { $method = "where{$where['type']}"; $sql[] = $where['boolean'].' '.$this->$method($query, $where); } // if have clauses, strip off first boolean // operator, added query builders convenience can // avoid checking first clauses in each of compilers methods. if (count($sql) > 0) { $sql = implode(' ', $sql); return 'where '.$this->removeleadingboolean($sql); } return ''; }
$operators array in postgresgrammar.php (excerpt):
/** * of available clause operators. * * @var array */ protected $operators = array( '=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike', '&', '|', '#', '<<', '>>', );
notice ?
not valid operator ;-)
specialized postgresql protected methods in postgresgrammar.php (excerpt):
/** * compile additional clauses updates 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 compile join constraints, either use them // clause or append them existing base clauses. if need // strip leading boolean when using where. $joinwhere = $this->compileupdatejoinwheres($query); if (trim($basewhere) == '') { return 'where '.$this->removeleadingboolean($joinwhere); } return $basewhere.' '.$joinwhere; } /** * compile "join" clauses update. * * @param \illuminate\database\query\builder $query * @return string */ protected function compileupdatejoinwheres(builder $query) { $joinwheres = array(); // here loop through of join constraints , compile them // out implode them. should give "where" syntax after // has been built , join real wheres. foreach ($query->joins $join) { foreach ($join->clauses $clause) { $joinwheres[] = $this->compilejoinconstraint($clause); } } return implode(' ', $joinwheres); }
consider these sort of specialization of compilewheres mentioned above, remaining cases apart 2 (only 2!!!) specific ones compiled using parent class method (illuminate\database\query\grammars\grammar).
other recommended relevant resources
you may find valuable posts in blog (softonsofa).
i particularly recommend:
- laravel query builder global scope – how use custom connection , query builder in laravel 4, not in right version shows how extend query builder.
- laravel 5 eloquent global scope how-to, in laravel 5.0 orthogonal question yet instructive (right version, plumbing details).
last not least, laravel documentation best place grab more architecture foundations (service providers, service container, facades , on). mastering handy devise robust extension of framework.
hopefully input gives enough hints possible extension point of laravel query builder offered , may serve starting point write postgresql extension addressing ?
operator issue in whereraw
.
please give back/share when done.
Comments
Post a Comment