From 89de275db2b77d0abf9fa1ec066ef11e262c88af Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 24 Jan 2013 15:10:22 +0200 Subject: Add support for mapping char[N] to CHAR/VARCHAR database types Also improve query support for arrays (decaying). --- common/query/array/driver.cxx | 170 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 common/query/array/driver.cxx (limited to 'common/query/array/driver.cxx') diff --git a/common/query/array/driver.cxx b/common/query/array/driver.cxx new file mode 100644 index 0000000..2bda0a9 --- /dev/null +++ b/common/query/array/driver.cxx @@ -0,0 +1,170 @@ +// file : common/query/array/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test query support for C arrays. +// + +#include +#include // std::auto_ptr +#include +#include + +#include +#include + +#include // DATABASE_* +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +#if defined(DATABASE_MYSQL) +const odb::mysql::database_type_id bt = odb::mysql::id_blob; +const odb::mysql::database_type_id st = odb::mysql::id_string; +#elif defined(DATABASE_SQLITE) +const odb::sqlite::database_type_id bt = odb::sqlite::id_blob; +const odb::sqlite::database_type_id st = odb::sqlite::id_text; +#elif defined(DATABASE_PGSQL) +const odb::pgsql::database_type_id bt = odb::pgsql::id_bytea; +const odb::pgsql::database_type_id st = odb::pgsql::id_string; +#elif defined(DATABASE_ORACLE) +const odb::oracle::database_type_id bt = odb::oracle::id_raw; +const odb::oracle::database_type_id st = odb::oracle::id_string; +#elif defined(DATABASE_MSSQL) +const odb::mssql::database_type_id bt = odb::mssql::id_binary; +const odb::mssql::database_type_id st = odb::mssql::id_string; +#else +# error unknown database +#endif + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + typedef odb::query query; + + const char buf[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}; + + // + // + { + object o1 (1, "abc", buf); + object o2 (2, "bcd", buf); + object o3 (3, "cde", buf); + + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + + // string + // + assert (size (db->query (query::s == "abc")) == 1); + assert (size (db->query (query::s == query::_val ("bcd"))) == 1); + assert (size (db->query ("s = " + query::_val ("bcd"))) == 1); + assert (size (db->query ("s = " + query::_ref ("bcd"))) == 1); + + { + char a[] = "bcd"; + char* ra = a; + assert (size (db->query (query::s == a)) == 1); + assert (size (db->query (query::s == query::_val (a))) == 1); + assert (size (db->query (query::s == query::_ref (ra))) == 1); + assert (size (db->query ("s = " + query::_val (a))) == 1); + assert (size (db->query ("s = " + query::_ref (a))) == 1); + } + + { + const char a[] = "bcd"; + const char* ra = a; + assert (size (db->query (query::s == a)) == 1); + assert (size (db->query (query::s == query::_val (a))) == 1); + assert (size (db->query (query::s == query::_ref (ra))) == 1); + assert (size (db->query ("s = " + query::_val (a))) == 1); + assert (size (db->query ("s = " + query::_ref (a))) == 1); + } + + { + const char* p = "cde"; + assert (size (db->query (query::s == p)) == 1); + assert (size (db->query (query::s == query::_val (p))) == 1); + assert (size (db->query (query::s == query::_ref (p))) == 1); + assert (size (db->query ("s = " + query::_val (p))) == 1); + assert (size (db->query ("s = " + query::_ref (p))) == 1); + } + + { + char a[] = "cde"; + char* p = a; + assert (size (db->query (query::s == p)) == 1); + assert (size (db->query (query::s == query::_val (p))) == 1); + assert (size (db->query (query::s == query::_ref (p))) == 1); + assert (size (db->query ("s = " + query::_val (p))) == 1); + assert (size (db->query ("s = " + query::_ref (p))) == 1); + } + + string s ("abc"); + //assert (size (db->query (query::s == s)) == 1); + assert (size (db->query (query::s == s.c_str ())) == 1); + //assert (size (db->query (query::s == query::_val (s))) == 1); + assert (size (db->query (query::s == query::_val (s.c_str ()))) == 1); + assert (size (db->query ("s = " + query::_val (s))) == 1); + assert (size (db->query ("s = " + query::_ref (s))) == 1); + + assert (size (db->query (query::s == query::s1)) == 3); + + // std::array + // +#ifdef ODB_CXX11 + array a; + strcpy (a.data (), "abc"); + + assert (size (db->query (query::a == a)) == 1); + assert (size (db->query (query::a == query::_val (a))) == 1); + assert (size (db->query (query::a == query::_ref (a))) == 1); + assert (size (db->query ("a = " + query::_val (a))) == 1); + assert (size (db->query ("a = " + query::_ref (a))) == 1); +#endif + + // char + // + assert (size (db->query (query::c == 'a')) == 1); + + char c ('b'); + assert (size (db->query (query::c == query::_val (c))) == 1); + assert (size (db->query (query::c == query::_ref (c))) == 1); + + assert (size (db->query ("c = " + query::_val ('c'))) == 1); + assert (size (db->query ("c = " + query::_ref (c))) == 1); + + assert (size (db->query (query::c == query::c1)) == 3); + + // buffer + // + assert (size (db->query (query::b == buf)) == 3); + assert (size (db->query (query::b == query::_val (buf))) == 3); + assert (size (db->query (query::b == query::_ref (buf))) == 3); + assert (size (db->query ("b = " + query::_val (buf))) == 3); + assert (size (db->query ("b = " + query::_ref (buf))) == 3); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} -- cgit v1.1