add introspection vocab to examine tables

db4
Doug Coleman 2009-04-14 11:26:54 -05:00
parent 4ad9580d19
commit 1494800840
10 changed files with 107 additions and 17 deletions

View File

@ -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* ;

View File

@ -0,0 +1 @@
Doug Coleman

View File

@ -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 )
<sequence-parser> skip-whitespace
[ " " take-until-sequence ]
[ take-token sqlite-type>fql-type ]
[ take-rest ] tri <column> ;
: parse-columns ( string -- seq )
"," split [ parse-column ] map ;
M: object parse-create-statement ( string -- table-schema )
<sequence-parser> {
[ "CREATE TABLE " take-sequence* ]
[ "(" take-until-sequence ]
[ "(" take-sequence* ]
[ take-rest [ CHAR: ) = ] trim-tail parse-columns ]
} cleave <table-schema> ;
: query-table-schema ( name -- table-schema )
query-table-schema* [ parse-create-statement ] map ;

View File

@ -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 ;
: <sqlite-db-connection> ( handle -- db-connection )
sqlite-db-connection new-db-connection ;
M: sqlite-db db-open ( db -- db-connection )
path>> sqlite-open <sqlite-db-connection> ;

View File

@ -0,0 +1 @@
Doug Coleman

View File

@ -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

View File

@ -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 ;

View File

@ -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 ;

View File

@ -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 ;

View File

@ -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 ;