Mercurial > hg > orthanc-databases
comparison MySQL/Plugins/StoragePlugin.cpp @ 16:9e419261f1c9
mysql storage area working
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Jul 2018 10:10:35 +0200 |
parents | 17bce6a07b2b |
children | 54ea251aed70 |
comparison
equal
deleted
inserted
replaced
15:dfc7002add9c | 16:9e419261f1c9 |
---|---|
17 * You should have received a copy of the GNU Affero General Public License | 17 * You should have received a copy of the GNU Affero General Public License |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | 19 **/ |
20 | 20 |
21 | 21 |
22 #include "../../Framework/Plugins/StorageBackend.h" | |
23 | |
24 #include <Core/Logging.h> | |
25 | |
26 | |
27 #include "../../Framework/Common/Integer64Value.h" | |
22 #include "../../Framework/MySQL/MySQLDatabase.h" | 28 #include "../../Framework/MySQL/MySQLDatabase.h" |
23 #include "../../Framework/Plugins/StorageBackend.h" | 29 #include "../../Framework/MySQL/MySQLResult.h" |
24 | 30 #include "../../Framework/MySQL/MySQLStatement.h" |
25 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h> | 31 #include "../../Framework/MySQL/MySQLTransaction.h" |
26 #include <Core/Logging.h> | 32 |
33 #include <boost/math/special_functions/round.hpp> | |
34 | |
35 namespace OrthancDatabases | |
36 { | |
37 class MySQLStorageArea : public StorageBackend | |
38 { | |
39 private: | |
40 class Factory : public IDatabaseFactory | |
41 { | |
42 private: | |
43 MySQLStorageArea& that_; | |
44 | |
45 public: | |
46 Factory(MySQLStorageArea& that) : | |
47 that_(that) | |
48 { | |
49 } | |
50 | |
51 virtual Dialect GetDialect() const | |
52 { | |
53 return Dialect_MySQL; | |
54 } | |
55 | |
56 virtual IDatabase* Open() | |
57 { | |
58 return that_.OpenInternal(); | |
59 } | |
60 }; | |
61 | |
62 OrthancPluginContext* context_; | |
63 MySQLParameters parameters_; | |
64 bool clearAll_; | |
65 | |
66 IDatabase* OpenInternal() | |
67 { | |
68 std::auto_ptr<MySQLDatabase> db(new MySQLDatabase(parameters_)); | |
69 | |
70 db->Open(); | |
71 | |
72 if (parameters_.HasLock()) | |
73 { | |
74 db->AdvisoryLock(43 /* some arbitrary constant */); | |
75 } | |
76 | |
77 { | |
78 MySQLTransaction t(*db); | |
79 | |
80 int64_t size; | |
81 if (db->LookupGlobalIntegerVariable(size, "max_allowed_packet")) | |
82 { | |
83 int mb = boost::math::iround(static_cast<double>(size) / | |
84 static_cast<double>(1024 * 1024)); | |
85 LOG(WARNING) << "Your MySQL server cannot " | |
86 << "store DICOM files larger than " << mb << "MB"; | |
87 LOG(WARNING) << " => Consider increasing \"max_allowed_packet\" " | |
88 << "in \"my.cnf\" if this limit is insufficient for your use"; | |
89 } | |
90 else | |
91 { | |
92 LOG(WARNING) << "Unable to auto-detect the maximum size of DICOM " | |
93 << "files that can be stored in this MySQL server"; | |
94 } | |
95 | |
96 if (clearAll_) | |
97 { | |
98 db->Execute("DROP TABLE IF EXISTS StorageArea", false); | |
99 } | |
100 | |
101 db->Execute("CREATE TABLE IF NOT EXISTS StorageArea(" | |
102 "uuid VARCHAR(64) NOT NULL PRIMARY KEY," | |
103 "content LONGBLOB NOT NULL," | |
104 "type INTEGER NOT NULL)", false); | |
105 | |
106 t.Commit(); | |
107 } | |
108 | |
109 return db.release(); | |
110 } | |
111 | |
112 public: | |
113 MySQLStorageArea(const MySQLParameters& parameters) : | |
114 StorageBackend(new Factory(*this)), | |
115 parameters_(parameters), | |
116 clearAll_(false) | |
117 { | |
118 } | |
119 | |
120 void SetClearAll(bool clear) | |
121 { | |
122 clearAll_ = clear; | |
123 } | |
124 }; | |
125 } | |
126 | |
127 | |
27 | 128 |
28 | 129 |
29 static bool DisplayPerformanceWarning() | 130 static bool DisplayPerformanceWarning() |
30 { | 131 { |
31 (void) DisplayPerformanceWarning; // Disable warning about unused function | 132 (void) DisplayPerformanceWarning; // Disable warning about unused function |
78 return 0; | 179 return 0; |
79 } | 180 } |
80 | 181 |
81 try | 182 try |
82 { | 183 { |
83 // TODO | 184 OrthancDatabases::MySQLParameters parameters(mysql); |
84 //OrthancDatabases::StorageBackend::Register(); | 185 OrthancDatabases::StorageBackend::Register |
186 (context, new OrthancDatabases::MySQLStorageArea(parameters)); | |
85 } | 187 } |
86 catch (Orthanc::OrthancException& e) | 188 catch (Orthanc::OrthancException& e) |
87 { | 189 { |
88 LOG(ERROR) << e.What(); | 190 LOG(ERROR) << e.What(); |
89 return -1; | 191 return -1; |