/** * @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 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 itShouldBeAbleToSetHavingOperatorToOr() { $this->query->setTable('user')->setColumns(array('userId' => 'user_id', 'username' => 'name', 'email' => 'email', 'created_at'))->groupBy(array('user_id', 'name'))->having('OR')->equals('user_id', 1)->equals('user_id', 2); $expected = 'SELECT user.user_id AS \'userId\', user.name AS \'username\', user.email AS \'email\', user.created_at' . ' FROM user GROUP BY user.user_id, user.name HAVING (user.user_id = :v1) OR (user.user_id = :v2)'; $this->assertSame($expected, $this->writer->write($this->query)); $expected = array(':v1' => 1, ':v2' => 2); $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 string $table * * @return $this */ public function setTable($table) { $this->select->setTable($table); return $this; }