diff options
Diffstat (limited to 'mapping')
-rw-r--r-- | mapping/README | 11 | ||||
-rw-r--r-- | mapping/database.hxx | 21 | ||||
-rw-r--r-- | mapping/traits-mysql.hxx | 85 | ||||
-rw-r--r-- | mapping/traits-sqlite.hxx | 127 | ||||
-rw-r--r-- | mapping/traits.hxx | 81 |
5 files changed, 243 insertions, 82 deletions
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 <database> --generate-query --generate-schema \ --hxx-prologue '#include "traits.hxx"' person.hxx - + Where <database> 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 <odb/mysql/database.hxx> +#elif defined(DATABASE_SQLITE) +# include <odb/schema-catalog.hxx> +# include <odb/sqlite/database.hxx> #endif inline std::auto_ptr<odb::database> @@ -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<database> (new odb::mysql::database (argc, argv)); + auto_ptr<database> db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + 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 (); + } #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 <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef TRAITS_MYSQL_HXX +#define TRAITS_MYSQL_HXX + +// +// MySQL implementation. +// + +#include <cstring> + +#include <odb/mysql/traits.hxx> + +#include "person.hxx" // date + +namespace odb +{ + namespace mysql + { + template <> + class value_traits<bool, details::buffer, id_string> + { + 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<date, MYSQL_TIME, id_date> + { + 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 <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef TRAITS_SQLITE_HXX +#define TRAITS_SQLITE_HXX + +// +// SQLite implementation. +// + +#include <cstring> +#include <sstream> + +#include <odb/sqlite/traits.hxx> + +#include "person.hxx" // date + +namespace odb +{ + namespace sqlite + { + template <> + class value_traits<bool, details::buffer, id_text> + { + 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<date, details::buffer, id_text> + { + 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 <cstring> - -#include "person.hxx" // date - -// MySQL implementation. +// Include one of the database system-specific traits implementations. // #if defined(DATABASE_MYSQL) - -#include <odb/mysql/traits.hxx> - -namespace odb -{ - namespace mysql - { - template <> - class value_traits<bool, details::buffer, id_string> - { - 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<date, MYSQL_TIME, id_date> - { - 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 |