diff options
Diffstat (limited to 'odb-tests/pgsql')
30 files changed, 2201 insertions, 0 deletions
diff --git a/odb-tests/pgsql/buildfile b/odb-tests/pgsql/buildfile new file mode 100644 index 0000000..c6385c0 --- /dev/null +++ b/odb-tests/pgsql/buildfile @@ -0,0 +1,6 @@ +# file : pgsql/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +./: {*/ -bulk/} + +./: bulk/: include = ($pgsql && !$multi && $pgsql_bulk) diff --git a/odb-tests/pgsql/bulk/buildfile b/odb-tests/pgsql/bulk/buildfile new file mode 100644 index 0000000..af0bc7f --- /dev/null +++ b/odb-tests/pgsql/bulk/buildfile @@ -0,0 +1,42 @@ +# file : pgsql/bulk/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($pgsql) "pgsql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" + assert ($pgsql_bulk) "bulk operations are disabled for pgsql" +} + +import libodb = libodb%lib{odb} + +import libs = libpq%lib{pq} +import libs += libodb-pgsql%lib{odb-pgsql} +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 pgsql_bulk_ \ + --generate-schema \ + --generate-query + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +if ($cxx.target.class == 'windows') + cxx.libs += ($cxx.target.system == 'mingw32' ? -lws2_32 : ws2_32.lib) + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{pgsql-client}: include = adhoc diff --git a/odb-tests/pgsql/bulk/driver.cxx b/odb-tests/pgsql/bulk/driver.cxx new file mode 100644 index 0000000..61e7d56 --- /dev/null +++ b/odb-tests/pgsql/bulk/driver.cxx @@ -0,0 +1,373 @@ +// file : pgsql/bulk/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test transaction savepoints. +// + +#include <libpq-fe.h> + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stddef.h> + +#ifndef _WIN32 +# include <sys/select.h> +# include <arpa/inet.h> +#else +# include <winsock2.h> +#endif + +// Note: hack (also, some platforms, like Mac OS, provide it). +// +#if !defined(_MSC_VER) && !defined(htonll) +# define htonll(x) ((((long long)htonl(x)) << 32) + htonl((x) >> 32)) +#endif + +#undef NDEBUG +#include <assert.h> + +static const size_t columns = 3; + +struct data +{ + long long id; + long long idata; + const char* sdata; +}; + +static char* values[columns]; +static int lengths[columns]; +static int formats[columns] = {1, 1, 1}; + +static const unsigned int types[columns] = { + 20, // int8 + 20, // int8 + 25 // text +}; + +static void +init (const struct data* d) +{ + values[0] = (char*)&d->id; + lengths[0] = sizeof (d->id); + + values[1] = (char*)&d->idata; + lengths[1] = sizeof (d->idata); + + values[2] = (char*)d->sdata; + lengths[2] = (int)strlen (d->sdata); +} + +static void +execute (PGconn* conn, const struct data* ds, size_t n) +{ + int sock = PQsocket (conn); + assert (sock != -1); + + if (PQsetnonblocking (conn, 1) == -1 || + PQenterPipelineMode (conn) == 0) + assert (false); + + // True if we've written and read everything, respectively. + // + bool wdone = false; + bool rdone = false; + + size_t wn = 0; + size_t rn = 0; + + while (!rdone) + { + fd_set wds; + if (!wdone) + { + FD_ZERO (&wds); + FD_SET (sock, &wds); + } + + fd_set rds; + FD_ZERO (&rds); + FD_SET (sock, &rds); + +#ifndef _WIN32 + if (select (sock + 1, &rds, wdone ? NULL : &wds, NULL, NULL) == -1) + { + if (errno == EINTR) + continue; + + assert (false); + } +#else + if (select (sock + 1, &rds, wdone ? NULL : &wds, NULL, NULL) == SOCKET_ERROR) + assert (false); +#endif + + // Try to minimize the chance of blocking the server by first processing + // the result and then sending more queries. + // + if (FD_ISSET (sock, &rds)) + { + if (PQconsumeInput (conn) == 0) + assert (false); + + while (wn > rn && PQisBusy (conn) == 0) + { + //fprintf (stderr, "PQgetResult %zu\n", rn); + + PGresult* res = PQgetResult (conn); + assert (res != NULL); + ExecStatusType stat = PQresultStatus (res); + + if (stat == PGRES_PIPELINE_SYNC) + { + assert (wdone && rn == n); + PQclear (res); + rdone = true; + break; + } + + if (stat == PGRES_FATAL_ERROR) + { + const char* s = PQresultErrorField (res, PG_DIAG_SQLSTATE); + + if (strcmp (s, "23505") == 0) + fprintf (stderr, "duplicate id at %zu\n", rn); + } + + PQclear (res); + assert (rn != n); + ++rn; + + // We get a NULL result after each query result. + // + { + PGresult* end = PQgetResult (conn); + assert (end == NULL); + } + } + } + + if (!wdone && FD_ISSET (sock, &wds)) + { + // Send queries until we get blocked (write-biased). This feels like + // a better overall strategy to keep the server busy compared to + // sending one query at a time and then re-checking if there is + // anything to read because the results of INSERT/UPDATE/DELETE are + // presumably small and quite a few of them can get buffered before + // the server gets blocked. + // + for (;;) + { + if (wn < n) + { + //fprintf (stderr, "PQsendQueryPrepared %zu\n", wn); + + init (ds + wn); + + if (PQsendQueryPrepared (conn, + "persist_object", + (int)(columns), + values, + lengths, + formats, + 1) == 0) + assert (false); + + if (++wn == n) + { + if (PQpipelineSync (conn) == 0) + assert (false); + + ++wn; + } + } + + // PQflush() result: + // + // 0 -- success (queue is now empty) + // 1 -- blocked + // -1 -- error + // + int r = PQflush (conn); + assert (r != -1); + + if (r == 0) + { + if (wn < n) + { + // If we continue here, then we are write-biased. And if we + // break, then we are read-biased. + // +#if 0 + break; +#else + continue; +#endif + } + + wdone = true; + } + + break; // Blocked or done. + } + } + } + + if (PQexitPipelineMode (conn) == 0 || + PQsetnonblocking (conn, 0) == -1) + assert (false); +} + +static void +test (PGconn* conn) +{ + const size_t batch = 500; + struct data ds[batch]; + + for (size_t i = 0; i != batch; ++i) + { + ds[i].id = htonll (i == batch / 2 ? i - 1 : i); // Cause duplicate PK. + ds[i].idata = htonll (i); + ds[i].sdata = "abc"; + } + + // Prepare the statement. + // + { + PGresult* res = PQprepare ( + conn, + "persist_object", + "INSERT INTO \"pgsql_bulk_object\" " + "(\"id\", " + "\"idata\", " + "\"sdata\") " + "VALUES " + "($1, $2, $3)", + (int)(columns), + types); + assert (PQresultStatus (res) == PGRES_COMMAND_OK); + PQclear (res); + } + + // Begin transaction. + // + { + PGresult* res = PQexec (conn, "begin"); + assert (PQresultStatus (res) == PGRES_COMMAND_OK); + PQclear (res); + } + + execute (conn, ds, batch); + + // Commit transaction. + // + { + PGresult* res = PQexec (conn, "commit"); + assert (PQresultStatus (res) == PGRES_COMMAND_OK); + PQclear (res); + } +} + +#include <vector> +#include <memory> // std::unique_ptr +#include <cstring> +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + bool fail_already_persistent (false); + + for (int i (1); i != argc; ++i) + { + if (strcmp (argv[i], "--fail-already-persistent") == 0) + { + fail_already_persistent = true; + + for (; i != argc - 1; ++i) + argv[i] = argv[i + 1]; + + --argc; + + break; + } + } + + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + connection_ptr cn (db->connection ()); + + if (false) + { + PGconn* conn (cn->handle ()); + test (conn); + } + + { + const unsigned long n (500); + + vector<object> os; + + for (unsigned long i (0); i != n; ++i) + { + os.push_back (object {i, i, string (i, 'x')}); + + if (fail_already_persistent && i == n / 2) + os.push_back (object {i, i, to_string (i)}); + } + + { + transaction t (cn->begin ()); + db->persist (os.begin (), os.end ()); + t.commit (); + } + + { + transaction t (cn->begin ()); + db->find<object> (2); + t.commit (); + } + + for (unsigned long i (0); i != n; ++i) + { + //assert (os[i].id == i + 1); + os[i].idata++; + } + + { + transaction t (cn->begin ()); + db->update (os.begin (), os.end ()); + t.commit (); + } + + { + transaction t (cn->begin ()); + db->erase (os.begin (), os.end ()); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/pgsql/bulk/test.hxx b/odb-tests/pgsql/bulk/test.hxx new file mode 100644 index 0000000..25dd138 --- /dev/null +++ b/odb-tests/pgsql/bulk/test.hxx @@ -0,0 +1,34 @@ +// file : pgsql/savepoint/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object bulk(1000) +struct object +{ + /* + object (unsigned long id) + : id_ (id) + { + } + + object () + { + } + */ + + #pragma db id //auto + unsigned long id; + + unsigned long idata; + + //#pragma db + std::string sdata; +}; + +#endif // TEST_HXX diff --git a/odb-tests/pgsql/bulk/testscript b/odb-tests/pgsql/bulk/testscript new file mode 100644 index 0000000..60b7d92 --- /dev/null +++ b/odb-tests/pgsql/bulk/testscript @@ -0,0 +1,18 @@ +# file : pgsql/custom/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../pgsql.testscript + ++$create_schema + +: basics +: +{ + $*; + + $* --fail-already-persistent 2>>EOE != 0 + multiple exceptions, 252 elements attempted, 1 failed, fatal: + [251] object already persistent + EOE +} diff --git a/odb-tests/pgsql/custom/buildfile b/odb-tests/pgsql/custom/buildfile new file mode 100644 index 0000000..b3032fa --- /dev/null +++ b/odb-tests/pgsql/custom/buildfile @@ -0,0 +1,40 @@ +# file : pgsql/custom/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($pgsql) "pgsql 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-pgsql%lib{odb-pgsql} +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 pgsql_custom_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --hxx-prologue '#include "traits.hxx"' \ + --hxx-prologue '#include "query.hxx"' + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{pgsql-client}: include = adhoc diff --git a/odb-tests/pgsql/custom/driver.cxx b/odb-tests/pgsql/custom/driver.cxx new file mode 100644 index 0000000..91ff0ce --- /dev/null +++ b/odb-tests/pgsql/custom/driver.cxx @@ -0,0 +1,125 @@ +// file : pgsql/custom/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test custom database type mapping in PostgreSQL. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + object o (1); + o.p = point (1.1111, 2222222222.2); + o.pv.push_back (point (1.1234, 2.2345)); + o.pv.push_back (point (3.3456, 4.4567)); + o.pv.push_back (point (0.0000001, 0.000000001)); // Scientific notation. + + o.n1 = "23.5154"; + o.n2 = "235154"; + o.n3 = "2222222222222222222222222222.111111111111111111111111111111"; + + o.iv.push_back (123); + o.iv.push_back (234); + o.iv.push_back (-345); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Query. + // + typedef pgsql::query<object> query; + typedef odb::result<object> result; + + { + transaction t (db->begin ()); + + // Point comparison. + // + { + result r (db->query<object> (query::p == o.p)); + assert (!r.empty ()); + } + + // Point comparison using native query. + // + { + result r (db->query<object> (query::p + "~=" + query::_val (o.p))); + assert (!r.empty ()); + } + + // Access to individual members. + // + { + result r (db->query<object> (query::p.x == o.p.x)); + assert (!r.empty ()); + } + + t.commit (); + } + + // Update. + // + o.p.x++; + o.p.y--; + o.pv[1].x--; + o.pv[1].y++; + o.n3 += "999"; + o.iv[0]++; + o.iv.pop_back (); + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/pgsql/custom/query.hxx b/odb-tests/pgsql/custom/query.hxx new file mode 100644 index 0000000..8b89047 --- /dev/null +++ b/odb-tests/pgsql/custom/query.hxx @@ -0,0 +1,158 @@ +// file : pgsql/custom/query.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef QUERY_HXX +#define QUERY_HXX + +#include <string> + +#include <odb/pgsql/query.hxx> + +#include "test.hxx" // point + +namespace odb +{ + namespace pgsql + { + template <> + struct query_column<point, id_string> + { + private: + const char* table_; + const char* column_; + const char* conversion_; + + std::string x_column_; + std::string y_column_; + + // Sub-columns for individual members. + // + public: + query_column<double, id_double> x, y; + + // is_null, is_not_null + // + public: + query_base + is_null () const + { + query_base q (table_, column_); + q += "IS NULL"; + return q; + } + + query_base + is_not_null () const + { + query_base q (table_, column_); + q += "IS NOT NULL"; + return q; + } + + // = + // + public: + query_base + equal (const point& v) const + { + return equal (val_bind<point> (v)); + } + + query_base + equal (val_bind<point> v) const + { + query_base q (table_, column_); + q += "~="; + q.append<point, id_string> (v, conversion_); + return q; + } + + query_base + equal (ref_bind<point> r) const + { + query_base q (table_, column_); + q += "~="; + q.append<point, id_string> (r, conversion_); + return q; + } + + friend query_base + operator== (const query_column& c, const point& v) + { + return c.equal (v); + } + + friend query_base + operator== (const point& v, const query_column& c) + { + return c.equal (v); + } + + friend query_base + operator== (const query_column& c, val_bind<point> v) + { + return c.equal (v); + } + + friend query_base + operator== (val_bind<point> v, const query_column& c) + { + return c.equal (v); + } + + friend query_base + operator== (const query_column& c, ref_bind<point> r) + { + return c.equal (r); + } + + friend query_base + operator== (ref_bind<point> r, const query_column& c) + { + return c.equal (r); + } + + // Column comparison. + // + public: + query_base + operator== (const query_column<point, id_string>& c) const + { + query_base q (table_, column_); + q += "~="; + q.append (c.table (), c.column ()); + return q; + } + + public: + query_column (const char* table, const char* column, const char* conv) + : table_ (table), column_ (column), conversion_ (conv), + x_column_ (std::string (column) + "[0]"), + y_column_ (std::string (column) + "[1]"), + x (table, x_column_.c_str (), 0), + y (table, y_column_.c_str (), 0) + { + } + + const char* + table () const + { + return table_; + } + + const char* + column () const + { + return column_; + } + + const char* + conversion () const + { + return conversion_; + } + }; + } +} + +#endif // QUERY_HXX diff --git a/odb-tests/pgsql/custom/test.hxx b/odb-tests/pgsql/custom/test.hxx new file mode 100644 index 0000000..ec3d496 --- /dev/null +++ b/odb-tests/pgsql/custom/test.hxx @@ -0,0 +1,86 @@ +// file : pgsql/custom/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> + +// Map POINT PostgreSQL type to the point C++ struct. The other half +// of this mapping is in traits.hxx (value_traits<point, id_string>). +// +#pragma db map type("POINT") as("TEXT") to("(?)::POINT") from("(?)::TEXT") + +#pragma db value type("POINT") +struct point +{ + point () {} + point (double x_, double y_): x (x_), y (y_) {} + + double x; + double y; +}; + +inline bool +operator== (const point& a, const point& b) +{ + return a.x == b.x && a.y == b.y; +} + +// Map NUMERIC PostgreSQL type to std::string (or any other type that +// provides the value_traits<?, id_string> specialization). +// +#pragma db map type("NUMERIC *(\\(.+\\))?") \ + as("TEXT") \ + to("(?)::NUMERIC$1") \ + from("(?)::TEXT") + +// Map INTEGER[] PostgreSQL type to std::vector<int>. The other half of +// this mapping is in traits.hxx (value_traits<std::vector<int>, id_string>). +// +#pragma db map type("INTEGER *\\[(\\d*)\\]") \ + as("TEXT") \ + to("(?)::INTEGER[$1]") \ + from("(?)::TEXT") + +#pragma db object +struct object +{ + object () {} + object (unsigned long id_) : id (id_) {} + + #pragma db id + unsigned long id; + + point p; + std::vector<point> pv; + + #pragma db type("NUMERIC(6, 4)") + std::string n1; + + #pragma db type("NUMERIC(6)") + std::string n2; + + #pragma db type("NUMERIC") + std::string n3; + + #pragma db type("INTEGER [123]") + std::vector<int> iv; + + bool + operator== (const object& y) const + { + return id == y.id && + p == y.p && + pv == y.pv && + n1 == y.n1 && + n2 == y.n2 && + n3 == y.n3 && + iv == y.iv; + } +}; + +#endif // TEST_HXX diff --git a/odb-tests/pgsql/custom/testscript b/odb-tests/pgsql/custom/testscript new file mode 100644 index 0000000..8e1448b --- /dev/null +++ b/odb-tests/pgsql/custom/testscript @@ -0,0 +1,11 @@ +# file : pgsql/custom/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../pgsql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/pgsql/custom/traits.hxx b/odb-tests/pgsql/custom/traits.hxx new file mode 100644 index 0000000..c45dec0 --- /dev/null +++ b/odb-tests/pgsql/custom/traits.hxx @@ -0,0 +1,166 @@ +// file : pgsql/custom/traits.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <limits> // std::numeric_limits +#include <vector> +#include <sstream> +#include <cstring> // std::memcpy + +#include <odb/pgsql/traits.hxx> + +#include "test.hxx" // point + +namespace odb +{ + namespace pgsql + { + template <> + class value_traits<point, id_string> + { + public: + typedef point value_type; + typedef point query_type; + typedef details::buffer image_type; + + static void + set_value (point& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (is_null) + v = point (); + else + { + // Point format is "(x,y)". + // + char c; + std::istringstream is (std::string (b.data (), n)); + + is >> c; // '(' + is >> v.x; + is >> c; // ',' + is >> v.y; + } + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const point& v) + { + is_null = false; + std::ostringstream os; + + // The formula for the number of decimla digits required is given in: + // + // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf + // + os.precision (std::numeric_limits<double>::digits10); + // os.precision (2 + std::numeric_limits<double>::digits * 301/1000); + + os << '(' << v.x << ',' << v.y << ')'; + + const std::string& s (os.str ()); + n = s.size (); + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), s.c_str (), n); + } + }; + + template <> + struct type_traits<point> + { + static const database_type_id db_type_id = id_string; + + struct conversion + { + static const char* to () {return "(?)::POINT";} + }; + }; + + template <> + class value_traits<std::vector<int>, id_string> + { + public: + typedef std::vector<int> value_type; + typedef value_type query_type; + typedef details::buffer image_type; + + static void + set_value (value_type& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v.clear (); + + if (!is_null) + { + // Array format is "{n1,n2,n3...}". + // + char c; + std::istringstream is (std::string (b.data (), n)); + + is >> c; // '{' + + for (c = static_cast<char> (is.peek ()); c != '}'; is >> c) + { + v.push_back (int ()); + is >> v.back (); + } + } + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + std::ostringstream os; + + os << '{'; + + for (value_type::const_iterator i (v.begin ()), e (v.end ()); i != e;) + { + os << *i; + + if (++i != e) + os << ','; + } + + os << '}'; + + const std::string& s (os.str ()); + n = s.size (); + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), s.c_str (), n); + } + }; + + template <> + struct type_traits<std::vector<int> > + { + static const database_type_id db_type_id = id_string; + + struct conversion + { + static const char* to () {return "(?)::INTEGER[]";} + }; + }; + } +} + +#endif // TRAITS_HXX diff --git a/odb-tests/pgsql/database/buildfile b/odb-tests/pgsql/database/buildfile new file mode 100644 index 0000000..217cc1f --- /dev/null +++ b/odb-tests/pgsql/database/buildfile @@ -0,0 +1,14 @@ +# file : pgsql/database/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($pgsql) "pgsql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libs = libodb-pgsql%lib{odb-pgsql} + +exe{driver}: {hxx cxx}{*} $libs testscript + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/pgsql/database/driver.cxx b/odb-tests/pgsql/database/driver.cxx new file mode 100644 index 0000000..56bde0a --- /dev/null +++ b/odb-tests/pgsql/database/driver.cxx @@ -0,0 +1,44 @@ +// file : pgsql/database/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test that database constructors are unambiguous (compilation only). +// + +#include <odb/pgsql/database.hxx> + +#undef NDEBUG +#include <cassert> + +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + // This code should not execute. + // + if (argc != 0) + return 0; + + { + database d1 ("bob", "secret", "db1"); + database d2 ("bob", "secret", "db1", "server1"); + database d3 ("bob", "secret", "db1", "server1", 999); + database d4 ("bob", "secret", "db1", "server1", 999, "extra"); + } + + { + database d1 ("bob", "secret", "db1", "server1", "ext1"); + database d2 ("bob", "secret", "db1", "server1", "ext1", "extra"); + } + + { + database d1 ("conninfo"); + } + + { + database d1 (argc, argv); + database d2 (argc, argv, false); + database d3 (argc, argv, true, "extra"); + } +} diff --git a/odb-tests/pgsql/database/testscript b/odb-tests/pgsql/database/testscript new file mode 100644 index 0000000..f57a8e1 --- /dev/null +++ b/odb-tests/pgsql/database/testscript @@ -0,0 +1,6 @@ +# file : pgsql/database/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +: basics +: +$* diff --git a/odb-tests/pgsql/index/buildfile b/odb-tests/pgsql/index/buildfile new file mode 100644 index 0000000..1ae888c --- /dev/null +++ b/odb-tests/pgsql/index/buildfile @@ -0,0 +1,37 @@ +# file : pgsql/index/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($pgsql) "pgsql 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-pgsql%lib{odb-pgsql} +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 pgsql_index_ \ + --generate-schema \ + --default-database common + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{pgsql-client}: include = adhoc diff --git a/odb-tests/pgsql/index/driver.cxx b/odb-tests/pgsql/index/driver.cxx new file mode 100644 index 0000000..dc4c11e --- /dev/null +++ b/odb-tests/pgsql/index/driver.cxx @@ -0,0 +1,44 @@ +// file : pgsql/index/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test PostgreSQL index creation. See also the common test. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + try + { + // This is just a schema creation test. + // + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + { + transaction t (db->begin ()); + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/pgsql/index/test.hxx b/odb-tests/pgsql/index/test.hxx new file mode 100644 index 0000000..d728188 --- /dev/null +++ b/odb-tests/pgsql/index/test.hxx @@ -0,0 +1,19 @@ +// file : pgsql/index/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + #pragma db id auto + unsigned long id_; + + int i; + #pragma db index type("UNIQUE CONCURRENTLY") method("BTREE") member(i) +}; + +#endif // TEST_HXX diff --git a/odb-tests/pgsql/index/testscript b/odb-tests/pgsql/index/testscript new file mode 100644 index 0000000..671a8c3 --- /dev/null +++ b/odb-tests/pgsql/index/testscript @@ -0,0 +1,11 @@ +# file : pgsql/index/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../pgsql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/pgsql/native/buildfile b/odb-tests/pgsql/native/buildfile new file mode 100644 index 0000000..da034db --- /dev/null +++ b/odb-tests/pgsql/native/buildfile @@ -0,0 +1,19 @@ +# file : pgsql/native/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($pgsql) "pgsql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libs = libodb-pgsql%lib{odb-pgsql} +import libs += lib{common} + +exe{driver}: {hxx cxx}{*} $libs testscript + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{pgsql-client}: include = adhoc diff --git a/odb-tests/pgsql/native/driver.cxx b/odb-tests/pgsql/native/driver.cxx new file mode 100644 index 0000000..843db41 --- /dev/null +++ b/odb-tests/pgsql/native/driver.cxx @@ -0,0 +1,75 @@ +// file : pgsql/native/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test PostgreSQL native SQL execution. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <libcommon/common.hxx> + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + // Create the database schema. + // + { + transaction t (db->begin ()); + + db->execute ("DROP TABLE IF EXISTS pgsql_native_test"); + db->execute ("CREATE TABLE pgsql_native_test (n INT PRIMARY KEY)"); + + t.commit (); + } + + // Insert a few rows. + // + { + transaction t (db->begin ()); + + assert ( + db->execute ("INSERT INTO pgsql_native_test (n) VALUES (1)") == 1); + + assert ( + db->execute ("INSERT INTO pgsql_native_test (n) VALUES (2)") == 1); + + t.commit (); + } + + // select a few rows. + // + { + transaction t (db->begin ()); + + assert ( + db->execute ("SELECT n FROM pgsql_native_test WHERE n < 3") == 2); + + assert ( + db->execute ("SELECT n FROM pgsql_native_test WHERE n > 3") == 0); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } + + return 0; +} diff --git a/odb-tests/pgsql/native/testscript b/odb-tests/pgsql/native/testscript new file mode 100644 index 0000000..4fd9d2c --- /dev/null +++ b/odb-tests/pgsql/native/testscript @@ -0,0 +1,9 @@ +# file : pgsql/custom/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../pgsql.testscript + +: basics +: +$* diff --git a/odb-tests/pgsql/truncation/buildfile b/odb-tests/pgsql/truncation/buildfile new file mode 100644 index 0000000..4f3823f --- /dev/null +++ b/odb-tests/pgsql/truncation/buildfile @@ -0,0 +1,38 @@ +# file : pgsql/truncation/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($pgsql) "pgsql 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-pgsql%lib{odb-pgsql} +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 pgsql_truncation_ \ + --generate-schema \ + --default-database common \ + --generate-query + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{pgsql-client}: include = adhoc diff --git a/odb-tests/pgsql/truncation/driver.cxx b/odb-tests/pgsql/truncation/driver.cxx new file mode 100644 index 0000000..337a26e --- /dev/null +++ b/odb-tests/pgsql/truncation/driver.cxx @@ -0,0 +1,163 @@ +// file : pgsql/truncation/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test insufficient buffer/truncation handling. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + // The default pre-allocated buffer is 512 bytes long. + // + string long_str (640, 'c'); // This will get the buffer to 1024 + string longer_str (1025, 'b'); + + try + { + // Test basic operations. + // + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + // Run persist/load so that the initial bindings are established + // (version == 0). + // + { + object1 o (1); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + db->load (1, o); + t.commit (); + } + + { + object2 o (2); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + db->load (2, o); + t.commit (); + } + + // Store/load the long string which should trigger buffer growth. + // + { + object1 o (3); + o.str_ = long_str; + + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object2> o (db->load<object2> (3)); + assert (o->str_ == long_str); + t.commit (); + } + + // Store/load longer string. + // + { + object1 o (3); + o.str_ = longer_str; + + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object2> o (db->load<object2> (3)); + assert (o->str_ == longer_str); + t.commit (); + } + } + + // Test query. + // + { + typedef pgsql::query<object1> query; + typedef odb::result<object1> result; + + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + // Run persist/query so that the initial bindings are established + // (version == 0). + // + { + object1 o (20); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + o.id_++; + db->persist (o); + o.id_++; + db->persist (o); + + result r (db->query<object1> (query::id == 20)); + assert (r.begin ()->id_ == 20); + t.commit (); + } + + // Test buffer growth with cached result. + // + { + object1 o; + + transaction t (db->begin ()); + + result r (db->query<object1> (query::id >= 20)); + result::iterator i (r.begin ()); + + o.id_ = i->id_; + o.str_ = long_str; + + // This forces buffer growth in the middle of result iteration. + // + db->update (o); + + ++i; + assert (i->str_ == "test string"); + + o.id_ = i->id_; + o.str_ = longer_str; + db->update (o); + + ++i; + assert (i->str_ == "test string"); + + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/pgsql/truncation/test.hxx b/odb-tests/pgsql/truncation/test.hxx new file mode 100644 index 0000000..73110c0 --- /dev/null +++ b/odb-tests/pgsql/truncation/test.hxx @@ -0,0 +1,46 @@ +// file : pgsql/truncation/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <odb/core.hxx> + +#pragma db object table ("test") +struct object1 +{ + object1 (unsigned long id) + : id_ (id) + { + } + + object1 () + { + } + + #pragma db id + unsigned long id_; + + std::string str_; +}; + +#pragma db object table ("test") +struct object2 +{ + object2 (unsigned long id) + : id_ (id) + { + } + + object2 () + { + } + + #pragma db id + unsigned long id_; + + std::string str_; +}; + +#endif // TEST_HXX diff --git a/odb-tests/pgsql/truncation/testscript b/odb-tests/pgsql/truncation/testscript new file mode 100644 index 0000000..c57b723 --- /dev/null +++ b/odb-tests/pgsql/truncation/testscript @@ -0,0 +1,11 @@ +# file : pgsql/truncation/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../pgsql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/pgsql/types/buildfile b/odb-tests/pgsql/types/buildfile new file mode 100644 index 0000000..cb861cb --- /dev/null +++ b/odb-tests/pgsql/types/buildfile @@ -0,0 +1,39 @@ +# file : pgsql/types/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($pgsql) "pgsql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libodb = libodb%lib{odb} +import libcommon = lib{common} +import libs = libodb-pgsql%lib{odb-pgsql} + +exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb and libcommon +# libraries are resolved for the odb_compile ad hoc rule (see build/root.build +# for details). +# +libue{test-meta}: $libodb $libcommon + +<{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 pgsql_types_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --cxx-prologue '#include "traits.hxx"' + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{pgsql-client}: include = adhoc diff --git a/odb-tests/pgsql/types/driver.cxx b/odb-tests/pgsql/types/driver.cxx new file mode 100644 index 0000000..710f601 --- /dev/null +++ b/odb-tests/pgsql/types/driver.cxx @@ -0,0 +1,165 @@ +// file : pgsql/types/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test PostgreSQL type conversion. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + object o (1); + + o.bool_ = true; + o.short_ = 12345; + o.int_ = -123456; + o.long_long_ = 123456; + + o.float_ = 1.123F; + o.float8_ = 1.123; + o.double_ = 1.123; + + o.date_ = 4015; + o.time_ = 48180000000LL; + o.timestamp_ = 346896000LL; + + string short_str (128, 's'); + string medium_str (250, 'm'); + string long_str (2040, 'l'); + + o.char_ = short_str; + o.varchar_ = medium_str; + o.text_ = long_str; + + o.bytea_.assign (long_str.c_str (), long_str.c_str () + long_str.size ()); + + unsigned char varbit_buf[8] = {1, 3, 1, 3, 1, 3, 1, 3}; + o.varbit_.size = 52; + o.varbit_.ubuffer_ = ubuffer (varbit_buf, 8); + + o.bit_.a = 0; + o.bit_.b = 1; + o.bit_.c = 0; + o.bit_.d = 1; + + // 6F846D41-C89A-4E4D-B22F-56443CFA543F + memcpy (o.uuid_, "\x6F\x84\x6D\x41\xC8\x9A\x4E\x4D\xB2\x2F" + "\x56\x44\x3C\xFA\x54\x3F", 16); + + o.enum_ = green; + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + typedef pgsql::query<object> query; + typedef odb::result<object> result; + + // Test UUID in queries. + // + { + char uuid[16]; + memcpy (uuid, o.uuid_, 16); + + transaction t (db->begin ()); + + { + result r (db->query<object> (query::uuid == uuid)); + assert (size (r) == 1); + } + + { + result r (db->query<object> (query::uuid == query::_val (uuid))); + assert (size (r) == 1); + } + + { + result r (db->query<object> (query::uuid == query::_ref (uuid))); + assert (size (r) == 1); + } + + { + const char* d (uuid); + result r (db->query<object> (query::uuid == d)); + assert (size (r) == 1); + } + + t.commit (); + } + + // 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 (); + } + + // PostgreSQL returns padded values for CHAR(N). + // + memcpy (o1.s2, " ", 16); + o1.s3[0] = o1.c1 = ' '; + memcpy (o2.s2, "1234567890 ", 16); + + { + 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/pgsql/types/test.hxx b/odb-tests/pgsql/types/test.hxx new file mode 100644 index 0000000..462ebad --- /dev/null +++ b/odb-tests/pgsql/types/test.hxx @@ -0,0 +1,220 @@ +// file : pgsql/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::memcmp, std::memcpy, std::str[n]cmp, std::strlen +#include <cstddef> // std::size_t + +#include <odb/core.hxx> + +#include <libcommon/buffer.hxx> + +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; +} + +struct varbit +{ + std::size_t size; + ubuffer ubuffer_; + + bool + compare (const varbit& x) const + { + if (size != x.size) + return false; + + std::size_t byte_len = size / 8; + + if (std::memcmp (ubuffer_.data (), x.ubuffer_.data (), byte_len) != 0) + return false; + + std::size_t trailing_bits = size % 8; + + if (trailing_bits != 0) + { + unsigned char mask (0xFFU << (8 - trailing_bits)); + + return (ubuffer_.data ()[byte_len] & mask) == + (x.ubuffer_.data ()[byte_len] & mask); + } + + return true; + } +}; + +inline bool +operator== (const varbit& x, const varbit& y) +{ + return x.compare (y); +} + +#pragma db value(bitfield) type ("BIT(4)") + +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 ("SMALLINT") + short short_; + + #pragma db type ("INT") + int int_; + + #pragma db type ("BIGINT") + long long long_long_; + + // Float types. + // + #pragma db type ("REAL") + float float_; + + #pragma db type ("FLOAT(32)") + double float8_; + + #pragma db type ("DOUBLE PRECISION") + double double_; + + // Data-time types. + // + #pragma db type ("DATE") + int date_; + + #pragma db type ("TIME") + long long time_; + + #pragma db type ("TIMESTAMP") + long long timestamp_; + + // String and binary types. + // + #pragma db type ("CHAR(128)") + std::string char_; + + #pragma db type ("VARCHAR(256)") + std::string varchar_; + + #pragma db type ("TEXT") + std::string text_; + + #pragma db type ("BYTEA") + std::vector<char> bytea_; + + #pragma db type ("VARBIT(1024)") + varbit varbit_; + + // #pragma db type ("BIT(4)") - assigned by #pragma db value + bitfield bit_; + + // Other types. + // + #pragma db type ("UUID") + char uuid_[16]; + + // Test ENUM representation. + // + color enum_; + + // Test NULL value. + // + #pragma db type ("TEXT") null + string_ptr null_; + + bool + operator== (const object& y) const + { + return + id_ == y.id_ && + bool_ == y.bool_ && + short_ == y.short_ && + int_ == y.int_ && + long_long_ == y.long_long_ && + float_ == y.float_ && + float8_ == y.float8_ && + double_ == y.double_ && + date_ == y.date_ && + time_ == y.time_ && + timestamp_ == y.timestamp_ && + char_ == y.char_ && + varchar_ == y.varchar_ && + text_ == y.text_ && + bytea_ == y.bytea_ && + bit_ == y.bit_ && + varbit_ == y.varbit_ && + memcmp (uuid_, y.uuid_, 16) == 0 && + enum_ == y.enum_ && + ((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; + } +}; + +#endif // TEST_HXX diff --git a/odb-tests/pgsql/types/testscript b/odb-tests/pgsql/types/testscript new file mode 100644 index 0000000..57218e1 --- /dev/null +++ b/odb-tests/pgsql/types/testscript @@ -0,0 +1,11 @@ +# file : pgsql/types/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../pgsql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/pgsql/types/traits.hxx b/odb-tests/pgsql/types/traits.hxx new file mode 100644 index 0000000..a1c8fbe --- /dev/null +++ b/odb-tests/pgsql/types/traits.hxx @@ -0,0 +1,171 @@ +// file : pgsql/types/traits.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <cassert> +#include <cstring> // std::memcpy, std::memset + +#include <odb/pgsql/traits.hxx> +#include <odb/pgsql/details/endian-traits.hxx> + +#include "test.hxx" // varbit, ubuffer, string_ptr + +namespace odb +{ + namespace pgsql + { + // The first 4 bytes of the image is a signed int specifying the + // number of significant bits contained by the BIT. The following + // bytes contain the bit data. + // + 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* i, + std::size_t, + bool is_null) + { + if (!is_null) + { + assert ( + details::endian_traits::ntoh ( + *reinterpret_cast<const int*> (i)) == 4); + + const unsigned char* d (i + 4); + + v.a = *d >> 4 & 1; + v.b = (*d >> 5) & 1; + v.c = (*d >> 6) & 1; + v.d = (*d >> 7) & 1; + } + else + v.a = v.b = v.c = v.d = 0; + } + + static void + set_image (unsigned char* i, + std::size_t, + std::size_t& n, + bool& is_null, + bitfield v) + { + is_null = false; + n = 5; + + *reinterpret_cast<int*> (i) = details::endian_traits::hton (4); + *(i + 4) = v.a << 4 | (v.b << 5) | (v.c << 6) | (v.d << 7); + } + }; + + // The first 4 bytes of the image is a signed int specifying the + // number of significant bits contained by the VARBIT. The following + // bytes contain the VARBIT data. + // + template <> + class value_traits<varbit, id_varbit> + { + public: + typedef varbit value_type; + typedef varbit query_type; + typedef details::ubuffer image_type; + + static void + set_value (varbit& v, + const details::ubuffer& b, + std::size_t n, + bool is_null) + { + if (!is_null) + { + v.size = static_cast<std::size_t> ( + details::endian_traits::ntoh ( + *reinterpret_cast<const int*> (b.data ()))); + + std::size_t byte_len (v.size / 8 + (v.size % 8 > 0 ? 1 : 0)); + assert (n >= byte_len + 4); + + v.ubuffer_.assign (b.data () + 4, byte_len); + } + + else + { + v.size = 0; + v.ubuffer_.assign (0, 0); + } + } + + static void + set_image (details::ubuffer& b, + std::size_t& n, + bool& is_null, + const varbit& v) + { + is_null = false; + n = 4 + v.size / 8 + (v.size % 8 > 0 ? 1 : 0); + + if (n > b.capacity ()) + b.capacity (n); + + // PostgreSQL requires all trailing bits of a VARBIT image + // to be zero. + // + std::memset (b.data (), 0, b.capacity ()); + + *reinterpret_cast<int*> (b.data ()) = + details::endian_traits::hton (static_cast<int> (v.size)); + + if (v.size != 0) + std::memcpy (b.data () + 4, v.ubuffer_.data (), n - 4); + } + }; + + 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 |