1 USING: io io.files io.files.temp io.directories io.launcher
2 kernel namespaces prettyprint tools.test db.sqlite db sequences
3 continuations db.types db.tuples unicode accessors arrays
4 sorting layouts math.parser ;
7 : db-path ( -- path ) "test-" cell number>string ".db" 3append temp-file ;
8 : test.db ( -- sqlite-db ) db-path <sqlite-db> ;
10 { } [ [ db-path delete-file ] ignore-errors ] unit-test
14 "create table person (name varchar(30), country varchar(30))" sql-command
15 "insert into person values('John', 'America')" sql-command
16 "insert into person values('Jane', 'New Zealand')" sql-command
21 { { { "John" "America" } { "Jane" "New Zealand" } } } [
23 "select * from person" sql-query
27 { { { "1" "John" "America" } { "2" "Jane" "New Zealand" } } }
28 [ test.db [ "select rowid, * from person" sql-query ] with-db ] unit-test
32 "insert into person(name, country) values('Jimmy', 'Canada')"
39 { "1" "John" "America" }
40 { "2" "Jane" "New Zealand" }
41 { "3" "Jimmy" "Canada" }
43 } [ test.db [ "select rowid, * from person" sql-query ] with-db ] unit-test
48 "insert into person(name, country) values('Jose', 'Mexico')" sql-command
49 "insert into person(name, country) values('Jose', 'Mexico')" sql-command
57 "select * from person" sql-query length
64 "insert into person(name, country) values('Jose', 'Mexico')"
66 "insert into person(name, country) values('Jose', 'Mexico')"
74 "select * from person" sql-query length
78 [ \ swap ensure-table ] must-fail
80 ! You don't need a primary key
81 TUPLE: things one two ;
84 { "one" "ONE" INTEGER +not-null+ }
85 { "two" "TWO" INTEGER +not-null+ }
88 { { { 0 0 } { 0 1 } { 1 0 } { 1 1 } } } [
91 0 0 things boa insert-tuple
92 0 1 things boa insert-tuple
93 1 1 things boa insert-tuple
94 1 0 things boa insert-tuple
95 f f things boa select-tuples
96 [ [ one>> ] [ two>> ] bi 2array ] map natural-sort
101 ! Tables can have different names than the name of the tuple
104 foo "BAR" { { "slot" "SOMETHING" INTEGER +not-null+ } } define-persistent
109 { "bye" "BUHBYE" INTEGER { +foreign-id+ foo "SOMETHING" } }
110 { "try" "RETHROW" INTEGER { +foreign-id+ foo "SOMETHING" } }
113 { T{ foo { slot 1 } } T{ hi { bye 1 } { try 1 } } } [
119 1 1 <hi> insert-tuple
120 f f <hi> select-tuple
127 ! Test SQLite triggers
130 TUPLE: user username data ;
131 TUPLE: watch show user ;
134 { "username" "USERNAME" TEXT +not-null+ +user-assigned-id+ }
135 { "data" "DATA" TEXT }
139 { "id" "ID" +db-assigned-id+ }
143 { "user" "USER" TEXT +not-null+ +user-assigned-id+
144 { +foreign-id+ user "USERNAME" } }
145 { "show" "SHOW" BIG-INTEGER +not-null+ +user-assigned-id+
146 { +foreign-id+ show "ID" } }
149 { T{ user { username "littledan" } { data "foo" } } } [
154 "littledan" "foo" user boa insert-tuple
155 "mark" "bar" user boa insert-tuple
156 show new insert-tuple
157 show new select-tuple
158 "littledan" f user boa select-tuple
159 [ id>> ] [ username>> ] bi*
160 watch boa insert-tuple
161 watch new select-tuple
162 user>> f user boa select-tuple
169 "mew" "foo" user boa insert-tuple
170 "denny" "kitty" user boa insert-tuple