public function testAssemble() { $clause = new WhereClause(); $eq = new EqualPredicate(); $eq->setField('one')->setExpression((new StringExpression())->setValue('val')); $clause->addPredicate($eq); $this->assertEquals('WHERE one = "val"', QueryAssembler::stringify($clause)); $eq = new GreaterThanPredicate(); $eq->setField('two')->setExpression((new NumericExpression())->setValue(5)); $clause->addPredicate($eq); $this->assertEquals('WHERE one = "val" AND two > 5', QueryAssembler::stringify($clause)); }
public function testAssemble() { $statement = new DeleteStatement(); $update = new DeleteClause(); $update->setTable('tbl'); $statement->addClause($update); $this->assertEquals('DELETE FROM tbl', QueryAssembler::stringify($statement)); $where = new WhereClause(); $where->addPredicate((new NotEqualPredicate())->setField('username')); $statement->addClause($where); $this->assertEquals('DELETE FROM tbl WHERE username IS NOT NULL', QueryAssembler::stringify($statement)); $where->addPredicate((new LikePredicate())->setField('name')->setExpression(EndsWithExpression::create('Joh'))); $this->assertEquals('DELETE FROM tbl ' . 'WHERE username IS NOT NULL AND name LIKE "%Joh"', QueryAssembler::stringify($statement)); }
public function testAssemble() { $statement = new QueryStatement(); $select = new SelectClause(); $select->addExpression(new AllSelectExpression()); $statement->addClause($select); $this->assertEquals('SELECT *', QueryAssembler::stringify($statement)); $from = new FromClause(); $from->setTable('tbl'); $statement->addClause($from); $this->assertEquals('SELECT * FROM tbl', QueryAssembler::stringify($statement)); $where = new WhereClause(); $where->addPredicate((new NotEqualPredicate())->setField('username')); $statement->addClause($where); $this->assertEquals('SELECT * FROM tbl WHERE username IS NOT NULL', QueryAssembler::stringify($statement)); $where->addPredicate((new LikePredicate())->setField('name')->setExpression(StartsWithExpression::create('Joh'))); $this->assertEquals('SELECT * FROM tbl ' . 'WHERE username IS NOT NULL AND name LIKE "Joh%"', QueryAssembler::stringify($statement)); $orderBy = new OrderByClause(); $statement->addClause($orderBy); $orderBy->addField((new FieldExpression())->setField('user_id')); $this->assertEquals('SELECT * FROM tbl ' . 'WHERE username IS NOT NULL AND name LIKE "Joh%" ' . 'ORDER BY user_id', QueryAssembler::stringify($statement)); $orderBy->addField((new FieldExpression())->setField('age'), 'DESC'); $this->assertEquals('SELECT * FROM tbl ' . 'WHERE username IS NOT NULL AND name LIKE "Joh%" ' . 'ORDER BY user_id, age DESC', QueryAssembler::stringify($statement)); $groupBy = new GroupByClause(); $statement->addClause($groupBy); $groupBy->addField((new FieldExpression())->setField('role')); $this->assertEquals('SELECT * FROM tbl ' . 'WHERE username IS NOT NULL AND name LIKE "Joh%" ' . 'GROUP BY role ' . 'ORDER BY user_id, age DESC', QueryAssembler::stringify($statement)); $having = new HavingClause(); $statement->addClause($having); $having->addPredicate((new LessThanPredicate())->setField('tasks')->setExpression((new NumericExpression())->setValue(4))); $this->assertEquals('SELECT * FROM tbl ' . 'WHERE username IS NOT NULL AND name LIKE "Joh%" ' . 'GROUP BY role ' . 'HAVING tasks < 4 ' . 'ORDER BY user_id, age DESC', QueryAssembler::stringify($statement)); $limit = new LimitClause(); $limit->setLimit(10); $limit->setOffset(20); $statement->addClause($limit); $this->assertEquals('SELECT * FROM tbl ' . 'WHERE username IS NOT NULL AND name LIKE "Joh%" ' . 'GROUP BY role ' . 'HAVING tasks < 4 ' . 'ORDER BY user_id, age DESC ' . 'LIMIT 20,10', QueryAssembler::stringify($statement)); }