tests
[m6w6/pq-gateway] / lib / pq / Query / Expr.php
index d02b13fea5d07781a9f136eb249fff5de8963796..0f3d86e52ba1db3ae3cfb99ca9391e7f3e2396e1 100644 (file)
@@ -8,17 +8,21 @@ class Expr
         * @var string
         */
        protected $expression;
+       
+       /**
+        * @var \pq\Query\Expr
+        */
+       protected $next;
 
        /**
         * @param string $e the expression or a format string followed by arguments
         * @param string ...
         */
-       function __construct($e) {
+       function __construct($e, $arg = null) {
                if (func_num_args() > 1) {
-                       $this->expression = call_user_func_array("sprintf", func_get_args());
-               } else {
-                       $this->expression = $e;
+                       $e = call_user_func_array("sprintf", func_get_args());
                }
+               $this->expression = trim($e);
        }
 
        /**
@@ -26,6 +30,33 @@ class Expr
         * @return string
         */
        function __toString() {
-               return (string) $this->expression;
+               $string = $this->expression;
+               if ($this->next) {
+                       $string .= " " . $this->next;
+               }
+               return (string) $string;
+       }
+       
+       /**
+        * Check for NULL
+        * @return bool
+        */
+       function isNull() {
+               return !strcasecmp($this->expression, "null");
+       }
+       
+       /**
+        * Append an expresssion
+        * @param \pq\Query\Expr $next
+        * @return \pq\Query\Expr $this
+        * @throws \UnexpectedValueException if any expr is NULL
+        */
+       function add(Expr $next) {
+               if ($this->isNull() || $next->isNull()) {
+                       throw new \UnexpectedValueException("Cannot add anything to NULL");
+               }
+               for ($that = $this; $that->next; $that = $that->next);
+               $that->next = $next;
+               return $this;
        }
 }