diff options
Diffstat (limited to 'odb-tests/mysql/types')
-rw-r--r-- | odb-tests/mysql/types/buildfile | 39 | ||||
-rw-r--r-- | odb-tests/mysql/types/driver.cxx | 168 | ||||
-rw-r--r-- | odb-tests/mysql/types/test.hxx | 336 | ||||
-rw-r--r-- | odb-tests/mysql/types/testscript | 11 | ||||
-rw-r--r-- | odb-tests/mysql/types/traits.hxx | 198 |
5 files changed, 752 insertions, 0 deletions
diff --git a/odb-tests/mysql/types/buildfile b/odb-tests/mysql/types/buildfile new file mode 100644 index 0000000..9065b7d --- /dev/null +++ b/odb-tests/mysql/types/buildfile @@ -0,0 +1,39 @@ +# file : mysql/types/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($mysql) "mysql 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-mysql%lib{odb-mysql} +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 mysql_types_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --hxx-prologue '#include "traits.hxx"' + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{mysql-client}: include = adhoc diff --git a/odb-tests/mysql/types/driver.cxx b/odb-tests/mysql/types/driver.cxx new file mode 100644 index 0000000..2354b04 --- /dev/null +++ b/odb-tests/mysql/types/driver.cxx @@ -0,0 +1,168 @@ +// file : mysql/types/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test MySQL type conversion. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace mysql = odb::mysql; +using namespace mysql; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + mysql_version v; + { + transaction t (db->begin ()); + db->query<mysql_version> ().begin ().load (v); + t.commit (); + } + + //cerr << "MySQL " << v.major << '.' << v.minor << '.' << v.release + // << " protocol " << v.protocol << endl; + + object o (1); + + o.bool_ = true; + o.schar_ = -123; + o.uchar_ = 123; + o.short_ = -12345; + o.ushort_ = 12345; + o.mint_ = -123456; + o.umint_ = 123456; + o.int_ = -123456; + o.uint_ = 123456; + o.long_long_ = -123456; + o.ulong_long_ = 123456; + + o.float_ = 1.123F; + o.float8_ = 1.123; + o.double_ = 1.123; + o.decimal_ = "123.456"; + + o.date_ = date_time (false, 2010, 8, 29, 0, 0, 0); + o.time_ = date_time (true, 0, 0, 0, 12, 26, 59); + o.date_time_ = date_time (false, 2010, 8, 29, 12, 26, 59); + o.timestamp_ = date_time (false, 2010, 8, 29, 12, 26, 59); + o.year_ = 2010; + + // If we are running against MySQL 5.6.4 or later, add fractional + // seconds and also alter the table to allow sub-second precision. + // + if (v.major > 5 || + (v.major == 5 && (v.minor > 6 || + (v.minor == 6 && v.release >= 4)))) + { + o.time_.microseconds = 123456; + o.date_time_.microseconds = 234567; + o.timestamp_.microseconds = 345678; + + transaction t (db->begin ()); + db->execute ("ALTER TABLE `mysql_types_object`" \ + " MODIFY COLUMN `time` TIME(6)," \ + " MODIFY COLUMN `date_time` DATETIME(6)," \ + " MODIFY COLUMN `timestamp` TIMESTAMP(6)"); + t.commit (); + } + + string short_str (128, 's'); + string medium_str (250, 'm'); + string long_str (2040, 'l'); + + const char* sb (short_str.c_str ()), *se (sb + short_str.size ()); + const char* mb (medium_str.c_str ()), *me (mb + medium_str.size ()); + const char* lb (long_str.c_str ()), *le (lb + long_str.size ()); + + o.char_ = short_str; + o.binary_.assign (sb, se); + o.varchar_ = medium_str; + o.varbinary_.assign (mb, me); + o.tinytext_ = short_str; + o.tinyblob_.assign (sb, se); + o.text_ = long_str; + o.blob_.assign (lb, le); + o.mediumtext_ = long_str; + o.mediumblob_.assign (lb, le); + o.longtext_ = long_str; + o.longblob_.assign (lb, le); + + o.bit_.a = 1; + o.bit_.b = 0; + o.bit_.c = 0; + o.bit_.d = 1; + + o.enum_def_ = green; + o.enum_cst_ = blue; + o.enum_str_ = "green"; + o.set_.insert ("green"); + o.set_.insert ("red"); + o.set_.insert ("blue"); + + { + 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); + } + + // Test char array. + // + { + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "1234567890123456"); + + { + 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/mysql/types/test.hxx b/odb-tests/mysql/types/test.hxx new file mode 100644 index 0000000..6a8527b --- /dev/null +++ b/odb-tests/mysql/types/test.hxx @@ -0,0 +1,336 @@ +// file : mysql/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 + +#include <odb/core.hxx> + +typedef std::vector<char> buffer; + +struct date_time +{ + date_time () + : negative (false), + year (0), + month (0), + day (0), + hour (0), + minute (0), + second (0), + microseconds (0) + { + } + + date_time (bool n, + unsigned int y, + unsigned int m, + unsigned int d, + unsigned int h, + unsigned int min, + unsigned int sec, + unsigned int msec = 0) + : negative (n), + year (y), + month (m), + day (d), + hour (h), + minute (min), + second (sec), + microseconds (msec) + { + } + + bool + operator== (const date_time& y) const + { + return + negative == y.negative && + year == y.year && + month == y.month && + day == y.day && + hour == y.hour && + minute == y.minute && + second == y.second && + microseconds == y.microseconds; + } + + bool negative; + unsigned int year; + unsigned int month; + unsigned int day; + unsigned int hour; + unsigned int minute; + unsigned int second; + unsigned int microseconds; +}; + +struct bitfield +{ + unsigned int a: 1; + unsigned int b: 1; + unsigned int c: 1; + unsigned int d: 1; +}; + +inline bool +operator== (bitfield x, bitfield y) +{ + return + x.a == y.a && + x.b == y.b && + x.c == y.c && + x.d == y.d; +} + +#pragma db value(bitfield) type ("BIT(4)") + +typedef std::set<std::string> set; +typedef std::unique_ptr<std::string> string_ptr; + +enum color {red, green, blue}; + +#pragma db object +struct object +{ + object () {} + object (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + // Integral types. + // + #pragma db type ("BOOL") + bool bool_; + + #pragma db type ("TINYINT") + signed char schar_; + + #pragma db type ("TINYINT UNSIGNED") + unsigned char uchar_; + + #pragma db type ("SMALLINT") + short short_; + + #pragma db type ("SMALLINT UNSIGNED") + unsigned short ushort_; + + #pragma db type ("MEDIUMINT") + int mint_; + + #pragma db type ("MEDIUMINT UNSIGNED") + unsigned int umint_; + + #pragma db type ("INT") + int int_; + + #pragma db type ("INT UNSIGNED") + unsigned int uint_; + + #pragma db type ("BIGINT") + long long long_long_; + + #pragma db type ("BIGINT UNSIGNED") + unsigned long long ulong_long_; + + // Float types. + // + #pragma db type ("FLOAT") + float float_; + + #pragma db type ("FLOAT(32)") + double float8_; + + #pragma db type ("DOUBLE") + double double_; + + #pragma db type ("DECIMAL(6,3)") + std::string decimal_; + + // Data-time types. + // + #pragma db type ("DATE") + date_time date_; + + #pragma db type ("TIME") + date_time time_; + + #pragma db type ("DATETIME") + date_time date_time_; + + #pragma db type ("TIMESTAMP") + date_time timestamp_; + + #pragma db type ("YEAR") + short year_; + + // String and binary types. + // + #pragma db type ("CHAR(128)") + std::string char_; + + #pragma db type ("BINARY(128)") + buffer binary_; + + #pragma db type ("VARCHAR(256)") + std::string varchar_; + + #pragma db type ("VARBINARY(256)") + buffer varbinary_; + + #pragma db type ("TINYTEXT") + std::string tinytext_; + + #pragma db type ("TINYBLOB") + buffer tinyblob_; + + #pragma db type ("TEXT") + std::string text_; + + #pragma db type ("BLOB") + buffer blob_; + + #pragma db type ("MEDIUMTEXT") + std::string mediumtext_; + + #pragma db type ("MEDIUMBLOB") + buffer mediumblob_; + + #pragma db type ("LONGTEXT") + std::string longtext_; + + #pragma db type ("LONGBLOB") + buffer longblob_; + + // Other types. + // + // #pragma db type ("BIT(4)") - assigned by #pragma db value + bitfield bit_; + + // Test ENUM representations (integer and string). + // + color enum_def_; + + // Map to a custom MySQL ENUM type. + // + #pragma db type ("ENUM('R', 'G', 'B')") + color enum_cst_; + + #pragma db type ("ENUM('red', 'green', 'blue')") + std::string enum_str_; + + #pragma db type ("SET('red', 'green', 'blue')") + set set_; + + // Test NULL value. + // + #pragma db type ("TEXT") null + string_ptr null_; + + bool + operator== (const object& y) const + { + return + id_ == y.id_ && + bool_ == y.bool_ && + schar_ == y.schar_ && + uchar_ == y.uchar_ && + short_ == y.short_ && + ushort_ == y.ushort_ && + mint_ == y.mint_ && + umint_ == y.umint_ && + int_ == y.int_ && + uint_ == y.uint_ && + long_long_ == y.long_long_ && + ulong_long_ == y.ulong_long_ && + float_ == y.float_ && + float8_ == y.float8_ && + double_ == y.double_ && + decimal_ == y.decimal_ && + date_ == y.date_ && + time_ == y.time_ && + date_time_ == y.date_time_ && + timestamp_ == y.timestamp_ && + year_ == y.year_ && + char_ == y.char_ && + binary_ == y.binary_ && + varchar_ == y.varchar_ && + varbinary_ == y.varbinary_ && + tinytext_ == y.tinytext_ && + tinyblob_ == y.tinyblob_ && + text_ == y.text_ && + blob_ == y.blob_ && + mediumtext_ == y.mediumtext_ && + mediumblob_ == y.mediumblob_ && + longtext_ == y.longtext_ && + longblob_ == y.longblob_ && + bit_ == y.bit_ && + enum_def_ == y.enum_def_ && + enum_cst_ == y.enum_cst_ && + enum_str_ == y.enum_str_ && + set_ == y.set_ && + ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_); + } +}; + +// Test char array. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s) + : id_ (id) + { + std::memcpy (s1, s, std::strlen (s) + 1); // VC++ strncpy deprecation. + std::memcpy (s2, s, std::strlen (s) + 1); + s3[0] = c1 = *s; + } + + #pragma db id + unsigned long id_; + + char s1[17]; + + #pragma db type("CHAR(16)") + char s2[16]; + + char s3[1]; + char c1; + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ && + std::strcmp (s1, y.s1) == 0 && + std::strncmp (s2, y.s2, sizeof (s2)) == 0 && + s3[0] == y.s3[0] && + c1 == y.c1; + } +}; + +// MySQL server version view. +// +#pragma db view query( \ + "SELECT " \ + "CAST(SUBSTRING_INDEX(@@version, '.', 1) AS UNSIGNED)," \ + "CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(@@version, '.', 2), '.', -1) AS UNSIGNED)," \ + "CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(@@version, '-', 1), '.', -1) AS UNSIGNED)," \ + "@@protocol_version") +struct mysql_version +{ + unsigned int major; + unsigned int minor; + unsigned int release; + + unsigned int protocol; +}; + +#endif // TEST_HXX diff --git a/odb-tests/mysql/types/testscript b/odb-tests/mysql/types/testscript new file mode 100644 index 0000000..2962d1c --- /dev/null +++ b/odb-tests/mysql/types/testscript @@ -0,0 +1,11 @@ +# file : mysql/types/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../mysql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/mysql/types/traits.hxx b/odb-tests/mysql/types/traits.hxx new file mode 100644 index 0000000..d4ce200 --- /dev/null +++ b/odb-tests/mysql/types/traits.hxx @@ -0,0 +1,198 @@ +// file : mysql/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/mysql/traits.hxx> + +#include "test.hxx" // date_time, string_ptr + +namespace odb +{ + namespace mysql + { + template <database_type_id ID> + class value_traits<date_time, ID> + { + public: + typedef date_time value_type; + typedef date_time query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (date_time& v, const MYSQL_TIME& i, bool is_null) + { + if (!is_null) + { + v.negative = i.neg; + v.year = i.year; + v.month = i.month; + v.day = i.day; + v.hour = i.hour; + v.minute = i.minute; + v.second = i.second; + v.microseconds = static_cast<unsigned int> (i.second_part); + } + else + v = date_time (); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const date_time& v) + { + is_null = false; + i.neg = v.negative; + i.year = v.year; + i.month = v.month; + i.day = v.day; + i.hour = v.hour; + i.minute = v.minute; + i.second = v.second; + i.second_part = v.microseconds; + } + }; + + template <> + class value_traits<bitfield, id_bit> + { + public: + typedef bitfield value_type; + typedef bitfield query_type; + typedef unsigned char* image_type; + + static void + set_value (bitfield& v, + const unsigned char* s, + std::size_t, + bool is_null) + { + if (!is_null) + { + v.a = *s & 1; + v.b = (*s >> 1) & 1; + v.c = (*s >> 2) & 1; + v.d = (*s >> 3) & 1; + } + else + v.a = v.b = v.c = v.d = 0; + } + + static void + set_image (unsigned char* s, + std::size_t, + std::size_t& n, + bool& is_null, + bitfield v) + { + is_null = false; + n = 1; + *s = v.a | (v.b << 1) | (v.c << 2) | (v.d << 3); + } + }; + + template <> + class value_traits<set, id_set> + { + public: + typedef set value_type; + typedef set query_type; + typedef details::buffer image_type; + + static void + set_value (set& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v.clear (); + + if (!is_null) + { + const char* s (b.data ()); + const char* e (s + n); + + while (s < e) + { + const char* p (s); + + while (p < e && *p != ',') + ++p; + + v.insert (std::string (s, p - s)); + s = p; + + if (p != e) + ++s; + } + } + } + + static void + set_image (details::buffer& buf, + std::size_t& n, + bool& is_null, + const set& v) + { + is_null = false; + n = 0; + + for (set::const_iterator b (v.begin ()), i (b); i != v.end (); ++i) + { + std::size_t m (i->size () + (i != b ? 1 : 0)); + + if (n + m > buf.capacity ()) + buf.capacity (n + m, n); + + if (i != b) + buf.data ()[n++] = ','; + + std::memcpy (buf.data () + n, i->c_str (), i->size ()); + n += i->size (); + } + } + }; + + template <> + class value_traits<string_ptr, id_string> + { + 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 |