1 #include "dbsqlitecipherinstance.h"
2 #include "dbsqlitecipher.h"
3 #include <QDebug>
4 
DbSqliteCipherInstance(const QString & name,const QString & path,const QHash<QString,QVariant> & connOptions)5 DbSqliteCipherInstance::DbSqliteCipherInstance(const QString& name, const QString& path, const QHash<QString, QVariant>& connOptions) :
6     AbstractDb3<SqlCipher>(name, path, connOptions)
7 {
8 }
9 
initAfterOpen()10 void DbSqliteCipherInstance::initAfterOpen()
11 {
12     SqlQueryPtr res;
13 
14     QString key = connOptions[DbSqliteCipher::PASSWORD_OPT].toString();
15     if (!key.isEmpty())
16     {
17         res = exec(QString("PRAGMA key = '%1';").arg(key), Flag::NO_LOCK);
18         if (res->isError())
19             qWarning() << "Error while defining SQLCipher key:" << res->getErrorText();
20     }
21 
22     QString pragmas = connOptions[DbSqliteCipher::PRAGMAS_OPT].toString();
23     QStringList pragmaList = quickSplitQueries(pragmas);
24     for (const QString& pragma : pragmaList)
25     {
26         res = exec(pragma, Flag::NO_LOCK);
27         if (res->isError())
28             qWarning() << "Error while defining SQLCipher pragma" << pragma << ":" << res->getErrorText();
29     }
30 
31     AbstractDb3<SqlCipher>::initAfterOpen();
32 }
33 
getAttachSql(Db * otherDb,const QString & generatedAttachName)34 QString DbSqliteCipherInstance::getAttachSql(Db* otherDb, const QString& generatedAttachName)
35 {
36     QString pass = "";
37     if (otherDb->getConnectionOptions().contains(DbSqliteCipher::PASSWORD_OPT))
38         pass = otherDb->getConnectionOptions()[DbSqliteCipher::PASSWORD_OPT].toString().replace("'", "''");
39 
40     return QString("ATTACH '%1' AS %2 KEY '%3';").arg(otherDb->getPath(), generatedAttachName, pass);
41 }
42