diff --git a/extra/db2/db2.factor b/extra/db2/db2.factor index 4687a6329f..bda3d7ad70 100644 --- a/extra/db2/db2.factor +++ b/extra/db2/db2.factor @@ -54,10 +54,3 @@ 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: string sql-command [ sql-command ] each ; -! M: string sql-query [ sql-query ] map ; -! M: string sql-bind-command* sql-bind-command* ; -! M: string sql-bind-query* sql-bind-query* ; -! M: string sql-bind-typed-command sql-bind-typed-command* ; -! M: string sql-bind-typed-query sql-bind-typed-query* ; diff --git a/extra/db2/introspection/authors.txt b/extra/db2/introspection/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/db2/introspection/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/extra/db2/introspection/introspection.factor b/extra/db2/introspection/introspection.factor new file mode 100644 index 0000000000..8ab08876aa --- /dev/null +++ b/extra/db2/introspection/introspection.factor @@ -0,0 +1,34 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: combinators constructors db2.connections +db2.sqlite.types kernel sequence-parser sequences splitting ; +IN: db2.introspection + +TUPLE: table-schema table columns ; +CONSTRUCTOR: table-schema ( table columns -- table-schema ) ; + +TUPLE: column name type modifiers ; +CONSTRUCTOR: column ( name type modifiers -- column ) ; + +HOOK: query-table-schema* db-connection ( name -- table-schema ) +HOOK: parse-create-statement db-connection ( name -- table-schema ) + +: parse-column ( string -- column ) + skip-whitespace + [ " " take-until-sequence ] + [ take-token sqlite-type>fql-type ] + [ take-rest ] tri ; + +: parse-columns ( string -- seq ) + "," split [ parse-column ] map ; + +M: object parse-create-statement ( string -- table-schema ) + { + [ "CREATE TABLE " take-sequence* ] + [ "(" take-until-sequence ] + [ "(" take-sequence* ] + [ take-rest [ CHAR: ) = ] trim-tail parse-columns ] + } cleave ; + +: query-table-schema ( name -- table-schema ) + query-table-schema* [ parse-create-statement ] map ; diff --git a/extra/db2/sqlite/connections/connections.factor b/extra/db2/sqlite/connections/connections.factor index b99603f4ef..ae96e58d28 100644 --- a/extra/db2/sqlite/connections/connections.factor +++ b/extra/db2/sqlite/connections/connections.factor @@ -4,11 +4,6 @@ USING: accessors combinators db2.connections db2.sqlite db2.sqlite.errors db2.sqlite.lib kernel db2.errors ; IN: db2.sqlite.connections -TUPLE: sqlite-db-connection < db-connection ; - -: ( handle -- db-connection ) - sqlite-db-connection new-db-connection ; - M: sqlite-db db-open ( db -- db-connection ) path>> sqlite-open ; diff --git a/extra/db2/sqlite/introspection/authors.txt b/extra/db2/sqlite/introspection/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/db2/sqlite/introspection/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/extra/db2/sqlite/introspection/introspection-tests.factor b/extra/db2/sqlite/introspection/introspection-tests.factor new file mode 100644 index 0000000000..d8ebc4d60e --- /dev/null +++ b/extra/db2/sqlite/introspection/introspection-tests.factor @@ -0,0 +1,38 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: db2.connections db2.introspection +db2.sqlite.introspection db2.tester db2.types tools.test ; +IN: db2.sqlite.introspection.tests + + +: test-sqlite-introspection ( -- ) + [ + { + T{ table-schema + { table "computer" } + { columns + { + T{ column + { name "name" } + { type VARCHAR } + { modifiers "" } + } + T{ column + { name "os" } + { type VARCHAR } + { modifiers "" } + } + } + } + } + } + ] [ + + sqlite-test-db [ + "computer" query-table-schema + ] with-db + ] unit-test + + ; + +[ test-sqlite-introspection ] test-sqlite diff --git a/extra/db2/sqlite/introspection/introspection.factor b/extra/db2/sqlite/introspection/introspection.factor new file mode 100644 index 0000000000..41def2c558 --- /dev/null +++ b/extra/db2/sqlite/introspection/introspection.factor @@ -0,0 +1,16 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays db2 db2.introspection db2.sqlite multiline +sequences ; +IN: db2.sqlite.introspection + +M: sqlite-db-connection query-table-schema* + 1array +<" +SELECT sql FROM + (SELECT * FROM sqlite_master UNION ALL + SELECT * FROM sqlite_temp_master) +WHERE type!='meta' and tbl_name = ? +ORDER BY tbl_name, type DESC, name +"> + sql-bind-query* first ; diff --git a/extra/db2/sqlite/statements/statements.factor b/extra/db2/sqlite/statements/statements.factor index 64ce390308..0033ad06e1 100644 --- a/extra/db2/sqlite/statements/statements.factor +++ b/extra/db2/sqlite/statements/statements.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors db2.connections db2.sqlite.connections db2.sqlite.ffi db2.sqlite.lib db2.statements destructors kernel -namespaces ; +namespaces db2.sqlite ; IN: db2.sqlite.statements TUPLE: sqlite-statement < statement ; diff --git a/extra/db2/sqlite/types/types.factor b/extra/db2/sqlite/types/types.factor index 7124568fbe..c8df3b2272 100644 --- a/extra/db2/sqlite/types/types.factor +++ b/extra/db2/sqlite/types/types.factor @@ -1,8 +1,9 @@ ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays calendar.format combinators db2.types -db2.sqlite.ffi db2.sqlite.lib math fry -kernel present sequences serialize urls ; +USING: accessors arrays calendar.format combinators +db2.sqlite.ffi db2.sqlite.lib db2.sqlite.statements +db2.statements db2.types db2.utils fry kernel math present +sequences serialize urls ; IN: db2.sqlite.types : (bind-sqlite-type) ( handle key value type -- ) @@ -93,3 +94,11 @@ M: sqlite-statement bind-typed-sequence ( sequence statement -- ) handle>> '[ [ _ ] 2dip 1+ swap first2 swap bind-next-sqlite-type ] each-index ; + +ERROR: no-fql-type type ; + +: sqlite-type>fql-type ( string -- type ) + { + { "varchar" [ VARCHAR ] } + [ no-fql-type ] + } case ; diff --git a/extra/db2/utils/utils.factor b/extra/db2/utils/utils.factor index c9b009e917..71fa9bc5ae 100644 --- a/extra/db2/utils/utils.factor +++ b/extra/db2/utils/utils.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel ; +USING: arrays kernel math math.parser strings ; IN: db2.utils : ?when ( object quot -- object' ) dupd when ; inline @@ -9,3 +9,6 @@ IN: db2.utils : assoc-with ( object sequence quot -- obj curry ) swapd [ [ -rot ] dip call ] 2curry ; inline + +: ?number>string ( n/string -- string ) + dup number? [ number>string ] when ;