! 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 ;
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
: <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
! 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 [
combinators.short-circuit accessors math.parser ;
IN: db.errors.postgresql
-! ERROR: relation "foo" does not exist
-
: quote? ( ch -- ? ) "'\"" member? ;
: quoted? ( str -- ? )
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
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 )
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 ;