public function assembleField(FieldExpression $field) { if ($field->hasTable()) { return $this->assembleTableExpression($field->getTable()) . '.' . $this->escapeField($field->getField()); } else { return $this->escapeField($field->getField()); } }
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 testDuplicateKey() { $clause = new DuplicateKeyClause(); $clause->addPredicate(EqualPredicate::create('field1', FieldExpression::create('field2'))); $clause->addPredicate(EqualPredicate::create('field3', 5)); $this->assertEquals('ON DUPLICATE KEY UPDATE field1 = field2, field3 = 5', QueryAssembler::stringify($clause)); }
public function getDestination() { if (is_array($this->_dest)) { return FieldExpression::createWithTable($this->_dest[1], $this->_dest[0]); } return $this->_dest; }
public function addField($field) { if (is_scalar($field)) { $field = FieldExpression::create($field); } $this->_fields[] = $field; }
public function testGettersAndSetters() { $predicate = new FinalAbstractOperatorPredicateTest(); $predicate->setExpression((new NumericExpression())->setValue(1)); $predicate->setField('test'); $this->assertEquals(FieldExpression::create('test'), $predicate->getField()); $this->assertEquals((new NumericExpression())->setValue(1), $predicate->getExpression()); }
public function setField($field, $table = null) { if ($field === null || $field instanceof IExpression) { $this->_field = $field; } else { $this->_field = FieldExpression::createWithTable($field, $table); } return $this; }
public function testGettersAndSetters() { $clause = new InsertClause(); $nameField = new FieldExpression(); $nameField->setField('name'); $idField = new FieldExpression(); $idField->setField('id'); $this->assertFalse($clause->hasFields()); $clause->addField($nameField); $this->assertTrue($clause->hasFields()); $this->assertSame([$nameField], $clause->getFields()); $clause->clearFields(); $clause->setFields([$nameField, $idField]); $this->assertTrue($clause->hasFields()); $clause->clearFields(); $this->assertFalse($clause->hasFields()); $this->setExpectedException("InvalidArgumentException"); $clause->setFields([$nameField, $idField, 'abc']); }
public function testGettersAndSetters() { $predicate = new BetweenPredicate(); $predicate->setValues((new NumericExpression())->setValue(1), (new NumericExpression())->setValue(5)); $this->assertEquals([(new NumericExpression())->setValue(1), (new NumericExpression())->setValue(5)], $predicate->getRangeValues()); $this->assertEquals((new NumericExpression())->setValue(1), $predicate->getRangeStart()); $this->assertEquals((new NumericExpression())->setValue(5), $predicate->getRangeEnd()); $predicate->setField('test'); $this->assertEquals(FieldExpression::create('test'), $predicate->getField()); }
public function testSettersAndGetters() { $selector = new FieldSelectExpression(); $selector->setField('new_field'); $this->assertEquals(FieldExpression::create('new_field'), $selector->getField()); $selector->setAlias('alias'); $this->assertEquals('alias', $selector->getAlias()); $field = FieldExpression::create('testfield'); $selector->setField($field); $this->assertSame($field, $selector->getField()); }
public function testCreate() { $class = new FinalJoinTrait(); $class->addClause((new FromClause())->setTable('tbl')); $class->join(TableExpression::create('tbl2'), 'email'); $this->assertEquals("FROM tbl INNER JOIN tbl2 ON tbl.email = tbl2.email", QueryAssembler::stringify($class)); $class->join('tbl3', 'user', 'user_id'); $this->assertEquals('FROM tbl INNER JOIN tbl2 ON tbl.email = tbl2.email ' . 'INNER JOIN tbl3 ON tbl.user = tbl3.user_id', QueryAssembler::stringify($class)); $class->joinWithPredicates(TableSelectExpression::createWithAlias('tbl4', 't4'), EqualPredicate::create(FieldExpression::createWithTable('email', 'tbl2'), FieldExpression::createWithTable('email', 't4'))); $this->assertEquals('FROM tbl INNER JOIN tbl2 ON tbl.email = tbl2.email ' . 'INNER JOIN tbl3 ON tbl.user = tbl3.user_id ' . 'INNER JOIN tbl4 AS t4 ON tbl2.email = t4.email', QueryAssembler::stringify($class)); }
public function setPropertyArray($properties) { $this->_properties = []; foreach ($properties as $property) { if (Strings::containsAny($property, ['"', "'", ')'])) { $this->_properties[] = $property; } else { $this->_properties[] = FieldExpression::create($property); } } return $this; }
public static function buildPredicates(array $input, $table = null, $inverse = false) { $predicates = []; foreach ($input as $key => $value) { if ($value === null || is_scalar($value) || $value instanceof ValueExpression) { $pred = $inverse ? new NotEqualPredicate() : new EqualPredicate(); $pred->setField(FieldExpression::createWithTable($key, $table)); if (!$value instanceof ValueExpression) { $value = ValueExpression::create($value); } $pred->setExpression($value); $predicates[] = $pred; } else { if ($value instanceof IPredicate) { $predicates[] = $value; } else { if (is_array($value)) { if (is_int($key)) { $predicates = array_merge($predicates, static::buildPredicates($value, $table, $inverse)); } else { if (static::_isControlKeyword($key)) { switch ($key) { case 'NOT': $predicates = array_merge($predicates, static::buildPredicates($value, $table, true)); break; case 'OR': $pred = new OrPredicateSet(); $pred->setPredicates(static::buildPredicates($value, $table, $inverse)); $predicates[] = $pred; break; case 'AND': $pred = new PredicateSet(); $pred->setPredicates(static::buildPredicates($value, $table, $inverse)); $predicates[] = $pred; break; } } else { $pred = $inverse ? new NotInPredicate() : new InPredicate(); $pred->setField(FieldExpression::createWithTable($key, $table)); $pred->setExpression(ArrayExpression::create($value)); $predicates[] = $pred; } } } } } } return $predicates; }
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 insertInto($table, ...$fields) { /** * @var $this IStatement */ $clause = new InsertClause(); $this->addClause($clause); $clause->setTable($table); foreach ($fields as $field) { if (!$field instanceof FieldExpression) { $field = FieldExpression::create($field); } $clause->addField($field); } return $this; }
public function testAssemble() { $expression = new IncrementExpression(); $expression->setField(FieldExpression::create('new_field')); $this->assertEquals('new_field + 0', QueryAssembler::stringify($expression)); $expression->setIncrementValue('abc'); $this->assertEquals('new_field + 0', QueryAssembler::stringify($expression)); $expression->setIncrementValue('1'); $this->assertEquals('new_field + 1', QueryAssembler::stringify($expression)); $expression->setIncrementValue(1); $this->assertEquals('new_field + 1', QueryAssembler::stringify($expression)); $stmt = QueryBuilder::update('tbl'); $stmt->set('inc_field', $expression); $qa = new QueryAssembler($stmt); $this->assertEquals('UPDATE tbl SET inc_field = new_field + ?', $qa->getQuery()); $this->assertEquals([1], $qa->getParameters()); }
public function testAssemble() { $statement = QueryBuilder::insertInto('tbl'); $this->assertEquals('INSERT INTO tbl ()', QueryAssembler::stringify($statement)); $statement = QueryBuilder::insertInto('tbl', 'field'); $this->assertEquals('INSERT INTO tbl (field)', QueryAssembler::stringify($statement)); $statement->insertInto('tbl', FieldExpression::create('id'), 'name'); $this->assertEquals('INSERT INTO tbl (id, name)', QueryAssembler::stringify($statement)); $statement->values(null, ValueExpression::create('Test')); $this->assertEquals('INSERT INTO tbl (id, name) ' . 'VALUES (NULL, "Test")', QueryAssembler::stringify($statement)); $statement->values('row2v1', 'row2v2'); $this->assertEquals('INSERT INTO tbl (id, name) ' . 'VALUES (NULL, "Test"), ("row2v1", "row2v2")', QueryAssembler::stringify($statement)); $statement = QueryBuilder::insertInto('tbl', 'field1', 'field2')->values('value1', 'value2')->onDuplicateKeyUpdate('field1', IncrementExpression::create('field2', 1)); $assembler = new QueryAssembler($statement); $this->assertEquals('INSERT INTO tbl (field1, field2) VALUES (?, ?) ON DUPLICATE KEY UPDATE field1 = field2 + ?', $assembler->getQuery()); $this->assertEquals(['value1', 'value2', 1], $assembler->getParameters()); }
public function testSettersAndGetters() { $expression = new FieldExpression(); $expression->setField('new_field'); $this->assertEquals('new_field', QueryAssembler::stringify($expression)); }
public function testTableName() { $this->assertEquals('"mytable"."myfield"', CqlAssembler::stringify(FieldExpression::createWithTable('myfield', 'mytable'))); }
public function setField($field) { $this->_field = is_scalar($field) ? FieldExpression::create($field) : $field; return $this; }
public function assembleFieldExpression(FieldExpression $expr) { return ($expr->hasTable() ? $this->assembleSegment($expr->getTable()) . '.' : '') . $this->escapeField($expr->getField()); }
public function testListExpression() { $this->assertEquals("['test',1]", CqlAssembler::stringify(ListExpression::create(['test', 1]))); $this->assertEquals("\"testfield\" - ['test']", CqlAssembler::stringify(SubtractExpression::create(FieldExpression::create('testfield'), ListExpression::create('test')))); }
public function testAssemble() { $expression = MultiplyExpression::create(FieldExpression::create('fieldname'), NumericExpression::create(4)); $this->assertEquals('(fieldname * 4)', QueryAssembler::stringify($expression)); }
public function testFieldName() { $this->assertEquals('`myfield`', MySQLAssembler::stringify(FieldExpression::create('myfield'))); }
public function testSetExpression() { $this->assertEquals("{'test':'myval','test2':2}", CqlAssembler::stringify(MapExpression::create(['test' => 'myval', 'test2' => 2]))); $this->assertEquals("\"testfield\" + {'test'}", CqlAssembler::stringify(AdditionExpression::create(FieldExpression::create('testfield'), SetExpression::create('test')))); }
public function testSetExpression() { $this->assertEquals("{'test',1}", CqlAssembler::stringify(SetExpression::create(['test', 1]))); $this->assertEquals("\"testfield\" + {'test'}", CqlAssembler::stringify(AdditionExpression::create(FieldExpression::create('testfield'), SetExpression::create('test')))); }