Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class IDatabase{
- virtual void execute(const char* statement) = 0;
- virtual void beginTransaction() = 0;
- virtual void commitTransaction() = 0;
- virtual void rollbackTransaction() = 0;
- }
- void initialize_db(IDatabase* db){
- db->beginTransaction();
- db->execute("CREATE TABLE foo ...");
- ...
- db->commitTransaction();
- }
- class MysqlDatabase: public IDatabase { ...
- class MockDatabase: public IDatabase { ...
- class SQLiteDatabase: public IDatabase { ...
- ========================
- struct DatabaseVtable{
- void (*execute) (void*, const char* statement);
- void (*beginTransaction)(void*);
- void (*commitTransaction)(void*);
- void (*rollbackTransaction)(void*);
- };
- struct Database{
- const DatabaseVTable* vt;
- };
- void initialize_db(Database* db){
- db->vt->beginTransaction(db);
- db->vt->execute(db, "CREATE TABLE foo ...");
- ...
- db->vt->commitTransaction(db);
- }
- const DatabaseVtable MysqlDatabaseVTable = {
- &mysql_execute,
- &mysql_beinTransaction,
- ...
- };
- struct MysqlDatabase{
- const DatabaseVTable* vt;
- mysql_connection_t conn;
- }
- void init_mysql_db(MysqlDatabase* db){
- db->vt = &MysqlDatabaseVTable;
- ....
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement