-
Notifications
You must be signed in to change notification settings - Fork 25
/
sql-null.sls
67 lines (55 loc) · 1.78 KB
/
sql-null.sls
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
;;; "sql-null.scm" -- SQL NULL object and the ternary logic -*- Scheme -*-
;;; Ivan Shmakov, 2007 This code is in public domain.
(library (sql-null)
(export sql-null sql-null? sql-not sql-or sql-or sql-and sql-coalesce)
(import (chezscheme))
;;from chicken data-structures.scm Copyright (c) 2008-2014, The Chicken Team
(define (constantly . xs)
(if (eq? 1 (length xs))
(let ([x (car xs)])
(lambda _ x) )
(lambda _ (apply values xs)) ) )
;; We could also (define-record sql-null) and alias sql-null to make-sql-null
;; but that implies creating many new objects, which we don't want.
(define-record-type sql-null-type)
(define sql-null-object (make-sql-null-type))
(define sql-null (constantly sql-null-object))
(define sql-null? sql-null-type?)
(define (sql-not o)
(if (sql-null? o) o (not o)))
(define-syntax sql-or
(syntax-rules ()
((sql-or a ...)
(sql-or/null #f a ...))))
(define-syntax sql-or/null
(syntax-rules ()
((sql-or/null null)
null)
((sql-or/null null a b ...)
(let ((ea a))
(cond ((sql-null? ea) (sql-or/null ea b ...))
((not ea) (sql-or/null null b ...))
(else ea))))))
(define-syntax sql-and
(syntax-rules ()
((sql-and a ...)
(sql-and/null #t a ...))))
(define-syntax sql-and/null
(syntax-rules ()
((sql-and/null null)
null)
((sql-and/null null a b ...)
(let ((ea a))
(cond ((sql-null? ea) (sql-and/null ea b ...))
(ea (sql-and/null null b ...))
(else ea))))))
(define-syntax sql-coalesce
(syntax-rules ()
((sql-coalesce)
(sql-null))
((sql-coalesce a b ...)
(let ((ea a))
(if (sql-null? ea)
(sql-coalesce b ...)
ea)))))
)