Skip to content

Commit

Permalink
Add day of week ORM function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladislav Gallay committed Jun 13, 2015
1 parent 9d2dc4d commit 4ee00d9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/Database/Dialect/PostgresDialectTrait.php
Expand Up @@ -134,13 +134,19 @@ protected function _transformFunctionExpression(FunctionExpression $expression)
->name('')
->type(' + INTERVAL')
->iterateParts(function ($p, $key) {
if ($key === 1) {
if ($key === 1) {
$interval = sprintf("'%s'", key($p));
$p = [$interval => 'literal'];
}
return $p;
});
break;
case 'DAYOFWEEK':
$expression
->name('EXTRACT')
->add(['DOW' => 'literal'], [], true)
->type(' FROM');
break;
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Database/Dialect/SqliteDialectTrait.php
Expand Up @@ -138,6 +138,13 @@ protected function _transformFunctionExpression(FunctionExpression $expression)
}
return $p;
});
break;
case 'DAYOFWEEK':
$expression
->name('STRFTIME')
->type(' ,')
->add(["'%w'" => 'literal'], [], true);
break;
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/Database/Dialect/SqlserverDialectTrait.php
Expand Up @@ -241,7 +241,7 @@ protected function _transformFunctionExpression(FunctionExpression $expression)
break;
case 'DATE_ADD':
$params = [];
$visitor = function ($p, $key) (&$params) {
$visitor = function ($p, $key) use (&$params) {
if ($key === 0) {
$params[2] = $value;
} else {
Expand All @@ -250,8 +250,8 @@ protected function _transformFunctionExpression(FunctionExpression $expression)
$params[1] = $valueUnit[0];
}
return $p;
});
$manipulator = function ($p, $key) ($params) {
};
$manipulator = function ($p, $key) use ($params) {
return $params[$key];
};

Expand All @@ -262,6 +262,13 @@ protected function _transformFunctionExpression(FunctionExpression $expression)
->iterateParts($manipulator)
->add($params[2]);
break;
case 'DAYOFWEEK':
$expression
->name('DATEPART')
->type(' ')
->add(['weekday, ' => 'literal'], [], true)
->add([') - (1' => 'literal']); // SqlServer starts on index 1
break;
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/Database/FunctionsBuilder.php
Expand Up @@ -199,6 +199,32 @@ public function dateAdd($expression, $value, $unit, $types = [])
return $expression;
}

/**
* Returns a FunctionExpression representing a call to SQL WEEKDAY function.
* 0 - Sunday, 1 - Monday, 2 - Tuesday...
*
* @param mixed $expression the function argument
* @param array $types list of types to bind to the arguments
* @return FunctionExpression
*/
public function dayOfWeek($expression, $types = [])
{
return $this->_literalArgumentFunction('DAYOFWEEK', $expression, $types);
}

/**
* Returns a FunctionExpression representing a call to SQL WEEKDAY function.
* 0 - Sunday, 1 - Monday, 2 - Tuesday...
*
* @param mixed $expression the function argument
* @param array $types list of types to bind to the arguments
* @return FunctionExpression
*/
public function weekday($expression, $types = [])
{
return $this->dayOfWeek($expression, $types);
}

/**
* Returns a FunctionExpression representing a call that will return the current
* date and time. By default it returns both date and time, but you can also
Expand Down
18 changes: 17 additions & 1 deletion tests/TestCase/Database/FunctionsBuilderTest.php
Expand Up @@ -173,10 +173,26 @@ public function testExtract()
*
* @return void
*/
public function testExtract()
public function testDateAdd()
{
$function = $this->functions->dateAdd('created', -3, 'day');
$this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
$this->assertEquals("DATE_ADD(created, INTERVAL -3 day)", $function->sql(new ValueBinder));
}

/**
* Tests generating a DAYOFWEEK() function
*
* @return void
*/
public function testDayOfWeek()
{
$function = $this->functions->dayOfWeek('created');
$this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
$this->assertEquals("DAYOFWEEK(created)", $function->sql(new ValueBinder));

$function = $this->functions->weekday('created');
$this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
$this->assertEquals("DAYOFWEEK(created)", $function->sql(new ValueBinder));
}
}

0 comments on commit 4ee00d9

Please sign in to comment.