]> gitweb.factorcode.org Git - factor.git/blob - basis/db/tuples/tuples-docs.factor
db.tuples[-docs]: add update-tuples
[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 an 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" }
71     { "a database type (see " { $link "db.types" } ")" }
72 } "Throws an error if the slot name (column one from each row) is not a slot in the tuple or its superclases." }
73 { $examples
74     { $code "USING: db.tuples db.types ;"
75         "TUPLE: boat id year name ;"
76         "boat \"BOAT\" {"
77         "    { \"id\" \"ID\" +db-assigned-id+ }"
78         "    { \"year\" \"YEAR\" INTEGER }"
79         "    { \"name\" \"NAME\" TEXT }"
80         "} define-persistent"
81     }
82 } ;
83
84 HELP: create-table
85 { $values
86      { "class" class } }
87 { $description "Creates an SQL table from a mapping defined by " { $link define-persistent } ". If the table already exists, the database will likely throw an error." } ;
88
89 HELP: ensure-table
90 { $values
91      { "class" class } }
92 { $description "Creates an SQL table from a mapping defined by " { $link define-persistent } ". If the table already exists, the error is silently ignored." } ;
93
94 HELP: ensure-tables
95 { $values
96      { "classes" "a sequence of classes" } }
97 { $description "Creates an SQL table from a mapping defined by " { $link define-persistent } ". If a table already exists, the error is silently ignored." } ;
98
99 HELP: recreate-table
100 { $values
101      { "class" class } }
102 { $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." }
103 { $warning { $emphasis "THIS WORD WILL DELETE YOUR DATA." } $nl
104 " Use " { $link ensure-table } " unless you want to delete the data in this table." } ;
105
106 { create-table ensure-table ensure-tables recreate-table } related-words
107
108 HELP: drop-table
109 { $values
110      { "class" class } }
111 { $description "Drops an existing table which deletes all of the data. The database will probably throw an error if the table does not exist." }
112 { $warning { $emphasis "THIS WORD WILL DELETE YOUR DATA." } } ;
113
114 HELP: insert-tuple
115 { $values
116      { "tuple" tuple } }
117 { $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." }
118 { $notes "Objects should only be inserted into a database once per object. To store the object after the initial insert, call " { $link update-tuple } "." } ;
119
120 HELP: update-tuple
121 { $values
122      { "tuple" tuple } }
123 { $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." } ;
124
125 HELP: update-tuples
126 { $values
127      { "query/tuple" tuple }
128      { "quot" { $quotation ( tuple -- tuple'/f ) } } }
129 { $description "An SQL query is constructed from the slots of the exemplar tuple that are not " { $link f } ". The " { $snippet "quot" } " is applied to each tuple from the database that matches the query, and the changed tuple is stored back to the database. If the " { $snippet "quot" } " returns " { $link f } ", the tuple is dropped, and its data remains unmodified in the database."
130 $nl
131 "The word is equivalent to the following code:"
132 { $code "query/tuple select-tuples quot map sift [ update-tuple ] each" }
133 "The difference is that " { $snippet "update-tuples" } " handles query results one by one, thus avoiding the overhead of allocating the intermediate array of tuples, which " { $link select-tuples } " would do. This is important when processing large amounts of data in limited memory." } ;
134
135 HELP: delete-tuples
136 { $values
137      { "tuple" tuple } }
138 { $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 an SQL statement that deletes tuples." }
139 { $warning "This word will delete your data." } ;
140
141 { insert-tuple update-tuple update-tuples delete-tuples } related-words
142
143 HELP: each-tuple
144 { $values
145      { "query/tuple" tuple }
146      { "quot" { $quotation ( tuple -- ) } } }
147 { $description "An SQL query is constructed from the slots of the exemplar tuple that are not " { $link f } ". The " { $snippet "quot" } " is applied to each tuple from the database that matches the query constructed from the exemplar tuple." } ;
148
149 HELP: select-tuple
150 { $values
151      { "query/tuple" tuple }
152      { "tuple/f" { $maybe tuple } } }
153 { $description "An 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." } ;
154
155 HELP: select-tuples
156 { $values
157      { "query/tuple" tuple }
158      { "tuples" "an array of tuples" } }
159 { $description "An 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." } ;
160
161 HELP: count-tuples
162 { $values
163      { "query/tuple" tuple }
164      { "n" integer } }
165 { $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 } "." } ;
166
167 { each-tuple select-tuple select-tuples count-tuples } related-words
168
169
170
171 ARTICLE: "db-tuples" "High-level tuple/database integration"
172 "Start with a tutorial:"
173 { $subsections "db-tuples-tutorial" }
174 "Database types supported:"
175 { $subsections "db.types" }
176 "Useful words:"
177 { $subsections "db-tuples-words" }
178 "For porting " { $vocab-link "db.tuples" } " to other databases:"
179 { $subsections "db-tuples-protocol" }
180 ;
181
182 ARTICLE: "db-tuples-words" "High-level tuple/database words"
183 "Making tuples work with a database:"
184 { $subsections define-persistent }
185 "Creating tables:"
186 { $subsections
187     create-table
188     ensure-table
189     ensure-tables
190     recreate-table
191 }
192 "Dropping tables:"
193 { $subsections drop-table }
194 "Inserting a tuple:"
195 { $subsections insert-tuple }
196 "Updating tuples:"
197 { $subsections
198     update-tuple
199     update-tuples
200 }
201 "Deleting tuples:"
202 { $subsections delete-tuples }
203 "Querying tuples:"
204 { $subsections
205     each-tuple
206     select-tuple
207     select-tuples
208     count-tuples
209 } ;
210
211 ARTICLE: "db-tuples-protocol" "Tuple database protocol"
212 "Creating a table:"
213 { $subsections create-sql-statement }
214 "Dropping a table:"
215 { $subsections drop-sql-statement }
216 "Inserting a tuple:"
217 { $subsections
218     <insert-db-assigned-statement>
219     <insert-user-assigned-statement>
220 }
221 "Updating a tuple:"
222 { $subsections <update-tuple-statement> }
223 "Deleting tuples:"
224 { $subsections <delete-tuples-statement> }
225 "Selecting tuples:"
226 { $subsections <select-by-slots-statement> }
227 "Counting tuples:"
228 { $subsections <count-statement> } ;
229
230 ARTICLE: "db-tuples-tutorial" "Tuple database tutorial"
231 "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
232 "We're going to store books in this tutorial."
233 { $code "TUPLE: book id title author date-published edition cover-price condition ;" }
234 "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
235 "To actually bind the tuple slots to the database types, we'll use " { $link define-persistent } "."
236 { $code
237 "USING: db.tuples db.types ;
238 book \"BOOK\"
239 {
240     { \"id\" \"ID\" +db-assigned-id+ }
241     { \"title\" \"TITLE\" VARCHAR }
242     { \"author\" \"AUTHOR\" VARCHAR }
243     { \"date-published\" \"DATE_PUBLISHED\" TIMESTAMP }
244     { \"edition\" \"EDITION\" INTEGER }
245     { \"cover-price\" \"COVER_PRICE\" DOUBLE }
246     { \"condition\" \"CONDITION\" VARCHAR }
247 } define-persistent" }
248 "That's all we'll have to do with the database for this tutorial. Now let's make a book."
249 { $code "USING: calendar namespaces ;
250 T{ book
251     { title \"Factor for Sheeple\" }
252     { author \"Mister Stacky Pants\" }
253     { date-published T{ timestamp { year 2009 } { month 3 } { day 3 } } }
254     { edition 1 }
255     { cover-price 13.37 }
256 } book set" }
257 "Now we've created a book. Let's save it to the database."
258 { $code "USING: db db.sqlite fry io.files.temp ;
259 : with-book-tutorial ( quot -- )
260      '[ \"book-tutorial.db\" temp-file <sqlite-db> _ with-db ] call ; inline
261
262 [
263     book recreate-table
264     book get insert-tuple
265 ] with-book-tutorial" }
266 "Is it really there?"
267 { $code "[
268     T{ book { title \"Factor for Sheeple\" } } select-tuples .
269 ] with-book-tutorial" }
270 "Oops, we spilled some orange juice on the book cover."
271 { $code "book get \"Small orange juice stain on cover\" >>condition" }
272 "Now let's save the modified book."
273 { $code "[
274     book get update-tuple
275 ] with-book-tutorial" }
276 "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 } "."
277 { $code "[
278     T{ book { title \"Factor for Sheeple\" } } select-tuples
279 ] with-book-tutorial" }
280 "Let's drop the table because we're done."
281 { $code "[
282     book drop-table
283 ] with-book-tutorial" }
284 "To summarize, the steps for using Factor's tuple database are:"
285 { $list
286     "Make a new tuple to represent your data"
287     { "Map the Factor types to the database types with " { $link define-persistent } }
288     { "Make a custom database combinator (see " { $link "db-custom-database-combinators" } ") to open your database and run a " { $link quotation } }
289     { "Create a table with " { $link create-table } ", " { $link ensure-table } ", or " { $link recreate-table } }
290     { "Start making and storing objects with " { $link insert-tuple } ", " { $link update-tuple } ", " { $link delete-tuples } ", and " { $link select-tuples } }
291 } ;
292
293 ABOUT: "db-tuples"