Example #1
0
 public function testResolveBindObjects()
 {
     $this->assertEquals(['abc', PDO::PARAM_STR], $this->object->resolveBind(Query::func('SUBSTRING()'), 'abc'));
     $this->assertEquals([123, PDO::PARAM_INT], $this->object->resolveBind('foo', Query::expr('foo', '+', 123)));
 }
Example #2
0
 public function testSelectJoinsWithFunction()
 {
     $this->loadFixtures(['Users', 'Countries']);
     $query = $this->object->select()->where('User.id', 1);
     $query->fields(['id', 'username', Query::func('SUBSTR', ['username' => Func::FIELD, 1, 3])->asAlias('shortName')]);
     $query->leftJoin(['countries', 'Country'], ['id', 'name', 'iso', Query::func('SUBSTR', ['Country.name' => Func::FIELD, 1, 6])->asAlias('countryName')], ['country_id' => 'Country.id']);
     $this->assertEquals(new Entity(['id' => 1, 'username' => 'miles', 'shortName' => 'mil', 'countryName' => 'United', 'Country' => new Entity(['id' => 1, 'name' => 'United States of America', 'iso' => 'USA'])]), $query->first());
 }
Example #3
0
 public function testFunc()
 {
     $this->assertInstanceOf('Titon\\Db\\Query\\Func', Query::func('SUBSTRING', ['Foo', 1, 2]));
 }
Example #4
0
 /**
  * Perform an aggregation on the database and return the calculated value.
  * The currently supported aggregates are `avg`, `count`, `min`, `max`, and `sum`.
  *
  * @param \Titon\Db\Query $query
  * @param string $function
  * @param string $field
  * @return int
  */
 public function aggregate(Query $query, $function, $field)
 {
     $query->fields(Query::func(strtoupper($function), [$field => Func::FIELD])->asAlias('aggregate'));
     $results = $this->getDriver()->setContext('read')->executeQuery($query)->find();
     if (isset($results[0])) {
         return (int) $results[0]['aggregate'];
     }
     return 0;
 }
Example #5
0
 public function testFormatSelectFieldsFunctions()
 {
     $this->assertRegExp('/SUBSTR\\((`|\\")?name(`|\\")?, \\-3\\)/', $this->object->formatSelectFields([Query::func('SUBSTR', ['name' => Func::FIELD, -3])])[0]);
     $this->assertRegExp('/SUBSTR\\((`|\\")?name(`|\\")?, \\-3\\) AS (`|\\")?alias(`|\\")?/', $this->object->formatSelectFields([Query::func('SUBSTR', ['name' => Func::FIELD, -3])->asAlias('alias')], 'foo')[0]);
 }