]> gitweb.factorcode.org Git - factor.git/blob - basis/db/types/types.factor
Fix permission bits
[factor.git] / basis / db / types / types.factor
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 symbols
7 classes.singleton accessors quotations random ;
8 IN: db.types
9
10 HOOK: persistent-table db ( -- hash )
11 HOOK: compound db ( string obj -- hash )
12
13 TUPLE: sql-spec class slot-name column-name type primary-key modifiers ;
14
15 TUPLE: literal-bind key type value ;
16 C: <literal-bind> literal-bind
17
18 TUPLE: generator-bind slot-name key generator-singleton type ;
19 C: <generator-bind> generator-bind
20 SINGLETON: random-id-generator
21
22 TUPLE: low-level-binding value ;
23 C: <low-level-binding> low-level-binding
24
25 SINGLETONS: +db-assigned-id+ +user-assigned-id+ +random-id+ ;
26 UNION: +primary-key+ +db-assigned-id+ +user-assigned-id+ +random-id+ ;
27
28 SYMBOLS: +autoincrement+ +serial+ +unique+ +default+ +null+ +not-null+
29 +foreign-id+ +has-many+ +on-delete+ +restrict+ +cascade+ +set-null+
30 +set-default+ ;
31
32 SYMBOL: IGNORE
33
34 : filter-ignores ( tuple specs -- specs' )
35     [ <mirror> [ nip IGNORE = ] assoc-filter keys ] dip
36     [ slot-name>> swap member? not ] with filter ;
37
38 ERROR: no-slot ;
39
40 : offset-of-slot ( string tuple -- n )
41     class superclasses [ "slots" word-prop ] map concat
42     slot-named dup [ no-slot ] unless offset>> ;
43
44 : get-slot-named ( name tuple -- value )
45     tuck offset-of-slot slot ;
46
47 : set-slot-named ( value name obj -- )
48     tuck offset-of-slot set-slot ;
49
50 ERROR: not-persistent class ;
51
52 : db-table ( class -- object )
53     dup "db-table" word-prop [ ] [ not-persistent ] ?if ;
54
55 : db-columns ( class -- object )
56     superclasses [ "db-columns" word-prop ] map concat ;
57
58 : db-relations ( class -- object )
59     "db-relations" word-prop ;
60
61 : find-primary-key ( specs -- seq )
62     [ primary-key>> ] filter ;
63
64 : set-primary-key ( value tuple -- )
65     [
66         class db-columns
67         find-primary-key first slot-name>>
68     ] keep set-slot-named ;
69
70 : primary-key? ( spec -- ? )
71     primary-key>> +primary-key+? ;
72
73 : db-assigned-id-spec? ( specs -- ? )
74     [ primary-key>> +db-assigned-id+? ] contains? ;
75
76 : user-assigned-id-spec? ( specs -- ? )
77     [ primary-key>> +user-assigned-id+? ] contains? ;
78
79 : normalize-spec ( spec -- )
80     dup type>> dup +primary-key+? [
81         >>primary-key drop
82     ] [
83         drop dup modifiers>> [
84             +primary-key+?
85         ] deep-find
86         [ >>primary-key drop ] [ drop ] if*
87     ] if ;
88
89 : db-assigned? ( class -- ? )
90     db-columns find-primary-key db-assigned-id-spec? ;
91
92 : relation? ( spec -- ? ) [ +has-many+ = ] deep-find ;
93
94 SYMBOLS: INTEGER BIG-INTEGER SIGNED-BIG-INTEGER UNSIGNED-BIG-INTEGER
95 DOUBLE REAL BOOLEAN TEXT VARCHAR DATE TIME DATETIME TIMESTAMP BLOB
96 FACTOR-BLOB NULL URL ;
97
98 : <sql-spec> ( class slot-name column-name type modifiers -- sql-spec )
99     sql-spec new
100         swap >>modifiers
101         swap >>type
102         swap >>column-name
103         swap >>slot-name
104         swap >>class
105         dup normalize-spec ;
106
107 : spec>tuple ( class spec -- tuple )
108     3 f pad-right [ first3 ] keep 3 tail <sql-spec> ;
109
110 : number>string* ( n/string -- string )
111     dup number? [ number>string ] when ;
112
113 : remove-db-assigned-id ( specs -- obj )
114     [ +db-assigned-id+? not ] filter ;
115
116 : remove-relations ( specs -- newcolumns )
117     [ relation? not ] filter ;
118
119 : remove-id ( specs -- obj )
120     [ primary-key>> not ] filter ;
121
122 ! SQLite Types: http://www.sqlite.org/datatype3.html
123 ! NULL INTEGER REAL TEXT BLOB
124 ! PostgreSQL Types:
125 ! http://developer.postgresql.org/pgdocs/postgres/datatype.html
126
127 : ?at ( obj assoc -- value/obj ? )
128     dupd at* [ [ nip ] [ drop ] if ] keep ;
129
130 ERROR: unknown-modifier modifier ;
131
132 : lookup-modifier ( obj -- string )
133     {
134         { [ dup array? ] [ unclip lookup-modifier swap compound ] }
135         [ persistent-table ?at [ unknown-modifier ] unless third ]
136     } cond ;
137
138 ERROR: no-sql-type type ;
139
140 : (lookup-type) ( obj -- string )
141     persistent-table ?at [ no-sql-type ] unless ;
142
143 : lookup-type ( obj -- string )
144     dup array? [
145         unclip (lookup-type) first nip
146     ] [
147         (lookup-type) first
148     ] if ;
149
150 : lookup-create-type ( obj -- string )
151     dup array? [
152         unclip (lookup-type) second swap compound
153     ] [
154         (lookup-type) second
155     ] if ;
156
157 : modifiers ( spec -- string )
158     modifiers>> [ lookup-modifier ] map " " join
159     [ "" ] [ " " prepend ] if-empty ;
160
161 : join-space ( string1 string2 -- new-string )
162     " " swap 3append ;
163
164 : paren ( string -- new-string )
165     "(" swap ")" 3append ;
166
167 HOOK: bind% db ( spec -- )
168 HOOK: bind# db ( spec obj -- )
169
170 ERROR: no-column column ;
171
172 : >reference-string ( string pair -- string )
173     first2
174     [ [ unparse join-space ] [ db-columns ] bi ] dip
175     swap [ column-name>> = ] with find nip
176     [ no-column ] unless*
177     column-name>> paren append ;