+ (with-dataset *ds-employees*
+ (values
+ (let ((lenin (car (clsql:select 'employee
+ :where [= 1 [slot-value 'employee 'emplid]]
+ :flatp t
+ :caching nil))))
+ (format nil "~a ~a: ~a"
+ (first-name lenin)
+ (last-name lenin)
+ (employee-email lenin)))
+ (progn
+ (setf (slot-value employee1 'first-name) "Dimitriy"
+ (slot-value employee1 'last-name) "Ivanovich"
+ (slot-value employee1 'email) "ivanovich@soviet.org")
+ (clsql:update-record-from-slots employee1 '(first-name last-name email))
+ (let ((lenin (car (clsql:select 'employee
+ :where [= 1 [slot-value 'employee 'emplid]]
+ :flatp t
+ :caching nil))))
+ (format nil "~a ~a: ~a"
+ (first-name lenin)
+ (last-name lenin)
+ (employee-email lenin))))))
+ "Vladimir Lenin: lenin@soviet.org"
+ "Dimitriy Ivanovich: ivanovich@soviet.org")
+
+(deftest :oodml/update-records/4
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-node ()
+ (let ((base (car (clsql:select 'node
+ :where [= 1 [slot-value 'node 'node-id]]
+ :flatp t :caching nil))))
+ (format nil "~a ~a"
+ (slot-value base 'node-id)
+ (slot-value base 'title)))))
+ (values
+ (print-fresh-node)
+ (let ((base (car (clsql:select 'node
+ :where [= 1 [slot-value 'node 'node-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value base 'title) "Altered title")
+ (clsql:update-records-from-instance base)
+ (print-fresh-node)))))
+ "1 Bare node"
+ "1 Altered title")
+
+(deftest :oodml/update-records/4-slots ;just like 4, but use slots fns.
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-setting ()
+ (let ((node (car (clsql:select 'setting
+ :where [= 3 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (format nil "~a ~a ~a"
+ (slot-value node 'setting-id)
+ (slot-value node 'title)
+ (slot-value node 'vars)))))
+ (values
+ (print-fresh-setting)
+ (let ((node (car (clsql:select 'setting
+ :where [= 3 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Altered title")
+ (setf (slot-value node 'vars) "Altered vars")
+ (clsql-sys:update-record-from-slot node 'title)
+ (clsql-sys:update-record-from-slot node 'vars)
+ (print-fresh-setting))
+ (let ((node (car (clsql:select 'setting
+ :where [= 3 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Setting2")
+ (setf (slot-value node 'vars) "var 2")
+ (clsql:update-records-from-instance node)
+ (clsql-sys:update-record-from-slots node '(vars title))
+ (print-fresh-setting)))))
+ "3 Setting2 var 2"
+ "3 Altered title Altered vars"
+ "3 Setting2 var 2")
+
+(deftest :oodml/update-records/5
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-setting ()
+ (let ((node (car (clsql:select 'setting
+ :where [= 3 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (format nil "~a ~a ~a"
+ (slot-value node 'setting-id)
+ (slot-value node 'title)
+ (slot-value node 'vars)))))
+ (values
+ (print-fresh-setting)
+ (let ((node (car (clsql:select 'setting
+ :where [= 3 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Altered title")
+ (setf (slot-value node 'vars) "Altered vars")
+ (clsql:update-records-from-instance node)
+ (print-fresh-setting)))))
+ "3 Setting2 var 2"
+ "3 Altered title Altered vars")
+
+(deftest :oodml/update-records/5-slots
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-setting ()
+ (let ((node (car (clsql:select 'setting
+ :where [= 3 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (format nil "~a ~a ~a"
+ (slot-value node 'setting-id)
+ (slot-value node 'title)
+ (slot-value node 'vars)))))
+ (values
+ (print-fresh-setting)
+ (let ((node (car (clsql:select 'setting
+ :where [= 3 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Altered title")
+ (setf (slot-value node 'vars) "Altered vars")
+ (clsql-sys:update-record-from-slot node 'title)
+ (clsql-sys:update-record-from-slot node 'vars)
+ (print-fresh-setting))
+ (let ((node (car (clsql:select 'setting
+ :where [= 3 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Setting2")
+ (setf (slot-value node 'vars) "var 2")
+ (clsql-sys:update-record-from-slots node '(title vars))
+ (print-fresh-setting)))))
+ "3 Setting2 var 2"
+ "3 Altered title Altered vars"
+ "3 Setting2 var 2")
+
+(deftest :oodml/update-records/6
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-setting ()
+ (let ((node (car (clsql:select 'setting
+ :where [= 7 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (format nil "~a ~a ~a"
+ (slot-value node 'setting-id)
+ (slot-value node 'title)
+ (or (slot-value node 'vars) "NIL")))))
+ (values
+ (print-fresh-setting)
+ (let ((node (car (clsql:select 'setting
+ :where [= 7 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Altered title")
+ (setf (slot-value node 'vars) "Altered vars")
+ (clsql:update-records-from-instance node)
+ (print-fresh-setting))
+ (let ((node (car (clsql:select 'setting
+ :where [= 7 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "theme-2")
+ (setf (slot-value node 'vars) nil)
+ (clsql:update-records-from-instance node)
+ (print-fresh-setting)))))
+ "7 theme-2 NIL"
+ "7 Altered title Altered vars"
+ "7 theme-2 NIL")
+
+(deftest :oodml/update-records/7
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-user ()
+ "requery to get what the db has, and print out."
+ (let ((node (car (clsql:select 'user
+ :where [= 5 [slot-value 'user 'user-id]]
+ :flatp t :caching nil))))
+ (format nil "~a ~a ~a"
+ (slot-value node 'user-id)
+ (slot-value node 'title)
+ (slot-value node 'nick)))))
+ (values
+ (print-fresh-user)
+ (let ((node (car (clsql:select 'user
+ :where [= 5 [slot-value 'user 'user-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Altered title")
+ (setf (slot-value node 'nick) "Altered nick")
+ (clsql:update-records-from-instance node)
+ (print-fresh-user)))))
+ "5 user-2 second user"
+ "5 Altered title Altered nick")
+
+(deftest :oodml/update-records/8
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-theme ()
+ (let ((node (car (clsql:select 'theme
+ :where [= 6 [slot-value 'theme 'theme-id]]
+ :flatp t :caching nil))))
+ (with-slots (node-id setting-id theme-id title vars doc) node
+ (format nil "~a ~a ~a ~a ~a ~a"
+ node-id setting-id theme-id
+ title (or vars "NIL") doc)))))
+ (values
+ (print-fresh-theme)
+ (let ((node (car (clsql:select 'setting
+ :where [= 6 [slot-value 'setting 'setting-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Altered title")
+ (setf (slot-value node 'vars) nil)
+ (clsql:update-records-from-instance node)
+ (print-fresh-theme))
+ (let ((node (car (clsql:select 'theme
+ :where [= 6 [slot-value 'theme 'theme-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "Altered title again")
+ (setf (slot-value node 'doc) "altered doc")
+ (clsql:update-records-from-instance node)
+ (print-fresh-theme))
+ (let ((node (car (clsql:select 'theme
+ :where [= 6 [slot-value 'theme 'theme-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value node 'title) "theme-1")
+ (setf (slot-value node 'vars) "empty")
+ (setf (slot-value node 'doc) "first theme")
+ (clsql:update-records-from-instance node)
+ (print-fresh-theme)))))
+ "6 6 6 theme-1 empty first theme"
+ "6 6 6 Altered title NIL first theme"
+ "6 6 6 Altered title again NIL altered doc"
+ "6 6 6 theme-1 empty first theme")
+
+(deftest :oodml/update-records/9
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-subloc ()
+ (let ((sl (car (clsql:select 'subloc
+ :where [= 10 [slot-value 'subloc 'subloc-id]]
+ :flatp t :caching nil))))
+ (unless sl
+ (error "Couldn't find expected sublocation"))
+ (format nil "~a ~a ~a"
+ (slot-value sl 'subloc-id)
+ (slot-value sl 'title)
+ (slot-value sl 'loc)))))
+ (values
+ (print-fresh-subloc)
+ (let ((sl (car (clsql:select 'subloc
+ :where [= 10 [slot-value 'subloc 'subloc-id]]
+ :flatp t :caching nil))))
+ (setf (slot-value sl 'title) "Altered subloc title")
+ (setf (slot-value sl 'loc) "Altered loc")
+ (clsql:update-records-from-instance sl)
+ (print-fresh-subloc)))))
+ "10 subloc-1 a subloc"
+ "10 Altered subloc title Altered loc")
+
+(deftest :oodml/update-records/9-slots ;like 9, but use slots fns.
+ (with-dataset *ds-nodes*
+ (flet ((print-fresh-subloc ()
+ (let ((sl (car (clsql:select 'subloc
+ :where [= 10 [slot-value 'subloc 'subloc-id]]
+ :flatp t :caching nil))))
+ (unless sl
+ (error "In psfl: found no sublocation with id = 10"))
+ (format nil "~a ~a ~a"
+ (slot-value sl 'subloc-id)
+ (slot-value sl 'title)
+ (slot-value sl 'loc)))))
+ (values
+ (print-fresh-subloc)
+ (let ((sl (car (clsql:select 'subloc
+ :where [= 10 [slot-value 'subloc 'subloc-id]]
+ :flatp t :caching nil))))
+ (unless sl
+ (error "Select for modification: Found no sublocation with id = 10"))
+ (setf (slot-value sl 'title) "Altered subloc title")
+ (setf (slot-value sl 'loc) "Altered loc")
+ (clsql:update-record-from-slot sl 'title)
+ (clsql:update-record-from-slot sl 'loc)
+ (print-fresh-subloc))
+ (let ((sl (car (clsql:select 'subloc
+ :where [= 10 [slot-value 'subloc 'subloc-id]]
+ :flatp t :caching nil))))
+ (unless sl
+ (error "Select for next modification: Found no sublocation with id = 10"))
+ (setf (slot-value sl 'title) "subloc-1")
+ (setf (slot-value sl 'loc) "a subloc")
+ (clsql:update-record-from-slots sl '(title loc))
+ (print-fresh-subloc)))))
+ "10 subloc-1 a subloc"
+ "10 Altered subloc title Altered loc"
+ "10 subloc-1 a subloc")
+
+;; Verify that we can set a float to nil and then read it back
+;; (was failing in Postgresql at somepoint)
+(deftest :oodml/update-records/10
+ (with-dataset *ds-employees*
+ (let ((emp (first (clsql:select 'employee :where [= [emplid] 1] :flatp t))))
+ (setf (height emp) nil)
+ (clsql-sys:update-record-from-slot emp 'height)
+ (values
+ (clsql:select [height] :from [employee] :where [= [emplid] 1])
+ (progn
+ (setf (height emp) 42.0)
+ (clsql-sys:update-record-from-slot emp 'height)
+ (clsql:select [height] :from [employee] :where [= [emplid] 1]))
+ (progn
+ (setf (height emp) 24.13d0)
+ (clsql-sys:update-record-from-slot emp 'height)
+ (clsql:select [height] :from [employee] :where [= [emplid] 1])))))
+ ((nil))
+ ((42.0d0))
+ ((24.13d0)))
+
+(deftest :oodml/update-records/11
+ (with-dataset *ds-artists*
+ (clsql:update-records-from-instance artist1)
+ (list (name artist1) (artist_id artist1)))
+ ("Mogwai" 1))
+
+(deftest :oodml/update-records/12
+ (with-dataset *ds-artists*
+ (clsql:update-records-from-instance artist1)
+ (list (name artist1) (genre artist1)))
+ ("Mogwai" "Unknown"))
+
+;; tests update-instance-from-records