1 (in-package #:clsql-tests)
3 (clsql-sys:file-enable-sql-reader-syntax)
4 (defparameter company1 nil)
5 (defparameter employee1 nil)
6 (defparameter employee2 nil)
7 (defparameter employee3 nil)
8 (defparameter employee4 nil)
9 (defparameter employee5 nil)
10 (defparameter employee6 nil)
11 (defparameter employee7 nil)
12 (defparameter employee8 nil)
13 (defparameter employee9 nil)
14 (defparameter employee10 nil)
15 (defparameter address1 nil)
16 (defparameter address2 nil)
17 (defparameter address3 nil)
18 (defparameter employee-address1 nil)
19 (defparameter employee-address2 nil)
20 (defparameter employee-address3 nil)
21 (defparameter employee-address4 nil)
22 (defparameter employee-address5 nil)
23 (defparameter employee-address6 nil)
26 ((extraterrestrial :initform nil :initarg :extraterrestrial)))
28 (def-view-class person (thing)
29 ((height :db-kind :base :accessor height :type float
31 (married :db-kind :base :accessor married :type boolean
33 (birthday :type clsql:wall-time :initarg :birthday)
34 (bd-utime :type clsql:universal-time :initarg :bd-utime)
35 (hobby :db-kind :virtual :initarg :hobby :initform nil)))
37 (def-view-class employee (person)
40 :db-constraints (:not-null :unique)
45 :db-constraints :not-null
57 :accessor employee-email
64 :accessor employee-company
66 :db-info (:join-class company
68 :foreign-key companyid
74 :accessor employee-manager
76 :db-info (:join-class employee
81 :accessor employee-addresses
83 :db-info (:join-class employee-address
88 (:base-table employee))
90 (def-view-class company ()
93 :db-constraints :not-null
98 :db-constraints :not-null
106 :initarg :presidentid)
110 :db-info (:join-class employee
111 :home-key presidentid
115 :reader company-employees
117 :db-info (:join-class employee
118 :home-key (companyid groupid)
119 :foreign-key (ecompanyid groupid)
122 (def-view-class address ()
125 :db-constraints :not-null
130 :initarg :street-number)
134 :initarg :street-name)
137 :void-value "no city"
144 :initarg :postal-code))
147 ;; many employees can reside at many addressess
148 (def-view-class employee-address ()
149 ((aemplid :type integer :initarg :emplid)
150 (aaddressid :type integer :initarg :addressid)
151 (verified :type boolean :initarg :verified)
152 (address :db-kind :join
153 :db-info (:join-class address
155 :foreign-key addressid
156 :retrieval :immediate))
157 (employee :db-kind :join
158 :db-info (:join-class employee
161 :retrieval :immediate)))
162 (:base-table "ea_join"))
164 (def-view-class deferred-employee-address ()
165 ((aemplid :type integer :initarg :emplid)
166 (aaddressid :type integer :initarg :addressid)
167 (verified :type boolean :initarg :verified)
168 (address :db-kind :join
169 :db-info (:join-class address
171 :foreign-key addressid
174 (:base-table "ea_join"))
178 (defun initialize-ds-employees ()
179 ;; (start-sql-recording :type :both)
180 (let ((*backend-warning-behavior*
181 (if (member *test-database-type* '(:postgresql :postgresql-socket))
184 (mapc #'clsql:create-view-from-class
185 '(employee company address employee-address)))
188 (setq *test-start-utime* (get-universal-time))
189 (let* ((*db-auto-sync* t)
190 (now-time (clsql:utime->time *test-start-utime*)))
191 (setf company1 (make-instance 'company
195 :name "Widgets Inc.")
196 employee1 (make-instance 'employee
200 :height (1+ (random 1.00))
201 :bd-utime *test-start-utime*
203 :first-name "Vladimir"
205 :email "lenin@soviet.org"
207 employee2 (make-instance 'employee
210 :height (1+ (random 1.00))
212 :bd-utime *test-start-utime*
216 :email "stalin@soviet.org"
219 employee3 (make-instance 'employee
222 :height (1+ (random 1.00))
224 :bd-utime *test-start-utime*
228 :email "trotsky@soviet.org"
231 employee4 (make-instance 'employee
234 :height (1+ (random 1.00))
236 :bd-utime *test-start-utime*
239 :last-name "Kruschev"
240 :email "kruschev@soviet.org"
243 employee5 (make-instance 'employee
247 :height (1+ (random 1.00))
248 :bd-utime *test-start-utime*
251 :last-name "Brezhnev"
252 :email "brezhnev@soviet.org"
255 employee6 (make-instance 'employee
259 :height (1+ (random 1.00))
260 :bd-utime *test-start-utime*
263 :last-name "Andropov"
264 :email "andropov@soviet.org"
267 employee7 (make-instance 'employee
270 :height (1+ (random 1.00))
272 :bd-utime *test-start-utime*
274 :first-name "Konstantin"
275 :last-name "Chernenko"
276 :email "chernenko@soviet.org"
279 employee8 (make-instance 'employee
282 :height (1+ (random 1.00))
284 :bd-utime *test-start-utime*
286 :first-name "Mikhail"
287 :last-name "Gorbachev"
288 :email "gorbachev@soviet.org"
291 employee9 (make-instance 'employee
295 :height (1+ (random 1.00))
296 :bd-utime *test-start-utime*
300 :email "yeltsin@soviet.org"
303 employee10 (make-instance 'employee
307 :height (1+ (random 1.00))
308 :bd-utime *test-start-utime*
310 :first-name "Vladimir"
312 :email "putin@soviet.org"
315 address1 (make-instance 'address
318 :street-name "Park Place"
321 address2 (make-instance 'address
323 address3 (make-instance 'address
325 employee-address1 (make-instance 'employee-address
329 employee-address2 (make-instance 'employee-address
333 employee-address3 (make-instance 'employee-address
337 employee-address4 (make-instance 'employee-address
341 employee-address5 (make-instance 'employee-address
344 employee-address6 (make-instance 'employee-address
348 ;; sleep to ensure birthdays are no longer at current time
349 ;(sleep 1) ;want to find the test that depends on it, put the sleep there.
352 ;; Lenin manages everyone ;
353 (clsql:add-to-relation employee2 'manager employee1)
354 (clsql:add-to-relation employee3 'manager employee1)
355 (clsql:add-to-relation employee4 'manager employee1)
356 (clsql:add-to-relation employee5 'manager employee1)
357 (clsql:add-to-relation employee6 'manager employee1)
358 (clsql:add-to-relation employee7 'manager employee1)
359 (clsql:add-to-relation employee8 'manager employee1)
360 (clsql:add-to-relation employee9 'manager employee1)
361 (clsql:add-to-relation employee10 'manager employee1)
362 ;; Everyone works for Widgets Inc. ;
363 (clsql:add-to-relation company1 'employees employee1)
364 (clsql:add-to-relation company1 'employees employee2)
365 (clsql:add-to-relation company1 'employees employee3)
366 (clsql:add-to-relation company1 'employees employee4)
367 (clsql:add-to-relation company1 'employees employee5)
368 (clsql:add-to-relation company1 'employees employee6)
369 (clsql:add-to-relation company1 'employees employee7)
370 (clsql:add-to-relation company1 'employees employee8)
371 (clsql:add-to-relation company1 'employees employee9)
372 (clsql:add-to-relation company1 'employees employee10)
373 ;; Lenin is president of Widgets Inc. ;
374 (clsql:add-to-relation company1 'president employee1)
377 ;; store these instances
379 (clsql:update-records-from-instance employee1)
380 (clsql:update-records-from-instance employee2)
381 (clsql:update-records-from-instance employee3)
382 (clsql:update-records-from-instance employee4)
383 (clsql:update-records-from-instance employee5)
384 (clsql:update-records-from-instance employee6)
385 (clsql:update-records-from-instance employee7)
386 (clsql:update-records-from-instance employee8)
387 (clsql:update-records-from-instance employee9)
388 (clsql:update-records-from-instance employee10)
389 (clsql:update-records-from-instance company1)
390 (clsql:update-records-from-instance address1)
391 (clsql:update-records-from-instance address2)
396 (def-dataset *ds-employees*
397 (:setup initialize-ds-employees)
399 (mapc #'clsql-sys:drop-view-from-class
400 '(employee company address employee-address))
402 (clsql-sys:execute-command "DROP TABLE ea_join")))))