r9449: * db-odbc/odbc-sql.lisp, db-aodbc/aodbc-sql.lisp: Move common code to
[clsql.git] / tests / test-fdml.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File:    test-fdml.lisp
4 ;;;; Author:  Marcus Pearce <m.t.pearce@city.ac.uk>, Kevin Rosenberg
5 ;;;; Created: 30/03/2004
6 ;;;; Updated: $Id$
7 ;;;;
8 ;;;; Tests for the CLSQL Functional Data Manipulation Language
9 ;;;; (FDML).
10 ;;;;
11 ;;;; This file is part of CLSQL.
12 ;;;;
13 ;;;; CLSQL users are granted the rights to distribute and use this software
14 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
15 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
16 ;;;; ======================================================================
17
18 (in-package #:clsql-tests)
19
20 #.(clsql:locally-enable-sql-reader-syntax)
21
22 (setq *rt-fdml*
23       '(
24         
25 ;; inserts a record using all values only and then deletes it 
26 (deftest :fdml/insert/1
27     (progn
28       (clsql:insert-records :into [employee] 
29                            :values `(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
30                                      1 1 1.85 t ,(clsql:get-time)))
31       (values 
32        (clsql:select [first-name] [last-name] [email]
33                     :from [employee] :where [= [emplid] 11])
34        (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
35               (clsql:select [*] :from [employee] :where [= [emplid] 11]))))
36   (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
37
38 ;; inserts a record using attributes and values and then deletes it
39 (deftest :fdml/insert/2
40     (progn
41       (clsql:insert-records :into [employee] 
42                            :attributes '(emplid groupid first_name last_name
43                                          email ecompanyid managerid)
44                            :values '(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
45                                      1 1))
46       (values 
47        (clsql:select [first-name] [last-name] [email] :from [employee]
48                     :where [= [emplid] 11])
49        (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
50               (clsql:select [*] :from [employee] :where [= [emplid] 11]))))
51   (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
52
53 ;; inserts a record using av-pairs and then deletes it
54 (deftest :fdml/insert/3
55     (progn
56       (clsql:insert-records :into [employee] 
57                            :av-pairs'((emplid 11) (groupid 1)
58                                       (first_name "Yuri")
59                                       (last_name "Gagarin")
60                                       (email "gagarin@soviet.org")
61                                       (ecompanyid 1) (managerid 1)))
62       (values 
63        (clsql:select [first-name] [last-name] [email] :from [employee]
64                     :where [= [emplid] 11])
65        (progn (clsql:delete-records :from [employee] :where [= [emplid] 11])
66               (clsql:select [first-name] [last-name] [email] :from [employee]
67                            :where [= [emplid] 11]))))
68   (("Yuri" "Gagarin" "gagarin@soviet.org")) nil)
69
70 ;; inserts a records using a query from another table 
71 (deftest :fdml/insert/4
72     (progn
73       (clsql:create-table [employee2] '(([forename] string)
74                                  ([surname] string)
75                                  ([email] string)))
76       (clsql:insert-records :into [employee2] 
77                      :query [select [first-name] [last-name] [email] 
78                                     :from [employee]]
79                      :attributes '(forename surname email))
80       (prog1
81           (equal (clsql:select [*] :from [employee2])
82                  (clsql:select [first-name] [last-name] [email]
83                               :from [employee]))
84         (clsql:drop-table [employee2] :if-does-not-exist :ignore)))
85   t)
86
87 ;; updates a record using attributes and values and then deletes it
88 (deftest :fdml/update/1
89     (progn
90       (clsql:update-records [employee] 
91                            :attributes '(first_name last_name email)
92                            :values '("Yuri" "Gagarin" "gagarin@soviet.org")
93                            :where [= [emplid] 1])
94       (values 
95        (clsql:select [first-name] [last-name] [email] :from [employee]
96                     :where [= [emplid] 1])
97        (progn
98          (clsql:update-records [employee] 
99                               :av-pairs'((first_name "Vladamir")
100                                          (last_name "Lenin")
101                                          (email "lenin@soviet.org"))
102                               :where [= [emplid] 1])
103          (clsql:select [first-name] [last-name] [email] :from [employee]
104                       :where [= [emplid] 1]))))
105   (("Yuri" "Gagarin" "gagarin@soviet.org"))
106   (("Vladamir" "Lenin" "lenin@soviet.org")))
107
108 ;; updates a record using av-pairs and then deletes it
109 (deftest :fdml/update/2
110     (progn
111       (clsql:update-records [employee] 
112                            :av-pairs'((first_name "Yuri")
113                                       (last_name "Gagarin")
114                                       (email "gagarin@soviet.org"))
115                            :where [= [emplid] 1])
116       (values 
117        (clsql:select [first-name] [last-name] [email] :from [employee]
118                     :where [= [emplid] 1])
119        (progn
120          (clsql:update-records [employee]
121                               :av-pairs'((first_name "Vladamir")
122                                          (last_name "Lenin")
123                                          (email "lenin@soviet.org"))
124                               :where [= [emplid] 1])
125          (clsql:select [first-name] [last-name] [email]
126                       :from [employee] :where [= [emplid] 1]))))
127   (("Yuri" "Gagarin" "gagarin@soviet.org"))
128   (("Vladamir" "Lenin" "lenin@soviet.org")))
129
130
131 ;; Computed values are not always classified as numeric by psqlodbc
132 (deftest :fdml/query/1
133     (let ((count (caar (clsql:query "SELECT COUNT(*) FROM EMPLOYEE WHERE (EMAIL LIKE '%org')" :field-names nil))))
134       (if (stringp count)
135           (nth-value 0 (parse-integer count))
136         count))
137   10)
138
139 (deftest :fdml/query/2
140     (multiple-value-bind (rows field-names)
141         (clsql:query
142          "SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEE WHERE (EMPLID <= 5) ORDER BY LAST_NAME")
143       (values rows (mapcar 'string-upcase field-names)))
144   (("Leonid" "Brezhnev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
145    ("Josef" "Stalin") ("Leon" "Trotsky"))
146   ("FIRST_NAME" "LAST_NAME"))
147
148 (deftest :fdml/query/3
149     (caar (clsql:query "SELECT EMPLID FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil))
150   6)
151   
152 (deftest :fdml/query/4
153     (typep (caar (clsql:query "SELECT HEIGHT FROM EMPLOYEE WHERE LAST_NAME = 'Andropov'" :field-names nil))
154      'float)
155   t)
156   
157 (deftest :fdml/query/5
158  (clsql:query (clsql:sql [select [first-name] [sum [emplid]] :from [employee]] 
159                         [group-by [first-name]] [order-by [sum [emplid]]])
160               :field-names nil :result-types nil)
161  (("Josef" "2") ("Leon" "3") ("Nikita" "4") ("Leonid" "5") ("Yuri" "6")
162   ("Konstantin" "7") ("Mikhail" "8") ("Boris" "9") ("Vladamir" "11")))
163
164 (deftest :fdml/query/6
165  (clsql:query (clsql:sql [union [select [emplid] :from [employee]] 
166                          [select [groupid] :from [company]]])
167               :field-names nil :result-types nil :flatp t)
168  ("1" "2" "3" "4" "5" "6" "7" "8" "9" "10"))
169
170 (deftest :fdml/query/7
171  (clsql:query (clsql:sql [intersect [select [emplid] :from [employee]] 
172                          [select [groupid] :from [company]]])
173               :field-names nil :result-types nil :flatp t)
174  ("1"))
175
176 (deftest :fdml/query/8
177  (clsql:query (clsql:sql [except [select [emplid] :from [employee]] 
178                          [select [groupid] :from [company]]])
179               :field-names nil :result-types nil :flatp t)
180  ("2" "3" "4" "5" "6" "7" "8" "9" "10")) 
181
182 (deftest :fdml/execute-command/1
183     (values
184      (clsql:table-exists-p [foo] :owner *test-database-user*)
185      (progn
186        (clsql:execute-command "create table foo (bar integer)")
187        (clsql:table-exists-p [foo] :owner *test-database-user*))
188      (progn
189        (clsql:execute-command "drop table foo")
190        (clsql:table-exists-p [foo] :owner *test-database-user*)))
191   nil t nil)
192
193
194 ;; compare min, max and average hieghts in inches (they're quite short
195 ;; these guys!) 
196 (deftest :fdml/select/1
197     (let ((max (clsql:select [function "floor"
198                              [/ [* [max [height]] 100] 2.54]]
199                              :from [employee]
200                              :result-types nil 
201                              :flatp t))
202           (min (clsql:select [function "floor"
203                              [/ [* [min [height]] 100] 2.54]]
204                              :from [employee]
205                              :result-types nil 
206                              :flatp t))
207           (avg (clsql:select [function "floor"
208                              [avg [/ [* [height] 100] 2.54]]]
209                              :from [employee]
210                              :result-types nil 
211                              :flatp t)))
212       (apply #'< (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
213                          (append min avg max))))
214  t)
215
216 (deftest :fdml/select/2
217  (clsql:select [first-name] :from [employee] :flatp t :distinct t
218                             :field-names nil 
219                             :result-types nil 
220                             :order-by [first-name])
221  ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
222   "Yuri"))
223
224 (deftest :fdml/select/3
225     (clsql:select [first-name] [count [*]] :from [employee]
226                           :result-types nil 
227                           :group-by [first-name]
228                           :order-by [first-name]
229                           :field-names nil)
230  (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
231   ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1")))
232
233 (deftest :fdml/select/4
234     (clsql:select [last-name] :from [employee] 
235                           :where [like [email] "%org"]
236                           :order-by [last-name]
237                           :field-names nil 
238                           :result-types nil 
239                           :flatp t)
240  ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
241   "Stalin" "Trotsky" "Yeltsin"))
242
243 (deftest :fdml/select/5
244     (clsql:select [email] :from [employee] :flatp t :result-types nil 
245                           :where [in [employee emplid]
246                           [select [managerid] :from [employee]]]
247                           :field-names nil)
248   ("lenin@soviet.org"))
249
250 (deftest :fdml/select/6
251     (if (clsql-sys:db-type-has-fancy-math? *test-database-underlying-type*)
252         (mapcar #'(lambda (s) (parse-integer s :junk-allowed t))
253                 (clsql:select [function "trunc" [height]] :from [employee]
254                               :result-types nil 
255                               :field-names nil
256                               :flatp t))
257         (mapcar #'(lambda (s) (truncate (parse-integer s :junk-allowed t)))
258                 (clsql:select [height] :from [employee] :flatp t 
259                               :field-names nil :result-types nil)))
260  (1 1 1 1 1 1 1 1 1 1))
261
262 (deftest :fdml/select/7
263     (clsql:select [max [emplid]] :from [employee] :flatp t 
264                   :field-names nil :result-types nil)
265   ("10"))
266
267 (deftest :fdml/select/8
268     (clsql:select [min [emplid]] :from [employee] :flatp t 
269                   :field-names nil :result-types nil)
270   ("1"))
271
272 (deftest :fdml/select/9
273     (subseq 
274      (car 
275       (clsql:select [avg [emplid]] :from [employee] :flatp t 
276                     :field-names nil :result-types nil)) 
277      0 3)
278   "5.5")
279
280 (deftest :fdml/select/10
281     (clsql:select [last-name] :from [employee]
282                   :where [not [in [emplid]
283                   [select [managerid] :from [company]]]]
284                   :result-types nil 
285                   :field-names nil 
286                   :flatp t
287                   :order-by [last-name])
288  ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
289   "Trotsky" "Yeltsin"))
290
291 (deftest :fdml/select/11
292     (clsql:select [last-name] :from [employee] :where [married] :flatp t
293                   :field-names nil :order-by [emplid] :result-types nil)
294   ("Lenin" "Stalin" "Trotsky"))
295
296 (deftest :fdml/select/12
297     (let ((v 1))
298       (clsql:select [last-name] :from [employee] :where [= [emplid] v]
299                     :field-names nil :result-types nil))
300   (("Lenin")))
301
302 (deftest :fdml/select/13
303      (multiple-value-bind (results field-names) 
304          (clsql:select [emplid] [last-name] :from [employee] 
305                        :where [= [emplid] 1])
306        (values results (mapcar #'string-downcase field-names)))
307  ((1 "Lenin"))
308  ("emplid" "last_name"))
309
310 (deftest :fdml/select/14
311      (floatp (car (clsql:select [height] :from [employee] :where [= [emplid] 1] 
312                                 :flatp t)))
313   t)
314
315 (deftest :fdml/select/15
316     (multiple-value-bind (rows field-names)
317         (clsql:select [addressid] [street-number] [street-name] [city_field] [zip] 
318          :from [addr]
319          :where [= 1 [addressid]])
320       (values
321        rows
322        (mapcar #'string-downcase field-names)))
323   ((1 10 "Park Place" "Leningrad" 123))
324   ("addressid" "street_number" "street_name" "city_field" "zip"))
325
326 (deftest :fdml/select/16
327     (clsql:select [emplid] :from [employee] :where [= 1 [emplid]]
328      :field-names nil)
329   ((1)))
330
331 (deftest :fdml/select/17
332     (clsql:select [emplid] [last-name] :from [employee] :where [= 1 [emplid]]
333      :field-names nil)
334   ((1 "Lenin")))
335
336 (deftest :fdml/select/18
337     (clsql:select [emplid :string] [last-name] :from [employee] :where [= 1 [emplid]]
338      :field-names nil)
339   (("1" "Lenin")))
340
341 (deftest :fdml/select/19
342     (clsql:select [emplid] :from [employee] :order-by [emplid] 
343                            :where [between [* [emplid] 10] [* 5 10] [* 10 10]]
344                            :field-names nil :result-types nil :flatp t)
345  ("5" "6" "7" "8" "9" "10"))
346
347 (deftest :fdml/select/20
348     (clsql:select [emplid] :from [employee] :order-by [emplid] 
349                            :where [not [between [* [emplid] 10] [* 5 10] [* 10 10]]]
350                            :field-names nil :result-types nil :flatp t)
351  ("1" "2" "3" "4"))
352
353 (deftest :fdml/select/21 
354   (clsql:select [substr [first-name] 1 4] :from [employee] 
355                 :flatp t :order-by [emplid] :field-names nil)
356  ("Vlad" "Jose" "Leon" "Niki" "Leon" "Yuri" "Kons" "Mikh" "Bori" "Vlad"))
357
358 (deftest :fdml/select/22 
359   (clsql:select [|| [first-name] " " [last-name]] :from [employee]
360                 :flatp t :order-by [emplid] :field-names nil)
361  ("Vladamir Lenin" "Josef Stalin" "Leon Trotsky" "Nikita Kruschev"
362  "Leonid Brezhnev" "Yuri Andropov" "Konstantin Chernenko" "Mikhail Gorbachev"
363  "Boris Yeltsin" "Vladamir Putin"))
364
365 (deftest :fdml/select/23
366  (clsql:select [emplid] :from [employee] :where [in [emplid] '(1 2 3 4)]
367                         :flatp t :order-by [emplid] :field-names nil
368                         :result-types nil)
369  ("1" "2" "3" "4"))
370
371 (deftest :fdml/select/24
372  (clsql:select [distinct [first-name]] :from [employee] :flatp t
373                :order-by [first-name] :field-names nil :result-types nil)
374  ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
375   "Yuri"))
376
377 (deftest :fdml/select/25
378  (clsql:select [first-name] :from (clsql-sys:convert-to-db-default-case "employee" *default-database*)
379   :flatp t :distinct t
380   :field-names nil 
381   :result-types nil 
382   :order-by [first-name])
383  ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
384   "Yuri"))
385
386 (deftest :fdml/select/26
387  (clsql:select ["table" first-name] ["table" last-name] 
388   :from '([employee "table"] [employee "join"])
389   :where [and [= ["table" first-name] 
390                  ["join" first-name]]
391               [not [= ["table" emplid] 
392                       ["join" emplid]]]]
393   :order-by '(["table" last-name])
394   :result-types nil :field-names nil)
395  (("Vladamir" "Lenin") ("Vladamir" "Putin")))
396
397 (deftest :fdml/select/27 
398  (clsql:select [coalesce [managerid] 10] :from [employee] :order-by [emplid]
399   :field-names nil :result-types nil :flatp t)
400  ("10" "1" "1" "1" "1" "1" "1" "1" "1" "1"))
401   
402 (deftest :fdml/select/28 
403  (loop for column in `([*] [emplid]) collect         
404       (clsql:select [count column] :from [employee] 
405                     :flatp t :result-types nil :field-names nil))
406  (("10") ("10")))
407
408 (deftest :fdml/select/29 
409  (clsql:select [first-name] [last-name] :from [employee] 
410                        :result-types nil :field-names nil 
411                        :order-by '(([first-name] :asc) ([last-name] :desc)))
412  (("Boris" "Yeltsin") ("Josef" "Stalin") ("Konstantin" "Chernenko")
413   ("Leon" "Trotsky") ("Leonid" "Brezhnev") ("Mikhail" "Gorbachev")
414   ("Nikita" "Kruschev") ("Vladamir" "Putin") ("Vladamir" "Lenin")
415   ("Yuri" "Andropov")))
416
417 (deftest :fdml/select/30 
418  (clsql:select [first-name] [last-name] :from [employee] 
419                        :result-types nil :field-names nil 
420                        :order-by '(([first-name] :asc) ([last-name] :asc)))
421  (("Boris" "Yeltsin") ("Josef" "Stalin") ("Konstantin" "Chernenko")
422   ("Leon" "Trotsky") ("Leonid" "Brezhnev") ("Mikhail" "Gorbachev")
423   ("Nikita" "Kruschev") ("Vladamir" "Lenin") ("Vladamir" "Putin")
424   ("Yuri" "Andropov")))
425
426 (deftest :fdml/select/31
427  (clsql:select [last-name] :from [employee]                   
428               :set-operation [union [select [first-name] :from [employee]
429                                             :order-by [last-name]]]
430               :flatp t
431               :result-types nil 
432               :field-names nil)
433  ("Andropov" "Boris" "Brezhnev" "Chernenko" "Gorbachev" "Josef" "Konstantin"
434  "Kruschev" "Lenin" "Leon" "Leonid" "Mikhail" "Nikita" "Putin" "Stalin"
435  "Trotsky" "Vladamir" "Yeltsin" "Yuri"))
436
437 (deftest :fdml/select/32
438     (clsql:select [emplid] :from [employee]
439                 :where [= [emplid] [any [select [companyid] :from [company]]]]
440                 :flatp t :result-types nil :field-names nil)
441   ("1"))
442
443 (deftest :fdml/select/33
444  (clsql:select [last-name] :from [employee] 
445               :where [> [emplid] [all [select [groupid] :from [employee]]]]
446               :order-by [last-name] 
447               :flatp t :result-types nil :field-names nil)
448 ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Putin" "Stalin"
449  "Trotsky" "Yeltsin"))
450
451 (deftest :fdml/select/34
452   (loop for x from 1 below 5
453    collect
454    (car
455     (clsql:select [last-name] :from [employee] 
456                   :where [= [emplid] x]
457                   :flatp t :result-types nil :field-names nil)))
458   ("Lenin" "Stalin" "Trotsky" "Kruschev"))
459
460 (deftest :fdml/do-query/1
461     (let ((result '()))
462     (clsql:do-query ((name) [select [last-name] :from [employee]
463                                    :order-by [last-name]])
464       (push name result))
465     result)
466  ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
467             "Chernenko" "Brezhnev" "Andropov")) 
468
469 (deftest :fdml/map-query/1
470     (clsql:map-query 'list #'identity
471                     [select [last-name] :from [employee] :flatp t
472                             :order-by [last-name]])
473   ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
474               "Stalin" "Trotsky" "Yeltsin"))
475
476 (deftest :fdml/map-query/2
477     (clsql:map-query 'vector #'identity
478                     [select [last-name] :from [employee] :flatp t
479                             :order-by [last-name]])
480   #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
481     "Stalin" "Trotsky" "Yeltsin"))
482
483 (deftest :fdml/map-query/3 
484  (clsql:map-query 'list #'identity
485                   [select [last-name] :from [employee] :order-by [last-name]])
486  (("Andropov") ("Brezhnev") ("Chernenko") ("Gorbachev") ("Kruschev") ("Lenin")
487   ("Putin") ("Stalin") ("Trotsky") ("Yeltsin")))
488
489 (deftest :fdml/map-query/4 
490  (clsql:map-query 'list #'identity
491                   [select [first-name] [last-name] :from [employee] 
492                           :order-by [last-name]])
493  (("Yuri" "Andropov") ("Leonid" "Brezhnev") ("Konstantin" "Chernenko")
494   ("Mikhail" "Gorbachev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
495   ("Vladamir" "Putin") ("Josef" "Stalin") ("Leon" "Trotsky") 
496   ("Boris" "Yeltsin")))
497   
498 (deftest :fdml/loop/1
499     (loop for (forename surname)
500       being each tuple in
501       [select [first-name] [last-name] :from [employee] :order-by [last-name]]
502       collect (concatenate 'string forename " " surname))
503   ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
504                    "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin"
505    "Josef Stalin" "Leon Trotsky" "Boris Yeltsin"))
506
507 (deftest :fdml/loop/2
508     (loop for (addressid)
509       being each tuple in
510       [select [addressid] :from [addr] :order-by [addressid]]
511      collect addressid)
512   (1 2))
513
514 (deftest :fdml/loop/3
515     (loop for addressid
516       being each tuple in
517       [select [addressid] :from [addr] :order-by [addressid]]
518       collect addressid)
519   (1 2))
520
521 ;; starts a transaction deletes a record and then rolls back the deletion 
522 (deftest :fdml/transaction/1
523     (let ((results '()))
524       ;; test if we are in a transaction
525       (push (clsql:in-transaction-p) results)
526       ;;start a transaction 
527       (clsql:start-transaction)
528       ;; test if we are in a transaction
529       (push (clsql:in-transaction-p) results)
530       ;;Putin has got to go
531       (clsql:delete-records :from [employee] :where [= [last-name] "Putin"])
532       ;;Should be nil 
533       (push 
534        (clsql:select [*] :from [employee] :where [= [last-name] "Putin"])
535        results)
536       ;;Oh no, he's still there
537       (clsql:rollback)
538       ;; test that we are out of the transaction
539       (push (clsql:in-transaction-p) results)
540       ;; Check that we got him back alright 
541       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
542                          :flatp t)
543             results)
544       (apply #'values (nreverse results)))
545   nil t nil nil ("putin@soviet.org"))
546
547 ;; starts a transaction, updates a record and then rolls back the update
548 (deftest :fdml/transaction/2
549     (let ((results '()))
550       ;; test if we are in a transaction
551       (push (clsql:in-transaction-p) results)
552       ;;start a transaction 
553       (clsql:start-transaction)
554       ;; test if we are in a transaction
555       (push (clsql:in-transaction-p) results)
556       ;;Putin has got to go
557       (clsql:update-records [employee]
558        :av-pairs '((email "putin-nospam@soviet.org"))
559        :where [= [last-name] "Putin"])
560       ;;Should be new value  
561       (push (clsql:select [email] :from [employee]
562                          :where [= [last-name] "Putin"]
563                          :flatp t)
564             results)
565       ;;Oh no, he's still there
566       (clsql:rollback)
567       ;; test that we are out of the transaction
568       (push (clsql:in-transaction-p) results)
569       ;; Check that we got him back alright 
570       (push (clsql:select [email] :from [employee] :where [= [last-name] "Putin"]
571                          :flatp t)
572             results)
573       (apply #'values (nreverse results)))
574   nil t ("putin-nospam@soviet.org") nil ("putin@soviet.org")) 
575
576 ;; runs an update within a transaction and checks it is committed
577 (deftest :fdml/transaction/3
578     (let ((results '()))
579       ;; check status 
580       (push (clsql:in-transaction-p) results)
581       ;; update records 
582       (push
583        (clsql:with-transaction () 
584          (clsql:update-records [employee] 
585                               :av-pairs '((email "lenin-nospam@soviet.org"))
586                               :where [= [emplid] 1]))
587        results)
588       ;; check status 
589       (push (clsql:in-transaction-p) results)
590       ;; check that was committed 
591       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
592                          :flatp t)
593             results)
594       ;; undo the changes 
595       (push
596        (clsql:with-transaction () 
597          (clsql:update-records [employee] 
598                               :av-pairs '((email "lenin@soviet.org"))
599                               :where [= [emplid] 1]))
600        results)
601       ;; and check status 
602       (push (clsql:in-transaction-p) results)
603       ;; check that was committed 
604       (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
605                          :flatp t)
606             results)
607       (apply #'values (nreverse results)))
608   nil :committed nil ("lenin-nospam@soviet.org") :committed
609   nil ("lenin@soviet.org"))
610
611 ;; runs a valid update and an invalid one within a transaction and checks
612 ;; that the valid update is rolled back when the invalid one fails. 
613 (deftest :fdml/transaction/4
614     (let ((results '()))
615       ;; check status
616       (push (clsql:in-transaction-p) results)
617       (handler-case 
618           (clsql:with-transaction () 
619             ;; valid update
620             (clsql:update-records [employee] 
621                                   :av-pairs '((email "lenin-nospam@soviet.org"))
622                                   :where [= [emplid] 1])
623             ;; invalid update which generates an error 
624             (clsql:update-records [employee] 
625                                   :av-pairs
626                                   '((emale "lenin-nospam@soviet.org"))
627                                   :where [= [emplid] 1]))
628         (clsql:sql-database-error ()
629           (progn
630             ;; check status 
631             (push (clsql:in-transaction-p) results)
632             ;; and check nothing done 
633             (push (clsql:select [email] :from [employee] :where [= [emplid] 1]
634                                :flatp t)
635                   results)
636             (apply #'values (nreverse results))))))
637   nil nil ("lenin@soviet.org"))
638
639 ))
640
641 #.(clsql:restore-sql-reader-syntax-state)