summaryrefslogtreecommitdiffstats
path: root/rdms.scm
blob: 0fd4a2cf80407e608239c6ef680e97ae25876078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
;;; "rdms.scm" rewrite 6 - the saga continues
; Copyright 1994 Aubrey Jaffer
;
;Permission to copy this software, to redistribute it, and to use it
;for any purpose is granted, subject to the following restrictions and
;understandings.
;
;1.  Any copy made of this software must include this copyright notice
;in full.
;
;2.  I have made no warrantee or representation that the operation of
;this software will be error-free, and I am under no obligation to
;provide any services, by way of maintenance, update, or otherwise.
;
;3.  In conjunction with products arising from the use of this
;material, there shall be no use of my name in any advertising,
;promotional, or sales literature without prior written consent in
;each case.

(define rdms:catalog-name '*catalog-data*)
(define rdms:domains-name '*domains-data*)
(define rdms:columns-name '*columns*)

(define catalog:init-cols
  '((1 #t table-name		#f atom)
    (2 #f column-limit		#f uint)
    (3 #f coltab-name		#f atom)
    (4 #f bastab-id		#f base-id)
    (5 #f user-integrity-rule	#f expression)
    (6 #f view-procedure	#f expression)))

(define catalog:column-limit-pos 2)
(define catalog:coltab-name-pos 3)
(define catalog:bastab-id-pos 4)
(define catalog:integrity-rule-pos 5)
(define catalog:view-proc-pos 6)

(define columns:init-cols
  '((1 #t column-number		#f uint)
    (2 #f primary-key?		#f boolean)
    (3 #f column-name		#f symbol)
    (4 #f column-integrity-rule	#f expression)
    (5 #f domain-name		#f domain)))

(define columns:primary?-pos 2)
(define columns:name-pos 3)
(define columns:integrity-rule-pos 4)
(define columns:domain-name-pos 5)

(define domains:init-cols
  '((1 #t domain-name		#f atom)
    (2 #f foreign-table		#f atom)
    (3 #f domain-integrity-rule	#f expression)
    (4 #f type-id		#f type)
    (5 #f type-param		#f expression)))

(define domains:foreign-pos 2)
(define domains:integrity-rule-pos 3)
(define domains:type-id-pos 4)
(define domains:type-param-pos 5)

(define domains:init-data
  `((atom #f
          (lambda (x) (or (not x) (symbol? x) (number? x)))
          atom
          #f)
    (type #f
	  #f				;type checked when openning
	  symbol
	  #f)
    (base-id #f
	     (lambda (x) (or (symbol? x) (number? x)))
	     base-id
	     #f)
    (uint #f
          (lambda (x)
            (and (number? x)
                 (integer? x)
                 (not (negative? x))))
          integer
          #f)
    (expression #f #f expression #f)
    (boolean #f boolean? boolean #f)
    (symbol #f symbol? symbol #f)
    (string #f string? string #f)
    (domain ,rdms:domains-name #f atom #f)))

(define (rdms:warn identifier msg obj)
  (display identifier) (display #\ ) (display msg) (write obj) (newline))
(define rdms:error slib:error)

(define (make-relational-system base)
  (define basic
    (lambda (name)
      (let ((meth (base name)))
	(cond ((not meth) (rdms:error 'make-relational-system
				      "essential method missing for:" name)))
	meth)))

  (define (desc-row-type row)
    (let ((domain (assq (car (cddddr row)) domains:init-data)))
      (and domain (cadddr domain))))

  (let ((make-base (base 'make-base))
	(open-base (basic 'open-base))
	(write-base (base 'write-base))
	(sync-base (base 'sync-base))
	(close-base (basic 'close-base))
	(base:supported-type? (basic 'supported-type?))
	(base:supported-key-type? (basic 'supported-key-type?))
	(base:make-table (base 'make-table))
	(base:open-table (basic 'open-table))
	(base:kill-table (base 'kill-table))
	(present? (basic 'present?))
	(base:ordered-for-each-key (basic 'ordered-for-each-key))
	(base:for-each-primary-key (basic 'for-each-key))
	(base:map-primary-key (basic 'map-key))
	(base:catalog-id (basic 'catalog-id))
	(cat:keyify-1 ((basic 'make-keyifier-1)
		       (desc-row-type (assv 1 catalog:init-cols))))
	(itypes
	 (lambda (rows)
	   (map (lambda (row)
		  (let ((domrow (assq (car (cddddr row)) domains:init-data)))
		    (cond (domrow (cadddr domrow))
			  (else (rdms:error 'itypes "type not found for:"
					    (car (cddddr row)))))))
		rows))))

    (define (init-tab lldb id descriptor rows)
      (let ((han (base:open-table lldb id 1 (itypes descriptor)))
	    (keyify-1
	     ((base 'make-keyifier-1) (desc-row-type (assv 1 descriptor))))
	    (putter ((basic 'make-putter) 1 (itypes descriptor))))
	(for-each (lambda (row) (putter han (keyify-1 (car row)) (cdr row)))
		  rows)))

    (define cat:get-row
      (let ((cat:getter ((basic 'make-getter) 1 (itypes catalog:init-cols))))
	(lambda (bastab key)
	  (cat:getter bastab (cat:keyify-1 key)))))

    (define dom:get-row
      (let ((dom:getter ((basic 'make-getter) 1 (itypes domains:init-cols)))
	    (dom:keyify-1 ((basic 'make-keyifier-1)
			   (desc-row-type (assv 1 domains:init-cols)))))
	(lambda (bastab key)
	  (dom:getter bastab (dom:keyify-1 key)))))

    (define des:get-row
      (let ((des:getter ((basic 'make-getter) 1 (itypes columns:init-cols)))
	    (des:keyify-1 ((basic 'make-keyifier-1)
			   (desc-row-type (assv 1 columns:init-cols)))))
	(lambda (bastab key)
	  (des:getter bastab (des:keyify-1 key)))))

    (define (create-database filename)
      (cond ((and filename (file-exists? filename))
	     (rdms:warn 'create-database "file exists:" filename)))
      (let* ((lldb (make-base filename 1 (itypes catalog:init-cols)))
	     (cattab (and lldb (base:open-table lldb base:catalog-id 1
						(itypes catalog:init-cols)))))
	(cond
	 ((not lldb) (rdms:error 'make-base "failed.") #f)
	 ((not cattab) (rdms:error 'make-base "catalog missing.")
		       (close-base lldb)
		       #f)
	 (else
	  (let ((desdes-id (base:make-table lldb 1 (itypes columns:init-cols)))
		(domdes-id (base:make-table lldb 1 (itypes columns:init-cols)))
		(catdes-id (base:make-table lldb 1 (itypes columns:init-cols)))
		(domtab-id (base:make-table lldb 1 (itypes domains:init-cols)))
		)
	    (cond
	     ((not (and catdes-id domdes-id domtab-id desdes-id))
	      (rdms:error 'create-database "make-table failed.")
	      (close-base lldb)
	      #f)
	     (else
	      (init-tab lldb desdes-id columns:init-cols columns:init-cols)
	      (init-tab lldb domdes-id columns:init-cols domains:init-cols)
	      (init-tab lldb catdes-id columns:init-cols catalog:init-cols)
	      (init-tab lldb domtab-id domains:init-cols domains:init-data)
	      (init-tab
	       lldb base:catalog-id catalog:init-cols
	       `((*catalog-desc* 5 ,rdms:columns-name ,catdes-id #f #f)
		 (*domains-desc* 5 ,rdms:columns-name ,domdes-id #f #f)
		 (,rdms:catalog-name 6 *catalog-desc* ,base:catalog-id #f #f)
		 (,rdms:domains-name 5 *domains-desc* ,domtab-id #f #f)
		 (,rdms:columns-name 5 ,rdms:columns-name ,desdes-id #f #f)))
	      (init-database
	       filename #t lldb cattab
	       (base:open-table lldb domtab-id 1 (itypes domains:init-cols))
	       #f))))))))

    (define (base:catalog->domains lldb base:catalog)
      (let ((cat:row (cat:get-row base:catalog rdms:domains-name)))
	(and cat:row
	     (base:open-table lldb
			      (list-ref cat:row (+ -2 catalog:bastab-id-pos))
			      1 (itypes domains:init-cols)))))

    (define (open-database filename mutable)
      (let* ((lldb (open-base filename mutable))
	     (base:catalog
	      (and lldb (base:open-table lldb base:catalog-id
					 1 (itypes catalog:init-cols))))
	     (base:domains
	      (and base:catalog (base:catalog->domains lldb base:catalog))))
	(cond
	 ((not lldb) #f)
	 ((not base:domains) (close-base lldb) #f)
	 (else (init-database
		filename mutable lldb base:catalog base:domains #f)))))

    (define (init-database rdms:filename mutable lldb
			   base:catalog base:domains rdms:catalog)

      (define (write-database filename)
	(write-base lldb filename)
	(set! rdms:filename filename))

      (define (close-database)
	(close-base lldb)
	(set! rdms:filename #f)
	(set! base:catalog #f)
	(set! base:domains #f)
	(set! rdms:catalog #f))

      (define row-ref (lambda (row pos) (list-ref row (+ -2 pos))))
      (define row-eval (lambda (row pos)
			 (let ((ans (list-ref row (+ -2 pos))))
			   (and ans (slib:eval ans)))))

      (define (open-table table-name writable)
	(define cat:row (cat:get-row base:catalog table-name))
	(cond ((and writable (not mutable))
	       (rdms:error "can't open-table for writing:" table-name)))
	(let ((column-limit (row-ref cat:row catalog:column-limit-pos))
	      (desc-table
	       (base:open-table
		lldb
		(row-ref (cat:get-row
			  base:catalog
			  (row-ref cat:row catalog:coltab-name-pos))
			 catalog:bastab-id-pos)
		1 (itypes columns:init-cols)))
	      (base-table #f)
	      (base:get #f)
	      (primary-limit 1)
	      (column-name-alist '())
	      (column-foreign-list '())
	      (column-domain-list '())
	      (column-type-list '())
	      (export-alist '())
	      (cirs '())
	      (dirs '())
	      (list->key #f)
	      (key->list #f))

	  (if (not desc-table)
	      (rdms:error "descriptor table doesn't exist for:" table-name))
	  (do ((ci column-limit (+ -1 ci)))
	      ((zero? ci))
	    (let* ((des:row (des:get-row desc-table ci))
		   (column-name (row-ref des:row columns:name-pos))
		   (column-domain (row-ref des:row columns:domain-name-pos)))
	      (set! cirs
		    (cons (row-eval des:row columns:integrity-rule-pos) cirs))
	      (set! column-name-alist
		    (cons (cons column-name ci) column-name-alist))
	      (cond
	       (column-domain
		(let ((dom:row (dom:get-row base:domains column-domain)))
		  (set! dirs
			(cons (row-eval dom:row domains:integrity-rule-pos)
			      dirs))
		  (set! column-type-list
			(cons (row-ref dom:row domains:type-id-pos)
			      column-type-list))
		  (set! column-domain-list
			(cons column-domain column-domain-list))
		  (set! column-foreign-list
			(cons 
			 (let ((foreign-name
				(row-ref dom:row domains:foreign-pos)))
			   (cond
			    ((or (not foreign-name)
				 (eq? foreign-name table-name)) #f)
			    (else
			     (let* ((tab (open-table foreign-name #f))
				    (p? (and tab (tab 'get 1))))
			       (cond
				((not tab)
				 (rdms:error "foreign key table missing for:"
					     foreign-name))
				((not (= (tab 'primary-limit) 1))
				 (rdms:error "foreign key table wrong type:"
					     foreign-name))
				(else p?))))))
			 column-foreign-list))))
	       (else
		(rdms:error "missing domain for column:" ci column-name)))
	      (cond
	       ((row-ref des:row columns:primary?-pos)
		(set! primary-limit (max primary-limit ci))
		(cond
		 ((base:supported-key-type? (car column-type-list)))
		 (else (rdms:error "key type not supported by base tables:"
				   (car column-type-list)))))
	       ((base:supported-type? (car column-type-list)))
	       (else (rdms:error "type not supported by base tables:"
				 (car column-type-list))))))
	  (set! base-table
		(base:open-table lldb (row-ref cat:row catalog:bastab-id-pos)
				 primary-limit column-type-list))
	  (set! base:get ((basic 'make-getter) primary-limit column-type-list))
	  (set! list->key
		((basic 'make-list-keyifier) primary-limit column-type-list))
	  (set! key->list
		((basic 'make-key->list) primary-limit column-type-list))
	  (let ((export-method
		 (lambda (name proc)
		   (set! export-alist
			 (cons (cons name proc) export-alist))))
		(generalize-to-table
		 (lambda (operation)
		   (lambda ()
		     (base:for-each-primary-key base-table operation))))
		(accumulate-over-table
		 (lambda (operation)
		   (lambda () (base:map-primary-key base-table operation))))
		(ckey:retrieve		;ckey gets whole row (assumes exists)
		 (if (= primary-limit column-limit) key->list
		     (lambda (ckey) (append (key->list ckey)
					    (base:get base-table ckey))))))
	    (export-method
	     'row:retrieve
	     (if (= primary-limit column-limit)
		 (lambda keys
		   (let ((ckey (list->key keys)))
		     (and (present? base-table ckey) keys)))
		 (lambda keys
		   (let ((vals (base:get base-table (list->key keys))))
		     (and vals (append keys vals))))))
	    (export-method 'row:retrieve*
			   (accumulate-over-table
			    (if (= primary-limit column-limit) key->list
				ckey:retrieve)))
	    (export-method
	     'for-each-row
	     (let ((r (if (= primary-limit column-limit) key->list
			  ckey:retrieve)))
	       (lambda (proc) (base:ordered-for-each-key
			       base-table (lambda (ckey) (proc (r ckey)))))))
	    (cond
	     ((and mutable writable)
	      (letrec
		  ((combine-primary-keys
		    (cond
		     ((and (= primary-limit column-limit)
			   (> primary-limit 0))
		      list->key)
		     ((eq? list->key car) list->key)
		     (else
		      (case primary-limit
			((1) (let ((keyify-1 ((base 'make-keyifier-1)
					      (car column-type-list))))
			       (lambda (row) (keyify-1 (car row)))))
			((2) (lambda (row)
			       (list->key (list (car row) (cadr row)))))
			((3) (lambda (row)
			       (list->key (list (car row) (cadr row)
						(caddr row)))))
			((4) (lambda (row)
			       (list->key
				(list (car row) (cadr row)
				      (caddr row) (cadddr row)))))
			(else (rdms:error 'combine-primary-keys
					  "bad number of primary keys"
					  primary-limit))))))
		   (uir (row-eval cat:row catalog:integrity-rule-pos))
		   (check-rules
		    (lambda (row)
		      (if (= column-limit (length row)) #t
			  (rdms:error "bad row length:" row))
		      (for-each
		       (lambda (cir dir value column-name column-domain
				    foreign)
			 (cond
			  ((and dir (not (dir value)))
			   (rdms:error "violated domain integrity rule:"
				       table-name column-name
				       column-domain value))
			  ((and cir (not (cir value)))
			   (rdms:error "violated column integrity rule:"
				       table-name column-name value))
			  ((and foreign (not (foreign value)))
			   (rdms:error "foreign key missing:"
				       table-name column-name value))))
		       cirs dirs row column-name-alist column-domain-list
		       column-foreign-list)
		      (cond ((and uir (not (uir row)))
			     (rdms:error "violated user integrity rule:"
					 row)))))
		   (putter
		    ((basic 'make-putter) primary-limit column-type-list))
		   (row:insert
		    (lambda (row)
		      (check-rules row)
		      (let ((ckey (combine-primary-keys row)))
			(if (present? base-table ckey)
			    (rdms:error 'row:insert "row present:" row))
			(putter base-table ckey
				(list-tail row primary-limit)))))
		   (row:update
		    (lambda (row)
		      (check-rules row)
		      (putter base-table (combine-primary-keys row)
			      (list-tail row primary-limit)))))

		(export-method 'row:insert row:insert)
		(export-method 'row:insert*
			       (lambda (rows) (for-each row:insert rows)))
		(export-method 'row:update row:update)
		(export-method 'row:update*
			       (lambda (rows) (for-each row:update rows))))

	      (letrec ((base:delete (basic 'delete))
		       (ckey:remove (lambda (ckey)
				      (let ((r (ckey:retrieve ckey)))
					(and r (base:delete base-table ckey))
					r))))
		(export-method 'row:remove
			       (lambda keys
				 (let ((ckey (list->key keys)))
				   (and (present? base-table ckey)
					(ckey:remove ckey)))))
		(export-method 'row:delete
			       (lambda keys
				 (base:delete base-table (list->key keys))))
		(export-method 'row:remove*
			       (accumulate-over-table ckey:remove))
		(export-method 'row:delete*
			       (generalize-to-table
				(lambda (ckey) (base:delete base-table ckey))))
		(export-method 'close-table
			       (lambda () (set! base-table #f)
				       (set! desc-table #f)
				       (set! export-alist #f))))))

	    (export-method 'column-names (map car column-name-alist))
	    (export-method 'column-foreigns column-foreign-list)
	    (export-method 'column-domains column-domain-list)
	    (export-method 'column-types column-type-list)
	    (export-method 'primary-limit primary-limit)

	    (let ((translate-column
		   (lambda (column)
		     ;;(print 'translate-column column column-name-alist)
		     (let ((colp (assq column column-name-alist)))
		       (cond (colp (cdr colp))
			     ((and (number? column)
				   (integer? column)
				   (<= 1 column column-limit))
			      column)
			     (else (rdms:error "column not in table:"
					       column table-name)))))))
	      (lambda args
		(cond
		 ((null? args) #f)
		 ((null? (cdr args))
		  (let ((pp (assq (car args) export-alist)))
		    (and pp (cdr pp))))
		 ((not (null? (cddr args)))
		  (rdms:error "too many arguments to methods:" args))
		 (else
		  (let ((ci (translate-column (cadr args))))
		    (cond
		     ((<= ci primary-limit) ;primary-key?
		      (let ((key-extractor
			     ((base 'make-key-extractor)
			      primary-limit column-type-list ci)))
			(case (car args)
			  ((get) (lambda keys
				   (and (present? base-table (list->key keys))
					(list-ref keys (+ -1 ci)))))
			  ((get*) (lambda ()
				    (base:map-primary-key
				     base-table
				     (lambda (ckey) (key-extractor ckey)))))
			  (else #f))))
		     (else
		      (let ((index (- ci (+ 1 primary-limit))))
			(case (car args)
			  ((get) (lambda keys
				   (let ((row (base:get base-table
							(list->key keys))))
				     (and row (list-ref row index)))))
			  ((get*) (lambda ()
				    (base:map-primary-key
				     base-table
				     (lambda (ckey)
				       (list-ref (base:get base-table ckey)
						 index)))))
			  (else #f)))))))))))))

      (define create-table
	(and
	 mutable
	 (lambda (table-name . desc)
	   (if (not rdms:catalog)
	       (set! rdms:catalog (open-table rdms:catalog-name #t)) #f)
	   (cond
	    ((table-exists? table-name)
	     (rdms:error "table already exists:" table-name) #f)
	    ((null? desc)
	     (let ((colt-id
		    (base:make-table lldb 1 (itypes columns:init-cols))))
	       ((rdms:catalog 'row:insert)
		(list table-name
		      (length columns:init-cols)
		      ((rdms:catalog 'get 'coltab-name)
		       rdms:columns-name)
		      colt-id
		      #f
		      #f)))
	     (open-table table-name #t))
	    ((null? (cdr desc))
	     (set! desc (car desc))
	     (let ((colt-id ((rdms:catalog 'get 'bastab-id) desc)))
	       (cond
		(colt-id
		 (let ((coltable (open-table desc #f))
		       (types '())
		       (prilimit 0)
		       (colimit 0)
		       (colerr #f))
		   (for-each (lambda (n p d)
			       (if (number? n) (set! colimit (max colimit n))
				   (set! colerr #t))
			       (if p (set! prilimit (+ 1 prilimit)) #f)
			       (set! types
				     (cons (dom:get-row base:domains d)
					   types)))
			     ((coltable 'get* 'column-number))
			     ((coltable 'get* 'primary-key?))
			     ((coltable 'get* 'domain-name)))
		   (cond (colerr (rdms:error "some column lacks a number.") #f)
			 ((or (< prilimit 1)
			      (and (> prilimit 4)
				   (not (= prilimit colimit))))
			  (rdms:error "unreasonable number of primary keys:"
				      prilimit))
			 (else
			  ((rdms:catalog 'row:insert)
			   (list table-name colimit desc
				 (base:make-table lldb prilimit types) #f #f))
			  (open-table table-name #t)))))
		(else
		 (rdms:error "table descriptor not found for:" desc) #f))))
	    (else (rdms:error 'create-table "too many args:"
			      (cons table-name desc))
		  #f)))))

      (define (table-exists? table-name)
	(present? base:catalog (cat:keyify-1 table-name)))

      (define delete-table
	(and mutable
	     (lambda (table-name)
	       (if (not rdms:catalog)
		   (set! rdms:catalog (open-table rdms:catalog-name #t)) #f)
	       (let ((table (open-table table-name #t))
		     (row ((rdms:catalog 'row:remove) table-name)))
		 (and row (base:kill-table
			   lldb
			   (list-ref row (+ -1 catalog:bastab-id-pos))
			   (table 'primary-limit)
			   (table 'column-type-list))
		      row)))))

      (lambda (operation-name)
	(case operation-name
	  ((close-database) close-database)
	  ((write-database) write-database)
	  ((open-table) open-table)
	  ((delete-table) delete-table)
	  ((create-table) create-table)
	  ((table-exists?) table-exists?)
	  (else #f)))
      )
    (lambda (operation-name)
      (case operation-name
	((create-database) create-database)
	((open-database) open-database)
	(else #f)))
    ))