diff options
Diffstat (limited to 'pgsql/types')
-rw-r--r-- | pgsql/types/driver.cxx | 90 | ||||
-rw-r--r-- | pgsql/types/makefile | 112 | ||||
-rw-r--r-- | pgsql/types/test.hxx | 158 | ||||
-rw-r--r-- | pgsql/types/test.std | 0 | ||||
-rw-r--r-- | pgsql/types/traits.hxx | 208 |
5 files changed, 568 insertions, 0 deletions
diff --git a/pgsql/types/driver.cxx b/pgsql/types/driver.cxx new file mode 100644 index 0000000..5c5145a --- /dev/null +++ b/pgsql/types/driver.cxx @@ -0,0 +1,90 @@ +// file : pgsql/types/driver.cxx +// author : Constantin Michael <constantin@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test PostgreSQL type conversion. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_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.numeric_ = "123.456"; + + o.date_ = 4015; + o.time_ = 48180000000; + o.timestamp_ = 346896000; + + string short_str (128, 's'); + string medium_str (250, 'm'); + string long_str (2040, 'l'); + buffer long_buf (long_str.c_str (), long_str.size ()); + + unsigned char varbit_buf[8] = {1, 2, 1, 2, 1, 1, 1, 1}; + + o.char_ = short_str; + o.varchar_ = medium_str; + o.text_ = long_str; + o.bytea_ = long_buf; + o.varbit_ = buffer (varbit_buf, 8); + + o.bit_.a = 0; + o.bit_.b = 1; + o.bit_.c = 0; + o.bit_.d = 1; + + o.enum_ = green; + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_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/pgsql/types/makefile b/pgsql/types/makefile new file mode 100644 index 0000000..88fdb4a --- /dev/null +++ b/pgsql/types/makefile @@ -0,0 +1,112 @@ +# file : pgsql/types/makefile +# author : Constantin Michael <constantin@codesynthesis.com> +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +$(call import,\ + $(scf_root)/import/libodb-pgsql/stub.make,\ + l: odb_pgsql.l,cpp-options: odb_pgsql.l.cpp-options) + +# Build. +# +$(driver): $(cxx_obj) $(odb_pgsql.l) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(odb_pgsql.l.cpp-options) $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database pgsql --generate-schema \ +--generate-query --cxx-prologue '\#include "traits.hxx"' + +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export extra_headers := traits.hxx +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/pgsql/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(extra_headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(odb_rules)) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/pgsql/types/test.hxx b/pgsql/types/test.hxx new file mode 100644 index 0000000..39de674 --- /dev/null +++ b/pgsql/types/test.hxx @@ -0,0 +1,158 @@ +// file : pgsql/types/test.hxx +// author : Constantin Michael <constantin@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <set> +#include <string> +#include <memory> // std::auto_ptr +#include <cstring> // std::memcmp + +#include <odb/core.hxx> + +#include <common/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; +} + +#pragma db value(bitfield) type ("BIT(4) NOT NULL") + +typedef std::auto_ptr<std::string> string_ptr; + +enum color {red, green, blue}; + +#pragma db object +struct object +{ + object (unsigned long id) + : id_ (id) + { + } + + object () + { + } + + #pragma db id + unsigned long id_; + + // Integral types. + // + #pragma db type ("BOOL NOT NULL") + bool bool_; + + #pragma db type ("SMALLINT NOT NULL") + short short_; + + #pragma db type ("INT NOT NULL") + int int_; + + #pragma db type ("BIGINT NOT NULL") + long long long_long_; + + // Float types. + // + #pragma db type ("REAL NOT NULL") + float float_; + + #pragma db type ("FLOAT(32) NOT NULL") + double float8_; + + #pragma db type ("DOUBLE PRECISION NOT NULL") + double double_; + + // #pragma db type ("NUMERIC(6,3) NOT NULL") + // std::string numeric_; + + // Data-time types. + // + #pragma db type ("DATE NOT NULL") + int date_; + + #pragma db type ("TIME NOT NULL") + long long time_; + + #pragma db type ("TIMESTAMP NOT NULL") + long long timestamp_; + + // String and binary types. + // + #pragma db type ("CHAR(128) NOT NULL") + std::string char_; + + #pragma db type ("VARCHAR(256) NOT NULL") + std::string varchar_; + + #pragma db type ("TEXT NOT NULL") + std::string text_; + + #pragma db type ("BYTEA NOT NULL") + buffer bytea_; + + #pragma db type ("VARBIT(1024) NOT NULL") + buffer varbit_; + + // #pragma db type ("BIT(4) NOT NULL") - assigned by #pragma db value + bitfield bit_; + + // Other types. + // + #pragma db type ("UUID NOT NULL") + unsigned char uuid_[16]; + + // Test ENUM representation. + // + color enum_; + + // Test NULL value. + // + #pragma db type ("TEXT") + 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_ && + // numeric__ == y.numeric_ && + 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_); + } +}; + +#endif // TEST_HXX diff --git a/pgsql/types/test.std b/pgsql/types/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pgsql/types/test.std diff --git a/pgsql/types/traits.hxx b/pgsql/types/traits.hxx new file mode 100644 index 0000000..b7734b5 --- /dev/null +++ b/pgsql/types/traits.hxx @@ -0,0 +1,208 @@ +// file : pgsql/types/traits.hxx +// author : Constantin Michael <constantin@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <cstring> // std::memcpy, std::memset + +#include <odb/pgsql/traits.hxx> +#include <odb/pgsql/details/endian-traits.hxx> + +#include "test.hxx" // date_time, buffer, string_ptr + +namespace odb +{ + namespace pgsql + { + template <> + class value_traits<buffer, id_bytea> + { + public: + typedef buffer value_type; + typedef buffer query_type; + typedef details::buffer image_type; + + static void + set_value (buffer& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (!is_null) + v.assign (b.data (), n); + else + v.assign (0, 0); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const buffer& v) + { + is_null = false; + n = v.size (); + + if (n > b.capacity ()) + b.capacity (n); + + if (n != 0) + std::memcpy (b.data (), v.data (), n); + } + }; + + 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) + { + unsigned char const* 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); + } + }; + + template <> + class value_traits<buffer, id_varbit> + { + public: + typedef buffer value_type; + typedef buffer query_type; + typedef details::ubuffer image_type; + + static void + set_value (buffer& v, + const details::ubuffer& b, + std::size_t n, + bool is_null) + { + if (!is_null) + v.assign (b.data () + 4, n - 4); + else + v.assign (0, 0); + } + + static void + set_image (details::ubuffer& b, + std::size_t& n, + bool& is_null, + const buffer& v) + { + is_null = false; + n = v.size () + 4; + + if (n > b.capacity ()) + b.capacity (n); + + int bit_len = static_cast<int> (v.size () * 8); + + *reinterpret_cast<int*> (b.data ()) = + details::endian_traits::hton (bit_len); + + if (bit_len != 0) + std::memcpy (b.data () + 4, v.data (), v.size ()); + } + }; + + template <> + class value_traits<unsigned char[16], id_uuid> + { + public: + typedef unsigned char* value_type; + typedef details::ubuffer query_type; + typedef unsigned char* image_type; + + static void + set_value (unsigned char v[16], + unsigned char const* i, + bool is_null) + { + if (!is_null) + std::memcpy (v, i, 16); + else + std::memset (v, 0, 16); + } + + static void + set_image (unsigned char* i, bool& is_null, unsigned char const v[16]) + { + is_null = false; + std::memcpy (i, v, 16); + } + }; + + 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 |