From 534c5958af6104aea60a9fcb0d823a5c3cfed1bc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 25 Mar 2011 13:09:06 +0200 Subject: SQLite support --- mapping/traits-sqlite.hxx | 127 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 mapping/traits-sqlite.hxx (limited to 'mapping/traits-sqlite.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 -- cgit v1.1