Commits

Robert Clipsham committed 51586ee

Added support for SQL functions, currently just NOW().

Comments (0)

Files changed (2)

serenity/SqlQuery.d

     Bind[] parameters;
 }
 
+struct Value
+{
+    bool isFunction = false;
+    union
+    {
+        char[] value;
+        Function func;
+    }
+}
+
 class SqlQuery
 {
     private QueryType mQt;
     private char[][] mColumns;
     private char[][] mWhere;
     private Bind[] mBinds;
-    private char[][] mValues;
+    private Value[] mValues;
     private long mLimit;
     private long mOffset;
 
      * Returns:
      *  A list of values represented as strings
      */
-    public char[][] getValues()
+    public Value[] getValues()
     {
         return mValues;
     }
          {
              static if (is(typeof(param) == bool))
              {
-                 mValues ~= param ? "1" : "0";
+                 mValues ~= Value(false, param ? "1" : "0");
+             }
+             else static if (is(typeof(param) == Function))
+             {
+                 Value v;
+                 v.isFunction = true;
+                 v.func = param;
+                 mValues ~= v;
              }
              else
              {
-                mValues ~= to!(char[])(param);
+                 mValues ~= Value(false, to!(char[])(param));
              }
          }
          return this;
          return this;
      }
 
+    /**
+     * NOW() SQL function
+     *
+     * Returns:
+     *  A Function representing NOW()
+     */
+    public Function now()
+    {
+        return Function(Function.Now, null);
+    }
+
+    /**
+     * Execute a SQL query in a worker thread
+     *
+     * Returns:
+     *  The result of the query
+     */
     public Result!(T) bgExecute(T)()
     {
         return execute!(T)(true);
     }
 
+    /**
+     * Execute a SQL query
+     *
+     * Params:
+     *  bg = Execute the query in a worker thread?
+     * Returns:
+     *  The result of the query
+     */
     public Result!(T) execute(T)(bool bg=false)
     {
         if (mQt == QueryType.Select && mColumns.length == 1 && mColumns[0] == "*")

serenity/SqlitePrinter.d

                 auto values = doc.getValues();
                 foreach (i, value; values)
                 {
-                    if (value == "?")
+                    if (value.isFunction)
                     {
-                        dg("?");
+                        switch (value.func.name)
+                        {
+                            case Function.Now:
+                                dg(`strftime('%Y-%m-%dT%H:%M:%S:%f', 'now')`);
+                                break;
+                            default:
+                                throw new SqlPrinterException("Unsupported datatype for SQLite");
+                                break;
+                        }
                     }
                     else
                     {
-                        dg("'", value, "'");
+                        if (value.value == "?")
+                        {
+                            dg("?");
+                        }
+                        else
+                        {
+                            dg("'", value.value, "'");
+                        }
                     }
                     if (i != values.length - 1)
                     {