diff options
author | Constantin Michael <constantin@codesynthesis.com> | 2011-07-13 11:03:13 +0200 |
---|---|---|
committer | Constantin Michael <constantin@codesynthesis.com> | 2011-07-13 11:03:13 +0200 |
commit | e440e73a889c8929730632d62ebc84e32475b549 (patch) | |
tree | cefbcd5cac5e14e54c5a482af58e19d5973ea2e0 /schema | |
parent | 292f71768c16e14369c7aea4ef0590b0a741c3bc (diff) |
Add PostgreSQL
Diffstat (limited to 'schema')
-rw-r--r-- | schema/custom/database.hxx | 16 | ||||
-rw-r--r-- | schema/custom/driver.cxx | 42 | ||||
-rw-r--r-- | schema/embedded/database.hxx | 8 |
3 files changed, 64 insertions, 2 deletions
diff --git a/schema/custom/database.hxx b/schema/custom/database.hxx index 932e6c4..82441b7 100644 --- a/schema/custom/database.hxx +++ b/schema/custom/database.hxx @@ -1,4 +1,4 @@ -// file : schema/custom/database.hxx +// file : template/database.hxx // author : Boris Kolpackov <boris@codesynthesis.com> // copyright : not copyrighted - public domain @@ -20,6 +20,8 @@ # include <odb/mysql/database.hxx> #elif defined(DATABASE_SQLITE) # include <odb/sqlite/database.hxx> +#elif defined(DATABASE_PGSQL) +# include <odb/pgsql/database.hxx> #endif inline std::auto_ptr<odb::database> @@ -37,6 +39,8 @@ create_database (int& argc, char* argv[]) odb::mysql::database::print_usage (cerr); #elif defined(DATABASE_SQLITE) odb::sqlite::database::print_usage (cerr); +#elif defined(DATABASE_PGSQL) + odb::pgsql::database::print_usage (cerr); #endif exit (0); @@ -48,6 +52,16 @@ create_database (int& argc, char* argv[]) auto_ptr<database> db ( new odb::sqlite::database ( argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + + // Create the database schema. + // + { + transaction t (db->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + } +#elif defined(DATABASE_PGSQL) + auto_ptr<database> db (new odb::pgsql::database (argc, argv)); #endif return db; diff --git a/schema/custom/driver.cxx b/schema/custom/driver.cxx index 3e49c72..d08bb62 100644 --- a/schema/custom/driver.cxx +++ b/schema/custom/driver.cxx @@ -26,6 +26,7 @@ main (int argc, char* argv[]) // Create the database schema. // +#if defined(DATABASE_MYSQL) || defined(DATABASE_SQLITE) { transaction t (db->begin ()); @@ -60,6 +61,47 @@ main (int argc, char* argv[]) t.commit (); } +#elif defined(DATABASE_PGSQL) + { + // PostgreSQL-specific SQL. + // + transaction t (db->begin ()); + + db->execute ("DROP TABLE IF EXISTS \"Employer\" CASCADE"); + db->execute ("DROP TABLE IF EXISTS \"Employee\" CASCADE"); + db->execute ("DROP TABLE IF EXISTS \"EmployeeDegree\" CASCADE"); + + db->execute ( + "CREATE TABLE \"Employer\" (" + "name VARCHAR (255) NOT NULL PRIMARY KEY)"); + + db->execute ( + "CREATE TABLE \"Employee\" (" + "ssn INTEGER NOT NULL PRIMARY KEY," + "first_name VARCHAR (255) NOT NULL," + "last_name VARCHAR (255) NOT NULL," + "employer VARCHAR (255) NOT NULL)"); + + db->execute ( + "CREATE TABLE \"EmployeeDegree\" (" + "ssn INTEGER NOT NULL," + "degree VARCHAR (255) NOT NULL)"); + + db->execute ( + "ALTER TABLE \"Employee\" " + "ADD FOREIGN KEY (employer) " + "REFERENCES \"Employer\" " + "INITIALLY DEFERRED"); + + db->execute ( + "ALTER TABLE \"EmployeeDegree\" " + "ADD FOREIGN KEY (ssn) " + "REFERENCES \"Employee\" " + "INITIALLY DEFERRED"); + + t.commit (); + } +#endif // Create a few persistent objects. // diff --git a/schema/embedded/database.hxx b/schema/embedded/database.hxx index ab733e2..82441b7 100644 --- a/schema/embedded/database.hxx +++ b/schema/embedded/database.hxx @@ -1,4 +1,4 @@ -// file : schema/embedded/database.hxx +// file : template/database.hxx // author : Boris Kolpackov <boris@codesynthesis.com> // copyright : not copyrighted - public domain @@ -20,6 +20,8 @@ # include <odb/mysql/database.hxx> #elif defined(DATABASE_SQLITE) # include <odb/sqlite/database.hxx> +#elif defined(DATABASE_PGSQL) +# include <odb/pgsql/database.hxx> #endif inline std::auto_ptr<odb::database> @@ -37,6 +39,8 @@ create_database (int& argc, char* argv[]) odb::mysql::database::print_usage (cerr); #elif defined(DATABASE_SQLITE) odb::sqlite::database::print_usage (cerr); +#elif defined(DATABASE_PGSQL) + odb::pgsql::database::print_usage (cerr); #endif exit (0); @@ -56,6 +60,8 @@ create_database (int& argc, char* argv[]) schema_catalog::create_schema (*db); t.commit (); } +#elif defined(DATABASE_PGSQL) + auto_ptr<database> db (new odb::pgsql::database (argc, argv)); #endif return db; |