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
|
(use sqlite3 srfi-1 posix regex regex-case srfi-69 csv-xml s11n md5 message-digest base64)
(import (prefix sqlite3 sqlite3:))
(import (prefix base64 base64:))
(declare (unit sdb))
;;
(define (sdb:open) ;; (conc *toppath* "/megatest.db") (car *configinfo*)))
(if (not *toppath*)
(if (not (launch:setup-for-run))
(begin
(debug:print 0 "ERROR: Attempted to open db when not in megatest area. Exiting.")
(exit))))
(let* ((dbpath (conc *toppath* "/db/sdb.db")) ;; fname)
(dbexists (let ((fe (file-exists? dbpath)))
(if fe
fe
(begin
(create-directory (conc *toppath* "/db") #t)
#f))))
(sdb (sqlite3:open-database dbpath))
(handler (make-busy-timeout (if (args:get-arg "-override-timeout")
(string->number (args:get-arg "-override-timeout"))
136000))))
(sqlite3:set-busy-handler! sdb handler)
(if (not dbexists)
(sdb:initialize sdb))
(sqlite3:execute sdb "PRAGMA synchronous = 1;")
sdb))
(define (sdb:initialize sdb)
(sqlite3:execute sdb "CREATE TABLE IF NOT EXISTS strs
(id INTEGER PRIMARY KEY,
str TEXT,
CONSTRAINT str UNIQUE (str));")
(sqlite3:execute sdb "CREATE INDEX strindx ON strs (str);"))
;; (define sumup (let ((a 0))(lambda (x)(set! a (+ x a)) a)))
(define (sdb:register-string sdb str)
(sqlite3:execute sdb "INSERT OR IGNORE INTO strs (str) VALUES (?);" str))
(define (sdb:string->id sdb str-cache str)
|
|
<
<
<
<
<
|
|
|
|
|
<
<
|
|
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
|
(use sqlite3 srfi-1 posix regex regex-case srfi-69 csv-xml s11n md5 message-digest base64)
(import (prefix sqlite3 sqlite3:))
(import (prefix base64 base64:))
(declare (unit sdb))
;;
(define (sdb:open fname)
(let* ((dbpath (pathname-directory fname))
(dbexists (let ((fe (file-exists? fname)))
(if fe
fe
(begin
(create-directory dbpath #t)
#f))))
(sdb (sqlite3:open-database fname))
(handler (make-busy-timeout 136000)))
(sqlite3:set-busy-handler! sdb handler)
(if (not dbexists)
(sdb:initialize sdb))
(sqlite3:execute sdb "PRAGMA synchronous = 1;")
sdb))
(define (sdb:initialize sdb)
(sqlite3:execute sdb "CREATE TABLE IF NOT EXISTS strs
(id INTEGER PRIMARY KEY,
str TEXT,
CONSTRAINT str UNIQUE (str));")
(sqlite3:execute sdb "CREATE INDEX IF NOT EXISTS strindx ON strs (str);"))
;; (define sumup (let ((a 0))(lambda (x)(set! a (+ x a)) a)))
(define (sdb:register-string sdb str)
(sqlite3:execute sdb "INSERT OR IGNORE INTO strs (str) VALUES (?);" str))
(define (sdb:string->id sdb str-cache str)
|
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
|
(lambda (istr)
(set! str istr)
(hash-table-set! id-cache id str))
sdb
"SELECT str FROM strs WHERE id=?;" id))
str))
(define sdb:qry
(let ((sdb #f)
(scache (make-hash-table))
(icache (make-hash-table)))
(lambda (cmd var)
(if (not sdb)(set! sdb (sdb:open)))
(case cmd
((init) (if (not sdb)(set! sdb (sdb:open))))
((finalize!) (if sdb (sqlite3:finalize! sdb)))
((getid) (let ((id (sdb:string->id sdb scache var)))
(if id
id
(begin
(sdb:register-string sdb var)
(sdb:string->id sdb scache var)))))
((getstr) (if (or (number? var)
(string->number var))
(sdb:id->string sdb icache var)
var))
(else #f)))))
|
>
>
|
<
|
>
>
>
>
>
|
>
>
>
>
|
>
>
|
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
|
(lambda (istr)
(set! str istr)
(hash-table-set! id-cache id str))
sdb
"SELECT str FROM strs WHERE id=?;" id))
str))
;; Numbers get passed though in both directions
;;
(define (make-sdb:qry fname)
(let ((sdb #f)
(scache (make-hash-table))
(icache (make-hash-table)))
(lambda (cmd var)
(case cmd
((setup) (set! sdb (if (not sdb)
(sdb:open (if var var fname)))))
((setdb) (set! sdb var))
((getdb) sdb)
((finalize) (if sdb
(begin
(sqlite3:finalize! sdb)
(set! sdb #f))))
((getid) (let ((id (if (or (number? var)
(string->number var))
var
(sdb:string->id sdb scache var))))
(if id
id
(begin
(sdb:register-string sdb var)
(sdb:string->id sdb scache var)))))
((getstr) (if (or (number? var)
(string->number var))
(sdb:id->string sdb icache var)
var))
((passid) var)
((passstr) var)
(else #f)))))
|