SHARE
TWEET

Untitled

a guest Sep 10th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SqlPromise::SqlPromise(const QSqlQuery &query): m_state{std::make_shared<State>()}
  2. {
  3.     m_state->query = query;
  4. }
  5.  
  6. SqlPromise *SqlPromise::executor(std::function<void (QSqlQuery&)> exec)
  7. {
  8.     m_state->execute = exec;
  9.     return this;
  10. }
  11.  
  12. SqlPromise *SqlPromise::then(std::function<void (QSqlQuery)> then)
  13. {
  14.     m_state->resolve = then;
  15.     return this;
  16. }
  17.  
  18. SqlPromise *SqlPromise::reject(ErrorHandler reject)
  19. {
  20.     m_state->reject = reject;
  21.     return this;
  22. }
  23.  
  24. void SqlPromise::execute()
  25. {
  26.     try {
  27.         m_state->execute(m_state->query);
  28.     } catch (...) {
  29.         m_state->exception = std::current_exception();
  30.     }
  31. }
  32.  
  33. void SqlPromise::notify()
  34. {
  35.     if (m_state->exception) {
  36.         if (m_state->reject) {
  37.             m_state->reject(m_state->exception);
  38.         } else {
  39.             std::rethrow_exception(m_state->exception);
  40.         }
  41.     } else {
  42.         if (m_state->resolve)
  43.             m_state->resolve(m_state->query);
  44.     }
  45. }
  46.  
  47. DatabaseManager::DatabaseManager(QObject *parent): QObject(parent),
  48.     m_context(new DBContext)
  49. {
  50.     connect(&m_dbThread, &QThread::finished, m_context, &DBContext::deleteLater);
  51.     QObject::connect(this, &DatabaseManager::execute, m_context, [this](SqlPromise p){
  52.         p.execute();
  53.         emit this->ready(p);
  54.     });
  55.     QObject::connect(this, &DatabaseManager::execute, this, [](SqlPromise p){
  56.         p.notify();
  57.     });
  58. }
  59.  
  60. DatabaseManager &DatabaseManager::instance()
  61. {
  62.     static DatabaseManager manager;
  63.     return manager;
  64. }
  65.  
  66. SqlPromise DatabaseManager::exec(std::function<void (QSqlQuery&)> exec)
  67. {
  68.     SqlPromise promise(m_context->makeQuery());
  69.     promise.executor(exec);
  70.     emit execute(promise);
  71.     return promise;
  72. }
  73.  
  74. std::function<void (QSqlQuery)> sqlErrorsChecker(std::function<void (QSqlQuery)> nextHandler,
  75.                                                  ErrorWriterHandler reject)
  76. {
  77.     return [nextHandler, reject] (QSqlQuery q) {
  78.         QSqlError lastErr = q.lastError();
  79.         if (lastErr.type() == QSqlError::NoError) {
  80.             if (nextHandler) nextHandler(q);
  81.         } else {
  82.             if (reject) {
  83.                 reject([lastErr](std::function<void(QString)> errorWriter) {
  84.                     errorWriter(lastErr.text());
  85.                 });
  86.             }
  87.         }
  88.     };
  89. }
  90.  
  91. void executeQuery(std::function<void(QSqlQuery &)> executor,
  92.                   std::function<void (QSqlQuery)> resolve,
  93.                   ErrorWriterHandler reject)
  94. {
  95.     DatabaseManager::instance()
  96.             .exec(executor)
  97.             .then(sqlErrorsChecker(resolve, reject))
  98.             ->reject([reject](std::exception_ptr ex){
  99.         reject([ex](std::function<void(QString)> writer){
  100.             writer("Unexpected error");
  101.         });
  102.     });
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top