From b6ebd340651eaf347c86d3ba0dbed82c6cc8fb8d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 16 Sep 2013 07:07:33 +0200 Subject: Implement logical column drop for SQLite --- evolution/add-foreign-key/model.hxx | 2 + evolution/combined/driver.cxx | 2 +- evolution/combined/makefile | 2 +- evolution/combined/model.hxx | 4 +- evolution/drop-column/driver.cxx | 11 ++--- evolution/drop-column/makefile | 2 +- evolution/drop-column/model.hxx | 42 +++++++++------- evolution/soft-delete/driver.cxx | 99 ++++++++++++++++++++++++++----------- evolution/soft-delete/makefile | 2 +- evolution/soft-delete/model.hxx | 8 --- 10 files changed, 106 insertions(+), 68 deletions(-) (limited to 'evolution') diff --git a/evolution/add-foreign-key/model.hxx b/evolution/add-foreign-key/model.hxx index 6b116e9..19dee89 100644 --- a/evolution/add-foreign-key/model.hxx +++ b/evolution/add-foreign-key/model.hxx @@ -10,6 +10,8 @@ #include +#include // DATABASE_XXX + #pragma db model version(1, MODEL_VERSION) #define MODEL_NAMESPACE_IMPL(V) v##V diff --git a/evolution/combined/driver.cxx b/evolution/combined/driver.cxx index 8eb3eb2..4152133 100644 --- a/evolution/combined/driver.cxx +++ b/evolution/combined/driver.cxx @@ -57,13 +57,13 @@ main (int argc, char* argv[]) o.dui = 1; o.anui = 1; o.dnui = 1; + o.dc = 1; o.dt.push_back (1); o.aui = 1; #ifndef DATABASE_SQLITE o.dfk = new object1 (1); o.acn = 1; - o.dc = 1; o.acnn.reset (); o.afk = 1; #endif diff --git a/evolution/combined/makefile b/evolution/combined/makefile index 8aecc83..5c0bbbb 100644 --- a/evolution/combined/makefile +++ b/evolution/combined/makefile @@ -37,7 +37,7 @@ $(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): odb_common_options = --generate-query \ ---generate-schema --at-once --table-prefix evo_comb_ +--generate-schema --at-once --sqlite-override-null --table-prefix evo_comb_ $(gen): odb_common_options += --database $(db_id) $(gen1) $(dist): export odb_options1 = $(odb_common_options) --init-changelog $(gen2) $(dist): export odb_options2 = $(odb_common_options) --omit-create \ diff --git a/evolution/combined/model.hxx b/evolution/combined/model.hxx index 2cdc9d7..53faddf 100644 --- a/evolution/combined/model.hxx +++ b/evolution/combined/model.hxx @@ -122,13 +122,11 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) std::vector dt; #endif - // Drop column. Not supported by SQLite. + // Drop column. Logical drop (set NULL) in SQLite. // -#ifndef DATABASE_SQLITE #if MODEL_VERSION == 2 unsigned long dc; #endif -#endif // Alter column NOT NULL. Not supported by SQLite. // diff --git a/evolution/drop-column/driver.cxx b/evolution/drop-column/driver.cxx index 4078c58..6e72f7d 100644 --- a/evolution/drop-column/driver.cxx +++ b/evolution/drop-column/driver.cxx @@ -29,10 +29,6 @@ main (int argc, char* argv[]) try { auto_ptr db (create_database (argc, argv, false)); - - // SQLite doesn't support dropping of columns. - // -#ifndef DATABASE_SQLITE bool embedded (schema_catalog::exists (*db)); // 1 - base version @@ -60,9 +56,11 @@ main (int argc, char* argv[]) object o (1); o.str = "abc"; o.num = 123; + o.ptr = new object1 (1, 2); { transaction t (db->begin ()); + db->persist (*o.ptr); db->persist (o); t.commit (); } @@ -87,6 +85,7 @@ main (int argc, char* argv[]) assert (p->str == "abc"); assert (p->num == 123); + assert (p->ptr->id.x == 1 && p->ptr->id.y == 2); t.commit (); } @@ -108,7 +107,8 @@ main (int argc, char* argv[]) { transaction t (db->begin ()); auto_ptr p (db->load (1)); - assert (p->str == ""); + assert (p->str == "" && p->ptr == 0); + db->erase (value (1, 2)); // SQLite logical delete test. t.commit (); } break; @@ -119,7 +119,6 @@ main (int argc, char* argv[]) return 1; } } -#endif // DATABASE_SQLITE } catch (const odb::exception& e) { diff --git a/evolution/drop-column/makefile b/evolution/drop-column/makefile index 135c3fc..f22097e 100644 --- a/evolution/drop-column/makefile +++ b/evolution/drop-column/makefile @@ -37,7 +37,7 @@ $(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): odb_common_options = --generate-query \ ---generate-schema --at-once --table-prefix evo_drop_c_ +--generate-schema --at-once --sqlite-override-null --table-prefix evo_drop_c_ $(gen): odb_common_options += --database $(db_id) $(gen1) $(dist): export odb_options1 = $(odb_common_options) --init-changelog $(gen2) $(dist): export odb_options2 = $(odb_common_options) --omit-create \ diff --git a/evolution/drop-column/model.hxx b/evolution/drop-column/model.hxx index feff041..b69cd0d 100644 --- a/evolution/drop-column/model.hxx +++ b/evolution/drop-column/model.hxx @@ -11,8 +11,6 @@ #include #include -#include // DATABASE_XXX - #pragma db model version(1, MODEL_VERSION) #define MODEL_NAMESPACE_IMPL(V) v##V @@ -20,32 +18,42 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) { + #pragma db value + struct value + { + value (int x_ = 0, int y_ = 0): x (x_), y (y_) {} + int x; + int y; + }; + + #pragma db object + struct object1 + { + object1 (int x = 0, int y = 0): id (x, y) {} + + #pragma db id + value id; + }; + #pragma db object struct object { - object (unsigned long id = 0): id_ (id) {} + object (unsigned long id = 0): id_ (id), ptr (0) {} + ~object () {delete ptr;} #pragma db id unsigned long id_; - // SQLite doesn't support dropping of columns. - // -#ifndef DATABASE_SQLITE -#if MODEL_VERSION >= 2 - -#if MODEL_VERSION == 3 - #pragma db deleted(3) -#endif std::string str; - -#if MODEL_VERSION == 3 - #pragma db deleted(3) -#endif unsigned long num; + object1* ptr; + }; +#if MODEL_VERSION == 3 + #pragma db member(object::str) deleted(3) + #pragma db member(object::num) deleted(3) + #pragma db member(object::ptr) deleted(3) #endif -#endif - }; } #undef MODEL_NAMESPACE diff --git a/evolution/soft-delete/driver.cxx b/evolution/soft-delete/driver.cxx index a5e9927..7c53f28 100644 --- a/evolution/soft-delete/driver.cxx +++ b/evolution/soft-delete/driver.cxx @@ -14,6 +14,7 @@ #include #include +#include // DATABASE_XXX #include "test2.hxx" #include "test3.hxx" @@ -68,10 +69,6 @@ main (int argc, char* argv[]) } } - // SQLite doesn't support dropping of columns. - // -#ifndef DATABASE_SQLITE - // Test basic soft-deleted member logic. // { @@ -265,8 +262,6 @@ main (int argc, char* argv[]) } } -#endif // DATABASE_SQLITE - // Test soft-deleted container member in a non-versioned object. // { @@ -325,10 +320,6 @@ main (int argc, char* argv[]) } } - // SQLite doesn't support dropping of columns. - // -#ifndef DATABASE_SQLITE - // Test basic soft-deleted member logic. // { @@ -938,8 +929,6 @@ main (int argc, char* argv[]) } } -#endif // DATABASE_SQLITE - // Test soft-deleted container member in a non-versioned object. // { @@ -1083,10 +1072,6 @@ main (int argc, char* argv[]) catch (const odb::exception&) {} } - // SQLite doesn't support dropping of columns. - // -#ifndef DATABASE_SQLITE - // Test basic soft-deleted member logic. // { @@ -1114,13 +1099,18 @@ main (int argc, char* argv[]) i->str == "" && i->num == 123 && i->vec.empty () && i->ptr == 0); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::str == "abc"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::str.is_null ())) == 1); +#endif t.commit (); } @@ -1275,13 +1265,18 @@ main (int argc, char* argv[]) result::iterator i (r.begin ()); assert (i != r.end () && i->str == "" && i->num == 123); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::str == "abc"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::str.is_null ())) == 1); +#endif t.commit (); } } @@ -1298,13 +1293,16 @@ main (int argc, char* argv[]) transaction t (db->begin ()); auto_ptr p (db->load (1)); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->load (*p, p->s); // No such column. assert (false); } catch (const odb::exception&) {} - +#endif t.commit (); } @@ -1364,13 +1362,18 @@ main (int argc, char* argv[]) db->load (*i, i->s); assert (i->str == "" && i->num == 123 && i->vec.empty ()); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::str == "abc"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::str.is_null ())) == 1); +#endif t.commit (); } @@ -1445,13 +1448,18 @@ main (int argc, char* argv[]) object& o (static_cast (*i)); assert (o.bstr == "" && o.dstr == "" && o.num == 123); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::bstr == "ab"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::bstr.is_null ())) == 1); +#endif t.commit (); } @@ -1465,13 +1473,18 @@ main (int argc, char* argv[]) assert (i != r.end () && i->bstr == "" && i->dstr == "" && i->num); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::dstr == "abc"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::dstr.is_null ())) == 1); +#endif t.commit (); } @@ -1519,13 +1532,16 @@ main (int argc, char* argv[]) transaction t (db->begin ()); auto_ptr p (db->load (1)); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->load (*p, p->s); // No such column. assert (false); } catch (const odb::exception&) {} - +#endif t.commit (); } @@ -1587,13 +1603,18 @@ main (int argc, char* argv[]) object& o (static_cast (*i)); assert (o.bstr == "" && o.dstr == "" && o.num == 123); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::bstr == "ab"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::bstr.is_null ())) == 1); +#endif t.commit (); } @@ -1608,13 +1629,18 @@ main (int argc, char* argv[]) assert (i != r.end () && i->bstr == "" && i->dstr == "" && i->num); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::dstr == "abc"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::dstr.is_null ())) == 1); +#endif t.commit (); } @@ -1703,13 +1729,18 @@ main (int argc, char* argv[]) result::iterator i (r.begin ()); assert (i != r.end () && i->str == "" && i->num == 123); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::str == "abc"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::str.is_null ())) == 1); +#endif t.commit (); } @@ -1762,13 +1793,18 @@ main (int argc, char* argv[]) result::iterator i (r.begin ()); assert (i != r.end () && i->str == "" && i->num == 123); + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::str == "abc"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::str.is_null ())) == 1); +#endif t.commit (); } @@ -1813,13 +1849,18 @@ main (int argc, char* argv[]) assert (i != r.end () && i->str == "" && i->num == 123); id = i->id; + // Logical delete in SQLite. + // +#ifndef DATABASE_SQLITE try { db->query (query::str == "abc"); // No such column. assert (false); } catch (const odb::exception&) {} - +#else + assert (size (db->query (query::str.is_null ())) == 1); +#endif t.commit (); } @@ -1860,8 +1901,6 @@ main (int argc, char* argv[]) } } -#endif // DATABASE_SQLITE - // Test soft-deleted container member in a non-versioned object. // { diff --git a/evolution/soft-delete/makefile b/evolution/soft-delete/makefile index 12d02a3..f4c5091 100644 --- a/evolution/soft-delete/makefile +++ b/evolution/soft-delete/makefile @@ -37,7 +37,7 @@ $(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): odb_common_options = --generate-query --generate-prepared \ ---generate-schema --at-once --table-prefix evo_soft_d_ +--generate-schema --at-once --sqlite-override-null --table-prefix evo_soft_d_ $(gen): odb_common_options += --database $(db_id) $(gen1) $(dist): export odb_options1 = $(odb_common_options) --init-changelog $(gen2) $(dist): export odb_options2 = $(odb_common_options) --omit-create \ diff --git a/evolution/soft-delete/model.hxx b/evolution/soft-delete/model.hxx index ef5bc68..0a81116 100644 --- a/evolution/soft-delete/model.hxx +++ b/evolution/soft-delete/model.hxx @@ -13,8 +13,6 @@ #include #include -#include // DATABASE_XXX - #pragma db model version(1, MODEL_VERSION) #define MODEL_NAMESPACE_IMPL(V) v##V @@ -43,10 +41,6 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) #endif } - // SQLite doesn't support dropping of columns. - // -#ifndef DATABASE_SQLITE - // Test basic soft-deleted member logic. // #pragma db namespace table("t2_") @@ -433,8 +427,6 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) #endif } -#endif // DATABASE_SQLITE - // Test soft-deleted container member in a non-versioned object. // #pragma db namespace table("t21_") -- cgit v1.1