add sql-bind-query, sql-bind-command for writing raw sql
parent
48a0858135
commit
1789a58bbf
|
@ -1,7 +1,8 @@
|
||||||
! Copyright (C) 2009 Doug Coleman.
|
! Copyright (C) 2009 Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors continuations destructors fry kernel
|
USING: accessors continuations db2.result-sets db2.sqlite.lib
|
||||||
namespaces sequences strings db2.statements ;
|
db2.sqlite.result-sets db2.sqlite.statements db2.statements
|
||||||
|
destructors fry kernel math namespaces sequences strings ;
|
||||||
IN: db2
|
IN: db2
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
@ -19,3 +20,20 @@ PRIVATE>
|
||||||
: sql-query ( sql -- sequence )
|
: sql-query ( sql -- sequence )
|
||||||
f f <statement> [ statement>result-sequence ] with-disposal ;
|
f f <statement> [ statement>result-sequence ] with-disposal ;
|
||||||
|
|
||||||
|
: sql-bind-command ( sequence string -- )
|
||||||
|
f f <statement> [
|
||||||
|
sqlite-maybe-prepare [
|
||||||
|
handle>> '[ [ _ ] 2dip 1+ swap sqlite-bind-text ] each-index
|
||||||
|
] [
|
||||||
|
sqlite-result-set new-result-set advance-row
|
||||||
|
] bi
|
||||||
|
] with-disposal ;
|
||||||
|
|
||||||
|
: sql-bind-query ( in-sequence string -- out-sequence )
|
||||||
|
f f <statement> [
|
||||||
|
sqlite-maybe-prepare [
|
||||||
|
handle>> '[ [ _ ] 2dip 1+ swap sqlite-bind-text ] each-index
|
||||||
|
] [
|
||||||
|
statement>result-sequence
|
||||||
|
] bi
|
||||||
|
] with-disposal ;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
! Copyright (C) 2009 Doug Coleman.
|
! Copyright (C) 2009 Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors kernel sequences ;
|
USING: accessors kernel sequences combinators ;
|
||||||
IN: db2.result-sets
|
IN: db2.result-sets
|
||||||
|
|
||||||
TUPLE: result-set sql in out handle n max ;
|
TUPLE: result-set sql in out handle n max ;
|
||||||
|
@ -16,10 +16,14 @@ GENERIC# column-typed 1 ( result-set column -- sql )
|
||||||
dup #rows >>max
|
dup #rows >>max
|
||||||
0 >>n ;
|
0 >>n ;
|
||||||
|
|
||||||
: new-result-set ( query handle class -- result-set )
|
: new-result-set ( query class -- result-set )
|
||||||
new
|
new
|
||||||
swap >>handle
|
swap {
|
||||||
swap [ sql>> >>sql ] [ in>> >>in ] [ out>> >>out ] tri ;
|
[ handle>> >>handle ]
|
||||||
|
[ sql>> >>sql ]
|
||||||
|
[ in>> >>in ]
|
||||||
|
[ out>> >>out ]
|
||||||
|
} cleave ;
|
||||||
|
|
||||||
: sql-row ( result-set -- seq )
|
: sql-row ( result-set -- seq )
|
||||||
dup #columns [ column ] with map ;
|
dup #columns [ column ] with map ;
|
||||||
|
|
|
@ -11,7 +11,7 @@ M: sqlite-result-set dispose
|
||||||
|
|
||||||
M: sqlite-statement statement>result-set*
|
M: sqlite-statement statement>result-set*
|
||||||
sqlite-maybe-prepare
|
sqlite-maybe-prepare
|
||||||
dup handle>> sqlite-result-set new-result-set
|
sqlite-result-set new-result-set
|
||||||
dup advance-row ;
|
dup advance-row ;
|
||||||
|
|
||||||
M: sqlite-result-set advance-row ( result-set -- )
|
M: sqlite-result-set advance-row ( result-set -- )
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (C) 2009 Doug Coleman.
|
! Copyright (C) 2009 Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: tools.test db2.statements kernel db2 db2.tester
|
USING: tools.test db2.statements kernel db2 db2.tester
|
||||||
continuations db2.errors ;
|
continuations db2.errors accessors ;
|
||||||
IN: db2.statements.tests
|
IN: db2.statements.tests
|
||||||
|
|
||||||
{ 1 0 } [ [ drop ] statement-each ] must-infer-as
|
{ 1 0 } [ [ drop ] statement-each ] must-infer-as
|
||||||
|
@ -11,6 +11,9 @@ IN: db2.statements.tests
|
||||||
: test-sql-command ( -- )
|
: test-sql-command ( -- )
|
||||||
[ "drop table computer;" sql-command ] ignore-errors
|
[ "drop table computer;" sql-command ] ignore-errors
|
||||||
|
|
||||||
|
[ "drop table computer;" sql-command ]
|
||||||
|
[ [ sql-table-missing? ] [ table>> "computer" = ] bi and ] must-fail-with
|
||||||
|
|
||||||
[ ] [
|
[ ] [
|
||||||
"create table computer(name varchar, os varchar);"
|
"create table computer(name varchar, os varchar);"
|
||||||
sql-command
|
sql-command
|
||||||
|
@ -32,7 +35,17 @@ IN: db2.statements.tests
|
||||||
[ "selectt" sql-query ]
|
[ "selectt" sql-query ]
|
||||||
[ sql-syntax-error? ] must-fail-with
|
[ sql-syntax-error? ] must-fail-with
|
||||||
|
|
||||||
|
[ ] [
|
||||||
|
{ "clubber" "windows" }
|
||||||
|
"insert into computer (name, os) values(?, ?);"
|
||||||
|
sql-bind-command
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
[ { { "windows" } } ] [
|
||||||
|
{ "clubber" }
|
||||||
|
"select os from computer where name = ?;" sql-bind-query
|
||||||
|
] unit-test
|
||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
[ test-sql-command ] test-dbs
|
[ test-sql-command ] test-dbs
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue