aboutsummaryrefslogtreecommitdiff
path: root/sqlite/types
diff options
context:
space:
mode:
Diffstat (limited to 'sqlite/types')
-rw-r--r--sqlite/types/driver.cxx64
-rw-r--r--sqlite/types/makefile108
-rw-r--r--sqlite/types/test.hxx145
-rw-r--r--sqlite/types/test.std0
-rw-r--r--sqlite/types/traits.hxx96
5 files changed, 413 insertions, 0 deletions
diff --git a/sqlite/types/driver.cxx b/sqlite/types/driver.cxx
new file mode 100644
index 0000000..855cada
--- /dev/null
+++ b/sqlite/types/driver.cxx
@@ -0,0 +1,64 @@
+// file : sqlite/types/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test SQLite type conversion.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/sqlite/database.hxx>
+#include <odb/sqlite/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.integer_ = -123456;
+ o.real_ = 1.123;
+
+ string long_str (2040, 'l');
+ buffer long_buf (long_str.c_str (), long_str.size ());
+
+ o.text_ = long_str;
+ o.blob_ = long_buf;
+
+ {
+ transaction t (db->begin ());
+ db->persist (o);
+ t.commit ();
+ }
+
+ //
+ //
+ {
+ 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/sqlite/types/makefile b/sqlite/types/makefile
new file mode 100644
index 0000000..7836a39
--- /dev/null
+++ b/sqlite/types/makefile
@@ -0,0 +1,108 @@
+# file : sqlite/types/makefile
+# author : Boris Kolpackov <boris@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)
+
+# Build.
+#
+$(driver): $(cxx_obj) $(common.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base)
+$(cxx_obj) $(cxx_od): $(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 sqlite --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)/sqlite/,,$(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/sqlite/types/test.hxx b/sqlite/types/test.hxx
new file mode 100644
index 0000000..971a89f
--- /dev/null
+++ b/sqlite/types/test.hxx
@@ -0,0 +1,145 @@
+// file : sqlite/types/test.hxx
+// author : Boris Kolpackov <boris@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 <cstddef> // std::size_t
+#include <cstring> // std::{memcmp,memcpy}
+
+#include <odb/core.hxx>
+
+struct buffer
+{
+ ~buffer ()
+ {
+ delete[] data_;
+ }
+
+ buffer ()
+ : data_ (0), size_ (0)
+ {
+ }
+
+ buffer (const void* data, std::size_t size)
+ : data_ (0), size_ (size)
+ {
+ data_ = new char[size_];
+ std::memcpy (data_, data, size_);
+ }
+
+ buffer (const buffer& y)
+ : data_ (0), size_ (0)
+ {
+ assign (y.data_, y.size_);
+ }
+
+ buffer&
+ operator= (const buffer& y)
+ {
+ if (this != &y)
+ assign (y.data_, y.size_);
+
+ return *this;
+ }
+
+ void
+ assign (const void* data, std::size_t size)
+ {
+ if (size_ < size)
+ {
+ char* p (new char[size]);
+ delete[] data_;
+ data_ = p;
+ }
+
+ std::memcpy (data_, data, size);
+ size_ = size;
+ }
+
+ char*
+ data ()
+ {
+ return data_;
+ }
+
+ const char*
+ data () const
+ {
+ return data_;
+ }
+
+ std::size_t
+ size () const
+ {
+ return size_;
+ }
+
+ bool
+ operator== (const buffer& y) const
+ {
+ return size_ == y.size_ && std::memcmp (data_, y.data_, size_) == 0;
+ }
+
+private:
+ char* data_;
+ std::size_t size_;
+};
+
+typedef std::auto_ptr<std::string> string_ptr;
+
+#pragma db object
+struct object
+{
+ object (unsigned long id)
+ : id_ (id)
+ {
+ }
+
+ object ()
+ {
+ }
+
+ #pragma db id
+ unsigned long id_;
+
+ #pragma db type ("BOOL NOT NULL")
+ bool bool_;
+
+ #pragma db type ("INTEGER NOT NULL")
+ int integer_;
+
+ #pragma db type ("REAL NOT NULL")
+ double real_;
+
+ #pragma db type ("TEXT NOT NULL")
+ std::string text_;
+
+ #pragma db type ("BLOB NOT NULL")
+ buffer blob_;
+
+ // Test NULL value.
+ //
+ #pragma db type ("TEXT")
+ string_ptr null_;
+
+ bool
+ operator== (const object& y) const
+ {
+ return
+ id_ == y.id_ &&
+ bool_ == y.bool_ &&
+ integer_ == y.integer_ &&
+ real_ == y.real_ &&
+ text_ == y.text_ &&
+ blob_ == y.blob_ &&
+ ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_);
+ }
+};
+
+#endif // TEST_HXX
diff --git a/sqlite/types/test.std b/sqlite/types/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sqlite/types/test.std
diff --git a/sqlite/types/traits.hxx b/sqlite/types/traits.hxx
new file mode 100644
index 0000000..0f9c253
--- /dev/null
+++ b/sqlite/types/traits.hxx
@@ -0,0 +1,96 @@
+// file : sqlite/types/traits.hxx
+// author : Boris Kolpackov <boris@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/sqlite/traits.hxx>
+
+#include "test.hxx" // buffer
+
+namespace odb
+{
+ namespace sqlite
+ {
+ template <>
+ class value_traits<buffer, details::buffer, id_blob>
+ {
+ 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<string_ptr, details::buffer, 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