added dates/times to the parameter value coersion and pulled this into
[clsql.git] / sql / command-object.lisp
index 3b752ef3ca003a902c79b696eb4b8838a3d3773b..6af4bc86287a6e7aadfad03c179d01127c1140f8 100644 (file)
 (in-package #:clsql-sys)
 
 (defclass command-object ()
-  ((expression :accessor expression :initarg :expression :initform nil)
-   (parameters :accessor parameters :initarg :parameters :initform nil)
+  ((expression :accessor expression :initarg :expression :initform nil
+               :documentation "query that refers to parameters using \"$1\", \"$2\", \"$n\".
+       These match positions in the parameters list.")
+   (parameters :accessor parameters :initarg :parameters :initform nil
+               :documentation "list of parameters")
    (prepared-name :accessor prepared-name :initarg :prepared-name :initform ""
     :documentation "If we want this to be a prepared statement, give it a name
        to identify it to this session")
    (has-been-prepared :accessor has-been-prepared :initarg :has-been-prepared :initform nil
-                     :documentation "Have we already prepared this command object")
+                     :documentation "Have we already prepared this command object?")
    ))
 
+
+(defgeneric prepare-sql-parameter (sql-parameter)
+  (:documentation "This method is responsible for formatting parameters
+     as the database expects them (eg: :false is nil, nil is :null, dates are iso8601 strings)")
+  (:method (sql-parameter)
+    (typecase sql-parameter
+      (null :null)
+      (symbol
+       (if (member sql-parameter (list :false :F))
+           nil
+           (princ-to-string sql-parameter)))
+      (clsql-sys:date (format-date nil sql-parameter :format :iso8601))
+      (clsql-sys:wall-time (format-time nil sql-parameter :format :iso8601))
+      (t sql-parameter))))
+
 (defmethod initialize-instance :after ((o command-object) &key &allow-other-keys )
-  ;; Inits parameter nulls
+  ;; Inits parameter value coersion
   (setf (parameters o) (parameters o)))
 
 (defmethod (setf parameters) (new (o command-object))
   " This causes the semantics to match cl-sql instead of cl-postgresql
   "
   (setf (slot-value o 'parameters)
-       (loop for p in new
-             collecting (cond ((null p) :null)
-                              ((member p (list :false :F)) nil)
-                              (T p)))))
+       (loop for p in new collecting (prepare-sql-parameter p))))
 
 (defun reset-command-object (co)
   "Resets the command object to have no name and to be unprepared