diff options
Diffstat (limited to 'odb-tests/sqlite/types')
-rw-r--r-- | odb-tests/sqlite/types/buildfile | 35 | ||||
-rw-r--r-- | odb-tests/sqlite/types/driver.cxx | 113 | ||||
-rw-r--r-- | odb-tests/sqlite/types/test.hxx | 125 | ||||
-rw-r--r-- | odb-tests/sqlite/types/testscript | 9 | ||||
-rw-r--r-- | odb-tests/sqlite/types/traits.hxx | 57 |
5 files changed, 339 insertions, 0 deletions
diff --git a/odb-tests/sqlite/types/buildfile b/odb-tests/sqlite/types/buildfile new file mode 100644 index 0000000..305ecba --- /dev/null +++ b/odb-tests/sqlite/types/buildfile @@ -0,0 +1,35 @@ +# file : sqlite/types/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($sqlite) "sqlite should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libodb = libodb%lib{odb} + +import libs = libodb-sqlite%lib{odb-sqlite} +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta} + +exe{driver}: libue{test-meta} $libs + +# Specify the ODB custom options to be used by the odb_compile ad hoc rule +# (see build/root.build for details). +# +odb_options = --table-prefix sqlitex_types_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --cxx-prologue '#include "traits.hxx"' + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/types/driver.cxx b/odb-tests/sqlite/types/driver.cxx new file mode 100644 index 0000000..b444d94 --- /dev/null +++ b/odb-tests/sqlite/types/driver.cxx @@ -0,0 +1,113 @@ +// file : sqlite/types/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test SQLite type conversion. +// + +#include <limits> // std::numeric_limits +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace sqlite = odb::sqlite; +using namespace sqlite; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + object o (1); + + o.bool_ = true; + o.integer_ = -123456; + o.real_ = 1.123; + o.nan_ = numeric_limits<double>::quiet_NaN (); + + string long_str (2040, 'l'); + + o.text_ = long_str; +#ifdef _WIN32 + o.wtext_ = L"t\x00C8st string"; +#endif + o.blob_.assign (long_str.c_str (), long_str.c_str () + long_str.size ()); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + +#ifdef _WIN32 + { + typedef sqlite::query<object> query; + typedef odb::result<object> result; + + transaction t (db->begin ()); + result r (db->query<object> (query::wtext == L"t\x00C8st string")); + assert (!r.empty ()); + t.commit (); + } +#endif + + // Test char/wchar_t arrays + // + { +#ifndef _WIN32 + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "12345678901234567"); +#else + char_array o1 (1, "", L""); + char_array o2 (2, "1234567890", L"123456789\x00C8"); + char_array o3 (3, "12345678901234567", L"1234567890123456\x00C8"); +#endif + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<char_array> p1 (db->load<char_array> (1)); + unique_ptr<char_array> p2 (db->load<char_array> (2)); + unique_ptr<char_array> p3 (db->load<char_array> (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/sqlite/types/test.hxx b/odb-tests/sqlite/types/test.hxx new file mode 100644 index 0000000..fe0b274 --- /dev/null +++ b/odb-tests/sqlite/types/test.hxx @@ -0,0 +1,125 @@ +// file : sqlite/types/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <set> +#include <string> +#include <vector> +#include <memory> // std::unique_ptr +#include <cstring> // std::memcpy, std::str[n]cmp, std::strlen + +#ifdef _WIN32 +# include <cwchar> // std::wcslen, std::wcs[n]cmp +#endif + +#include <odb/core.hxx> + +typedef std::unique_ptr<std::string> string_ptr; + +#pragma db object +struct object +{ + object () {} + object (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + #pragma db type("BOOL") + bool bool_; + + #pragma db type("INTEGER") + int integer_; + + #pragma db type("REAL") + double real_; + + double nan_; // Represented in SQLite as NULL. + + #pragma db type("TEXT") + std::string text_; + +#ifdef _WIN32 + std::wstring wtext_; +#endif + + #pragma db type("BLOB") + std::vector<char> blob_; + + // Test NULL value. + // + #pragma db type("TEXT") null + string_ptr null_; + + bool + operator== (const object& y) const + { + return id_ == y.id_ + && bool_ == y.bool_ + && integer_ == y.integer_ + && real_ == y.real_ + && nan_ != nan_ + && text_ == y.text_ +#ifdef _WIN32 + && wtext_ == y.wtext_ +#endif + && blob_ == y.blob_ + && ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_); + } +}; + +// Test char/wchar_t arrays. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id + , const char* s +#ifdef _WIN32 + , const wchar_t* ws +#endif + ) + : id_ (id) + { + std::memcpy (s1, s, std::strlen (s) + 1); // VC++ strncpy deprecation. + s2[0] = c1 = *s; + +#ifdef _WIN32 + std::memcpy (ws1, ws, (std::wcslen (ws) + 1) * sizeof (wchar_t)); + ws2[0] = wc1 = *ws; +#endif + } + + #pragma db id + unsigned long id_; + + char s1[17]; + char s2[1]; + char c1; + +#ifdef _WIN32 + wchar_t ws1[17]; + wchar_t ws2[1]; + wchar_t wc1; +#endif + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ + && std::strncmp (s1, y.s1, sizeof (s1)) == 0 + && s2[0] == y.s2[0] + && c1 == y.c1 +#ifdef _WIN32 + && std::wcsncmp (ws1, y.ws1, sizeof (ws1) / 2) == 0 + && ws2[0] == y.ws2[0] + && wc1 == y.wc1 +#endif + ; + } +}; + +#endif // TEST_HXX diff --git a/odb-tests/sqlite/types/testscript b/odb-tests/sqlite/types/testscript new file mode 100644 index 0000000..5e50d32 --- /dev/null +++ b/odb-tests/sqlite/types/testscript @@ -0,0 +1,9 @@ +# file : sqlite/types/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* diff --git a/odb-tests/sqlite/types/traits.hxx b/odb-tests/sqlite/types/traits.hxx new file mode 100644 index 0000000..9483f58 --- /dev/null +++ b/odb-tests/sqlite/types/traits.hxx @@ -0,0 +1,57 @@ +// file : sqlite/types/traits.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <cstring> // std::memcpy, std::memset + +#include <odb/sqlite/traits.hxx> + +#include "test.hxx" // string_ptr + +namespace odb +{ + namespace sqlite + { + template <> + class value_traits<string_ptr, id_text> + { + public: + typedef string_ptr value_type; + typedef std::string query_type; + typedef details::buffer image_type; + + static void + set_value (string_ptr& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v.reset (is_null ? 0 : new std::string (b.data (), n)); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const string_ptr& v) + { + is_null = v.get () == 0; + + if (!is_null) + { + n = v->size (); + + if (n > b.capacity ()) + b.capacity (n); + + if (n != 0) + std::memcpy (b.data (), v->c_str (), n); + } + } + }; + } +} + +#endif // TRAITS_HXX |