1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.files kernel tools.test db db.tuples classes
4 db.types continuations namespaces math math.ranges
5 prettyprint calendar sequences db.sqlite math.intervals
6 db.postgresql accessors random math.bitwise
7 math.ranges strings urls fry ;
10 TUPLE: person the-id the-name the-number the-real
11 ts date time blob factor-blob url ;
13 : <person> ( name age real ts date time blob factor-blob url -- person )
25 : <user-assigned-person> ( id name age real ts date time blob factor-blob url -- person )
35 [ ] [ person recreate-table ] unit-test
36 [ ] [ person ensure-table ] unit-test
37 [ ] [ person drop-table ] unit-test
38 [ ] [ person create-table ] unit-test
39 [ person create-table ] must-fail
40 [ ] [ person ensure-table ] unit-test
42 [ ] [ person1 get insert-tuple ] unit-test
44 [ 1 ] [ person1 get the-id>> ] unit-test
46 [ ] [ person1 get 200 >>the-number drop ] unit-test
48 [ ] [ person1 get update-tuple ] unit-test
50 [ T{ person f 1 "billy" 200 3.14 } ]
51 [ T{ person f 1 } select-tuple ] unit-test
52 [ ] [ person2 get insert-tuple ] unit-test
55 T{ person f 1 "billy" 200 3.14 }
56 T{ person f 2 "johnny" 10 3.14 }
58 ] [ T{ person f f f f 3.14 } select-tuples ] unit-test
61 T{ person f 1 "billy" 200 3.14 }
62 T{ person f 2 "johnny" 10 3.14 }
64 ] [ T{ person f } select-tuples ] unit-test
68 T{ person f 2 "johnny" 10 3.14 }
70 ] [ T{ person f f f 10 3.14 } select-tuples ] unit-test
73 [ ] [ person1 get delete-tuples ] unit-test
74 [ f ] [ T{ person f 1 } select-tuple ] unit-test
76 [ ] [ person3 get insert-tuple ] unit-test
86 T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
87 T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
88 T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
89 B{ 115 116 111 114 101 105 110 97 98 108 111 98 }
91 ] [ T{ person f 3 } select-tuple ] unit-test
93 [ ] [ person4 get insert-tuple ] unit-test
102 T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
103 T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
104 T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
106 H{ { 1 2 } { 3 4 } { 5 "lol" } }
107 URL" http://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search"
109 ] [ T{ person f 4 } select-tuple ] unit-test
111 [ ] [ person drop-table ] unit-test ;
113 : db-assigned-person-schema ( -- )
116 { "the-id" "ID" +db-assigned-id+ }
117 { "the-name" "NAME" { VARCHAR 256 } +not-null+ }
118 { "the-number" "AGE" INTEGER { +default+ 0 } }
119 { "the-real" "REAL" DOUBLE { +default+ 0.3 } }
120 { "ts" "TS" TIMESTAMP }
124 { "factor-blob" "FB" FACTOR-BLOB }
127 "billy" 10 3.14 f f f f f f <person> person1 set
128 "johnny" 10 3.14 f f f f f f <person> person2 set
130 T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
131 T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
132 T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
133 B{ 115 116 111 114 101 105 110 97 98 108 111 98 } f f <person> person3 set
135 T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
136 T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
137 T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
138 f H{ { 1 2 } { 3 4 } { 5 "lol" } } URL" http://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search" <person> person4 set ;
140 : user-assigned-person-schema ( -- )
143 { "the-id" "ID" INTEGER +user-assigned-id+ }
144 { "the-name" "NAME" { VARCHAR 256 } +not-null+ }
145 { "the-number" "AGE" INTEGER { +default+ 0 } }
146 { "the-real" "REAL" DOUBLE { +default+ 0.3 } }
147 { "ts" "TS" TIMESTAMP }
151 { "factor-blob" "FB" FACTOR-BLOB }
154 1 "billy" 10 3.14 f f f f f f <user-assigned-person> person1 set
155 2 "johnny" 10 3.14 f f f f f f <user-assigned-person> person2 set
157 T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
158 T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
159 T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
160 B{ 115 116 111 114 101 105 110 97 98 108 111 98 }
161 f f <user-assigned-person> person3 set
163 T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
164 T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
165 T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
166 f H{ { 1 2 } { 3 4 } { 5 "lol" } } URL" http://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search" <user-assigned-person> person4 set ;
168 TUPLE: paste n summary author channel mode contents timestamp annotations ;
169 TUPLE: annotation n paste-id summary author mode contents ;
171 : db-assigned-paste-schema ( -- )
174 { "n" "ID" +db-assigned-id+ }
175 { "summary" "SUMMARY" TEXT }
176 { "author" "AUTHOR" TEXT }
177 { "channel" "CHANNEL" TEXT }
178 { "mode" "MODE" TEXT }
179 { "contents" "CONTENTS" TEXT }
180 { "date" "DATE" TIMESTAMP }
181 { "annotations" { +has-many+ annotation } }
184 annotation "ANNOTATION"
186 { "n" "ID" +db-assigned-id+ }
187 { "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "n" } }
188 { "summary" "SUMMARY" TEXT }
189 { "author" "AUTHOR" TEXT }
190 { "mode" "MODE" TEXT }
191 { "contents" "CONTENTS" TEXT }
192 } define-persistent ;
194 ! { "localhost" "postgres" "" "factor-test" } postgresql-db [
195 ! [ paste drop-table ] [ drop ] recover
196 ! [ annotation drop-table ] [ drop ] recover
197 ! [ paste drop-table ] [ drop ] recover
198 ! [ annotation drop-table ] [ drop ] recover
199 ! [ ] [ paste create-table ] unit-test
200 ! [ ] [ annotation create-table ] unit-test
203 : test-sqlite ( quot -- )
204 [ ] swap '[ "tuples-test.db" temp-file sqlite-db _ with-db ] unit-test ;
206 : test-postgresql ( quot -- )
207 [ ] swap '[ { "localhost" "postgres" "foob" "factor-test" } postgresql-db _ with-db ] unit-test ;
209 : test-repeated-insert
210 [ ] [ person ensure-table ] unit-test
211 [ ] [ person1 get insert-tuple ] unit-test
212 [ person1 get insert-tuple ] must-fail ;
214 TUPLE: serialize-me id data ;
216 : test-serialize ( -- )
217 serialize-me "SERIALIZED"
219 { "id" "ID" +db-assigned-id+ }
220 { "data" "DATA" FACTOR-BLOB }
222 [ serialize-me drop-table ] [ drop ] recover
223 [ ] [ serialize-me create-table ] unit-test
225 [ ] [ T{ serialize-me f f H{ { 1 2 } } } insert-tuple ] unit-test
227 { T{ serialize-me f 1 H{ { 1 2 } } } }
228 ] [ T{ serialize-me f 1 } select-tuples ] unit-test ;
230 TUPLE: exam id name score ;
232 : random-exam ( -- exam )
234 6 [ CHAR: a CHAR: z [a,b] random ] replicate >string
238 : test-intervals ( -- )
241 { "id" "ID" +db-assigned-id+ }
242 { "name" "NAME" TEXT }
243 { "score" "SCORE" INTEGER }
245 [ exam drop-table ] [ drop ] recover
246 [ ] [ exam create-table ] unit-test
248 [ ] [ T{ exam f f "Kyle" 100 } insert-tuple ] unit-test
249 [ ] [ T{ exam f f "Stan" 80 } insert-tuple ] unit-test
250 [ ] [ T{ exam f f "Kenny" 60 } insert-tuple ] unit-test
251 [ ] [ T{ exam f f "Cartman" 41 } insert-tuple ] unit-test
255 T{ exam f 3 "Kenny" 60 }
256 T{ exam f 4 "Cartman" 41 }
259 T{ exam f f f T{ interval f { 0 t } { 70 t } } } select-tuples
265 T{ exam f T{ interval f { 3 f } { 4 f } } f } select-tuples
269 T{ exam f 4 "Cartman" 41 }
272 T{ exam f T{ interval f { 3 f } { 4 t } } f } select-tuples
276 T{ exam f 3 "Kenny" 60 }
279 T{ exam f T{ interval f { 3 t } { 4 f } } f } select-tuples
283 T{ exam f 3 "Kenny" 60 }
284 T{ exam f 4 "Cartman" 41 }
287 T{ exam f T{ interval f { 3 t } { 4 t } } f } select-tuples
292 T{ exam f 1 "Kyle" 100 }
293 T{ exam f 2 "Stan" 80 }
296 T{ exam f f { "Stan" "Kyle" } } select-tuples
301 T{ exam f 1 "Kyle" 100 }
302 T{ exam f 2 "Stan" 80 }
303 T{ exam f 3 "Kenny" 60 }
306 T{ exam f T{ range f 1 3 1 } } select-tuples
311 T{ exam f 2 "Stan" 80 }
312 T{ exam f 3 "Kenny" 60 }
313 T{ exam f 4 "Cartman" 41 }
316 T{ exam f T{ interval f { 2 t } { 1.0/0.0 f } } } select-tuples
321 T{ exam f 1 "Kyle" 100 }
324 T{ exam f T{ interval f { -1.0/0.0 t } { 2 f } } } select-tuples
329 T{ exam f 1 "Kyle" 100 }
330 T{ exam f 2 "Stan" 80 }
331 T{ exam f 3 "Kenny" 60 }
332 T{ exam f 4 "Cartman" 41 }
335 T{ exam f T{ interval f { -1.0/0.0 t } { 1/0. f } } } select-tuples
340 T{ exam f 1 "Kyle" 100 }
341 T{ exam f 2 "Stan" 80 }
342 T{ exam f 3 "Kenny" 60 }
343 T{ exam f 4 "Cartman" 41 }
346 T{ exam } select-tuples
349 [ 4 ] [ T{ exam } f count-tuples ] unit-test ;
351 TUPLE: bignum-test id m n o ;
352 : <bignum-test> ( m n o -- obj )
359 bignum-test "BIGNUM_TEST"
361 { "id" "ID" +db-assigned-id+ }
362 { "m" "M" BIG-INTEGER }
363 { "n" "N" UNSIGNED-BIG-INTEGER }
364 { "o" "O" SIGNED-BIG-INTEGER }
366 [ bignum-test drop-table ] ignore-errors
367 [ ] [ bignum-test ensure-table ] unit-test
368 [ ] [ 63 2^ 1- dup dup <bignum-test> insert-tuple ] unit-test ;
371 ! [ T{ bignum-test f 1
372 ! -9223372036854775808 9223372036854775808 -9223372036854775808 } ]
373 ! [ T{ bignum-test f 1 } select-tuple ] unit-test ;
375 TUPLE: secret n message ;
381 { "n" "ID" +random-id+ system-random-generator }
382 { "message" "MESSAGE" TEXT }
385 [ ] [ secret recreate-table ] unit-test
387 [ t ] [ f "kilroy was here" <secret> [ insert-tuple ] keep n>> integer? ] unit-test
389 [ ] [ f "kilroy was here2" <secret> insert-tuple ] unit-test
391 [ ] [ f "kilroy was here3" <secret> insert-tuple ] unit-test
394 T{ secret } select-tuples
395 first message>> "kilroy was here" head?
399 T{ secret } select-tuples length 3 =
402 [ db-assigned-person-schema test-tuples ] test-sqlite
403 [ user-assigned-person-schema test-tuples ] test-sqlite
404 [ user-assigned-person-schema test-repeated-insert ] test-sqlite
405 [ test-bignum ] test-sqlite
406 [ test-serialize ] test-sqlite
407 [ test-intervals ] test-sqlite
408 [ test-random-id ] test-sqlite
410 [ db-assigned-person-schema test-tuples ] test-postgresql
411 [ user-assigned-person-schema test-tuples ] test-postgresql
412 [ user-assigned-person-schema test-repeated-insert ] test-postgresql
413 [ test-bignum ] test-postgresql
414 [ test-serialize ] test-postgresql
415 [ test-intervals ] test-postgresql
416 [ test-random-id ] test-postgresql
418 TUPLE: does-not-persist ;
421 [ does-not-persist create-sql-statement ]
422 [ class \ not-persistent = ] must-fail-with
426 [ does-not-persist create-sql-statement ]
427 [ class \ not-persistent = ] must-fail-with
431 TUPLE: suparclass id a ;
434 { "id" "ID" +db-assigned-id+ }
438 TUPLE: subbclass < suparclass b ;
440 subbclass "SUBCLASS" {
444 TUPLE: fubbclass < subbclass ;
446 fubbclass "FUBCLASS" { } define-persistent
448 : test-db-inheritance ( -- )
449 [ ] [ subbclass ensure-table ] unit-test
450 [ ] [ fubbclass ensure-table ] unit-test
453 subbclass new 5 >>a "hi" >>b dup insert-tuple id>> "id" set
457 subbclass new "id" get >>id select-tuple
458 [ subbclass? ] [ b>> ] [ a>> ] tri
461 [ ] [ fubbclass new 0 >>a "hi" >>b insert-tuple ] unit-test
463 [ t ] [ fubbclass new select-tuples [ fubbclass? ] all? ] unit-test ;
465 [ test-db-inheritance ] test-sqlite
466 [ test-db-inheritance ] test-postgresql
469 TUPLE: string-encoding-test id string ;
471 string-encoding-test "STRING_ENCODING_TEST" {
472 { "id" "ID" +db-assigned-id+ }
473 { "string" "STRING" TEXT }
476 : test-string-encoding ( -- )
477 [ ] [ string-encoding-test ensure-table ] unit-test
480 string-encoding-test new
481 "\u{copyright-sign}\u{bengali-letter-cha}" >>string
482 [ insert-tuple ] [ id>> "id" set ] bi
485 [ "\u{copyright-sign}\u{bengali-letter-cha}" ] [
486 string-encoding-test new "id" get >>id select-tuple string>>
489 [ test-string-encoding ] test-sqlite
490 [ test-string-encoding ] test-postgresql
492 ! Don't comment these out. These words must infer
493 \ bind-tuple must-infer
494 \ insert-tuple must-infer
495 \ update-tuple must-infer
496 \ delete-tuples must-infer
497 \ select-tuple must-infer
498 \ define-persistent must-infer
499 \ ensure-table must-infer
500 \ create-table must-infer
501 \ drop-table must-infer