public function testAssemble() { $expression = MatchSelectExpression::create(FieldExpression::createWithTable('field1', 'table1'), StringExpression::create('this is a test search')); $this->assertEquals('MATCH (`table1`.`field1`) AGAINST ("this is a test search")', MySQLAssembler::stringify($expression)); $expression->setAlias('score'); $this->assertEquals('MATCH (`table1`.`field1`) AGAINST ("this is a test search") AS `score`', MySQLAssembler::stringify($expression)); }
public function testStatics() { $this->assertEquals('(field_name T 5)', QueryAssembler::stringify(FinalAbstractArithmeticExpression::create(FieldExpression::create('field_name'), 5))); $this->assertEquals('(field_name T 5)', QueryAssembler::stringify(FinalAbstractArithmeticExpression::create(FieldExpression::create('field_name'), StringExpression::create(5)))); $this->assertEquals('(tbl.field_name T 5)', QueryAssembler::stringify(FinalAbstractArithmeticExpression::create(FieldExpression::createWithTable('field_name', 'tbl'), 5))); $this->assertEquals('(tbl.field_name T 5)', QueryAssembler::stringify(FinalAbstractArithmeticExpression::create(FieldExpression::createWithTable('field_name', 'tbl'), StringExpression::create(5)))); }
public function testAssemble() { $expression = new StringExpression(); $expression->setValue(1); $this->assertEquals('1', QueryAssembler::stringify($expression)); $expression->setValue('abc'); $this->assertEquals('"abc"', QueryAssembler::stringify($expression)); $expression->setValue('a"b"c'); $this->assertEquals('"a\\"b\\"c"', QueryAssembler::stringify($expression)); $expression->setValue('a"b"c'); $this->assertEquals("'a\"b\"c'", CqlAssembler::stringify($expression)); $expression->setValue("a'b'c"); $this->assertEquals("'a''b''c'", CqlAssembler::stringify($expression)); $expression->setValue("a''b''c"); $this->assertEquals("'a''''b''''c'", CqlAssembler::stringify($expression)); $expression->setValue("'a'b'c'"); $this->assertEquals("'''a''b''c'''", CqlAssembler::stringify($expression)); }
public function testGettersAndSetters() { $clause = new ValuesClause(); $field = new StringExpression(); $field->setValue('abc'); $null = new ValueExpression(); $this->assertFalse($clause->hasExpressions()); $clause->addExpression($field); $this->assertTrue($clause->hasExpressions()); $this->assertSame([$field], $clause->getExpressions()); $clause->clearExpressions(); $clause->setExpressions([$field, $null]); $this->assertTrue($clause->hasExpressions()); $clause->clearExpressions(); $this->assertFalse($clause->hasExpressions()); $this->setExpectedException("InvalidArgumentException"); $clause->setExpressions([$field, $null, 'abc']); }
public function testAssemble() { $expression = MatchExpression::create(FieldExpression::createWithTable('field1', 'table1'), StringExpression::create('this is a test search')); $expression->addField(FieldExpression::createWithTable('field2', 'table2')); $this->assertEquals('MATCH (`table1`.`field1`,`table2`.`field2`) AGAINST ("this is a test search")', MySQLAssembler::stringify($expression)); $expression->setSearchModifier(MatchExpression::BOOLEAN_MODE); $this->assertEquals('MATCH (`table1`.`field1`,`table2`.`field2`) AGAINST ("this is a test search" IN BOOLEAN MODE)', MySQLAssembler::stringify($expression)); $expression->setSearchModifier(MatchExpression::WITH_QUERY_EXPANSION); $this->assertEquals('MATCH (`table1`.`field1`,`table2`.`field2`) AGAINST ("this is a test search" WITH QUERY EXPANSION)', MySQLAssembler::stringify($expression)); $expression->setSearchModifier(MatchExpression::BOOLEAN_MODE); $stmt = QueryBuilder::select(AllSelectExpression::create())->from('tbl')->where(GreaterThanPredicate::create($expression, 0))->limit(5); $assembler = new MySQLAssembler($stmt); $this->assertEquals('SELECT * FROM `tbl` WHERE MATCH (`table1`.`field1`,`table2`.`field2`) AGAINST (? IN BOOLEAN MODE) > ? LIMIT ?', $assembler->getQuery()); $this->assertEquals(['this is a test search', 0, 5], $assembler->getParameters()); }
public function testAssemble() { $predicate = new InPredicate(); $predicate->setField('field'); $this->assertEquals('field IN NULL', QueryAssembler::stringify($predicate)); $predicate->setExpression((new NumericExpression())->setValue(1)); $this->assertEquals('field IN 1', QueryAssembler::stringify($predicate)); $predicate->setExpression(ArrayExpression::create(['1', 'my "quoted" test', 3])); $this->assertEquals('field IN ("1","my \\"quoted\\" test",3)', QueryAssembler::stringify($predicate)); $predicate->setExpression(ValueExpression::create([4])); $this->assertEquals('field IN (4)', QueryAssembler::stringify($predicate)); $changeExpr = StringExpression::create('my "quoted" test'); $predicate->setExpression(ArrayExpression::create(['1', $changeExpr, 3])); $stmt = QueryBuilder::select(AllSelectExpression::create()); $stmt->where($predicate); $assembler = new QueryAssembler($stmt); $this->assertEquals('SELECT * WHERE field IN (?,?,?)', $assembler->getQuery()); $this->assertEquals(['1', 'my "quoted" test', 3], $assembler->getParameters()); $changeExpr->setValue('changed text'); $this->assertEquals(['1', 'changed text', 3], $assembler->getParameters()); }
public function assembleValueExpression(ValueExpression $expression) { $value = $expression->getValue(); if ($value === null) { return $this->_assemblePrepared() ?: 'NULL'; } else { if (is_scalar($value)) { if (!is_string($value) && is_numeric($value)) { return $this->assembleNumericExpression(NumericExpression::create($value)); } else { if (is_bool($value)) { return $this->assembleBooleanExpression(BooleanExpression::create($value)); } } } else { if (is_array($value)) { return $this->assembleArrayExpression(ArrayExpression::create($value)); } } } return $this->assembleStringExpression(StringExpression::create($value)); }
/** * @expectedException \Exception * @expectedExceptionMessage Invalid Expression Type */ public function testInvalidExpression() { $predicate = new LikePredicate(); $predicate->setField('field'); $predicate->setExpression(StringExpression::create('a%bc')); }