refactoring the db protocol, remove fql-statements
parent
725b60e383
commit
6f1d15e84c
|
@ -8,49 +8,50 @@ IN: db2
|
||||||
|
|
||||||
GENERIC: sql-command ( object -- )
|
GENERIC: sql-command ( object -- )
|
||||||
GENERIC: sql-query ( object -- sequence )
|
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-command ( object -- )
|
||||||
GENERIC: sql-bind-query ( object -- sequence )
|
GENERIC: sql-bind-query ( object -- sequence )
|
||||||
GENERIC: sql-bind-typed-command ( object -- )
|
GENERIC: sql-bind-typed-command ( object -- )
|
||||||
GENERIC: sql-bind-typed-query ( object -- sequence )
|
GENERIC: sql-bind-typed-query ( object -- sequence )
|
||||||
|
|
||||||
M: string sql-command ( sql -- )
|
M: string sql-command ( string -- )
|
||||||
f f <statement> [ execute-statement ] with-disposal ;
|
f f <statement> sql-command ;
|
||||||
|
|
||||||
M: string sql-query ( sql -- sequence )
|
M: string sql-query ( string -- sequence )
|
||||||
f f <statement> [ statement>result-sequence ] with-disposal ;
|
f f <statement> sql-query ;
|
||||||
|
|
||||||
M: string sql-bind-command* ( sequence string -- )
|
M: statement sql-command ( statement -- )
|
||||||
f f <statement> [
|
[ execute-statement ] with-disposal ;
|
||||||
|
|
||||||
|
M: statement sql-query ( statement -- sequence )
|
||||||
|
[ statement>result-sequence ] with-disposal ;
|
||||||
|
|
||||||
|
M: statement sql-bind-command ( statement -- )
|
||||||
|
[
|
||||||
prepare-statement
|
prepare-statement
|
||||||
[ bind-sequence ] [ statement>result-set drop ] bi
|
[ bind-sequence ] [ statement>result-set drop ] bi
|
||||||
] with-disposal ;
|
] with-disposal ;
|
||||||
|
|
||||||
M: string sql-bind-query* ( in-sequence string -- out-sequence )
|
M: statement sql-bind-query ( statement -- sequence )
|
||||||
f f <statement> [
|
[
|
||||||
prepare-statement
|
prepare-statement
|
||||||
[ bind-sequence ] [ statement>result-sequence ] bi
|
[ bind-sequence ] [ statement>result-sequence ] bi
|
||||||
] with-disposal ;
|
] with-disposal ;
|
||||||
|
|
||||||
M: string sql-bind-typed-command* ( in-sequence string -- )
|
M: statement sql-bind-typed-command ( statement -- )
|
||||||
f f <statement> [
|
[
|
||||||
prepare-statement
|
prepare-statement
|
||||||
[ bind-typed-sequence ] [ statement>result-set drop ] bi
|
[ bind-typed-sequence ] [ statement>result-set drop ] bi
|
||||||
] with-disposal ;
|
] with-disposal ;
|
||||||
|
|
||||||
M: string sql-bind-typed-query* ( in-sequence string -- out-sequence )
|
M: statement sql-bind-typed-query ( statement -- sequence )
|
||||||
f f <statement> [
|
[
|
||||||
prepare-statement
|
prepare-statement
|
||||||
[ bind-typed-sequence ] [ statement>result-sequence ] bi
|
[ bind-typed-sequence ] [ statement>result-sequence ] bi
|
||||||
] with-disposal ;
|
] with-disposal ;
|
||||||
|
|
||||||
M: sequence sql-command [ sql-command ] each ;
|
M: sequence sql-command [ sql-command ] each ;
|
||||||
M: sequence sql-query [ sql-query ] map ;
|
M: sequence sql-query [ sql-query ] map ;
|
||||||
M: sequence sql-bind-command* [ sql-bind-command* ] with each ;
|
M: sequence sql-bind-command [ sql-bind-command ] each ;
|
||||||
M: sequence sql-bind-query* [ sql-bind-query* ] with map ;
|
M: sequence sql-bind-query [ sql-bind-query ] map ;
|
||||||
M: sequence sql-bind-typed-command* [ sql-bind-typed-command* ] with each ;
|
M: sequence sql-bind-typed-command [ sql-bind-typed-command ] each ;
|
||||||
M: sequence sql-bind-typed-query* [ sql-bind-typed-query* ] with map ;
|
M: sequence sql-bind-typed-query [ sql-bind-typed-query ] map ;
|
||||||
|
|
|
@ -5,10 +5,8 @@ db2.private db2.sqlite.lib db2.statements db2.utils destructors
|
||||||
kernel make math.parser sequences strings assocs db2.utils ;
|
kernel make math.parser sequences strings assocs db2.utils ;
|
||||||
IN: db2.fql
|
IN: db2.fql
|
||||||
|
|
||||||
TUPLE: fql-statement sql in out ;
|
GENERIC: expand-fql* ( object -- sequence/statement )
|
||||||
|
GENERIC: normalize-fql ( object -- sequence/statement )
|
||||||
GENERIC: expand-fql* ( object -- sequence/fql-statement )
|
|
||||||
GENERIC: normalize-fql ( object -- sequence/fql-statement )
|
|
||||||
|
|
||||||
! M: object normalize-fql ;
|
! M: object normalize-fql ;
|
||||||
|
|
||||||
|
@ -66,7 +64,7 @@ M: and expand-fql* ( obj -- string )
|
||||||
M: string expand-fql* ( string -- string ) ;
|
M: string expand-fql* ( string -- string ) ;
|
||||||
|
|
||||||
M: insert expand-fql*
|
M: insert expand-fql*
|
||||||
[ fql-statement new ] dip
|
[ statement new ] dip
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
[ "insert into " % into>> % ]
|
[ "insert into " % into>> % ]
|
||||||
|
@ -77,7 +75,7 @@ M: insert expand-fql*
|
||||||
] "" make >>sql ;
|
] "" make >>sql ;
|
||||||
|
|
||||||
M: update expand-fql*
|
M: update expand-fql*
|
||||||
[ fql-statement new ] dip
|
[ statement new ] dip
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
[ "update " % tables>> ", " join % ]
|
[ "update " % tables>> ", " join % ]
|
||||||
|
@ -93,7 +91,7 @@ M: update expand-fql*
|
||||||
] "" make >>sql ;
|
] "" make >>sql ;
|
||||||
|
|
||||||
M: delete expand-fql*
|
M: delete expand-fql*
|
||||||
[ fql-statement new ] dip
|
[ statement new ] dip
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
[ "delete from " % tables>> ", " join % ]
|
[ "delete from " % tables>> ", " join % ]
|
||||||
|
@ -104,7 +102,7 @@ M: delete expand-fql*
|
||||||
] "" make >>sql ;
|
] "" make >>sql ;
|
||||||
|
|
||||||
M: select expand-fql*
|
M: select expand-fql*
|
||||||
[ fql-statement new ] dip
|
[ statement new ] dip
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
[ "select " % names>> ", " join % ]
|
[ "select " % names>> ", " join % ]
|
||||||
|
@ -116,21 +114,3 @@ M: select expand-fql*
|
||||||
[ limit>> [ " limit " % # ] when* ]
|
[ limit>> [ " limit " % # ] when* ]
|
||||||
} cleave
|
} cleave
|
||||||
] "" make >>sql ;
|
] "" 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* ;
|
|
||||||
|
|
|
@ -29,4 +29,4 @@ GENERIC# column-typed 1 ( result-set column -- sql )
|
||||||
dup #columns [ column ] with map ;
|
dup #columns [ column ] with map ;
|
||||||
|
|
||||||
: sql-row-typed ( result-set -- seq )
|
: sql-row-typed ( result-set -- seq )
|
||||||
dup #columns [ column-typed ] with map ;
|
dup #columns [ B column-typed ] with map ;
|
||||||
|
|
|
@ -85,13 +85,13 @@ IN: db2.sqlite.types
|
||||||
[ no-sql-type ]
|
[ no-sql-type ]
|
||||||
} case ;
|
} case ;
|
||||||
|
|
||||||
M: sqlite-statement bind-sequence ( sequence statement -- )
|
M: sqlite-statement bind-sequence ( statement -- )
|
||||||
handle>> '[
|
[ in>> ] [ handle>> ] bi '[
|
||||||
[ _ ] 2dip 1+ swap sqlite-bind-text
|
[ _ ] 2dip 1+ swap sqlite-bind-text
|
||||||
] each-index ;
|
] each-index ;
|
||||||
|
|
||||||
M: sqlite-statement bind-typed-sequence ( sequence statement -- )
|
M: sqlite-statement bind-typed-sequence ( statement -- )
|
||||||
handle>> '[
|
[ in>> ] [ handle>> ] bi '[
|
||||||
[ _ ] 2dip 1+ swap first2 swap bind-next-sqlite-type
|
[ _ ] 2dip 1+ swap first2 swap bind-next-sqlite-type
|
||||||
] each-index ;
|
] each-index ;
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,8 @@ IN: db2.statements.tests
|
||||||
|
|
||||||
[ { { "rocky" "mac" } } ]
|
[ { { "rocky" "mac" } } ]
|
||||||
[
|
[
|
||||||
"select name, os from computer;" sql-query
|
"select name, os from computer;"
|
||||||
|
f f <statement> sql-query
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ "insert into" sql-command ]
|
[ "insert into" sql-command ]
|
||||||
|
@ -39,28 +40,30 @@ IN: db2.statements.tests
|
||||||
[ sql-syntax-error? ] must-fail-with
|
[ sql-syntax-error? ] must-fail-with
|
||||||
|
|
||||||
[ ] [
|
[ ] [
|
||||||
{ "clubber" "windows" }
|
|
||||||
"insert into computer (name, os) values(?, ?);"
|
"insert into computer (name, os) values(?, ?);"
|
||||||
sql-bind-command*
|
{ "clubber" "windows" }
|
||||||
|
f <statement>
|
||||||
|
sql-bind-command
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ { { "windows" } } ] [
|
[ { { "windows" } } ] [
|
||||||
{ "clubber" }
|
"select os from computer where name = ?;"
|
||||||
"select os from computer where name = ?;" sql-bind-query*
|
{ "clubber" } f <statement> sql-bind-query
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ { { "windows" } } ] [
|
[ { { "windows" } } ] [
|
||||||
|
"select os from computer where name = ?;"
|
||||||
{ { VARCHAR "clubber" } }
|
{ { VARCHAR "clubber" } }
|
||||||
"select os from computer where name = ?;" sql-bind-typed-query*
|
f <statement> sql-bind-typed-query
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ ] [
|
[ ] [
|
||||||
|
"insert into computer (name, os) values(?, ?);"
|
||||||
{
|
{
|
||||||
{ VARCHAR "clubber" }
|
{ VARCHAR "clubber" }
|
||||||
{ VARCHAR "windows" }
|
{ VARCHAR "windows" }
|
||||||
}
|
} f <statement>
|
||||||
"insert into computer (name, os) values(?, ?);"
|
sql-bind-typed-command
|
||||||
sql-bind-typed-command*
|
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,8 @@ HOOK: <statement> db-connection ( sql in out -- statement )
|
||||||
GENERIC: statement>result-set* ( statement -- result-set )
|
GENERIC: statement>result-set* ( statement -- result-set )
|
||||||
GENERIC: execute-statement* ( statement type -- )
|
GENERIC: execute-statement* ( statement type -- )
|
||||||
GENERIC: prepare-statement* ( statement -- statement' )
|
GENERIC: prepare-statement* ( statement -- statement' )
|
||||||
GENERIC: bind-sequence ( sequence statement -- )
|
GENERIC: bind-sequence ( statement -- )
|
||||||
GENERIC: bind-typed-sequence ( sequence statement -- )
|
GENERIC: bind-typed-sequence ( statement -- )
|
||||||
|
|
||||||
: statement>result-set ( statement -- result-set )
|
: statement>result-set ( 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-sequence ( statement -- sequence )
|
||||||
statement>result-set [ [ sql-row ] statement-map ] with-disposal ;
|
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 ;
|
||||||
|
|
Loading…
Reference in New Issue