From 534c5958af6104aea60a9fcb0d823a5c3cfed1bc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 25 Mar 2011 13:09:06 +0200 Subject: SQLite support --- boost/database.hxx | 21 ++++++- composite/database.hxx | 21 ++++++- container/database.hxx | 21 ++++++- hello/database.hxx | 21 ++++++- inverse/database.hxx | 21 ++++++- mapping/README | 11 ++-- mapping/database.hxx | 21 ++++++- mapping/traits-mysql.hxx | 85 +++++++++++++++++++++++++++++ mapping/traits-sqlite.hxx | 127 +++++++++++++++++++++++++++++++++++++++++++ mapping/traits.hxx | 81 ++------------------------- query/database.hxx | 21 ++++++- relationship/database.hxx | 21 ++++++- schema/custom/database.hxx | 12 +++- schema/embedded/database.hxx | 20 ++++++- template/database.hxx | 21 ++++++- 15 files changed, 433 insertions(+), 92 deletions(-) create mode 100644 mapping/traits-mysql.hxx create mode 100644 mapping/traits-sqlite.hxx diff --git a/boost/database.hxx b/boost/database.hxx index fad4f16..98dd3f9 100644 --- a/boost/database.hxx +++ b/boost/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/composite/database.hxx b/composite/database.hxx index 58f7ac0..80047a8 100644 --- a/composite/database.hxx +++ b/composite/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/container/database.hxx b/container/database.hxx index 6e4c1e7..1f56099 100644 --- a/container/database.hxx +++ b/container/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/hello/database.hxx b/hello/database.hxx index 6616cfe..1bdd454 100644 --- a/hello/database.hxx +++ b/hello/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/inverse/database.hxx b/inverse/database.hxx index 4748faf..91ffb34 100644 --- a/inverse/database.hxx +++ b/inverse/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/mapping/README b/mapping/README index 6f8054e..2a06351 100644 --- a/mapping/README +++ b/mapping/README @@ -1,4 +1,4 @@ -This examples shows how to customize the mapping between C++ value types +This examples shows how to customize the mapping between C++ value types and database types. The example changes the default mapping for the 'bool' type which is now stored in the database as the "true" or "false" string. It also maps the user-defined 'date' type to a suitable database date type. @@ -11,13 +11,15 @@ person.hxx 'VARCHAR(5)' database type and 'date' to the 'DATE' database type. traits.hxx +traits-mysql.hxx +traits-sqlite.hxx ODB 'value_traits' template specializations for the 'bool' and 'date' types. These specializations implement conversion between these types and their database counterparts. person-odb.hxx person-odb.ixx -person-odb.cxx +person-odb.cxx person.sql The first three files contain the database support code and the last file contains the database schema for the person.hxx header. @@ -27,7 +29,7 @@ person.sql odb -d --generate-query --generate-schema \ --hxx-prologue '#include "traits.hxx"' person.hxx - + Where stands for the database system we are using, for example, 'mysql'. @@ -45,7 +47,7 @@ driver.cxx headers to gain access to the persistent classes and their database support code. It also includes database.hxx for the create_database() function declaration. - + In main() the driver first calls create_database() to obtain the database instance. It then persists a number of 'person' objects in the database and executes a query to find objects matching certain criteria. @@ -62,4 +64,3 @@ Once the database schema is ready, we can run the example (using MySQL as the database): ./driver --user odb_test --database odb_test - diff --git a/mapping/database.hxx b/mapping/database.hxx index 704f691..3bb4b62 100644 --- a/mapping/database.hxx +++ b/mapping/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/mapping/traits-mysql.hxx b/mapping/traits-mysql.hxx new file mode 100644 index 0000000..497d122 --- /dev/null +++ b/mapping/traits-mysql.hxx @@ -0,0 +1,85 @@ +// file : mapping/traits-mysql.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef TRAITS_MYSQL_HXX +#define TRAITS_MYSQL_HXX + +// +// MySQL implementation. +// + +#include + +#include + +#include "person.hxx" // date + +namespace odb +{ + namespace mysql + { + template <> + class value_traits + { + public: + typedef bool value_type; + typedef bool query_type; + typedef details::buffer image_type; + + static void + set_value (bool& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v = (!is_null && n == 4 && std::strncmp ("true", b.data (), n) == 0); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + bool v) + { + is_null = false; + n = v ? 4 : 5; + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), (v ? "true" : "false"), n); + } + }; + + template <> + class value_traits + { + public: + typedef date value_type; + typedef date query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (date& v, const MYSQL_TIME& i, bool is_null) + { + if (!is_null) + v = date (i.year, i.month, i.day); + else + v = date (0, 0, 0); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const date& v) + { + is_null = false; + i.neg = false; + i.year = v.year (); + i.month = v.month (); + i.day = v.day (); + } + }; + } +} + +#endif // TRAITS_MYSQL_HXX diff --git a/mapping/traits-sqlite.hxx b/mapping/traits-sqlite.hxx new file mode 100644 index 0000000..697f1b6 --- /dev/null +++ b/mapping/traits-sqlite.hxx @@ -0,0 +1,127 @@ +// file : mapping/traits-sqlite.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef TRAITS_SQLITE_HXX +#define TRAITS_SQLITE_HXX + +// +// SQLite implementation. +// + +#include +#include + +#include + +#include "person.hxx" // date + +namespace odb +{ + namespace sqlite + { + template <> + class value_traits + { + public: + typedef bool value_type; + typedef bool query_type; + typedef details::buffer image_type; + + static void + set_value (bool& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v = (!is_null && n == 4 && std::strncmp ("true", b.data (), n) == 0); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + bool v) + { + is_null = false; + n = v ? 4 : 5; + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), (v ? "true" : "false"), n); + } + }; + + // In SQLite there is no built-in DATE type. Rather, this type is + // mapped to TEXT in the YYYY-MM-DD format. The below implementation + // doesn't do any error checking for brevity. + // + template <> + class value_traits + { + public: + typedef date value_type; + typedef date query_type; + typedef details::buffer image_type; + + static void + set_value (date& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + using namespace std; + + if (!is_null) + { + istringstream is (string (b.data (), n)); + unsigned int y, m, d; + + is >> y; + is.ignore (1); + is >> m; + is.ignore (1); + is >> d; + + v = date (y, m, d); + } + else + v = date (0, 0, 0); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const date& v) + { + using namespace std; + + ostringstream os; + os.fill ('0'); + + os.width (4); + os << v.year () << '-'; + + os.width (2); + os << v.month () << '-'; + + os.width (2); + os << v.day (); + + const string& s (os.str ()); + + is_null = false; + n = s.size (); + + if (n > b.capacity ()) + b.capacity (n); + + memcpy (b.data (), s.c_str (), n); + } + }; + } +} + +#endif // TRAITS_SQLITE_HXX diff --git a/mapping/traits.hxx b/mapping/traits.hxx index 0533360..1009265 100644 --- a/mapping/traits.hxx +++ b/mapping/traits.hxx @@ -5,83 +5,12 @@ #ifndef TRAITS_HXX #define TRAITS_HXX -#include - -#include "person.hxx" // date - -// MySQL implementation. +// Include one of the database system-specific traits implementations. // #if defined(DATABASE_MYSQL) - -#include - -namespace odb -{ - namespace mysql - { - template <> - class value_traits - { - public: - typedef bool value_type; - typedef bool query_type; - typedef details::buffer image_type; - - static void - set_value (bool& v, - const details::buffer& b, - std::size_t n, - bool is_null) - { - v = (!is_null && n == 4 && std::strncmp ("true", b.data (), n) == 0); - } - - static void - set_image (details::buffer& b, - std::size_t& n, - bool& is_null, - bool v) - { - is_null = false; - n = v ? 4 : 5; - - if (n > b.capacity ()) - b.capacity (n); - - std::memcpy (b.data (), (v ? "true" : "false"), n); - } - }; - - template <> - class value_traits - { - public: - typedef date value_type; - typedef date query_type; - typedef MYSQL_TIME image_type; - - static void - set_value (date& v, const MYSQL_TIME& i, bool is_null) - { - if (!is_null) - v = date (i.year, i.month, i.day); - else - v = date (0, 0, 0); - } - - static void - set_image (MYSQL_TIME& i, bool& is_null, const date& v) - { - is_null = false; - i.neg = false; - i.year = v.year (); - i.month = v.month (); - i.day = v.day (); - } - }; - } -} - -#endif // DATABASE_MYSQL +# include "traits-mysql.hxx" +#elif defined(DATABASE_SQLITE) +# include "traits-sqlite.hxx" +#endif #endif // TRAITS_HXX diff --git a/query/database.hxx b/query/database.hxx index fe23d97..781c1a7 100644 --- a/query/database.hxx +++ b/query/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/relationship/database.hxx b/relationship/database.hxx index 8958831..3ad6394 100644 --- a/relationship/database.hxx +++ b/relationship/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/schema/custom/database.hxx b/schema/custom/database.hxx index 88536ab..932e6c4 100644 --- a/schema/custom/database.hxx +++ b/schema/custom/database.hxx @@ -18,6 +18,8 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include #endif inline std::auto_ptr @@ -33,14 +35,22 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr db ( + new odb::sqlite::database ( + argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); #endif + + return db; } #endif // DATABASE_HXX diff --git a/schema/embedded/database.hxx b/schema/embedded/database.hxx index 7483f52..ab733e2 100644 --- a/schema/embedded/database.hxx +++ b/schema/embedded/database.hxx @@ -18,6 +18,8 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include #endif inline std::auto_ptr @@ -33,14 +35,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX diff --git a/template/database.hxx b/template/database.hxx index 62f7cd1..698a64a 100644 --- a/template/database.hxx +++ b/template/database.hxx @@ -18,6 +18,9 @@ #if defined(DATABASE_MYSQL) # include +#elif defined(DATABASE_SQLITE) +# include +# include #endif inline std::auto_ptr @@ -33,14 +36,30 @@ create_database (int& argc, char* argv[]) #if defined(DATABASE_MYSQL) odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); #endif exit (0); } #if defined(DATABASE_MYSQL) - return auto_ptr (new odb::mysql::database (argc, argv)); + auto_ptr db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr 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 (); + } #endif + + return db; } #endif // DATABASE_HXX -- cgit v1.1