\ sql-function-missing new
swap >>message ;
+TUPLE: sql-database-exists < sql-error message ;
+: <sql-database-exists> ( message -- error )
+ \ sql-database-exists new
+ swap >>message ;
+
: ignore-error ( quot word -- )
'[ dup _ execute [ drop ] [ rethrow ] if ] recover ; inline
: ignore-function-missing ( quot -- )
\ sql-function-missing? ignore-error ; inline
+
+: ignore-database-exists ( quot -- )
+ \ sql-database-exists? ignore-error ; inline
| Error ("relation "|"table ")(!(" does not exist").)+:table " does not exist"
=> [[ table >string unquote <sql-table-missing> ]]
+DatabaseError =
+ Error ("database")(!(" already exists").)+:database " already exists"
+ => [[ database >string <sql-database-exists> ]]
+
FunctionError =
Error "function" (!(" already exists").)+:table " already exists"
=> [[ table >string <sql-function-exists> ]]
UnknownError = .* => [[ >string <sql-unknown-error> ]]
-PostgresqlSqlError = (TableError | FunctionError | SyntaxError | UnknownError)
+PostgresqlSqlError = (TableError | DatabaseError | FunctionError | SyntaxError | UnknownError)
;EBNF
db.tuples db.types unicode.case accessors system db.tester ;
IN: db.postgresql.tests
+
os windows? cpu x86.64? and [
+ ! Ensure the table exists
[ ] [ postgresql-test-db [ ] with-db ] unit-test
[ ] [
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors kernel math namespaces make sequences random
-strings math.parser math.intervals combinators math.bitwise
-nmake db db.tuples db.types classes words shuffle arrays
-destructors continuations db.tuples.private prettyprint
-db.private byte-arrays ;
+USING: accessors arrays byte-arrays classes combinators
+continuations db db.errors db.private db.tuples
+db.tuples.private db.types destructors kernel make math
+math.bitwise math.intervals math.parser namespaces nmake
+prettyprint random sequences shuffle strings words fry ;
IN: db.queries
GENERIC: where ( specs obj -- )
: drop-index ( index-name -- )
[ "drop index " % % ] "" make sql-command ;
+
+: create-database ( string -- )
+ "create database " ";" surround sql-command ;
+
+: ensure-database ( string -- )
+ '[ _ create-database ] ignore-database-exists ;
USING: concurrency.combinators db.pools db.sqlite db.tuples
db.types kernel math random threads tools.test db sequences
io prettyprint db.postgresql accessors io.files.temp
-namespaces fry system math.parser ;
+namespaces fry system math.parser db.queries assocs ;
IN: db.tester
+: postgresql-test-db-name ( -- string )
+ cpu name>> "-" "factor-test" 3append
+ H{ { CHAR: - CHAR: _ } { CHAR: . CHAR: _ } } substitute ;
+
: postgresql-test-db ( -- postgresql-db )
<postgresql-db>
"localhost" >>host
"postgres" >>username
"thepasswordistrust" >>password
- "factor-test" >>database ;
+ postgresql-test-db-name >>database ;
+
+: postgresql-template1-db ( -- postgresql-db )
+ <postgresql-db>
+ "localhost" >>host
+ "postgres" >>username
+ "thepasswordistrust" >>password
+ "template1" >>database ;
: sqlite-test-db ( -- sqlite-db )
"tuples-test.db" temp-file <sqlite-db> ;
-
! These words leak resources, but are useful for interactivel testing
: set-sqlite-db ( -- )
sqlite-db db-open db-connection set ;
] call ; inline
: test-postgresql ( quot -- )
+ postgresql-template1-db [
+ postgresql-test-db-name ensure-database
+ ] with-db
+
'[
os windows? cpu x86.64? and [
[ ] [ postgresql-test-db _ with-db ] unit-test