summaryrefslogtreecommitdiff
path: root/odb-examples/mapping
diff options
context:
space:
mode:
Diffstat (limited to 'odb-examples/mapping')
-rw-r--r--odb-examples/mapping/README79
-rw-r--r--odb-examples/mapping/buildfile46
-rw-r--r--odb-examples/mapping/database.hxx95
-rw-r--r--odb-examples/mapping/driver.cxx65
-rw-r--r--odb-examples/mapping/person.hxx106
-rw-r--r--odb-examples/mapping/testscript13
-rw-r--r--odb-examples/mapping/traits-mssql.hxx85
-rw-r--r--odb-examples/mapping/traits-mysql.hxx85
-rw-r--r--odb-examples/mapping/traits-oracle.hxx95
-rw-r--r--odb-examples/mapping/traits-pgsql.hxx118
-rw-r--r--odb-examples/mapping/traits-sqlite.hxx127
-rw-r--r--odb-examples/mapping/traits.hxx23
12 files changed, 937 insertions, 0 deletions
diff --git a/odb-examples/mapping/README b/odb-examples/mapping/README
new file mode 100644
index 0000000..48bbe4f
--- /dev/null
+++ b/odb-examples/mapping/README
@@ -0,0 +1,79 @@
+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.
+
+The example consists of the following files:
+
+person.hxx
+ Header file defining the 'date' value type and the 'person' persistent
+ class. It also uses the ODB value type pragma to map 'bool' to the
+ 'VARCHAR(5)' database type and 'date' to the 'DATE' database type.
+
+traits.hxx
+traits-mysql.hxx
+traits-sqlite.hxx
+traits-pgsql.hxx
+traits-oracle.hxx
+traits-mssql.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.sql
+ The first three files contain the database support code and the last file
+ contains the database schema for the person.hxx header.
+
+ These files are generated by the ODB compiler from person.hxx using the
+ following command line:
+
+ odb --std c++11 -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,
+ 'pgsql'.
+
+ The --hxx-prologue option included the traits.hxx header at the beginning
+ of the generated person-odb.hxx file. This makes the 'value_traits'
+ specializations defined in traits.hxx known to the generated database
+ support code.
+
+database.hxx
+ Contains the create_database() function which instantiates the concrete
+ database class corresponding to the database system we are using.
+
+driver.cxx
+ Driver for the example. It includes the person.hxx and person-odb.hxx
+ 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.
+
+To compile and link the example manually from the command line we can use the
+following commands (using PostgreSQL as an example; replace 'c++' with your
+C++ compiler name):
+
+c++ -DDATABASE_PGSQL -c person-odb.cxx
+c++ -DDATABASE_PGSQL -c driver.cxx
+c++ -o driver driver.o person-odb.o -lodb-pgsql -lodb
+
+To run the example we may first need to create the database schema (for some
+database systems, such as SQLite, the schema is embedded into the generated
+code which makes this step unnecessary). Using PostgreSQL as an example, this
+can be achieved with the following command:
+
+psql --username=odb_test --dbname=odb_test -f person.sql
+
+Here we use 'odb_test' as the database login and also 'odb_test' as the
+database name.
+
+Once the database schema is ready, we can run the example (using PostgreSQL as
+the database):
+
+./driver --user odb_test --database odb_test
diff --git a/odb-examples/mapping/buildfile b/odb-examples/mapping/buildfile
new file mode 100644
index 0000000..77d7b1a
--- /dev/null
+++ b/odb-examples/mapping/buildfile
@@ -0,0 +1,46 @@
+# file : mapping/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+import libodb = libodb%lib{odb}
+
+import libs = libodb-$database%lib{odb-$database}
+
+exe{driver}: {hxx cxx}{* -person-odb} {hxx ixx cxx}{person-odb} testscript
+
+# The metadata library target which we use to extract the poptions variable
+# value for specifying the preprocessor options (-I, etc) on the ODB compiler
+# command line.
+#
+libue{person-meta}: $libodb
+
+exe{driver}: libue{person-meta} $libs
+
+<{hxx ixx cxx}{person-odb}>: hxx{person} libue{person-meta} $odb
+{{
+ pops = $cxx.lib_poptions($<[1])
+ depdb hash $pops
+
+ depdb dyndep --dyn-target --target-what 'generated schema' --format lines \
+ -- echo ($sqlite ? '' : "$out_base/person.sql")
+
+ $odb --std c++11 \
+ --database $database \
+ --generate-schema \
+ --generate-query \
+ --hxx-prologue '#include "traits.hxx"' \
+ --output-dir $out_base \
+ --table-prefix mapping_ \
+ "-I$src_base" $pops \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base" "-I$src_base" -DDATABASE_$ucase($database)
+
+# Testscript's run-time prerequisites.
+#
+# @@ BUILD2: Eventually we should be able to mark it as test.input once
+# this is supported for testscript tests.
+#
+exe{driver}: ../alias{database-client}: include = adhoc
+
+testscript@./: schema = person
diff --git a/odb-examples/mapping/database.hxx b/odb-examples/mapping/database.hxx
new file mode 100644
index 0000000..94b4991
--- /dev/null
+++ b/odb-examples/mapping/database.hxx
@@ -0,0 +1,95 @@
+// file : mapping/database.hxx
+// copyright : not copyrighted - public domain
+
+//
+// Create concrete database instance based on the DATABASE_* macros.
+//
+
+#ifndef DATABASE_HXX
+#define DATABASE_HXX
+
+#include <string>
+#include <memory> // std::unique_ptr
+#include <cstdlib> // std::exit
+#include <iostream>
+
+#include <odb/database.hxx>
+
+#if defined(DATABASE_MYSQL)
+# include <odb/mysql/database.hxx>
+#elif defined(DATABASE_SQLITE)
+# include <odb/connection.hxx>
+# include <odb/transaction.hxx>
+# include <odb/schema-catalog.hxx>
+# include <odb/sqlite/database.hxx>
+#elif defined(DATABASE_PGSQL)
+# include <odb/pgsql/database.hxx>
+#elif defined(DATABASE_ORACLE)
+# include <odb/oracle/database.hxx>
+#elif defined(DATABASE_MSSQL)
+# include <odb/mssql/database.hxx>
+#else
+# error unknown database; did you forget to define the DATABASE_* macros?
+#endif
+
+inline std::unique_ptr<odb::database>
+create_database (int& argc, char* argv[])
+{
+ using namespace std;
+ using namespace odb::core;
+
+ if (argc > 1 && argv[1] == string ("--help"))
+ {
+ cout << "Usage: " << argv[0] << " [options]" << endl
+ << "Options:" << endl;
+
+#if defined(DATABASE_MYSQL)
+ odb::mysql::database::print_usage (cout);
+#elif defined(DATABASE_SQLITE)
+ odb::sqlite::database::print_usage (cout);
+#elif defined(DATABASE_PGSQL)
+ odb::pgsql::database::print_usage (cout);
+#elif defined(DATABASE_ORACLE)
+ odb::oracle::database::print_usage (cout);
+#elif defined(DATABASE_MSSQL)
+ odb::mssql::database::print_usage (cout);
+#endif
+
+ exit (0);
+ }
+
+#if defined(DATABASE_MYSQL)
+ unique_ptr<database> db (new odb::mysql::database (argc, argv));
+#elif defined(DATABASE_SQLITE)
+ unique_ptr<database> db (
+ new odb::sqlite::database (
+ argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
+
+ // Create the database schema. Due to bugs in SQLite foreign key
+ // support for DDL statements, we need to temporarily disable
+ // foreign keys.
+ //
+ {
+ connection_ptr c (db->connection ());
+
+ c->execute ("PRAGMA foreign_keys=OFF");
+
+ transaction t (c->begin ());
+ schema_catalog::create_schema (*db);
+ t.commit ();
+
+ c->execute ("PRAGMA foreign_keys=ON");
+ }
+#elif defined(DATABASE_PGSQL)
+ unique_ptr<database> db (new odb::pgsql::database (argc, argv));
+#elif defined(DATABASE_ORACLE)
+ unique_ptr<database> db (new odb::oracle::database (argc, argv));
+#elif defined(DATABASE_MSSQL)
+ unique_ptr<database> db (
+ new odb::mssql::database (argc, argv, false, "TrustServerCertificate=yes"));
+#endif
+
+ return db;
+}
+
+#endif // DATABASE_HXX
diff --git a/odb-examples/mapping/driver.cxx b/odb-examples/mapping/driver.cxx
new file mode 100644
index 0000000..5e9d067
--- /dev/null
+++ b/odb-examples/mapping/driver.cxx
@@ -0,0 +1,65 @@
+// file : mapping/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include "database.hxx" // create_database
+
+#include "person.hxx"
+#include "person-odb.hxx"
+
+using namespace std;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+ // Create a few persistent person objects.
+ //
+ {
+ person john ("John", "Doe", date (1978, 10, 13), true);
+ person jane ("Jane", "Doe", date (1975, 9, 23), false);
+ person joe ("Joe", "Dirt", date (1973, 12, 3), true);
+
+ transaction t (db->begin ());
+
+ db->persist (john);
+ db->persist (jane);
+ db->persist (joe);
+
+ t.commit ();
+ }
+
+ // Query for a person using data members of our custom-mapped types.
+ //
+ {
+ typedef odb::query<person> query;
+ typedef odb::result<person> result;
+
+ transaction t (db->begin ());
+
+ result r (db->query<person> (query::married &&
+ query::born == date (1978, 10, 13)));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ {
+ cout << i->first () << " " << i->last () << " " << i->born () << endl;
+ }
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-examples/mapping/person.hxx b/odb-examples/mapping/person.hxx
new file mode 100644
index 0000000..c1a43b9
--- /dev/null
+++ b/odb-examples/mapping/person.hxx
@@ -0,0 +1,106 @@
+// file : mapping/person.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef PERSON_HXX
+#define PERSON_HXX
+
+#include <string>
+#include <ostream>
+
+#include <odb/core.hxx>
+
+// In our database boolean values are stored as strings with valid
+// values being "true" and "false".
+//
+#pragma db value(bool) type("VARCHAR(5)")
+
+#pragma db value type("DATE")
+class date
+{
+public:
+ date (unsigned int year, unsigned int month, unsigned int day)
+ : year_ (year), month_ (month), day_ (day)
+ {
+ }
+
+ unsigned int
+ year () const
+ {
+ return year_;
+ }
+
+ unsigned int
+ month () const
+ {
+ return month_;
+ }
+
+ unsigned int
+ day () const
+ {
+ return day_;
+ }
+
+private:
+ unsigned int year_;
+ unsigned int month_;
+ unsigned int day_;
+};
+
+inline std::ostream&
+operator<< (std::ostream& os, const date& d)
+{
+ return os << d.year () << '-' << d.month () << '-' << d.day ();
+}
+
+#pragma db object
+class person
+{
+public:
+ person (const std::string& first,
+ const std::string& last,
+ const date& born,
+ bool married)
+ : first_ (first), last_ (last), born_ (born), married_ (married)
+ {
+ }
+
+ const std::string&
+ first () const
+ {
+ return first_;
+ }
+
+ const std::string&
+ last () const
+ {
+ return last_;
+ }
+
+ const date&
+ born () const
+ {
+ return born_;
+ }
+
+ bool
+ married () const
+ {
+ return married_;
+ }
+
+private:
+ friend class odb::access;
+
+ person (): born_ (0, 0, 0) {}
+
+ #pragma db id auto
+ unsigned long id_;
+
+ std::string first_;
+ std::string last_;
+ date born_;
+ bool married_;
+};
+
+#endif // PERSON_HXX
diff --git a/odb-examples/mapping/testscript b/odb-examples/mapping/testscript
new file mode 100644
index 0000000..f9bac50
--- /dev/null
+++ b/odb-examples/mapping/testscript
@@ -0,0 +1,13 @@
+# file : mapping/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../database-options.testscript
+.include ../$(database).testscript
+
++if! $sqlite
+ $create_schema
+end
+
+: basics
+:
+$* >|
diff --git a/odb-examples/mapping/traits-mssql.hxx b/odb-examples/mapping/traits-mssql.hxx
new file mode 100644
index 0000000..3713f82
--- /dev/null
+++ b/odb-examples/mapping/traits-mssql.hxx
@@ -0,0 +1,85 @@
+// file : mapping/traits-mssql.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef TRAITS_MSSQL_HXX
+#define TRAITS_MSSQL_HXX
+
+//
+// SQL Server implementation.
+//
+
+#include <cstddef> // std::size_t
+#include <cstring> // std::strncmp, std::memcpy
+#include <cassert>
+
+#include <odb/mssql/traits.hxx>
+
+#include "person.hxx" // date
+
+namespace odb
+{
+ namespace mssql
+ {
+ template <>
+ class value_traits<bool, id_string>
+ {
+ public:
+ typedef bool value_type;
+ typedef bool query_type;
+ typedef char* image_type;
+
+ static void
+ set_value (bool& v,
+ const char* b,
+ std::size_t n,
+ bool is_null)
+ {
+ v = (!is_null && n == 4 && std::strncmp ("true", b, n) == 0);
+ }
+
+ static void
+ set_image (char* b,
+ std::size_t c,
+ std::size_t& n,
+ bool& is_null,
+ bool v)
+ {
+ is_null = false;
+ n = v ? 4 : 5;
+
+ assert (n <= c);
+
+ std::memcpy (b, (v ? "true" : "false"), n);
+ }
+ };
+
+ template <>
+ class value_traits< ::date, id_date>
+ {
+ public:
+ typedef ::date value_type;
+ typedef ::date query_type;
+ typedef mssql::date image_type;
+
+ static void
+ set_value (value_type& v, const image_type& i, bool is_null)
+ {
+ if (!is_null)
+ v = value_type (static_cast<unsigned int> (i.year), i.month, i.day);
+ else
+ v = value_type (0, 0, 0);
+ }
+
+ static void
+ set_image (image_type& i, bool& is_null, const value_type& v)
+ {
+ is_null = false;
+ i.year = static_cast<SQLSMALLINT> (v.year ());
+ i.month = static_cast<SQLUSMALLINT> (v.month ());
+ i.day = static_cast<SQLUSMALLINT> (v.day ());
+ }
+ };
+ }
+}
+
+#endif // TRAITS_MSSQL_HXX
diff --git a/odb-examples/mapping/traits-mysql.hxx b/odb-examples/mapping/traits-mysql.hxx
new file mode 100644
index 0000000..a15d1c9
--- /dev/null
+++ b/odb-examples/mapping/traits-mysql.hxx
@@ -0,0 +1,85 @@
+// file : mapping/traits-mysql.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef TRAITS_MYSQL_HXX
+#define TRAITS_MYSQL_HXX
+
+//
+// MySQL implementation.
+//
+
+#include <cstddef> // std::size_t
+#include <cstring> // std::strncmp, std::memcpy
+
+#include <odb/mysql/traits.hxx>
+
+#include "person.hxx" // date
+
+namespace odb
+{
+ namespace mysql
+ {
+ template <>
+ class value_traits<bool, 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, 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/odb-examples/mapping/traits-oracle.hxx b/odb-examples/mapping/traits-oracle.hxx
new file mode 100644
index 0000000..a5683c6
--- /dev/null
+++ b/odb-examples/mapping/traits-oracle.hxx
@@ -0,0 +1,95 @@
+// file : mapping/traits-oracle.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef TRAITS_ORACLE_HXX
+#define TRAITS_ORACLE_HXX
+
+//
+// Oracle implementation.
+//
+
+#include <cstddef> // std::size_t
+#include <cstring> // std::strncmp, std::memcpy
+#include <cassert>
+
+#include <odb/oracle/traits.hxx>
+#include <odb/oracle/details/date.hxx>
+
+#include "person.hxx" // date
+
+namespace odb
+{
+ namespace oracle
+ {
+ template <>
+ class value_traits<bool, id_string>
+ {
+ public:
+ typedef bool value_type;
+ typedef bool query_type;
+ typedef char* image_type;
+
+ static void
+ set_value (bool& v,
+ const char* b,
+ std::size_t n,
+ bool is_null)
+ {
+ v = (!is_null && n == 4 && std::strncmp ("true", b, n) == 0);
+ }
+
+ static void
+ set_image (char* b,
+ std::size_t c,
+ std::size_t& n,
+ bool& is_null,
+ bool v)
+ {
+ is_null = false;
+ n = v ? 4 : 5;
+
+ assert (n <= c);
+
+ std::memcpy (b, (v ? "true" : "false"), n);
+ }
+ };
+
+ template <>
+ class value_traits<date, id_date>
+ {
+ public:
+ typedef date value_type;
+ typedef date query_type;
+ typedef char* image_type;
+
+ static void
+ set_value (date& v, const char* b, bool is_null)
+ {
+ if (!is_null)
+ {
+ short y;
+ unsigned char m, d, h, min, s;
+ details::get_date (b, y, m, d, h, min, s);
+
+ v = date (y, m, d);
+ }
+ else
+ v = date (0, 0, 0);
+ }
+
+ static void
+ set_image (char* b, bool& is_null, const date& v)
+ {
+ is_null = false;
+
+ short y (static_cast<short> (v.year ()));
+ unsigned char m (static_cast<unsigned char> (v.month ()));
+ unsigned char d (static_cast<unsigned char> (v.day ()));
+
+ details::set_date (b, y, m, d, 0, 0, 0);
+ }
+ };
+ }
+}
+
+#endif // TRAITS_ORACLE_HXX
diff --git a/odb-examples/mapping/traits-pgsql.hxx b/odb-examples/mapping/traits-pgsql.hxx
new file mode 100644
index 0000000..aeefa52
--- /dev/null
+++ b/odb-examples/mapping/traits-pgsql.hxx
@@ -0,0 +1,118 @@
+// file : mapping/traits-pgsql.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef TRAITS_PGSQL_HXX
+#define TRAITS_PGSQL_HXX
+
+//
+// PostgreSQL implementation.
+//
+
+#include <cstddef> // std::size_t
+#include <cstring> // std::strncmp, std::memset, std::memcpy
+#include <ctime> // localtime, mktime, time_t, tm
+
+#include <odb/pgsql/traits.hxx>
+#include <odb/pgsql/details/endian-traits.hxx>
+
+#include "person.hxx" // date
+
+namespace odb
+{
+ namespace pgsql
+ {
+ template <>
+ class value_traits<bool, 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);
+ }
+ };
+
+ // Mapping of date to PostgreSQL DATE. DATE is stored as the number
+ // of days since the PostgreSQL epoch 2000-01-01.
+ //
+ template <>
+ class value_traits<date, id_date>
+ {
+ public:
+ typedef date value_type;
+ typedef date query_type;
+ typedef int image_type;
+
+ // The difference between the PostgreSQL epoch and the Unix epoch
+ // in seconds.
+ //
+ static const time_t epoch_diff = 946684800;
+
+ static const time_t seconds_per_day = 86400;
+
+ static void
+ set_value (date& v, const int& i, bool is_null)
+ {
+ if (is_null)
+ {
+ v = date (0, 0, 0);
+ return;
+ }
+
+ time_t v_tt (epoch_diff +
+ static_cast<time_t> (details::endian_traits::ntoh (i)) *
+ seconds_per_day);
+
+ // Assume that the date is specified as UTC. Use localtime so as
+ // to avoid any timeshift that could be introduced by the system
+ // time locale not being UTC.
+ //
+ tm v_tm (*localtime (&v_tt));
+
+ v = date (v_tm.tm_year + 1900, v_tm.tm_mon + 1, v_tm.tm_mday);
+ }
+
+ static void
+ set_image (int& i, bool& is_null, const date& v)
+ {
+ is_null = false;
+
+ tm v_tm;
+ std::memset (&v_tm, 0, sizeof (v_tm));
+
+ v_tm.tm_mday = v.day ();
+ v_tm.tm_mon = v.month () - 1;
+ v_tm.tm_year = v.year () - 1900;
+
+ time_t v_tt (mktime (&v_tm));
+
+ i = details::endian_traits::hton (
+ static_cast<int> ((v_tt - epoch_diff) / seconds_per_day));
+ }
+ };
+ }
+}
+
+#endif // TRAITS_PGSQL_HXX
diff --git a/odb-examples/mapping/traits-sqlite.hxx b/odb-examples/mapping/traits-sqlite.hxx
new file mode 100644
index 0000000..cd7afc3
--- /dev/null
+++ b/odb-examples/mapping/traits-sqlite.hxx
@@ -0,0 +1,127 @@
+// file : mapping/traits-sqlite.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef TRAITS_SQLITE_HXX
+#define TRAITS_SQLITE_HXX
+
+//
+// SQLite implementation.
+//
+
+#include <cstddef> // std::size_t
+#include <cstring> // std::strncmp, std::memcpy
+#include <sstream>
+
+#include <odb/sqlite/traits.hxx>
+
+#include "person.hxx" // date
+
+namespace odb
+{
+ namespace sqlite
+ {
+ template <>
+ class value_traits<bool, 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, 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/odb-examples/mapping/traits.hxx b/odb-examples/mapping/traits.hxx
new file mode 100644
index 0000000..533b2e3
--- /dev/null
+++ b/odb-examples/mapping/traits.hxx
@@ -0,0 +1,23 @@
+// file : mapping/traits.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef TRAITS_HXX
+#define TRAITS_HXX
+
+// Include one of the database system-specific traits implementations.
+//
+#if defined(DATABASE_MYSQL)
+# include "traits-mysql.hxx"
+#elif defined(DATABASE_SQLITE)
+# include "traits-sqlite.hxx"
+#elif defined(DATABASE_PGSQL)
+# include "traits-pgsql.hxx"
+#elif defined(DATABASE_ORACLE)
+# include "traits-oracle.hxx"
+#elif defined(DATABASE_MSSQL)
+# include "traits-mssql.hxx"
+#else
+# error unknown database; did you forget to define the DATABASE_* macros?
+#endif
+
+#endif // TRAITS_HXX