a little refactoring, docs

db4
Doug Coleman 2008-09-24 19:30:46 -05:00
parent 72685aa77e
commit 8e86c31c31
3 changed files with 41 additions and 35 deletions

View File

@ -118,13 +118,16 @@ HELP: execute-statement
{ $values { "statement" statement } } { $values { "statement" statement } }
{ $description } ; { $description } ;
ARTICLE: "db" "Low-level database library" ARTICLE: "db" "Database library"
{ $subsection "db-custom-database-combinators" } { $subsection "db-custom-database-combinators" }
{ $subsection "db-protocol" } { $subsection "db-protocol" }
{ $subsection "db-lowlevel-tutorial" } { $subsection "db-lowlevel-tutorial" }
"Higher-level database:" "Higher-level database:"
{ $vocab-subsection "Database types" "db.types" } { $vocab-subsection "Database types" "db.types" }
{ $vocab-subsection "High-level tuple/database integration" "db.tuples" } { $vocab-subsection "High-level tuple/database integration" "db.tuples" }
! { $subsection "db-tuples" }
! { $subsection "db-tuples-protocol" }
! { $subsection "db-tuples-tutorial" }
"Supported database backends:" "Supported database backends:"
{ $vocab-subsection "SQLite" "db.sqlite" } { $vocab-subsection "SQLite" "db.sqlite" }
{ $vocab-subsection "PostgreSQL" "db.postgresql" } { $vocab-subsection "PostgreSQL" "db.postgresql" }
@ -144,7 +147,6 @@ ARTICLE: "db-porting-the-library" "Porting the database library"
"This section is not yet written." "This section is not yet written."
; ;
ARTICLE: "db-custom-database-combinators" "Custom database combinators" ARTICLE: "db-custom-database-combinators" "Custom database combinators"
"Every database library requires some effort on the programmer's part to initialize and open a database. SQLite uses files on your harddisk, so a simple pathname is all the setup required. With PostgreSQL, you log in to a networked server as a user on a specfic port." $nl "Every database library requires some effort on the programmer's part to initialize and open a database. SQLite uses files on your harddisk, so a simple pathname is all the setup required. With PostgreSQL, you log in to a networked server as a user on a specfic port." $nl

View File

@ -11,7 +11,18 @@ HELP: define-persistent
{ $list { $list
{ "a slot name from the " { $snippet "tuple class" } } { "a slot name from the " { $snippet "tuple class" } }
{ "the name of a database column that maps to the slot" } { "a database type (see " { $link "db.types" } ")" } { "the name of a database column that maps to the slot" } { "a database type (see " { $link "db.types" } ")" }
} } ; } "Throws an error if the slot name (column one from each row) is not a slot in the tuple or its superclases." }
{ $examples
{ $unchecked-example "USING: db.tuples db.types ;"
"TUPLE: boat id year name ;"
"boat \"BOAT\" {"
" { \"id\" \"ID\" +db-assigned-id+ }"
" { \"year\" \"YEAR\" INTEGER }"
" { \"name\" \"NAME\" TEXT }"
"} define-persistent"
""
}
} ;
HELP: create-table HELP: create-table
{ $values { $values
@ -64,30 +75,26 @@ HELP: delete-tuples
HELP: select-tuple HELP: select-tuple
{ $values { $values
{ "tuple" tuple } { "query/tuple" tuple }
{ "tuple/f" "a tuple or f" } } { "tuple/f" "a tuple or f" } }
{ $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." } ; { $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." } ;
HELP: select-tuples HELP: select-tuples
{ $values { $values
{ "tuple" tuple } { "query/tuple" tuple }
{ "tuples" "an array of tuples" } } { "tuples" "an array of tuples" } }
{ $description "A SQL query is constructed from the slots of the exemplar tuple that are not " { $link f } ". Returns a multiple tuples from the database that match the query constructed from the exemplar tuple." } ; { $description "A SQL query is constructed from the slots of the exemplar tuple that are not " { $link f } ". Returns a multiple tuples from the database that match the query constructed from the exemplar tuple." } ;
HELP: count-tuples HELP: count-tuples
{ $values { $values
{ "tuple" tuple } { "groups" "an array of slots to group by" } { "query/tuple" tuple }
{ "n" integer } } { "n" integer } }
{ $description "" } ; { $description "" } ;
HELP: query
{ $values
{ "tuple" tuple } { "query" query }
{ "tuples" "a sequence of tuples" } }
{ $description "Allows for queries with group by, order by, limit, and offset clauses. " } ;
{ select-tuple select-tuples count-tuples query } related-words { select-tuple select-tuples count-tuples query } related-words
ARTICLE: "db-tuples" "High-level tuple/database integration" ARTICLE: "db-tuples" "High-level tuple/database integration"
"Start with a tutorial:" "Start with a tutorial:"
{ $subsection "db-tuples-tutorial" } { $subsection "db-tuples-tutorial" }
@ -115,9 +122,7 @@ ARTICLE: "db-tuples-words" "High-level tuple/database words"
"Querying tuples:" "Querying tuples:"
{ $subsection select-tuple } { $subsection select-tuple }
{ $subsection select-tuples } { $subsection select-tuples }
{ $subsection count-tuples } { $subsection count-tuples } ;
"Advanced querying of tuples:"
{ $subsection query } ;
ARTICLE: "db-tuples-protocol" "High-level tuple/database protocol" ARTICLE: "db-tuples-protocol" "High-level tuple/database protocol"

View File

@ -6,6 +6,7 @@ math.parser io prettyprint db.types continuations
destructors mirrors sets ; destructors mirrors sets ;
IN: db.tuples IN: db.tuples
<PRIVATE
! returns a sequence of prepared-statements ! returns a sequence of prepared-statements
HOOK: create-sql-statement db ( class -- object ) HOOK: create-sql-statement db ( class -- object )
HOOK: drop-sql-statement db ( class -- object ) HOOK: drop-sql-statement db ( class -- object )
@ -20,22 +21,6 @@ HOOK: query>statement db ( query -- statement )
HOOK: insert-tuple* db ( tuple statement -- ) HOOK: insert-tuple* db ( tuple statement -- )
ERROR: no-slots-named class seq ;
: check-columns ( class columns -- )
tuck
[ [ first ] map ]
[ all-slots [ name>> ] map ] bi* diff
[ drop ] [ no-slots-named ] if-empty ;
: define-persistent ( class table columns -- )
pick dupd
check-columns
[ dupd "db-table" set-word-prop dup ] dip
[ relation? ] partition swapd
dupd [ spec>tuple ] with map
"db-columns" set-word-prop
"db-relations" set-word-prop ;
ERROR: not-persistent class ; ERROR: not-persistent class ;
: db-table ( class -- object ) : db-table ( class -- object )
@ -60,9 +45,7 @@ GENERIC: eval-generator ( singleton -- object )
: resulting-tuple ( exemplar-tuple row out-params -- tuple ) : resulting-tuple ( exemplar-tuple row out-params -- tuple )
rot class new [ rot class new [
[ [ [ slot-name>> ] dip set-slot-named ] curry 2each
[ slot-name>> ] dip set-slot-named
] curry 2each
] keep ; ] keep ;
: query-tuples ( exemplar-tuple statement -- seq ) : query-tuples ( exemplar-tuple statement -- seq )
@ -98,8 +81,25 @@ GENERIC: eval-generator ( singleton -- object )
: do-count ( exemplar-tuple statement -- tuples ) : do-count ( exemplar-tuple statement -- tuples )
[ [ bind-tuple ] [ nip default-query ] 2bi ] with-disposal ; [ [ bind-tuple ] [ nip default-query ] 2bi ] with-disposal ;
PRIVATE>
! High level ! High level
ERROR: no-slots-named class seq ;
: check-columns ( class columns -- )
tuck
[ [ first ] map ]
[ all-slots [ name>> ] map ] bi* diff
[ drop ] [ no-slots-named ] if-empty ;
: define-persistent ( class table columns -- )
pick dupd
check-columns
[ dupd "db-table" set-word-prop dup ] dip
[ relation? ] partition swapd
dupd [ spec>tuple ] with map
"db-columns" set-word-prop
"db-relations" set-word-prop ;
TUPLE: query tuple group order offset limit ; TUPLE: query tuple group order offset limit ;
@ -111,7 +111,6 @@ M: query >query clone ;
M: tuple >query <query> swap >>tuple ; M: tuple >query <query> swap >>tuple ;
: create-table ( class -- ) : create-table ( class -- )
create-sql-statement [ execute-statement ] with-disposals ; create-sql-statement [ execute-statement ] with-disposals ;