Add support for :default in db constraints. make-constraint-description: use next...
[clsql.git] / sql / oodml.lisp
index 5cf86230dfdab18cc4018ee9793ea4007bc6e7be..dc010fb886cb4507b01a5689bcf415bc9f86906d 100644 (file)
                                          (ordered-class-direct-slots view-class)
                                          (ordered-class-slots view-class))))
                (record-values (mapcar #'slot-value-list slots)))
+
           (cond ((and (not (normalizedp view-class))
                       (not record-values))
                  (error "No settable slots."))
                  (insert-records :into (sql-expression :table view-class-table)
                                  :av-pairs record-values
                                  :database database)
-                 (when pk-slot
-                   (if (or (and (listp (view-class-slot-db-constraints pk-slot))
-                                (member :auto-increment (view-class-slot-db-constraints pk-slot)))
-                           (eql (view-class-slot-db-constraints pk-slot) :auto-increment))
-                       (setf pk (or pk
-                                    (car (query "SELECT LAST_INSERT_ID();"
-                                                :flatp t :field-names nil
-                                                :database database))))
-                       (setf pk (or pk
-                                    (slot-value obj (slot-definition-name pk-slot))))))
+
+                 (when (and pk-slot (not pk))
+                   (setf pk (if (member :auto-increment (listify (view-class-slot-db-constraints pk-slot)))
+                                (setf (slot-value obj (slot-definition-name pk-slot))
+                                      (database-last-auto-increment-id database
+                                                                      table
+                                                                      pk-slot)))))
+                 (setf pk (or pk
+                              (slot-value
+                               obj (slot-definition-name pk-slot)))))
                  (when (eql this-class nil)
-                   (setf (slot-value obj 'view-database) database)))))))
+                   (setf (slot-value obj 'view-database) database))))))
+    ;; handle slots with defaults
+    (dolist (slot slots)
+      (when (and (slot-exists-p slot 'db-constraints)
+                (listp (view-class-slot-db-constraints slot))
+                (member :default (view-class-slot-db-constraints slot)))
+       (update-slot-from-record obj (slot-definition-name slot))))
+
     pk))
 
 (defmethod delete-instance-records ((instance standard-db-object))
                                                      :result-types nil
                                                      :database vd))))
              (when res
+              (setf (slot-value instance 'view-database) vd)
                (get-slot-values-from-view instance (mapcar #'car sels) (car res))))
             (pres)
             (t nil)))))
            (res (select att-ref :from  view-table :where view-qual
                                                   :result-types nil)))
       (when res
+       (setf (slot-value instance 'view-database) vd)
         (get-slot-values-from-view instance (list slot-def) (car res))))))
 
 (defmethod update-slot-with-null ((object standard-db-object)