From b1a6710e7d396538617550f1d00b4db2575485c7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 22 Mar 2011 15:48:14 +0200 Subject: Implement built-in value traits for SQLite --- odb/sqlite/makefile | 1 + odb/sqlite/traits.cxx | 54 ++++++++++++ odb/sqlite/traits.hxx | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 285 insertions(+) create mode 100644 odb/sqlite/traits.cxx create mode 100644 odb/sqlite/traits.hxx (limited to 'odb') diff --git a/odb/sqlite/makefile b/odb/sqlite/makefile index 4f34741..f46587f 100644 --- a/odb/sqlite/makefile +++ b/odb/sqlite/makefile @@ -13,6 +13,7 @@ error.cxx \ exceptions.cxx \ statement-cache.cxx \ statement.cxx \ +traits.cxx \ transaction.cxx \ transaction-impl.cxx diff --git a/odb/sqlite/traits.cxx b/odb/sqlite/traits.cxx new file mode 100644 index 0000000..de6c792 --- /dev/null +++ b/odb/sqlite/traits.cxx @@ -0,0 +1,54 @@ +// file : odb/sqlite/traits.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include // std::memcpy, std::strlen + +#include + +using namespace std; + +namespace odb +{ + namespace sqlite + { + using details::buffer; + + // default_value_traits + // + void default_value_traits:: + set_image (buffer& b, + size_t& n, + bool& is_null, + const string& v) + { + is_null = false; + n = v.size (); + + if (n > b.capacity ()) + b.capacity (n); + + if (n != 0) + memcpy (b.data (), v.c_str (), n); + } + + // default_value_traits + // + void default_value_traits:: + set_image (buffer& b, + size_t& n, + bool& is_null, + const char* v) + { + is_null = false; + n = strlen (v); + + if (n > b.capacity ()) + b.capacity (n); + + if (n != 0) + memcpy (b.data (), v, n); + } + } +} diff --git a/odb/sqlite/traits.hxx b/odb/sqlite/traits.hxx new file mode 100644 index 0000000..e5ef318 --- /dev/null +++ b/odb/sqlite/traits.hxx @@ -0,0 +1,230 @@ +// file : odb/sqlite/traits.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_TRAITS_HXX +#define ODB_SQLITE_TRAITS_HXX + +#include + +#include +#include // std::size_t + +#include +#include + +#include +#include + +namespace odb +{ + namespace sqlite + { + enum database_type_id + { + id_integer, + id_real, + id_text, + id_blob + }; + + // + // value_traits + // + + template + struct default_value_traits; + + template + class value_traits: public default_value_traits + { + }; + + template + struct default_value_traits + { + typedef T value_type; + typedef T query_type; + typedef I image_type; + + static void + set_value (T& v, I i, bool is_null) + { + if (!is_null) + v = T (i); + else + v = T (); + } + + static void + set_image (I& i, bool& is_null, T v) + { + is_null = false; + i = I (v); + } + }; + + // std::string specialization. + // + template <> + struct LIBODB_SQLITE_EXPORT default_value_traits< + std::string, details::buffer, id_text> + { + typedef std::string value_type; + typedef std::string query_type; + typedef details::buffer image_type; + + static void + set_value (std::string& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (!is_null) + v.assign (b.data (), n); + else + v.erase (); + } + + static void + set_image (details::buffer&, + std::size_t& n, + bool& is_null, + const std::string&); + }; + + // const char* specialization + // + // Specialization for const char* which only supports initialization + // of an image from the value but not the other way around. This way + // we can pass such values to the queries. + // + template <> + struct LIBODB_SQLITE_EXPORT default_value_traits< + const char*, details::buffer, id_text> + { + typedef const char* value_type; + typedef const char* query_type; + typedef details::buffer image_type; + + static void + set_image (details::buffer&, + std::size_t& n, + bool& is_null, + const char*); + }; + + // + // type_traits + // + + template + struct default_type_traits; + + template + class type_traits: public default_type_traits + { + }; + + // Integral types. + // + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_integer; + }; + + // Float types. + // + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_real; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_real; + }; + + // String type. + // + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_text; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_text; + }; + } +} + +#include + +#endif // ODB_SQLITE_TRAITS_HXX -- cgit v1.1