@quasipickle has v4 improved on supported SQL keywords?
In v3, i had to register custom functions on the db dialect to support things like INTERVAL
:
use Phalcon\Db\Dialect\Mysql;
class MyDialect extends Mysql
{
/**
* Transforms an intermediate representation for a expression into a database system valid expression
*
* @param array $expression
* @param string $escapeChar
* @param mixed $bindCounts
*
* @return string
* @throws \Exception
*/
public function getSqlExpression(array $expression, $escapeChar = null, $bindCounts = NULL)
{
if ($expression["type"] == 'functionCall') {
switch ($expression["name"]) {
case 'SIMILARITY':
case 'JARO_WINKLER_SIMILARITY':
if (count($expression["arguments"]) != 2) {
throw new \Exception('JARO_WINKLER_SIMILARITY requires 2 parameters');
}
return 'JARO_WINKLER_SIMILARITY('.$this->getSqlExpression($expression["arguments"][0]).', '.$this->getSqlExpression($expression["arguments"][1]).')';
break;
case 'DATE_INTERVAL':
if (count($expression["arguments"]) != 2) {
throw new \Exception('DATE_INTERVAL requires 2 parameters');
}
$unit = trim($expression["arguments"][1]['value'],"'");
switch($unit) {
case 'YEAR':
case 'MONTH':
case 'DAY':
case 'HOUR':
case 'MINUTE':
case 'SECOND':
return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' '.$unit;
break;
}
break;
case 'DATE_DIFF':
if (count($expression["arguments"]) != 3) {
throw new \Exception('DATE_DIFF requires 3 parameters');
}
$unit = trim($expression["arguments"][0]['value'],"'");
switch($unit) {
case 'YEAR':
case 'MONTH':
case 'DAY':
case 'HOUR':
case 'MINUTE':
case 'SECOND':
return 'TIMESTAMPDIFF('.$unit.', '.$this->getSqlExpression($expression["arguments"][1]).', '.$this->getSqlExpression($expression["arguments"][2]).')';
break;
}
break;
case 'TIME':
return "TIME(" . $this->getSqlExpression($expression["arguments"][0]) . ")";
break;
case 'AGE':
return "DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE()," . $this->getSqlExpression($expression["arguments"][0]) . ")), '%Y')";
break;
case 'FULLTEXT_MATCH':
if (count($expression["arguments"]) < 2) {
throw new \Exception('FULLTEXT_MATCH requires 2 parameters');
}
$arguments = array();
$length = count($expression["arguments"]) - 1;
for ($i = 0; $i < $length; $i++) {
$arguments[] = $this->getSqlExpression($expression["arguments"][$i]);
}
return 'MATCH(' . join(', ', $arguments) . ') AGAINST (' .
$this->getSqlExpression($expression["arguments"][$length]) . ')';
case 'FULLTEXT_MATCH_BMODE':
if (count($expression["arguments"]) < 2) {
throw new \Exception('FULLTEXT_MATCH requires 2 parameters');
}
$arguments = array();
$length = count($expression["arguments"]) - 1;
for ($i = 0; $i < $length; $i++) {
$arguments[] = $this->getSqlExpression($expression["arguments"][$i]);
}
return 'MATCH(' . join(', ', $arguments) . ') AGAINST (' .
$this->getSqlExpression($expression["arguments"][$length]) . ' IN BOOLEAN MODE)';
}
}
return parent::getSqlExpression($expression, $escapeChar);
}
}
// services.php
$di->setShared('db', function() {
$db = new Driver();
// etc...
$db->setDialect(new MyDialect);
return $db;
});
I've yet to migrate a project to v4, so i wouldnt know about that specifically...