]> gitweb.factorcode.org Git - factor.git/blob - basis/db/tuples/tuples-docs.factor
docs: using $maybe.
[factor.git] / basis / db / tuples / tuples-docs.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: classes help.markup help.syntax io.streams.string kernel
4 quotations sequences strings math db.types db.tuples.private db ;
5 IN: db.tuples
6
7 HELP: random-id-generator
8 { $description "Used to tell " { $link eval-generator } " to generate a random number for use as a key." } ;
9
10 HELP: create-sql-statement
11 { $values
12      { "class" class }
13      { "object" object } }
14 { $description "Generates the SQL code for creating a table for a given class." } ;
15
16 HELP: drop-sql-statement
17 { $values
18      { "class" class }
19      { "object" object } }
20 { $description "Generates the SQL code for dropping a table for a given class." } ;
21
22 HELP: insert-tuple-set-key
23 { $values
24      { "tuple" tuple } { "statement" statement } }
25 { $description "Inserts a tuple and sets its primary key in one word. This is necessary for some databases." } ;
26
27 HELP: <count-statement>
28 { $values
29      { "query" query }
30      { "statement" statement } }
31 { $description "A database-specific hook for generating the SQL for a count statement." } ;
32
33 HELP: <delete-tuples-statement>
34 { $values
35      { "tuple" tuple } { "class" class }
36      { "object" object } }
37 { $description "A database-specific hook for generating the SQL for an delete statement." } ;
38
39 HELP: <insert-db-assigned-statement>
40 { $values
41      { "class" class }
42      { "object" object } }
43 { $description "A database-specific hook for generating the SQL for an insert statement with a database-assigned primary key." } ;
44
45 HELP: <insert-user-assigned-statement>
46 { $values
47      { "class" class }
48      { "object" object } }
49 { $description "A database-specific hook for generating the SQL for an insert statement with a user-assigned primary key." } ;
50
51 HELP: <select-by-slots-statement>
52 { $values
53      { "tuple" tuple } { "class" class }
54      { "statement" tuple } }
55 { $description "A database-specific hook for generating the SQL for a select statement." } ;
56
57 HELP: <update-tuple-statement>
58 { $values
59      { "class" class }
60      { "object" object } }
61 { $description "A database-specific hook for generating the SQL for an update statement." } ;
62
63
64 HELP: define-persistent
65 { $values
66      { "class" class } { "table" string } { "columns" "an array of slot specifiers" } }
67 { $description "Defines a relation from a Factor " { $snippet "tuple class" } " to a SQL database table name. The format for the slot specifiers is as follows:"
68 { $list
69     { "a slot name from the " { $snippet "tuple class" } }
70     { "the name of a database column that maps to the slot" }        { "a database type (see " { $link "db.types" } ")" }
71 } "Throws an error if the slot name (column one from each row) is not a slot in the tuple or its superclases." }
72 { $examples
73     { $code "USING: db.tuples db.types ;"
74         "TUPLE: boat id year name ;"
75         "boat \"BOAT\" {"
76         "    { \"id\" \"ID\" +db-assigned-id+ }"
77         "    { \"year\" \"YEAR\" INTEGER }"
78         "    { \"name\" \"NAME\" TEXT }"
79         "} define-persistent"
80     }
81 } ;
82
83 HELP: create-table
84 { $values
85      { "class" class } }
86 { $description "Creates a SQL table from a mapping defined by " { $link define-persistent } ". If the table already exists, the database will likely throw an error." } ;
87
88 HELP: ensure-table
89 { $values
90      { "class" class } }
91 { $description "Creates a SQL table from a mapping defined by " { $link define-persistent } ". If the table already exists, the error is silently ignored." } ;
92
93 HELP: ensure-tables
94 { $values
95      { "classes" "a sequence of classes" } }
96 { $description "Creates a SQL table from a mapping defined by " { $link define-persistent } ". If a table already exists, the error is silently ignored." } ;
97
98 HELP: recreate-table
99 { $values
100      { "class" class } }
101 { $description "Drops an existing table and re-creates it from a mapping defined by " { $link define-persistent } ". If the table does not exist the error is silently ignored." }
102 { $warning { $emphasis "THIS WORD WILL DELETE YOUR DATA." } $nl
103 " Use " { $link ensure-table } " unless you want to delete the data in this table." } ;
104
105 { create-table ensure-table ensure-tables recreate-table } related-words
106
107 HELP: drop-table
108 { $values
109      { "class" class } }
110 { $description "Drops an existing table which deletes all of the data. The database will probably throw an error if the table does not exist." }
111 { $warning { $emphasis "THIS WORD WILL DELETE YOUR DATA." } } ;
112
113 HELP: insert-tuple
114 { $values
115      { "tuple" tuple } }
116 { $description "Inserts a tuple into a database if a relation has been defined with " { $link define-persistent } ". If a mapping states that the database assigns a primary key to the tuple, this value will be set after this word runs." }
117 { $notes "Objects should only be inserted into a database once per object. To store the object after the initial insert, call " { $link update-tuple } "." } ;
118
119 HELP: update-tuple
120 { $values
121      { "tuple" tuple } }
122 { $description "Updates a tuple that has already been inserted into a database. The tuple must have a primary key that has been set by " { $link insert-tuple } " or that is user-defined." } ;
123
124 HELP: delete-tuples
125 { $values
126      { "tuple" tuple } }
127 { $description "Uses the " { $snippet "tuple" } " as an exemplar object and deletes any objects that have the same slots set. If a slot is not " { $link f } ", then it is used to generate a SQL statement that deletes tuples." }
128 { $warning "This word will delete your data." } ;
129
130 { insert-tuple update-tuple delete-tuples } related-words
131
132 HELP: select-tuple
133 { $values
134      { "query/tuple" tuple }
135      { "tuple/f" { $maybe tuple } } }
136 { $description "A SQL query is constructed from the slots of the exemplar tuple that are not " { $link f } ". Returns a single tuple from the database if it matches the query constructed from the exemplar tuple." } ;
137
138 HELP: select-tuples
139 { $values
140      { "query/tuple" tuple }
141      { "tuples" "an array of tuples" } }
142 { $description "A SQL query is constructed from the slots of the exemplar tuple that are not " { $link f } ". Returns an array of multiple tuples from the database that match the query constructed from the exemplar tuple." } ;
143
144 HELP: count-tuples
145 { $values
146      { "query/tuple" tuple }
147      { "n" integer } }
148 { $description "Returns the number of items that would be returned if the query were a select query. Counting the tuples with this word is more efficient than calling " { $link length } " on the result of " { $link select-tuples } "." } ;
149
150 { select-tuple select-tuples count-tuples } related-words
151
152
153
154 ARTICLE: "db-tuples" "High-level tuple/database integration"
155 "Start with a tutorial:"
156 { $subsections "db-tuples-tutorial" }
157 "Database types supported:"
158 { $subsections "db.types" }
159 "Useful words:"
160 { $subsections "db-tuples-words" }
161 "For porting " { $vocab-link "db.tuples" } " to other databases:"
162 { $subsections "db-tuples-protocol" }
163 ;
164
165 ARTICLE: "db-tuples-words" "High-level tuple/database words"
166 "Making tuples work with a database:"
167 { $subsections define-persistent }
168 "Creating tables:"
169 { $subsections
170     create-table
171     ensure-table
172     ensure-tables
173     recreate-table
174 }
175 "Dropping tables:"
176 { $subsections drop-table }
177 "Inserting a tuple:"
178 { $subsections insert-tuple }
179 "Updating a tuple:"
180 { $subsections update-tuple }
181 "Deleting tuples:"
182 { $subsections delete-tuples }
183 "Querying tuples:"
184 { $subsections
185     select-tuple
186     select-tuples
187     count-tuples
188 } ;
189
190 ARTICLE: "db-tuples-protocol" "Tuple database protocol"
191 "Creating a table:"
192 { $subsections create-sql-statement }
193 "Dropping a table:"
194 { $subsections drop-sql-statement }
195 "Inserting a tuple:"
196 { $subsections
197     <insert-db-assigned-statement>
198     <insert-user-assigned-statement>
199 }
200 "Updating a tuple:"
201 { $subsections <update-tuple-statement> }
202 "Deleting tuples:"
203 { $subsections <delete-tuples-statement> }
204 "Selecting tuples:"
205 { $subsections <select-by-slots-statement> }
206 "Counting tuples:"
207 { $subsections <count-statement> } ;
208
209 ARTICLE: "db-tuples-tutorial" "Tuple database tutorial"
210 "Let's make a tuple and store it in a database. To follow along, click on each code example and run it in the listener. If you forget to run an example, just start at the top and run them all again in order." $nl
211 "We're going to store books in this tutorial."
212 { $code "TUPLE: book id title author date-published edition cover-price condition ;" }
213 "The title, author, and publisher should be strings; the date-published a timestamp; the edition an integer; the cover-price a float. These are the Factor types for which we will need to look up the corresponding " { $link "db.types" } ". " $nl
214 "To actually bind the tuple slots to the database types, we'll use " { $link define-persistent } "."
215 { $code
216 "USING: db.tuples db.types ;
217 book \"BOOK\"
218 {
219     { \"id\" \"ID\" +db-assigned-id+ }
220     { \"title\" \"TITLE\" VARCHAR }
221     { \"author\" \"AUTHOR\" VARCHAR }
222     { \"date-published\" \"DATE_PUBLISHED\" TIMESTAMP }
223     { \"edition\" \"EDITION\" INTEGER }
224     { \"cover-price\" \"COVER_PRICE\" DOUBLE }
225     { \"condition\" \"CONDITION\" VARCHAR }
226 } define-persistent" }
227 "That's all we'll have to do with the database for this tutorial. Now let's make a book."
228 { $code "USING: calendar namespaces ;
229 T{ book
230     { title \"Factor for Sheeple\" }
231     { author \"Mister Stacky Pants\" }
232     { date-published T{ timestamp { year 2009 } { month 3 } { day 3 } } }
233     { edition 1 }
234     { cover-price 13.37 }
235 } book set" }
236 "Now we've created a book. Let's save it to the database."
237 { $code "USING: db db.sqlite fry io.files.temp ;
238 : with-book-tutorial ( quot -- )
239      '[ \"book-tutorial.db\" temp-file <sqlite-db> _ with-db ] call ; inline
240
241 [
242     book recreate-table
243     book get insert-tuple
244 ] with-book-tutorial" }
245 "Is it really there?"
246 { $code "[
247     T{ book { title \"Factor for Sheeple\" } } select-tuples .
248 ] with-book-tutorial" }
249 "Oops, we spilled some orange juice on the book cover."
250 { $code "book get \"Small orange juice stain on cover\" >>condition" }
251 "Now let's save the modified book."
252 { $code "[
253     book get update-tuple
254 ] with-book-tutorial" }
255 "And select it again. You can query the database by any field -- just set it in the exemplar tuple you pass to " { $link select-tuples } "."
256 { $code "[
257     T{ book { title \"Factor for Sheeple\" } } select-tuples
258 ] with-book-tutorial" }
259 "Let's drop the table because we're done."
260 { $code "[
261     book drop-table
262 ] with-book-tutorial" }
263 "To summarize, the steps for using Factor's tuple database are:"
264 { $list
265     "Make a new tuple to represent your data"
266     { "Map the Factor types to the database types with " { $link define-persistent } }
267     { "Make a custom database combinator (see " { $link "db-custom-database-combinators" } ") to open your database and run a " { $link quotation } }
268     { "Create a table with " { $link create-table } ", " { $link ensure-table } ", or " { $link recreate-table } }
269     { "Start making and storing objects with " { $link insert-tuple } ", " { $link update-tuple } ", " { $link delete-tuples } ", and " { $link select-tuples } }
270 } ;
271
272 ABOUT: "db-tuples"