// This file was generated by ODB, object-relational mapping (ORM) // compiler for C++. // #include #include "Category-odb.hxx" #include #include // std::memcpy #include #include #include #include #include #include #include #include #include #include #include namespace odb { // Category // struct access::object_traits_impl< ::Category, id_sqlite >::extra_statement_cache_type { extra_statement_cache_type ( sqlite::connection&, image_type&, sqlite::binding&, sqlite::binding&) { } }; access::object_traits_impl< ::Category, id_sqlite >::id_type access::object_traits_impl< ::Category, id_sqlite >:: id (const image_type& i) { sqlite::database* db (0); ODB_POTENTIALLY_UNUSED (db); id_type id; { sqlite::value_traits< ::quint8, sqlite::id_integer >::set_value ( id, i.m_id_value, i.m_id_null); } return id; } bool access::object_traits_impl< ::Category, id_sqlite >:: grow (image_type& i, bool* t) { ODB_POTENTIALLY_UNUSED (i); ODB_POTENTIALLY_UNUSED (t); bool grew (false); // m_id // t[0UL] = false; // m_name // if (t[1UL]) { i.m_name_value.capacity (i.m_name_size); grew = true; } return grew; } void access::object_traits_impl< ::Category, id_sqlite >:: bind (sqlite::bind* b, image_type& i, sqlite::statement_kind sk) { ODB_POTENTIALLY_UNUSED (sk); using namespace sqlite; std::size_t n (0); // m_id // if (sk != statement_update) { b[n].type = sqlite::bind::integer; b[n].buffer = &i.m_id_value; b[n].is_null = &i.m_id_null; n++; } // m_name // b[n].type = sqlite::image_traits< ::QString, sqlite::id_text>::bind_value; b[n].buffer = i.m_name_value.data (); b[n].size = &i.m_name_size; b[n].capacity = i.m_name_value.capacity (); b[n].is_null = &i.m_name_null; n++; } void access::object_traits_impl< ::Category, id_sqlite >:: bind (sqlite::bind* b, id_image_type& i) { std::size_t n (0); b[n].type = sqlite::bind::integer; b[n].buffer = &i.id_value; b[n].is_null = &i.id_null; } bool access::object_traits_impl< ::Category, id_sqlite >:: init (image_type& i, const object_type& o, sqlite::statement_kind sk) { ODB_POTENTIALLY_UNUSED (i); ODB_POTENTIALLY_UNUSED (o); ODB_POTENTIALLY_UNUSED (sk); using namespace sqlite; bool grew (false); // m_id // if (sk == statement_insert) { ::quint8 const& v = o.id (); bool is_null (false); sqlite::value_traits< ::quint8, sqlite::id_integer >::set_image ( i.m_id_value, is_null, v); i.m_id_null = is_null; } // m_name // { ::QString const& v = o.name (); bool is_null (false); std::size_t cap (i.m_name_value.capacity ()); sqlite::value_traits< ::QString, sqlite::id_text >::set_image ( i.m_name_value, i.m_name_size, is_null, v); i.m_name_null = is_null; grew = grew || (cap != i.m_name_value.capacity ()); } return grew; } void access::object_traits_impl< ::Category, id_sqlite >:: init (object_type& o, const image_type& i, database* db) { ODB_POTENTIALLY_UNUSED (o); ODB_POTENTIALLY_UNUSED (i); ODB_POTENTIALLY_UNUSED (db); // m_id // { ::quint8 v; sqlite::value_traits< ::quint8, sqlite::id_integer >::set_value ( v, i.m_id_value, i.m_id_null); o.setId (v); } // m_name // { ::QString v; sqlite::value_traits< ::QString, sqlite::id_text >::set_value ( v, i.m_name_value, i.m_name_size, i.m_name_null); o.setName (v); } } void access::object_traits_impl< ::Category, id_sqlite >:: init (id_image_type& i, const id_type& id) { { bool is_null (false); sqlite::value_traits< ::quint8, sqlite::id_integer >::set_image ( i.id_value, is_null, id); i.id_null = is_null; } } const char access::object_traits_impl< ::Category, id_sqlite >::persist_statement[] = "INSERT INTO \"categories\" " "(\"id\", " "\"name\") " "VALUES " "(?, ?)"; const char access::object_traits_impl< ::Category, id_sqlite >::find_statement[] = "SELECT " "\"categories\".\"id\", " "\"categories\".\"name\" " "FROM \"categories\" " "WHERE \"categories\".\"id\"=?"; const char access::object_traits_impl< ::Category, id_sqlite >::update_statement[] = "UPDATE \"categories\" " "SET " "\"name\"=? " "WHERE \"id\"=?"; const char access::object_traits_impl< ::Category, id_sqlite >::erase_statement[] = "DELETE FROM \"categories\" " "WHERE \"id\"=?"; const char access::object_traits_impl< ::Category, id_sqlite >::query_statement[] = "SELECT " "\"categories\".\"id\", " "\"categories\".\"name\" " "FROM \"categories\""; const char access::object_traits_impl< ::Category, id_sqlite >::erase_query_statement[] = "DELETE FROM \"categories\""; const char access::object_traits_impl< ::Category, id_sqlite >::table_name[] = "\"categories\""; void access::object_traits_impl< ::Category, id_sqlite >:: persist (database& db, object_type& obj) { ODB_POTENTIALLY_UNUSED (db); using namespace sqlite; sqlite::connection& conn ( sqlite::transaction::current ().connection ()); statements_type& sts ( conn.statement_cache ().find_object ()); callback (db, static_cast (obj), callback_event::pre_persist); image_type& im (sts.image ()); binding& imb (sts.insert_image_binding ()); if (init (im, obj, statement_insert)) im.version++; im.m_id_null = true; if (im.version != sts.insert_image_version () || imb.version == 0) { bind (imb.bind, im, statement_insert); sts.insert_image_version (im.version); imb.version++; } insert_statement& st (sts.persist_statement ()); if (!st.execute ()) throw object_already_persistent (); obj.setId (static_cast< id_type > (st.id ())); callback (db, static_cast (obj), callback_event::post_persist); } void access::object_traits_impl< ::Category, id_sqlite >:: update (database& db, const object_type& obj) { ODB_POTENTIALLY_UNUSED (db); using namespace sqlite; using sqlite::update_statement; callback (db, obj, callback_event::pre_update); sqlite::transaction& tr (sqlite::transaction::current ()); sqlite::connection& conn (tr.connection ()); statements_type& sts ( conn.statement_cache ().find_object ()); const id_type& id ( obj.id ()); id_image_type& idi (sts.id_image ()); init (idi, id); image_type& im (sts.image ()); if (init (im, obj, statement_update)) im.version++; bool u (false); binding& imb (sts.update_image_binding ()); if (im.version != sts.update_image_version () || imb.version == 0) { bind (imb.bind, im, statement_update); sts.update_image_version (im.version); imb.version++; u = true; } binding& idb (sts.id_image_binding ()); if (idi.version != sts.update_id_image_version () || idb.version == 0) { if (idi.version != sts.id_image_version () || idb.version == 0) { bind (idb.bind, idi); sts.id_image_version (idi.version); idb.version++; } sts.update_id_image_version (idi.version); if (!u) imb.version++; } update_statement& st (sts.update_statement ()); if (st.execute () == 0) throw object_not_persistent (); callback (db, obj, callback_event::post_update); pointer_cache_traits::update (db, obj); } void access::object_traits_impl< ::Category, id_sqlite >:: erase (database& db, const id_type& id) { using namespace sqlite; ODB_POTENTIALLY_UNUSED (db); sqlite::connection& conn ( sqlite::transaction::current ().connection ()); statements_type& sts ( conn.statement_cache ().find_object ()); id_image_type& i (sts.id_image ()); init (i, id); binding& idb (sts.id_image_binding ()); if (i.version != sts.id_image_version () || idb.version == 0) { bind (idb.bind, i); sts.id_image_version (i.version); idb.version++; } if (sts.erase_statement ().execute () != 1) throw object_not_persistent (); pointer_cache_traits::erase (db, id); } access::object_traits_impl< ::Category, id_sqlite >::pointer_type access::object_traits_impl< ::Category, id_sqlite >:: find (database& db, const id_type& id) { using namespace sqlite; { pointer_type p (pointer_cache_traits::find (db, id)); if (!pointer_traits::null_ptr (p)) return p; } sqlite::connection& conn ( sqlite::transaction::current ().connection ()); statements_type& sts ( conn.statement_cache ().find_object ()); statements_type::auto_lock l (sts); if (l.locked ()) { if (!find_ (sts, &id)) return pointer_type (); } pointer_type p ( access::object_factory::create ()); pointer_traits::guard pg (p); pointer_cache_traits::insert_guard ig ( pointer_cache_traits::insert (db, id, p)); object_type& obj (pointer_traits::get_ref (p)); if (l.locked ()) { select_statement& st (sts.find_statement ()); ODB_POTENTIALLY_UNUSED (st); callback (db, obj, callback_event::pre_load); init (obj, sts.image (), &db); load_ (sts, obj, false); sts.load_delayed (0); l.unlock (); callback (db, obj, callback_event::post_load); pointer_cache_traits::load (ig.position ()); } else sts.delay_load (id, obj, ig.position ()); ig.release (); pg.release (); return p; } bool access::object_traits_impl< ::Category, id_sqlite >:: find (database& db, const id_type& id, object_type& obj) { using namespace sqlite; sqlite::connection& conn ( sqlite::transaction::current ().connection ()); statements_type& sts ( conn.statement_cache ().find_object ()); statements_type::auto_lock l (sts); if (!find_ (sts, &id)) return false; select_statement& st (sts.find_statement ()); ODB_POTENTIALLY_UNUSED (st); reference_cache_traits::position_type pos ( reference_cache_traits::insert (db, id, obj)); reference_cache_traits::insert_guard ig (pos); callback (db, obj, callback_event::pre_load); init (obj, sts.image (), &db); load_ (sts, obj, false); sts.load_delayed (0); l.unlock (); callback (db, obj, callback_event::post_load); reference_cache_traits::load (pos); ig.release (); return true; } bool access::object_traits_impl< ::Category, id_sqlite >:: reload (database& db, object_type& obj) { using namespace sqlite; sqlite::connection& conn ( sqlite::transaction::current ().connection ()); statements_type& sts ( conn.statement_cache ().find_object ()); statements_type::auto_lock l (sts); const id_type& id ( obj.id ()); if (!find_ (sts, &id)) return false; select_statement& st (sts.find_statement ()); ODB_POTENTIALLY_UNUSED (st); callback (db, obj, callback_event::pre_load); init (obj, sts.image (), &db); load_ (sts, obj, true); sts.load_delayed (0); l.unlock (); callback (db, obj, callback_event::post_load); return true; } bool access::object_traits_impl< ::Category, id_sqlite >:: find_ (statements_type& sts, const id_type* id) { using namespace sqlite; id_image_type& i (sts.id_image ()); init (i, *id); binding& idb (sts.id_image_binding ()); if (i.version != sts.id_image_version () || idb.version == 0) { bind (idb.bind, i); sts.id_image_version (i.version); idb.version++; } image_type& im (sts.image ()); binding& imb (sts.select_image_binding ()); if (im.version != sts.select_image_version () || imb.version == 0) { bind (imb.bind, im, statement_select); sts.select_image_version (im.version); imb.version++; } select_statement& st (sts.find_statement ()); st.execute (); auto_result ar (st); select_statement::result r (st.fetch ()); if (r == select_statement::truncated) { if (grow (im, sts.select_image_truncated ())) im.version++; if (im.version != sts.select_image_version ()) { bind (imb.bind, im, statement_select); sts.select_image_version (im.version); imb.version++; st.refetch (); } } return r != select_statement::no_data; } result< access::object_traits_impl< ::Category, id_sqlite >::object_type > access::object_traits_impl< ::Category, id_sqlite >:: query (database&, const query_base_type& q) { using namespace sqlite; using odb::details::shared; using odb::details::shared_ptr; sqlite::connection& conn ( sqlite::transaction::current ().connection ()); statements_type& sts ( conn.statement_cache ().find_object ()); image_type& im (sts.image ()); binding& imb (sts.select_image_binding ()); if (im.version != sts.select_image_version () || imb.version == 0) { bind (imb.bind, im, statement_select); sts.select_image_version (im.version); imb.version++; } std::string text (query_statement); if (!q.empty ()) { text += " "; text += q.clause (); } q.init_parameters (); shared_ptr st ( new (shared) select_statement ( conn, text, false, true, q.parameters_binding (), imb)); st->execute (); shared_ptr< odb::object_result_impl > r ( new (shared) sqlite::object_result_impl ( q, st, sts, 0)); return result (r); } unsigned long long access::object_traits_impl< ::Category, id_sqlite >:: erase_query (database&, const query_base_type& q) { using namespace sqlite; sqlite::connection& conn ( sqlite::transaction::current ().connection ()); std::string text (erase_query_statement); if (!q.empty ()) { text += ' '; text += q.clause (); } q.init_parameters (); delete_statement st ( conn, text, q.parameters_binding ()); return st.execute (); } } namespace odb { static bool create_schema (database& db, unsigned short pass, bool drop) { ODB_POTENTIALLY_UNUSED (db); ODB_POTENTIALLY_UNUSED (pass); ODB_POTENTIALLY_UNUSED (drop); if (drop) { switch (pass) { case 1: { return true; } case 2: { db.execute ("DROP TABLE IF EXISTS \"categories\""); db.execute ("CREATE TABLE IF NOT EXISTS \"schema_version\" (\n" " \"name\" TEXT NOT NULL PRIMARY KEY,\n" " \"version\" INTEGER NOT NULL,\n" " \"migration\" INTEGER NOT NULL)"); db.execute ("DELETE FROM \"schema_version\"\n" " WHERE \"name\" = ''"); return false; } } } else { switch (pass) { case 1: { db.execute ("CREATE TABLE \"categories\" (\n" " \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" " \"name\" TEXT NOT NULL)"); return true; } case 2: { db.execute ("CREATE TABLE IF NOT EXISTS \"schema_version\" (\n" " \"name\" TEXT NOT NULL PRIMARY KEY,\n" " \"version\" INTEGER NOT NULL,\n" " \"migration\" INTEGER NOT NULL)"); db.execute ("INSERT OR IGNORE INTO \"schema_version\" (\n" " \"name\", \"version\", \"migration\")\n" " VALUES ('', 1, 0)"); return false; } } } return false; } static const schema_catalog_create_entry create_schema_entry_ ( id_sqlite, "", &create_schema); static const schema_catalog_migrate_entry migrate_schema_entry_1_ ( id_sqlite, "", 1ULL, 0); } #include