コード例 #1
0
 public function assembleField(FieldExpression $field)
 {
     if ($field->hasTable()) {
         return $this->assembleTableExpression($field->getTable()) . '.' . $this->escapeField($field->getField());
     } else {
         return $this->escapeField($field->getField());
     }
 }
コード例 #2
0
 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));
 }
コード例 #3
0
 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))));
 }
コード例 #4
0
 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));
 }
コード例 #5
0
ファイル: JoinClause.php プロジェクト: packaged/querybuilder
 public function getDestination()
 {
     if (is_array($this->_dest)) {
         return FieldExpression::createWithTable($this->_dest[1], $this->_dest[0]);
     }
     return $this->_dest;
 }
コード例 #6
0
 public function addField($field)
 {
     if (is_scalar($field)) {
         $field = FieldExpression::create($field);
     }
     $this->_fields[] = $field;
 }
コード例 #7
0
 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());
 }
コード例 #8
0
 public function setField($field, $table = null)
 {
     if ($field === null || $field instanceof IExpression) {
         $this->_field = $field;
     } else {
         $this->_field = FieldExpression::createWithTable($field, $table);
     }
     return $this;
 }
コード例 #9
0
 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']);
 }
コード例 #10
0
 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());
 }
コード例 #11
0
 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());
 }
コード例 #12
0
 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));
 }
コード例 #13
0
 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;
 }
コード例 #14
0
ファイル: WhereClause.php プロジェクト: packaged/querybuilder
 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;
 }
コード例 #15
0
 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());
 }
コード例 #16
0
ファイル: InsertTrait.php プロジェクト: packaged/querybuilder
 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;
 }
コード例 #17
0
 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());
 }
コード例 #18
0
 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());
 }
コード例 #19
0
 public function testSettersAndGetters()
 {
     $expression = new FieldExpression();
     $expression->setField('new_field');
     $this->assertEquals('new_field', QueryAssembler::stringify($expression));
 }
コード例 #20
0
 public function testTableName()
 {
     $this->assertEquals('"mytable"."myfield"', CqlAssembler::stringify(FieldExpression::createWithTable('myfield', 'mytable')));
 }
コード例 #21
0
 public function setField($field)
 {
     $this->_field = is_scalar($field) ? FieldExpression::create($field) : $field;
     return $this;
 }
コード例 #22
0
 public function assembleFieldExpression(FieldExpression $expr)
 {
     return ($expr->hasTable() ? $this->assembleSegment($expr->getTable()) . '.' : '') . $this->escapeField($expr->getField());
 }
コード例 #23
0
 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'))));
 }
コード例 #24
0
 public function testAssemble()
 {
     $expression = MultiplyExpression::create(FieldExpression::create('fieldname'), NumericExpression::create(4));
     $this->assertEquals('(fieldname * 4)', QueryAssembler::stringify($expression));
 }
コード例 #25
0
 public function testFieldName()
 {
     $this->assertEquals('`myfield`', MySQLAssembler::stringify(FieldExpression::create('myfield')));
 }
コード例 #26
0
 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'))));
 }
コード例 #27
0
 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'))));
 }