sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

Separation of DB and ORM functionality request

Open ElephantInTheRoomFirst opened this issue 9 years ago • 14 comments

There is some functionality which does not need table(s), for example drop_table, user_version. These operate above ORM functionality. Creating ORM storage object to use this functionality is not always possible and logical. For example, checking existence of table, checking version of database. I see there are two alternatives.

  1. Creation of separate class which deals with database functionality and adding make_db equivalent to make_storage without tables. (preferred)
  2. extending make_storage to make tables optional.

ElephantInTheRoomFirst avatar Jun 14 '17 06:06 ElephantInTheRoomFirst

DB -int user_version() -void user_version(int value) -bool table_exists(const std::string &tableName) -std::string libversion() -void drop_table(const std::string &tableName) -int changes()??? -int total_changes()??? -int64 last_insert_rowid()??? -std::string current_timestamp(sqlite3 *db)???

Possible candidates with modification -void backup_table(sqlite3 *db) -void create_table(sqlite3 *db, const std::string &tableName) -void copy_table(sqlite3 *db, const std::string &name) -void rename_table(sqlite3 *db, const std::string &oldName, const std::string &newName)

ElephantInTheRoomFirst avatar Jun 14 '17 06:06 ElephantInTheRoomFirst

This is excess over-engineering. Separation will make interface complicated. If you do not need columns you can create a storage with no columns with auto emptyStorage = make_storage("empty.sqlite");

fnc12 avatar Jun 14 '17 06:06 fnc12

Thanks, let me try it.

ElephantInTheRoomFirst avatar Jun 14 '17 06:06 ElephantInTheRoomFirst

What will happen when I call select (i.e. CRUD) on this make_storage("empty.sqlite"); instance?

ElephantInTheRoomFirst avatar Jun 14 '17 06:06 ElephantInTheRoomFirst

You'll get compile time error with static_assert that tells you that type isn't mapped to the storage

fnc12 avatar Jun 14 '17 06:06 fnc12

auto DB = make_storage(GetDBPath());
DB.table_exists("TableNames");

sqlite_orm/sqlite_orm.h:5049: error: ‘sqlite_orm::storage_t<>::impl_type {aka struct sqlite_orm::storage_impl<>}’ has no member named ‘table_exists’ return this->impl.table_exists(tableName, db); ~~~~~~~~~~~^~~~~~~~~~~~

ElephantInTheRoomFirst avatar Jun 14 '17 07:06 ElephantInTheRoomFirst

Ok. I'll fix it now

fnc12 avatar Jun 14 '17 07:06 fnc12

Thanks, closed.

ElephantInTheRoomFirst avatar Jun 14 '17 07:06 ElephantInTheRoomFirst

When I calll sync_schema one Storage without tables, I do not get compile errors. These functions get called.

    template<class ...Ts>
    struct storage_impl {
        std::map<std::string, sync_schema_result> sync_schema(sqlite3 *, bool) {
            return {};
        }
        
        std::map<std::string, sync_schema_result> sync_schema_simulate(sqlite3 *, bool) {
            return {};
        }
};

ElephantInTheRoomFirst avatar Jun 14 '17 16:06 ElephantInTheRoomFirst

.

ElephantInTheRoomFirst avatar Jun 14 '17 16:06 ElephantInTheRoomFirst

That's correct. You get empty map cause there are no mapped types in a storage.

fnc12 avatar Jun 14 '17 17:06 fnc12

OK, Can you put assert there?

ElephantInTheRoomFirst avatar Jun 14 '17 17:06 ElephantInTheRoomFirst

No. It works correctly. sync_schema syncs tables. If there are no tables you get empty map and 0 tables are synced. There is nothing incorrect here.

fnc12 avatar Jun 14 '17 17:06 fnc12

OK

ElephantInTheRoomFirst avatar Jun 14 '17 17:06 ElephantInTheRoomFirst