/** * @return \NilPortugues\Sql\QueryBuilder\Syntax\Column * * @throws QueryException */ public function getColumns() { if (\is_null($this->select->getTable())) { throw new QueryException('No table specified for the Select instance'); } return SyntaxFactory::createColumns($this->columns, $this->select->getTable()); }
/** * @test */ public function itShouldBeAbleToWriteColumnAsAFuncWithoutBracketsStatement() { $this->query->setTable('user')->setColumns(array('user_id', 'username'))->setFunctionAsColumn('CURRENT_TIMESTAMP', array(), 'server_time')->where()->equals('user_id', 1); $expected = 'SELECT user.user_id, user.username, CURRENT_TIMESTAMP AS \'server_time\' FROM user WHERE (user.user_id = :v1)'; $this->assertSame($expected, $this->writer->write($this->query)); $expected = array(':v1' => 1); $this->assertEquals($expected, $this->writer->getValues()); }
/** * @test */ public function itShouldAllowWhereConditionAsLiteral() { $this->query->setTable('user')->where()->asLiteral("(username is not null and status=:status)")->notEquals('name', '%N%'); $expected = "SELECT user.* FROM user WHERE (username is not null and status=:status) AND (user.name <> :v1)"; $this->assertSame($expected, $this->writer->write($this->query)); $expected = array(':v1' => '%N%'); $this->assertEquals($expected, $this->writer->getValues()); }
/** * @test */ public function itShouldWriteJoin() { $this->query->isJoin(true)->setTable('user')->on()->equals('user_id', 1); $expected = 'JOIN user ON (user.user_id = :v1)'; $this->assertSame($expected, $this->writer->write($this->query)); $expected = array(':v1' => 1); $this->assertEquals($expected, $this->writer->getValues()); }
/** * @test */ public function itShouldBeAbleToDoWhereNotExists() { $select = new Select('banned_user'); $select->where()->equals('user_id', 1); $this->query->setTable('user')->setColumns(array('user_id', 'role_id'))->where()->notExists($select)->equals('user', 'Nil'); $expected = 'SELECT user.user_id, user.role_id FROM user WHERE (user.user = :v1) AND ' . 'NOT EXISTS (SELECT banned_user.* FROM banned_user WHERE (banned_user.user_id = :v2))'; $this->assertSame($expected, $this->writer->write($this->query)); $expected = array(':v1' => 'Nil', ':v2' => 1); $this->assertEquals($expected, $this->writer->getValues()); }
/** * @param Select $select * @param string $selfColumn * @param string $refColumn * * @return Select */ public function addJoin(Select $select, $selfColumn, $refColumn) { $select->isJoin(true); $table = $select->getTable()->getName(); if (!isset($this->joins[$table])) { $newColumn = array($selfColumn); $select->joinCondition()->equals($refColumn, SyntaxFactory::createColumn($newColumn, $this->select->getTable())); $this->joins[$table] = $select; } return $this->joins[$table]; }
/** * @param Select $select * * @return string */ protected function getLimitCount(Select $select) { return null === $select->getLimitCount() ? '0' : '1'; }
/** * @test */ public function itShouldSetNotExistsCondition() { $select1 = new Select('user'); $select1->where()->equals('user_id', 10); $result = $this->where->notExists($select1)->getNotExists(); $this->assertEquals(array($select1), $result); }
/** * @param Select $select * * @return array */ public function writeFuncAsColumns(Select $select) { $funcAsColumns = $select->getColumnFuncs(); $newColumns = []; if (!empty($funcAsColumns)) { foreach ($funcAsColumns as $alias => $value) { $funcName = $value['func']; $funcArgs = !empty($value['args']) ? '(' . implode(', ', $value['args']) . ')' : ''; $newFuncColumn = array($alias => $funcName . $funcArgs); $newColumns[] = SyntaxFactory::createColumn($newFuncColumn, null); } } return $newColumns; }
/** * @param Select $select * * @return string */ public function writeJoin(Select $select) { if (null === $this->whereWriter) { $this->whereWriter = WriterFactory::createWhereWriter($this, $this->placeholderWriter); } $sql = $select->getJoinType() ? "{$select->getJoinType()} " : ''; $sql .= 'JOIN '; $sql .= $this->writeTableWithAlias($select->getTable()); $sql .= ' ON '; $sql .= $this->whereWriter->writeWhere($select->getJoinCondition()); return $sql; }
/** * @test */ public function itShouldGetPartName() { $this->assertSame('SELECT', $this->query->partName()); }