refactoring the db protocol, remove fql-statements

db4
Doug Coleman 2009-04-14 16:14:40 -05:00
parent 725b60e383
commit 6f1d15e84c
6 changed files with 51 additions and 63 deletions

View File

@ -8,49 +8,50 @@ IN: db2
GENERIC: sql-command ( object -- )
GENERIC: sql-query ( object -- sequence )
GENERIC: sql-bind-command* ( sequence object -- )
GENERIC: sql-bind-query* ( sequence object -- sequence )
GENERIC: sql-bind-typed-command* ( sequence object -- )
GENERIC: sql-bind-typed-query* ( sequence object -- sequence )
GENERIC: sql-bind-command ( object -- )
GENERIC: sql-bind-query ( object -- sequence )
GENERIC: sql-bind-typed-command ( object -- )
GENERIC: sql-bind-typed-query ( object -- sequence )
M: string sql-command ( sql -- )
f f <statement> [ execute-statement ] with-disposal ;
M: string sql-command ( string -- )
f f <statement> sql-command ;
M: string sql-query ( sql -- sequence )
f f <statement> [ statement>result-sequence ] with-disposal ;
M: string sql-query ( string -- sequence )
f f <statement> sql-query ;
M: string sql-bind-command* ( sequence string -- )
f f <statement> [
M: statement sql-command ( statement -- )
[ execute-statement ] with-disposal ;
M: statement sql-query ( statement -- sequence )
[ statement>result-sequence ] with-disposal ;
M: statement sql-bind-command ( statement -- )
[
prepare-statement
[ bind-sequence ] [ statement>result-set drop ] bi
] with-disposal ;
M: string sql-bind-query* ( in-sequence string -- out-sequence )
f f <statement> [
M: statement sql-bind-query ( statement -- sequence )
[
prepare-statement
[ bind-sequence ] [ statement>result-sequence ] bi
] with-disposal ;
M: string sql-bind-typed-command* ( in-sequence string -- )
f f <statement> [
M: statement sql-bind-typed-command ( statement -- )
[
prepare-statement
[ bind-typed-sequence ] [ statement>result-set drop ] bi
] with-disposal ;
M: string sql-bind-typed-query* ( in-sequence string -- out-sequence )
f f <statement> [
M: statement sql-bind-typed-query ( statement -- sequence )
[
prepare-statement
[ bind-typed-sequence ] [ statement>result-sequence ] bi
] with-disposal ;
M: sequence sql-command [ sql-command ] each ;
M: sequence sql-query [ sql-query ] map ;
M: sequence sql-bind-command* [ sql-bind-command* ] with each ;
M: sequence sql-bind-query* [ sql-bind-query* ] with map ;
M: sequence sql-bind-typed-command* [ sql-bind-typed-command* ] with each ;
M: sequence sql-bind-typed-query* [ sql-bind-typed-query* ] with map ;
M: sequence sql-bind-command [ sql-bind-command ] each ;
M: sequence sql-bind-query [ sql-bind-query ] map ;
M: sequence sql-bind-typed-command [ sql-bind-typed-command ] each ;
M: sequence sql-bind-typed-query [ sql-bind-typed-query ] map ;

View File

@ -5,10 +5,8 @@ db2.private db2.sqlite.lib db2.statements db2.utils destructors
kernel make math.parser sequences strings assocs db2.utils ;
IN: db2.fql
TUPLE: fql-statement sql in out ;
GENERIC: expand-fql* ( object -- sequence/fql-statement )
GENERIC: normalize-fql ( object -- sequence/fql-statement )
GENERIC: expand-fql* ( object -- sequence/statement )
GENERIC: normalize-fql ( object -- sequence/statement )
! M: object normalize-fql ;
@ -66,7 +64,7 @@ M: and expand-fql* ( obj -- string )
M: string expand-fql* ( string -- string ) ;
M: insert expand-fql*
[ fql-statement new ] dip
[ statement new ] dip
[
{
[ "insert into " % into>> % ]
@ -77,7 +75,7 @@ M: insert expand-fql*
] "" make >>sql ;
M: update expand-fql*
[ fql-statement new ] dip
[ statement new ] dip
[
{
[ "update " % tables>> ", " join % ]
@ -93,7 +91,7 @@ M: update expand-fql*
] "" make >>sql ;
M: delete expand-fql*
[ fql-statement new ] dip
[ statement new ] dip
[
{
[ "delete from " % tables>> ", " join % ]
@ -104,7 +102,7 @@ M: delete expand-fql*
] "" make >>sql ;
M: select expand-fql*
[ fql-statement new ] dip
[ statement new ] dip
[
{
[ "select " % names>> ", " join % ]
@ -116,21 +114,3 @@ M: select expand-fql*
[ limit>> [ " limit " % # ] when* ]
} cleave
] "" make >>sql ;
M: fql-statement sql-command ( sql -- )
sql>> sql-command ;
M: fql-statement sql-query ( sql -- sequence )
sql>> sql-query ;
M: fql-statement sql-bind-command ( fql-statement -- )
[ in>> ] [ sql>> ] bi sql-bind-command* ;
M: fql-statement sql-bind-query ( fql-statement -- out-sequence )
[ in>> ] [ sql>> ] bi sql-bind-query* ;
M: fql-statement sql-bind-typed-command ( string -- )
[ in>> ] [ sql>> ] bi sql-bind-typed-command* ;
M: fql-statement sql-bind-typed-query ( string -- out-sequence )
[ in>> ] [ sql>> ] bi sql-bind-typed-query* ;

View File

@ -29,4 +29,4 @@ GENERIC# column-typed 1 ( result-set column -- sql )
dup #columns [ column ] with map ;
: sql-row-typed ( result-set -- seq )
dup #columns [ column-typed ] with map ;
dup #columns [ B column-typed ] with map ;

View File

@ -85,13 +85,13 @@ IN: db2.sqlite.types
[ no-sql-type ]
} case ;
M: sqlite-statement bind-sequence ( sequence statement -- )
handle>> '[
M: sqlite-statement bind-sequence ( statement -- )
[ in>> ] [ handle>> ] bi '[
[ _ ] 2dip 1+ swap sqlite-bind-text
] each-index ;
M: sqlite-statement bind-typed-sequence ( sequence statement -- )
handle>> '[
M: sqlite-statement bind-typed-sequence ( statement -- )
[ in>> ] [ handle>> ] bi '[
[ _ ] 2dip 1+ swap first2 swap bind-next-sqlite-type
] each-index ;

View File

@ -29,7 +29,8 @@ IN: db2.statements.tests
[ { { "rocky" "mac" } } ]
[
"select name, os from computer;" sql-query
"select name, os from computer;"
f f <statement> sql-query
] unit-test
[ "insert into" sql-command ]
@ -39,28 +40,30 @@ IN: db2.statements.tests
[ sql-syntax-error? ] must-fail-with
[ ] [
{ "clubber" "windows" }
"insert into computer (name, os) values(?, ?);"
sql-bind-command*
{ "clubber" "windows" }
f <statement>
sql-bind-command
] unit-test
[ { { "windows" } } ] [
{ "clubber" }
"select os from computer where name = ?;" sql-bind-query*
"select os from computer where name = ?;"
{ "clubber" } f <statement> sql-bind-query
] unit-test
[ { { "windows" } } ] [
"select os from computer where name = ?;"
{ { VARCHAR "clubber" } }
"select os from computer where name = ?;" sql-bind-typed-query*
f <statement> sql-bind-typed-query
] unit-test
[ ] [
"insert into computer (name, os) values(?, ?);"
{
{ VARCHAR "clubber" }
{ VARCHAR "windows" }
}
"insert into computer (name, os) values(?, ?);"
sql-bind-typed-command*
} f <statement>
sql-bind-typed-command
] unit-test

View File

@ -16,8 +16,8 @@ HOOK: <statement> db-connection ( sql in out -- statement )
GENERIC: statement>result-set* ( statement -- result-set )
GENERIC: execute-statement* ( statement type -- )
GENERIC: prepare-statement* ( statement -- statement' )
GENERIC: bind-sequence ( sequence statement -- )
GENERIC: bind-typed-sequence ( sequence statement -- )
GENERIC: bind-sequence ( statement -- )
GENERIC: bind-typed-sequence ( statement -- )
: statement>result-set ( statement -- result-set )
[ statement>result-set* ]
@ -47,3 +47,7 @@ M: object execute-statement* ( statement type -- )
: statement>result-sequence ( statement -- sequence )
statement>result-set [ [ sql-row ] statement-map ] with-disposal ;
: statement>typed-result-sequence ( statement -- sequence )
[ out>> ] [ statement>result-set ] bi
[ [ sql-row-typed ] with statement-map ] with-disposal ;