Advertisement
tinyevil

Untitled

Jul 13th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. class IDatabase{
  2.     virtual void execute(const char* statement) = 0;
  3.     virtual void beginTransaction() = 0;
  4.     virtual void commitTransaction() = 0;
  5.     virtual void rollbackTransaction() = 0;
  6. }
  7.  
  8. void initialize_db(IDatabase* db){
  9.     db->beginTransaction();
  10.     db->execute("CREATE TABLE foo ...");
  11.     ...
  12.     db->commitTransaction();
  13. }
  14.  
  15. class MysqlDatabase: public IDatabase { ...
  16.  
  17. class MockDatabase: public IDatabase { ...
  18.  
  19. class SQLiteDatabase: public IDatabase { ...
  20.  
  21. ========================
  22.  
  23. struct DatabaseVtable{
  24.     void (*execute) (void*, const char* statement);
  25.     void (*beginTransaction)(void*);
  26.     void (*commitTransaction)(void*);
  27.     void (*rollbackTransaction)(void*);
  28. };
  29.  
  30. struct Database{
  31.     const DatabaseVTable* vt;
  32. };
  33.  
  34. void initialize_db(Database* db){
  35.     db->vt->beginTransaction(db);
  36.     db->vt->execute(db, "CREATE TABLE foo ...");
  37.     ...
  38.     db->vt->commitTransaction(db);
  39. }
  40.  
  41.  
  42.  
  43. const DatabaseVtable MysqlDatabaseVTable = {
  44.     &mysql_execute,
  45.     &mysql_beinTransaction,
  46.     ...
  47. };
  48.  
  49. struct MysqlDatabase{
  50.     const DatabaseVTable* vt;
  51.     mysql_connection_t conn;
  52. }
  53.  
  54. void init_mysql_db(MysqlDatabase* db){
  55.     db->vt = &MysqlDatabaseVTable;
  56.     ....
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement