From 8b6d2d1f0c137c15859117b94edd4d6cf70532b9 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 25 Nov 2014 08:50:16 -0600
Subject: [PATCH] orm: Support Interval literals and expressions

This adds support for things like

    SqlInterval::MINUTE(3)
    SqlExpression::minus(SqlFunction::NOW(), SqlInterval::DAY(1))
---
 include/class.orm.php | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/include/class.orm.php b/include/class.orm.php
index 3ea8898bd..84cf48112 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -478,6 +478,47 @@ class SqlFunction {
     }
 }
 
+class SqlExpression extends SqlFunction {
+    var $operator;
+    var $operands;
+
+    function toSql($compiler, $model=false, $alias=false) {
+        $O = array();
+        foreach ($this->args as $operand)
+            $O[] = $compiler->input($operand);
+        return implode(' '.$this->func.' ', $O);
+    }
+
+    static function __callStatic($operator, $operands) {
+        switch ($operator) {
+            case 'minus':
+                $operator = '-'; break;
+            case 'plus':
+                $operator = '+'; break;
+            default:
+                throw new InvalidArgumentException('Invalid operator specified');
+        }
+        return parent::__callStatic($operator, $operands);
+    }
+}
+
+class SqlInterval extends SqlFunction {
+    var $type;
+
+    function toSql($compiler, $model=false, $alias=false) {
+        return sprintf('INTERVAL %s %s',
+            $compiler->input($this->args[0]),
+            $this->func);
+    }
+
+    static function __callStatic($interval, $args) {
+        if (count($args) != 1) {
+            throw new InvalidArgumentException("Interval expects a single interval value");
+        }
+        return parent::__callStatic($interval, $args);
+    }
+}
+
 class Aggregate extends SqlFunction {
 
     var $func;
-- 
GitLab