add more postgres error handling, remove usage of ignore-errors in db.tuples

db4
sheeple 2009-02-21 21:59:23 -06:00
parent d6d89e0a40
commit 02cec3a9f4
4 changed files with 49 additions and 11 deletions

View File

@ -1,6 +1,6 @@
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel ;
USING: accessors kernel continuations fry words ;
IN: db.errors
ERROR: db-error ;
@ -8,6 +8,11 @@ ERROR: sql-error location ;
ERROR: bad-schema ;
ERROR: sql-unknown-error < sql-error message ;
: <sql-unknown-error> ( message -- error )
\ sql-unknown-error new
swap >>message ;
ERROR: sql-table-exists < sql-error table ;
: <sql-table-exists> ( table -- error )
\ sql-table-exists new
@ -22,3 +27,28 @@ ERROR: sql-syntax-error < sql-error message ;
: <sql-syntax-error> ( message -- error )
\ sql-syntax-error new
swap >>message ;
ERROR: sql-function-exists < sql-error message ;
: <sql-function-exists> ( message -- error )
\ sql-function-exists new
swap >>message ;
ERROR: sql-function-missing < sql-error message ;
: <sql-function-missing> ( message -- error )
\ sql-function-missing new
swap >>message ;
: ignore-error ( quot word -- )
'[ dup _ execute [ drop ] [ rethrow ] if ] recover ; inline
: ignore-table-exists ( quot -- )
\ sql-table-exists? ignore-error ; inline
: ignore-table-missing ( quot -- )
\ sql-table-missing? ignore-error ; inline
: ignore-function-exists ( quot -- )
\ sql-function-exists? ignore-error ; inline
: ignore-function-missing ( quot -- )
\ sql-function-missing? ignore-error ; inline

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: accessors combinators.short-circuit db db.errors
db.errors.postgresql db.postgresql io.files.unique kernel namespaces
tools.test db.tester ;
tools.test db.tester continuations ;
IN: db.errors.postgresql.tests
postgresql-test-db [

View File

@ -4,8 +4,6 @@ USING: kernel db.errors peg.ebnf strings sequences math
combinators.short-circuit accessors math.parser ;
IN: db.errors.postgresql
! ERROR: relation "foo" does not exist
: quote? ( ch -- ? ) "'\"" member? ;
: quoted? ( str -- ? )
@ -24,18 +22,26 @@ EBNF: parse-postgresql-sql-error
Error = "ERROR:" [ ]+
TableError =
Error "relation " (!(" already exists").)+:table " already exists"
Error ("relation "|"table ")(!(" already exists").)+:table " already exists"
=> [[ table >string unquote <sql-table-exists> ]]
| Error "relation " (!(" does not exist").)+:table " does not exist"
| Error ("relation "|"table ")(!(" does not exist").)+:table " does not exist"
=> [[ table >string unquote <sql-table-missing> ]]
FunctionError =
Error "function" (!(" already exists").)+:table " already exists"
=> [[ table >string <sql-function-exists> ]]
| Error "function" (!(" does not exist").)+:table " does not exist"
=> [[ table >string <sql-function-missing> ]]
SyntaxError =
Error "syntax error at end of input":error
=> [[ error >string <sql-syntax-error> ]]
| Error "syntax error at or near " .+:syntaxerror
=> [[ syntaxerror >string unquote <sql-syntax-error> ]]
PostgresqlSqlError = (TableError | SyntaxError)
UnknownError = .* => [[ >string <sql-unknown-error> ]]
PostgresqlSqlError = (TableError | FunctionError | SyntaxError | UnknownError)
;EBNF

View File

@ -4,7 +4,7 @@ USING: arrays assocs classes db kernel namespaces
classes.tuple words sequences slots math accessors
math.parser io prettyprint db.types continuations
destructors mirrors sets db.types db.private fry
combinators.short-circuit ;
combinators.short-circuit db.errors ;
IN: db.tuples
HOOK: create-sql-statement db-connection ( class -- object )
@ -118,13 +118,15 @@ ERROR: no-defined-persistent object ;
ensure-defined-persistent
[
'[
_ drop-sql-statement [ execute-statement ] with-disposals
] ignore-errors
[
_ drop-sql-statement [ execute-statement ] with-disposals
] ignore-table-missing
] ignore-function-missing
] [ create-table ] bi ;
: ensure-table ( class -- )
ensure-defined-persistent
'[ _ create-table ] ignore-errors ;
'[ [ _ create-table ] ignore-table-exists ] ignore-function-exists ;
: ensure-tables ( classes -- ) [ ensure-table ] each ;