12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184 |
- <?php
- namespace Illuminate\Database\Query\Grammars;
- use RuntimeException;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Database\Query\JoinClause;
- use Illuminate\Database\Grammar as BaseGrammar;
- class Grammar extends BaseGrammar
- {
- /**
- * The grammar specific operators.
- *
- * @var array
- */
- protected $operators = [];
- /**
- * The components that make up a select clause.
- *
- * @var array
- */
- protected $selectComponents = [
- 'aggregate',
- 'columns',
- 'from',
- 'joins',
- 'wheres',
- 'groups',
- 'havings',
- 'orders',
- 'limit',
- 'offset',
- 'unions',
- 'lock',
- ];
- /**
- * Compile a select query into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return string
- */
- public function compileSelect(Builder $query)
- {
- if ($query->unions && $query->aggregate) {
- return $this->compileUnionAggregate($query);
- }
- // If the query does not have any columns set, we'll set the columns to the
- // * character to just get all of the columns from the database. Then we
- // can build the query and concatenate all the pieces together as one.
- $original = $query->columns;
- if (is_null($query->columns)) {
- $query->columns = ['*'];
- }
- // To compile the query, we'll spin through each component of the query and
- // see if that component exists. If it does we'll just call the compiler
- // function for the component which is responsible for making the SQL.
- $sql = trim($this->concatenate(
- $this->compileComponents($query))
- );
- $query->columns = $original;
- return $sql;
- }
- /**
- * Compile the components necessary for a select clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return array
- */
- protected function compileComponents(Builder $query)
- {
- $sql = [];
- foreach ($this->selectComponents as $component) {
- // To compile the query, we'll spin through each component of the query and
- // see if that component exists. If it does we'll just call the compiler
- // function for the component which is responsible for making the SQL.
- if (isset($query->$component) && ! is_null($query->$component)) {
- $method = 'compile'.ucfirst($component);
- $sql[$component] = $this->$method($query, $query->$component);
- }
- }
- return $sql;
- }
- /**
- * Compile an aggregated select clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $aggregate
- * @return string
- */
- protected function compileAggregate(Builder $query, $aggregate)
- {
- $column = $this->columnize($aggregate['columns']);
- // If the query has a "distinct" constraint and we're not asking for all columns
- // we need to prepend "distinct" onto the column name so that the query takes
- // it into account when it performs the aggregating operations on the data.
- if ($query->distinct && $column !== '*') {
- $column = 'distinct '.$column;
- }
- return 'select '.$aggregate['function'].'('.$column.') as aggregate';
- }
- /**
- * Compile the "select *" portion of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $columns
- * @return string|null
- */
- protected function compileColumns(Builder $query, $columns)
- {
- // If the query is actually performing an aggregating select, we will let that
- // compiler handle the building of the select clauses, as it will need some
- // more syntax that is best handled by that function to keep things neat.
- if (! is_null($query->aggregate)) {
- return;
- }
- $select = $query->distinct ? 'select distinct ' : 'select ';
- return $select.$this->columnize($columns);
- }
- /**
- * Compile the "from" portion of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param string $table
- * @return string
- */
- protected function compileFrom(Builder $query, $table)
- {
- return 'from '.$this->wrapTable($table);
- }
- /**
- * Compile the "join" portions of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $joins
- * @return string
- */
- protected function compileJoins(Builder $query, $joins)
- {
- return collect($joins)->map(function ($join) use ($query) {
- $table = $this->wrapTable($join->table);
- $nestedJoins = is_null($join->joins) ? '' : ' '.$this->compileJoins($query, $join->joins);
- $tableAndNestedJoins = is_null($join->joins) ? $table : '('.$table.$nestedJoins.')';
- return trim("{$join->type} join {$tableAndNestedJoins} {$this->compileWheres($join)}");
- })->implode(' ');
- }
- /**
- * Compile the "where" portions of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return string
- */
- protected function compileWheres(Builder $query)
- {
- // 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.
- if (is_null($query->wheres)) {
- return '';
- }
- // 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 = $this->compileWheresToArray($query)) > 0) {
- return $this->concatenateWhereClauses($query, $sql);
- }
- return '';
- }
- /**
- * Get an array of all the where clauses for the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return array
- */
- protected function compileWheresToArray($query)
- {
- return collect($query->wheres)->map(function ($where) use ($query) {
- return $where['boolean'].' '.$this->{"where{$where['type']}"}($query, $where);
- })->all();
- }
- /**
- * Format the where clause statements into one string.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $sql
- * @return string
- */
- protected function concatenateWhereClauses($query, $sql)
- {
- $conjunction = $query instanceof JoinClause ? 'on' : 'where';
- return $conjunction.' '.$this->removeLeadingBoolean(implode(' ', $sql));
- }
- /**
- * Compile a raw where clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereRaw(Builder $query, $where)
- {
- return $where['sql'];
- }
- /**
- * Compile a basic where clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereBasic(Builder $query, $where)
- {
- $value = $this->parameter($where['value']);
- return $this->wrap($where['column']).' '.$where['operator'].' '.$value;
- }
- /**
- * Compile a "where in" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereIn(Builder $query, $where)
- {
- if (! empty($where['values'])) {
- return $this->wrap($where['column']).' in ('.$this->parameterize($where['values']).')';
- }
- return '0 = 1';
- }
- /**
- * Compile a "where not in" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereNotIn(Builder $query, $where)
- {
- if (! empty($where['values'])) {
- return $this->wrap($where['column']).' not in ('.$this->parameterize($where['values']).')';
- }
- return '1 = 1';
- }
- /**
- * Compile a "where not in raw" clause.
- *
- * For safety, whereIntegerInRaw ensures this method is only used with integer values.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereNotInRaw(Builder $query, $where)
- {
- if (! empty($where['values'])) {
- return $this->wrap($where['column']).' not in ('.implode(', ', $where['values']).')';
- }
- return '1 = 1';
- }
- /**
- * Compile a where in sub-select clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereInSub(Builder $query, $where)
- {
- return $this->wrap($where['column']).' in ('.$this->compileSelect($where['query']).')';
- }
- /**
- * Compile a where not in sub-select clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereNotInSub(Builder $query, $where)
- {
- return $this->wrap($where['column']).' not in ('.$this->compileSelect($where['query']).')';
- }
- /**
- * Compile a "where in raw" clause.
- *
- * For safety, whereIntegerInRaw ensures this method is only used with integer values.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereInRaw(Builder $query, $where)
- {
- if (! empty($where['values'])) {
- return $this->wrap($where['column']).' in ('.implode(', ', $where['values']).')';
- }
- return '0 = 1';
- }
- /**
- * Compile a "where null" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereNull(Builder $query, $where)
- {
- return $this->wrap($where['column']).' is null';
- }
- /**
- * Compile a "where not null" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereNotNull(Builder $query, $where)
- {
- return $this->wrap($where['column']).' is not null';
- }
- /**
- * Compile a "between" where clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereBetween(Builder $query, $where)
- {
- $between = $where['not'] ? 'not between' : 'between';
- $min = $this->parameter(reset($where['values']));
- $max = $this->parameter(end($where['values']));
- return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
- }
- /**
- * Compile a "where date" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereDate(Builder $query, $where)
- {
- return $this->dateBasedWhere('date', $query, $where);
- }
- /**
- * Compile a "where time" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereTime(Builder $query, $where)
- {
- return $this->dateBasedWhere('time', $query, $where);
- }
- /**
- * Compile a "where day" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereDay(Builder $query, $where)
- {
- return $this->dateBasedWhere('day', $query, $where);
- }
- /**
- * Compile a "where month" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereMonth(Builder $query, $where)
- {
- return $this->dateBasedWhere('month', $query, $where);
- }
- /**
- * Compile a "where year" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereYear(Builder $query, $where)
- {
- return $this->dateBasedWhere('year', $query, $where);
- }
- /**
- * Compile a date based where clause.
- *
- * @param string $type
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function dateBasedWhere($type, Builder $query, $where)
- {
- $value = $this->parameter($where['value']);
- return $type.'('.$this->wrap($where['column']).') '.$where['operator'].' '.$value;
- }
- /**
- * Compile a where clause comparing two columns..
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereColumn(Builder $query, $where)
- {
- return $this->wrap($where['first']).' '.$where['operator'].' '.$this->wrap($where['second']);
- }
- /**
- * Compile a nested where clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereNested(Builder $query, $where)
- {
- // Here we will calculate what portion of the string we need to remove. If this
- // is a join clause query, we need to remove the "on" portion of the SQL and
- // if it is a normal query we need to take the leading "where" of queries.
- $offset = $query instanceof JoinClause ? 3 : 6;
- return '('.substr($this->compileWheres($where['query']), $offset).')';
- }
- /**
- * Compile a where condition with a sub-select.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereSub(Builder $query, $where)
- {
- $select = $this->compileSelect($where['query']);
- return $this->wrap($where['column']).' '.$where['operator']." ($select)";
- }
- /**
- * Compile a where exists clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereExists(Builder $query, $where)
- {
- return 'exists ('.$this->compileSelect($where['query']).')';
- }
- /**
- * Compile a where exists clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereNotExists(Builder $query, $where)
- {
- return 'not exists ('.$this->compileSelect($where['query']).')';
- }
- /**
- * Compile a where row values condition.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereRowValues(Builder $query, $where)
- {
- $columns = $this->columnize($where['columns']);
- $values = $this->parameterize($where['values']);
- return '('.$columns.') '.$where['operator'].' ('.$values.')';
- }
- /**
- * Compile a "where JSON boolean" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereJsonBoolean(Builder $query, $where)
- {
- $column = $this->wrapJsonBooleanSelector($where['column']);
- $value = $this->wrapJsonBooleanValue(
- $this->parameter($where['value'])
- );
- return $column.' '.$where['operator'].' '.$value;
- }
- /**
- * Compile a "where JSON contains" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereJsonContains(Builder $query, $where)
- {
- $not = $where['not'] ? 'not ' : '';
- return $not.$this->compileJsonContains(
- $where['column'], $this->parameter($where['value'])
- );
- }
- /**
- * Compile a "JSON contains" statement into SQL.
- *
- * @param string $column
- * @param string $value
- * @return string
- *
- * @throws \RuntimeException
- */
- protected function compileJsonContains($column, $value)
- {
- throw new RuntimeException('This database engine does not support JSON contains operations.');
- }
- /**
- * Prepare the binding for a "JSON contains" statement.
- *
- * @param mixed $binding
- * @return string
- */
- public function prepareBindingForJsonContains($binding)
- {
- return json_encode($binding);
- }
- /**
- * Compile a "where JSON length" clause.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $where
- * @return string
- */
- protected function whereJsonLength(Builder $query, $where)
- {
- return $this->compileJsonLength(
- $where['column'], $where['operator'], $this->parameter($where['value'])
- );
- }
- /**
- * Compile a "JSON length" statement into SQL.
- *
- * @param string $column
- * @param string $operator
- * @param string $value
- * @return string
- *
- * @throws \RuntimeException
- */
- protected function compileJsonLength($column, $operator, $value)
- {
- throw new RuntimeException('This database engine does not support JSON length operations.');
- }
- /**
- * Compile the "group by" portions of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $groups
- * @return string
- */
- protected function compileGroups(Builder $query, $groups)
- {
- return 'group by '.$this->columnize($groups);
- }
- /**
- * Compile the "having" portions of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $havings
- * @return string
- */
- protected function compileHavings(Builder $query, $havings)
- {
- $sql = implode(' ', array_map([$this, 'compileHaving'], $havings));
- return 'having '.$this->removeLeadingBoolean($sql);
- }
- /**
- * Compile a single having clause.
- *
- * @param array $having
- * @return string
- */
- protected function compileHaving(array $having)
- {
- // If the having clause is "raw", we can just return the clause straight away
- // without doing any more processing on it. Otherwise, we will compile the
- // clause into SQL based on the components that make it up from builder.
- if ($having['type'] === 'Raw') {
- return $having['boolean'].' '.$having['sql'];
- } elseif ($having['type'] === 'between') {
- return $this->compileHavingBetween($having);
- }
- return $this->compileBasicHaving($having);
- }
- /**
- * Compile a basic having clause.
- *
- * @param array $having
- * @return string
- */
- protected function compileBasicHaving($having)
- {
- $column = $this->wrap($having['column']);
- $parameter = $this->parameter($having['value']);
- return $having['boolean'].' '.$column.' '.$having['operator'].' '.$parameter;
- }
- /**
- * Compile a "between" having clause.
- *
- * @param array $having
- * @return string
- */
- protected function compileHavingBetween($having)
- {
- $between = $having['not'] ? 'not between' : 'between';
- $column = $this->wrap($having['column']);
- $min = $this->parameter(head($having['values']));
- $max = $this->parameter(last($having['values']));
- return $having['boolean'].' '.$column.' '.$between.' '.$min.' and '.$max;
- }
- /**
- * Compile the "order by" portions of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $orders
- * @return string
- */
- protected function compileOrders(Builder $query, $orders)
- {
- if (! empty($orders)) {
- return 'order by '.implode(', ', $this->compileOrdersToArray($query, $orders));
- }
- return '';
- }
- /**
- * Compile the query orders to an array.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $orders
- * @return array
- */
- protected function compileOrdersToArray(Builder $query, $orders)
- {
- return array_map(function ($order) {
- return $order['sql'] ?? $this->wrap($order['column']).' '.$order['direction'];
- }, $orders);
- }
- /**
- * Compile the random statement into SQL.
- *
- * @param string $seed
- * @return string
- */
- public function compileRandom($seed)
- {
- return 'RANDOM()';
- }
- /**
- * Compile the "limit" portions of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param int $limit
- * @return string
- */
- protected function compileLimit(Builder $query, $limit)
- {
- return 'limit '.(int) $limit;
- }
- /**
- * Compile the "offset" portions of the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param int $offset
- * @return string
- */
- protected function compileOffset(Builder $query, $offset)
- {
- return 'offset '.(int) $offset;
- }
- /**
- * Compile the "union" queries attached to the main query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return string
- */
- protected function compileUnions(Builder $query)
- {
- $sql = '';
- foreach ($query->unions as $union) {
- $sql .= $this->compileUnion($union);
- }
- if (! empty($query->unionOrders)) {
- $sql .= ' '.$this->compileOrders($query, $query->unionOrders);
- }
- if (isset($query->unionLimit)) {
- $sql .= ' '.$this->compileLimit($query, $query->unionLimit);
- }
- if (isset($query->unionOffset)) {
- $sql .= ' '.$this->compileOffset($query, $query->unionOffset);
- }
- return ltrim($sql);
- }
- /**
- * Compile a single union statement.
- *
- * @param array $union
- * @return string
- */
- protected function compileUnion(array $union)
- {
- $conjunction = $union['all'] ? ' union all ' : ' union ';
- return $conjunction.$union['query']->toSql();
- }
- /**
- * Compile a union aggregate query into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return string
- */
- protected function compileUnionAggregate(Builder $query)
- {
- $sql = $this->compileAggregate($query, $query->aggregate);
- $query->aggregate = null;
- return $sql.' from ('.$this->compileSelect($query).') as '.$this->wrapTable('temp_table');
- }
- /**
- * Compile an exists statement into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return string
- */
- public function compileExists(Builder $query)
- {
- $select = $this->compileSelect($query);
- return "select exists({$select}) as {$this->wrap('exists')}";
- }
- /**
- * Compile an insert statement into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $values
- * @return string
- */
- public function compileInsert(Builder $query, array $values)
- {
- // Essentially we will force every insert to be treated as a batch insert which
- // simply makes creating the SQL easier for us since we can utilize the same
- // basic routine regardless of an amount of records given to us to insert.
- $table = $this->wrapTable($query->from);
- if (! is_array(reset($values))) {
- $values = [$values];
- }
- $columns = $this->columnize(array_keys(reset($values)));
- // We need to build a list of parameter place-holders of values that are bound
- // to the query. Each insert should have the exact same amount of parameter
- // bindings so we will loop through the record and parameterize them all.
- $parameters = collect($values)->map(function ($record) {
- return '('.$this->parameterize($record).')';
- })->implode(', ');
- return "insert into $table ($columns) values $parameters";
- }
- /**
- * Compile an insert ignore statement into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $values
- * @return string
- */
- public function compileInsertOrIgnore(Builder $query, array $values)
- {
- throw new RuntimeException('This database engine does not support inserting while ignoring errors.');
- }
- /**
- * Compile an insert and get ID statement into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $values
- * @param string $sequence
- * @return string
- */
- public function compileInsertGetId(Builder $query, $values, $sequence)
- {
- return $this->compileInsert($query, $values);
- }
- /**
- * Compile an insert statement using a subquery into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $columns
- * @param string $sql
- * @return string
- */
- public function compileInsertUsing(Builder $query, array $columns, string $sql)
- {
- return "insert into {$this->wrapTable($query->from)} ({$this->columnize($columns)}) $sql";
- }
- /**
- * Compile an update statement into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param array $values
- * @return string
- */
- public function compileUpdate(Builder $query, $values)
- {
- $table = $this->wrapTable($query->from);
- // Each one of the columns in the update statements needs to be wrapped in the
- // keyword identifiers, also a place-holder needs to be created for each of
- // the values in the list of bindings so we can make the sets statements.
- $columns = collect($values)->map(function ($value, $key) {
- return $this->wrap($key).' = '.$this->parameter($value);
- })->implode(', ');
- // If the query has any "join" clauses, we will setup the joins on the builder
- // and compile them so we can attach them to this update, as update queries
- // can get join statements to attach to other tables when they're needed.
- $joins = '';
- if (isset($query->joins)) {
- $joins = ' '.$this->compileJoins($query, $query->joins);
- }
- // Of course, update queries may also be constrained by where clauses so we'll
- // need to compile the where clauses and attach it to the query so only the
- // intended records are updated by the SQL statements we generate to run.
- $wheres = $this->compileWheres($query);
- return trim("update {$table}{$joins} set $columns $wheres");
- }
- /**
- * Prepare the bindings for an update statement.
- *
- * @param array $bindings
- * @param array $values
- * @return array
- */
- public function prepareBindingsForUpdate(array $bindings, array $values)
- {
- $cleanBindings = Arr::except($bindings, ['select', 'join']);
- return array_values(
- array_merge($bindings['join'], $values, Arr::flatten($cleanBindings))
- );
- }
- /**
- * Compile a delete statement into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return string
- */
- public function compileDelete(Builder $query)
- {
- $wheres = is_array($query->wheres) ? $this->compileWheres($query) : '';
- return trim("delete from {$this->wrapTable($query->from)} $wheres");
- }
- /**
- * Prepare the bindings for a delete statement.
- *
- * @param array $bindings
- * @return array
- */
- public function prepareBindingsForDelete(array $bindings)
- {
- return Arr::flatten(
- Arr::except($bindings, 'select')
- );
- }
- /**
- * Compile a truncate table statement into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return array
- */
- public function compileTruncate(Builder $query)
- {
- return ['truncate '.$this->wrapTable($query->from) => []];
- }
- /**
- * Compile the lock into SQL.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param bool|string $value
- * @return string
- */
- protected function compileLock(Builder $query, $value)
- {
- return is_string($value) ? $value : '';
- }
- /**
- * Determine if the grammar supports savepoints.
- *
- * @return bool
- */
- public function supportsSavepoints()
- {
- return true;
- }
- /**
- * Compile the SQL statement to define a savepoint.
- *
- * @param string $name
- * @return string
- */
- public function compileSavepoint($name)
- {
- return 'SAVEPOINT '.$name;
- }
- /**
- * Compile the SQL statement to execute a savepoint rollback.
- *
- * @param string $name
- * @return string
- */
- public function compileSavepointRollBack($name)
- {
- return 'ROLLBACK TO SAVEPOINT '.$name;
- }
- /**
- * Wrap a value in keyword identifiers.
- *
- * @param \Illuminate\Database\Query\Expression|string $value
- * @param bool $prefixAlias
- * @return string
- */
- public function wrap($value, $prefixAlias = false)
- {
- if ($this->isExpression($value)) {
- return $this->getValue($value);
- }
- // If the value being wrapped has a column alias we will need to separate out
- // the pieces so we can wrap each of the segments of the expression on its
- // own, and then join these both back together using the "as" connector.
- if (stripos($value, ' as ') !== false) {
- return $this->wrapAliasedValue($value, $prefixAlias);
- }
- // If the given value is a JSON selector we will wrap it differently than a
- // traditional value. We will need to split this path and wrap each part
- // wrapped, etc. Otherwise, we will simply wrap the value as a string.
- if ($this->isJsonSelector($value)) {
- return $this->wrapJsonSelector($value);
- }
- return $this->wrapSegments(explode('.', $value));
- }
- /**
- * Wrap the given JSON selector.
- *
- * @param string $value
- * @return string
- */
- protected function wrapJsonSelector($value)
- {
- throw new RuntimeException('This database engine does not support JSON operations.');
- }
- /**
- * Wrap the given JSON selector for boolean values.
- *
- * @param string $value
- * @return string
- */
- protected function wrapJsonBooleanSelector($value)
- {
- return $this->wrapJsonSelector($value);
- }
- /**
- * Wrap the given JSON boolean value.
- *
- * @param string $value
- * @return string
- */
- protected function wrapJsonBooleanValue($value)
- {
- return $value;
- }
- /**
- * Split the given JSON selector into the field and the optional path and wrap them separately.
- *
- * @param string $column
- * @return array
- */
- protected function wrapJsonFieldAndPath($column)
- {
- $parts = explode('->', $column, 2);
- $field = $this->wrap($parts[0]);
- $path = count($parts) > 1 ? ', '.$this->wrapJsonPath($parts[1], '->') : '';
- return [$field, $path];
- }
- /**
- * Wrap the given JSON path.
- *
- * @param string $value
- * @param string $delimiter
- * @return string
- */
- protected function wrapJsonPath($value, $delimiter = '->')
- {
- $value = preg_replace("/([\\\\]+)?\\'/", "\\'", $value);
- return '\'$."'.str_replace($delimiter, '"."', $value).'"\'';
- }
- /**
- * Determine if the given string is a JSON selector.
- *
- * @param string $value
- * @return bool
- */
- protected function isJsonSelector($value)
- {
- return Str::contains($value, '->');
- }
- /**
- * Concatenate an array of segments, removing empties.
- *
- * @param array $segments
- * @return string
- */
- protected function concatenate($segments)
- {
- return implode(' ', array_filter($segments, function ($value) {
- return (string) $value !== '';
- }));
- }
- /**
- * Remove the leading boolean from a statement.
- *
- * @param string $value
- * @return string
- */
- protected function removeLeadingBoolean($value)
- {
- return preg_replace('/and |or /i', '', $value, 1);
- }
- /**
- * Get the grammar specific operators.
- *
- * @return array
- */
- public function getOperators()
- {
- return $this->operators;
- }
- }
|