comparison SQLite/Plugins/SQLiteIndex.cpp @ 226:a4918d57435c

DatabaseManager doesn't IDatabaseFactory anymore
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 02 Apr 2021 19:23:36 +0200
parents 94c9908e6aca
children 35598014f140
comparison
equal deleted inserted replaced
225:94c9908e6aca 226:a4918d57435c
32 #include <Logging.h> 32 #include <Logging.h>
33 #include <OrthancException.h> 33 #include <OrthancException.h>
34 34
35 namespace OrthancDatabases 35 namespace OrthancDatabases
36 { 36 {
37 IDatabase* SQLiteIndex::OpenInternal() 37 IDatabase* SQLiteIndex::OpenDatabaseConnection()
38 { 38 {
39 std::unique_ptr<SQLiteDatabase> db(new SQLiteDatabase);
40
41 if (path_.empty())
42 {
43 db->OpenInMemory();
44 }
45 else
46 {
47 db->Open(path_);
48 }
49
50 return db.release();
51 }
52
53
54 void SQLiteIndex::ConfigureDatabase(IDatabase& database)
55 {
56 SQLiteDatabase& db = dynamic_cast<SQLiteDatabase&>(database);
57
39 uint32_t expectedVersion = 6; 58 uint32_t expectedVersion = 6;
40 59
41 if (GetContext()) // "GetContext()" can possibly be NULL in the unit tests 60 if (GetContext()) // "GetContext()" can possibly be NULL in the unit tests
42 { 61 {
43 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(GetContext()); 62 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(GetContext());
50 << "expecting the DB schema version " << expectedVersion 69 << "expecting the DB schema version " << expectedVersion
51 << ", but this plugin is only compatible with version 6"; 70 << ", but this plugin is only compatible with version 6";
52 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin); 71 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
53 } 72 }
54 73
55 74 {
56 std::unique_ptr<SQLiteDatabase> db(new SQLiteDatabase); 75 SQLiteTransaction t(db);
57 76
58 if (path_.empty()) 77 if (!db.DoesTableExist("Resources"))
59 {
60 db->OpenInMemory();
61 }
62 else
63 {
64 db->Open(path_);
65 }
66
67 {
68 SQLiteTransaction t(*db);
69
70 if (!db->DoesTableExist("Resources"))
71 { 78 {
72 std::string query; 79 std::string query;
73 80
74 Orthanc::EmbeddedResources::GetFileResource 81 Orthanc::EmbeddedResources::GetFileResource
75 (query, Orthanc::EmbeddedResources::SQLITE_PREPARE_INDEX); 82 (query, Orthanc::EmbeddedResources::SQLITE_PREPARE_INDEX);
76 db->Execute(query); 83 db.Execute(query);
77 84
78 SetGlobalIntegerProperty(*db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion); 85 SetGlobalIntegerProperty(db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion);
79 SetGlobalIntegerProperty(*db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, 1); 86 SetGlobalIntegerProperty(db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, 1);
80 } 87 }
81 88
82 t.Commit(); 89 t.Commit();
83 } 90 }
84 91
85 db->Execute("PRAGMA ENCODING=\"UTF-8\";"); 92 db.Execute("PRAGMA ENCODING=\"UTF-8\";");
86 93
87 if (fast_) 94 if (fast_)
88 { 95 {
89 // Performance tuning of SQLite with PRAGMAs 96 // Performance tuning of SQLite with PRAGMAs
90 // http://www.sqlite.org/pragma.html 97 // http://www.sqlite.org/pragma.html
91 db->Execute("PRAGMA SYNCHRONOUS=NORMAL;"); 98 db.Execute("PRAGMA SYNCHRONOUS=NORMAL;");
92 db->Execute("PRAGMA JOURNAL_MODE=WAL;"); 99 db.Execute("PRAGMA JOURNAL_MODE=WAL;");
93 db->Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;"); 100 db.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;");
94 db->Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;"); 101 db.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;");
95 //db->Execute("PRAGMA TEMP_STORE=memory"); 102 //db.Execute("PRAGMA TEMP_STORE=memory");
96 } 103 }
97 104
98 { 105 {
99 SQLiteTransaction t(*db); 106 SQLiteTransaction t(db);
100 107
101 if (!db->DoesTableExist("Resources")) 108 if (!db.DoesTableExist("Resources"))
102 { 109 {
103 LOG(ERROR) << "Corrupted SQLite database"; 110 LOG(ERROR) << "Corrupted SQLite database";
104 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 111 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
105 } 112 }
106 113
107 int version = 0; 114 int version = 0;
108 if (!LookupGlobalIntegerProperty(version, *db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion) || 115 if (!LookupGlobalIntegerProperty(version, db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion) ||
109 version != 6) 116 version != 6)
110 { 117 {
111 LOG(ERROR) << "SQLite plugin is incompatible with database schema version: " << version; 118 LOG(ERROR) << "SQLite plugin is incompatible with database schema version: " << version;
112 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); 119 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
113 } 120 }
114 121
115 int revision; 122 int revision;
116 if (!LookupGlobalIntegerProperty(revision, *db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel)) 123 if (!LookupGlobalIntegerProperty(revision, db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel))
117 { 124 {
118 revision = 1; 125 revision = 1;
119 SetGlobalIntegerProperty(*db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, revision); 126 SetGlobalIntegerProperty(db, t, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, revision);
120 } 127 }
121 128
122 if (revision != 1) 129 if (revision != 1)
123 { 130 {
124 LOG(ERROR) << "SQLite plugin is incompatible with database schema revision: " << revision; 131 LOG(ERROR) << "SQLite plugin is incompatible with database schema revision: " << revision;
125 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); 132 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
126 } 133 }
127 134
128 t.Commit(); 135 t.Commit();
129 } 136 }
130
131 return db.release();
132 } 137 }
133 138
134 139
135 SQLiteIndex::SQLiteIndex(OrthancPluginContext* context, 140 SQLiteIndex::SQLiteIndex(OrthancPluginContext* context,
136 const std::string& path) : 141 const std::string& path) :