1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs db kernel math math.parser
4 sequences continuations sequences.deep prettyprint
5 words namespaces slots slots.private classes mirrors
6 classes.tuple combinators calendar.format classes.singleton
7 accessors quotations random db.private ;
10 HOOK: persistent-table db-connection ( -- hash )
11 HOOK: compound db-connection ( string obj -- hash )
13 TUPLE: sql-spec class slot-name column-name type primary-key modifiers ;
15 TUPLE: literal-bind key type value ;
16 C: <literal-bind> literal-bind
18 TUPLE: generator-bind slot-name key generator-singleton type ;
19 C: <generator-bind> generator-bind
20 SINGLETON: random-id-generator
22 TUPLE: low-level-binding value ;
23 C: <low-level-binding> low-level-binding
25 SINGLETONS: +db-assigned-id+ +user-assigned-id+ +random-id+ ;
26 UNION: +primary-key+ +db-assigned-id+ +user-assigned-id+ +random-id+ ;
28 SYMBOLS: +autoincrement+ +serial+ +unique+ +default+ +null+ +not-null+
29 +foreign-id+ +has-many+ +on-update+ +on-delete+ +restrict+ +cascade+
30 +set-null+ +set-default+ ;
34 : filter-ignores ( tuple specs -- specs' )
35 [ <mirror> [ nip IGNORE = ] assoc-filter keys ] dip
36 [ slot-name>> swap member? ] with reject ;
38 ERROR: not-persistent class ;
40 : db-table-name ( class -- object )
41 dup "db-table" word-prop [ ] [ not-persistent ] ?if ;
43 : db-columns ( class -- object )
44 superclasses [ "db-columns" word-prop ] map concat ;
46 : db-relations ( class -- object )
47 "db-relations" word-prop ;
49 : find-primary-key ( specs -- seq )
50 [ primary-key>> ] filter ;
52 : set-primary-key ( value tuple -- )
55 find-primary-key first slot-name>>
56 ] keep set-slot-named ;
58 : primary-key? ( spec -- ? )
59 primary-key>> +primary-key+? ;
61 : db-assigned-id-spec? ( specs -- ? )
62 [ primary-key>> +db-assigned-id+? ] any? ;
64 : user-assigned-id-spec? ( specs -- ? )
65 [ primary-key>> +user-assigned-id+? ] any? ;
67 : normalize-spec ( spec -- )
68 dup type>> dup +primary-key+? [
71 drop dup modifiers>> [
74 [ >>primary-key drop ] [ drop ] if*
77 : db-assigned? ( class -- ? )
78 db-columns find-primary-key db-assigned-id-spec? ;
80 : relation? ( spec -- ? ) [ +has-many+ = ] deep-find ;
82 SINGLETONS: INTEGER BIG-INTEGER SIGNED-BIG-INTEGER UNSIGNED-BIG-INTEGER
83 DOUBLE REAL BOOLEAN TEXT VARCHAR DATE TIME DATETIME TIMESTAMP BLOB
84 FACTOR-BLOB NULL URL ;
86 : <sql-spec> ( class slot-name column-name type modifiers -- sql-spec )
95 : spec>tuple ( class spec -- tuple )
96 3 f pad-tail [ first3 ] keep 3 tail <sql-spec> ;
98 : number>string* ( n/string -- string )
99 dup number? [ number>string ] when ;
101 : remove-db-assigned-id ( specs -- obj )
102 [ +db-assigned-id+? ] reject ;
104 : remove-relations ( specs -- newcolumns )
105 [ relation? ] reject ;
107 : remove-id ( specs -- obj )
108 [ primary-key>> ] reject ;
110 ! SQLite Types: http://www.sqlite.org/datatype3.html
111 ! NULL INTEGER REAL TEXT BLOB
113 ! http://developer.postgresql.org/pgdocs/postgres/datatype.html
115 ERROR: unknown-modifier modifier ;
117 : lookup-modifier ( obj -- string )
119 { [ dup array? ] [ unclip lookup-modifier swap compound ] }
120 [ persistent-table ?at [ unknown-modifier ] unless third ]
123 ERROR: no-sql-type type ;
125 : (lookup-type) ( obj -- string )
126 persistent-table ?at [ no-sql-type ] unless ;
128 : lookup-type ( obj -- string )
130 unclip (lookup-type) first nip
135 : lookup-create-type ( obj -- string )
137 unclip (lookup-type) second swap compound
142 : modifiers ( spec -- string )
143 modifiers>> [ lookup-modifier ] map " " join
144 [ "" ] [ " " prepend ] if-empty ;
146 HOOK: bind% db-connection ( spec -- )
147 HOOK: bind# db-connection ( spec obj -- )
149 ERROR: no-column column ;
151 : >reference-string ( string pair -- string )
153 [ [ db-table-name " " glue ] [ db-columns ] bi ] dip
154 swap [ column-name>> = ] with find nip
155 [ no-column ] unless*
156 column-name>> "(" ")" surround append ;