1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "DbiTest.h"
23
24 #include <QDir>
25
26 #include <U2Core/AppContext.h>
27 #include <U2Core/AppSettings.h>
28 #include <U2Core/U2DbiRegistry.h>
29 #include <U2Core/U2OpStatusUtils.h>
30 #include <U2Core/U2SafePoints.h>
31
32 #include <U2Test/TestRunnerSettings.h>
33
34 namespace U2 {
35
TestDbiProvider()36 TestDbiProvider::TestDbiProvider()
37 : useConnectionPool(false) {
38 initialized = false;
39 dbi = nullptr;
40 }
~TestDbiProvider()41 TestDbiProvider::~TestDbiProvider() {
42 close();
43 }
init(const QString & dbiFileName,bool _useConnectionPool)44 bool TestDbiProvider::init(const QString &dbiFileName, bool _useConnectionPool) {
45 if (initialized) {
46 close();
47 initialized = false;
48 }
49
50 TestRunnerSettings *trs = AppContext::getAppSettings()->getTestRunnerSettings();
51 QString originalFile = trs->getVar("COMMON_DATA_DIR") + "/unit_tests/" + dbiFileName;
52
53 QString tmpFile = QDir::temp().absoluteFilePath(QFileInfo(originalFile).fileName());
54
55 if (QFile::exists(tmpFile)) {
56 QFile::remove(tmpFile);
57 }
58
59 bool _create = false;
60 if (QFile::exists(originalFile)) {
61 SAFE_POINT(QFile::copy(originalFile, tmpFile), "db file not copied", false);
62 } else {
63 _create = true;
64 }
65
66 dbUrl = tmpFile;
67 useConnectionPool = _useConnectionPool;
68
69 U2DbiFactory *factory = AppContext::getDbiRegistry()->getDbiFactoryById(SQLITE_DBI_ID);
70 SAFE_POINT(factory != nullptr, "No dbi factory", false);
71 U2OpStatusImpl opStatus;
72
73 if (useConnectionPool) {
74 U2DbiRef ref;
75 ref.dbiFactoryId = factory->getId();
76 ref.dbiId = dbUrl;
77 dbi = AppContext::getDbiRegistry()->getGlobalDbiPool()->openDbi(ref, _create, opStatus);
78 CHECK_OP(opStatus, false);
79 } else {
80 dbi = factory->createDbi();
81 SAFE_POINT(nullptr != dbi, "dbi not created", false);
82 QHash<QString, QString> properties;
83 if (_create) {
84 properties[U2DbiOptions::U2_DBI_OPTION_CREATE] = U2DbiOptions::U2_DBI_VALUE_ON;
85 }
86 properties["url"] = dbUrl;
87 QVariantMap persistentData;
88 dbi->init(properties, persistentData, opStatus);
89 SAFE_POINT_OP(opStatus, false);
90 }
91 U2ObjectDbi *objDbi = dbi->getObjectDbi();
92 SAFE_POINT(nullptr != objDbi, "object dbi not loaded", false);
93
94 initialized = true;
95 return true;
96 }
close()97 void TestDbiProvider::close() {
98 U2OpStatusImpl opStatus;
99 if (dbi) {
100 if (useConnectionPool) {
101 U2DbiRegistry *dbiReg = AppContext::getDbiRegistry();
102 if (nullptr != dbiReg) {
103 dbiReg->getGlobalDbiPool()->releaseDbi(dbi, opStatus);
104 }
105 } else {
106 dbi->shutdown(opStatus);
107 SAFE_POINT_OP(opStatus, );
108 delete dbi;
109 }
110 }
111 dbi = nullptr;
112 initialized = false;
113 }
getDbi()114 U2Dbi *TestDbiProvider::getDbi() {
115 SAFE_POINT(initialized, "Dbi Provider is not initialized", nullptr);
116 return dbi;
117 }
118
119 } // namespace U2
120