add error handling to sqlite, postgresql is next. switching computers..

db4
Doug Coleman 2009-02-20 20:40:17 -06:00
parent b54833c728
commit 985597ba68
9 changed files with 98 additions and 14 deletions

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs classes continuations destructors kernel math
namespaces sequences classes.tuple words strings
tools.walker accessors combinators fry ;
tools.walker accessors combinators fry db.errors ;
IN: db
<PRIVATE
@ -77,7 +77,11 @@ GENERIC: bind-tuple ( tuple statement -- )
GENERIC: execute-statement* ( statement type -- )
M: object execute-statement* ( statement type -- )
drop query-results dispose ;
'[
_ _ drop query-results dispose
] [
parse-db-error rethrow
] recover ;
: execute-one-statement ( statement -- )
dup type>> execute-statement* ;

View File

@ -1,10 +1,20 @@
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel ;
USING: kernel db.private ;
IN: db.errors
HOOK: parse-db-error db-connection ( error -- error' )
ERROR: db-error ;
ERROR: sql-error ;
ERROR: table-exists ;
ERROR: bad-schema ;
ERROR: sql-syntax-error error ;
ERROR: sql-table-exists table ;
C: <sql-table-exists> sql-table-exists
ERROR: sql-table-missing table ;
C: <sql-table-missing> sql-table-missing

View File

@ -0,0 +1 @@
Doug Coleman

View File

@ -0,0 +1,4 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: tools.test db.errors.postgresql ;
IN: db.errors.postgresql.tests

View File

@ -0,0 +1,7 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: ;
IN: db.errors.postgresql
M: postgresql-db-connection parse-db-error
;

View File

@ -0,0 +1 @@
Doug Coleman

View File

@ -0,0 +1,26 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors combinators.short-circuit db db.errors
db.errors.sqlite db.sqlite io.files.unique kernel namespaces
tools.test ;
IN: db.errors.sqlite.tests
: sqlite-error-test-db-path ( -- path )
"sqlite" "error-test" make-unique-file ;
sqlite-error-test-db-path <sqlite-db> [
[
"insert into foo (id) values('1');" sql-command
] [
{ [ sql-table-missing? ] [ table>> "foo" = ] } 1&&
] must-fail-with
[
"create table foo(id);" sql-command
"create table foo(id);" sql-command
] [
{ [ sql-table-exists? ] [ table>> "foo" = ] } 1&&
] must-fail-with
] with-db

View File

@ -0,0 +1,31 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors combinators db.errors db.sqlite.private kernel
sequences peg.ebnf strings ;
IN: db.errors.sqlite
ERROR: unparsed-sqlite-error error ;
SINGLETONS: table-exists table-missing ;
: sqlite-table-error ( table message -- error )
{
{ table-exists [ <sql-table-exists> ] }
} case ;
EBNF: parse-sqlite-sql-error
TableMessage = " already exists" => [[ table-exists ]]
SqliteError =
"table " (!(TableMessage).)+:table TableMessage:message
=> [[ table >string message sqlite-table-error ]]
| "no such table: " .+:table
=> [[ table >string <sql-table-missing> ]]
;EBNF
M: sqlite-db-connection parse-db-error
dup n>> {
{ 1 [ string>> parse-sqlite-sql-error ] }
[ drop ]
} case ;

View File

@ -3,7 +3,7 @@ prettyprint sequences namespaces tools.test db db.private
db.tuples db.types unicode.case accessors system ;
IN: db.postgresql.tests
: test-db ( -- postgresql-db )
: postgresql-test-db ( -- postgresql-db )
<postgresql-db>
"localhost" >>host
"postgres" >>username
@ -11,10 +11,10 @@ IN: db.postgresql.tests
"factor-test" >>database ;
os windows? cpu x86.64? and [
[ ] [ test-db [ ] with-db ] unit-test
[ ] [ postgresql-test-db [ ] with-db ] unit-test
[ ] [
test-db [
postgresql-test-db [
[ "drop table person;" sql-command ] ignore-errors
"create table person (name varchar(30), country varchar(30));"
sql-command
@ -30,7 +30,7 @@ os windows? cpu x86.64? and [
{ "Jane" "New Zealand" }
}
] [
test-db [
postgresql-test-db [
"select * from person" sql-query
] with-db
] unit-test
@ -40,11 +40,11 @@ os windows? cpu x86.64? and [
{ "John" "America" }
{ "Jane" "New Zealand" }
}
] [ test-db [ "select * from person" sql-query ] with-db ] unit-test
] [ postgresql-test-db [ "select * from person" sql-query ] with-db ] unit-test
[
] [
test-db [
postgresql-test-db [
"insert into person(name, country) values('Jimmy', 'Canada')"
sql-command
] with-db
@ -56,10 +56,10 @@ os windows? cpu x86.64? and [
{ "Jane" "New Zealand" }
{ "Jimmy" "Canada" }
}
] [ test-db [ "select * from person" sql-query ] with-db ] unit-test
] [ postgresql-test-db [ "select * from person" sql-query ] with-db ] unit-test
[
test-db [
postgresql-test-db [
[
"insert into person(name, country) values('Jose', 'Mexico')" sql-command
"insert into person(name, country) values('Jose', 'Mexico')" sql-command
@ -69,14 +69,14 @@ os windows? cpu x86.64? and [
] must-fail
[ 3 ] [
test-db [
postgresql-test-db [
"select * from person" sql-query length
] with-db
] unit-test
[
] [
test-db [
postgresql-test-db [
[
"insert into person(name, country) values('Jose', 'Mexico')"
sql-command
@ -87,7 +87,7 @@ os windows? cpu x86.64? and [
] unit-test
[ 5 ] [
test-db [
postgresql-test-db [
"select * from person" sql-query length
] with-db
] unit-test