/** * Returns the SQL for a select statement * * @param SQLStatement $select * * @return string */ public function select(SQLStatement $select) { $limit = $select->getLimit(); if ($limit <= 0) { return parent::select($select); } $order = trim($this->handleOrderings($select->getOrder())); if (empty($order)) { $order = 'ORDER BY (SELECT 0)'; } $sql = $select->getDistinct() ? 'SELECT DISTINCT ' : 'SELECT '; $sql .= $this->handleColumns($select->getColumns()); $sql .= ', ROW_NUMBER() OVER (' . $order . ') AS opis_rownum'; $sql .= ' FROM '; $sql .= $this->handleTables($select->getTables()); $sql .= $this->handleJoins($select->getJoins()); $sql .= $this->handleWheres($select->getWheres()); $sql .= $this->handleGroupings($select->getGroupBy()); $sql .= $this->handleHavings($select->getHaving()); $offset = $select->getOffset(); if ($offset < 0) { $offset = 0; } $limit += $offset; $offset++; return 'SELECT * FROM (' . $sql . ') AS m1 WHERE opis_rownum BETWEEN ' . $offset . ' AND ' . $limit; }
/** * @param string|Closure $column * @param Closure $value * @param string $separator * * @return $this */ protected function addCondition($column, Closure $value = null, $separator) : self { if ($column instanceof Closure) { $this->sql->addHavingGroupCondition($column, $separator); } else { $value($this->expression->init($column, $separator)); } return $this; }
/** * Add a column * * @param string|Closure $name Column's name * @param string $alias (optional) Alias * * @return $this */ public function column($name, string $alias = null) : self { if ($name instanceof Closure) { $expression = $this->expression(); $name($expression); $name = $expression; } $this->sql->addColumn($name, $alias); return $this; }
/** * @param SQLStatement $update * * @return string */ public function update(SQLStatement $update) : string { $joins = $this->handleJoins($update->getJoins()); $tables = $update->getTables(); if ($joins !== '') { $joins = ' FROM ' . $this->handleTables($tables) . ' ' . $joins; $tables = array_values($tables); } $sql = 'UPDATE '; $sql .= $this->handleTables($tables); $sql .= $this->handleSetColumns($update->getColumns()); $sql .= $joins; $sql .= $this->handleWheres($update->getWheres()); return $sql; }
/** * Compiles a SELECT query. * * @param SQLStatement $select * * @return string */ public function select(SQLStatement $select) : string { $limit = $select->getLimit(); if ($limit <= 0) { return parent::select($select); } $sql = $select->getDistinct() ? 'SELECT DISTINCT ' : 'SELECT '; $sql .= $this->handleColumns($select->getColumns()); $sql .= ' FROM '; $sql .= $this->handleTables($select->getTables()); $sql .= $this->handleJoins($select->getJoins()); $sql .= $this->handleWheres($select->getWheres()); $sql .= $this->handleGroupings($select->getGroupBy()); $sql .= $this->handleOrderings($select->getOrder()); $sql .= $this->handleHavings($select->getHaving()); $offset = $select->getOffset(); if ($offset < 0) { return 'SELECT * FROM (' . $sql . ') M1 WHERE ROWNUM <= ' . $limit; } $limit += $offset; $offset++; return 'SELECT * FROM (SELECT M1.*, ROWNUM AS OPIS_ROWNUM FROM (' . $sql . ') M1 WHERE ROWNUM <= ' . $limit . ') WHERE OPIS_ROWNUM >= ' . $offset; }
/** * Returns the SQL for a delete statement * * @param SQLStatement $delete * @return string */ public function delete(SQLStatement $delete) : string { $sql = 'DELETE ' . $this->handleTables($delete->getTables()); $sql .= $sql === 'DELETE ' ? 'FROM ' : ' FROM '; $sql .= $this->handleTables($delete->getFrom()); $sql .= $this->handleJoins($delete->getJoins()); $sql .= $this->handleWheres($delete->getWheres()); return $sql; }
/** * @param bool $not * * @return WhereStatement */ protected function addNullCondition(bool $not) : WhereStatement { $this->sql->addWhereNullCondition($this->column, $this->separator, $not); return $this->statement; }
/** * @param string|float|int $value1 * @param string|float|int $value2 */ public function notBetween($value1, $value2) { $this->sql->addHavingBetweenCondition($this->aggregate, $value1, $value2, $this->separator, true); }
/** * Compiles a SELECT query. * * @access public * @param SQLStatement $select * @return array */ public function select(SQLStatement $select) { $sql = $select->getDistinct() ? 'SELECT DISTINCT ' : 'SELECT '; $sql .= $this->handleColumns($select->getColumns()); $sql .= $this->handleInto($select->getIntoTable(), $select->getIntoDatabase()); $sql .= ' FROM '; $sql .= $this->handleTables($select->getTables()); $sql .= $this->handleJoins($select->getJoins()); $sql .= $this->handleWheres($select->getWheres()); $sql .= $this->handleGroupings($select->getGroupBy()); $sql .= $this->handleOrderings($select->getOrder()); $sql .= $this->handleHavings($select->getHaving()); $sql .= $this->handleOffset($select->getOffset(), $select->getLimit()); $sql .= $this->handleLimit($select->getLimit(), $select->getOffset()); return $sql; }
/** * @param string $table */ public function into(string $table) { $this->sql->addTables([$table]); }
/** * @param Closure $select * @param string $separator * @param bool $not * @return WhereStatement */ protected function addWhereExistCondition(Closure $select, string $separator = 'AND', bool $not = false) : self { $this->sql->addWhereExistsCondition($select, $separator, $not); return $this; }