Mercurial > hg > orthanc-databases
annotate Framework/Odbc/OdbcDatabase.cpp @ 330:8f17f23c9af7
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Aug 2021 20:52:16 +0200 |
parents | b5fb8b77ce4d |
children | 79e21c33962d |
rev | line source |
---|---|
329
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 * the License, or (at your option) any later version. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * Affero General Public License for more details. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 **/ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 #include "OdbcDatabase.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 #include "../Common/ImplicitTransaction.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 #include "../Common/RetryDatabaseFactory.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 #include "../Common/Utf8StringValue.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 #include "OdbcPreparedStatement.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 #include "OdbcResult.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 #include <Logging.h> |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 #include <OrthancException.h> |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 #include <Toolbox.h> |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 #include <boost/algorithm/string/predicate.hpp> |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 #include <sqlext.h> |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 namespace OrthancDatabases |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 static void SetAutoCommitTransaction(SQLHDBC handle, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 bool autocommit) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 // Go to autocommit mode |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 SQLPOINTER value = (SQLPOINTER) (autocommit ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 if (!SQL_SUCCEEDED(SQLSetConnectAttr(handle, SQL_ATTR_AUTOCOMMIT, value, SQL_IS_UINTEGER))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 "Cannot switch the autocommit mode"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 class OdbcDatabase::OdbcImplicitTransaction : public ImplicitTransaction |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 private: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 OdbcDatabase& db_; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 protected: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 virtual IResult* ExecuteInternal(IPrecompiledStatement& statement, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 const Dictionary& parameters) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 return dynamic_cast<OdbcPreparedStatement&>(statement).Execute(parameters); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 virtual void ExecuteWithoutResultInternal(IPrecompiledStatement& statement, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 const Dictionary& parameters) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 std::unique_ptr<IResult> result(Execute(statement, parameters)); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 public: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 OdbcImplicitTransaction(OdbcDatabase& db) : |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 db_(db) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 SetAutoCommitTransaction(db_.GetHandle(), true); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 virtual bool DoesTableExist(const std::string& name) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 return db_.DoesTableExist(name.c_str()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 virtual bool DoesTriggerExist(const std::string& name) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 return false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
87 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 virtual void ExecuteMultiLines(const std::string& query) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 db_.ExecuteMultiLines(query); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 }; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 class OdbcDatabase::OdbcExplicitTransaction : public ITransaction |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 private: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
99 OdbcDatabase& db_; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
100 bool isOpen_; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
101 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 void EndTransaction(SQLSMALLINT completionType) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
103 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
104 if (!isOpen_) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
105 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
106 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, "Transaction is already finalized"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
107 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
108 else if (SQL_SUCCEEDED(SQLEndTran(SQL_HANDLE_DBC, db_.GetHandle(), completionType))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
109 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 isOpen_ = false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
111 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
113 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
114 SQLCHAR stateBuf[SQL_SQLSTATE_SIZE + 1]; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
115 SQLSMALLINT stateLength = 0; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
116 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
117 const SQLSMALLINT recNum = 1; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
118 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
119 if (SQL_SUCCEEDED(SQLGetDiagField(SQL_HANDLE_DBC, db_.GetHandle(), |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
120 recNum, SQL_DIAG_SQLSTATE, &stateBuf, sizeof(stateBuf), &stateLength))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
121 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
122 const std::string state(reinterpret_cast<const char*>(stateBuf)); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
123 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
124 if (state == "40001") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
125 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
126 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseCannotSerialize); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
127 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
128 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
129 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
130 switch (completionType) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
131 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
132 case SQL_COMMIT: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
133 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, "Cannot commit transaction"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
134 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
135 case SQL_ROLLBACK: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
136 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, "Cannot rollback transaction"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
137 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
138 default: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
142 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
143 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
144 public: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
145 OdbcExplicitTransaction(OdbcDatabase& db) : |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
146 db_(db), |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
147 isOpen_(true) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
148 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 SetAutoCommitTransaction(db_.GetHandle(), false); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
150 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
151 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
152 virtual ~OdbcExplicitTransaction() |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
153 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
154 if (isOpen_) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
155 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
156 LOG(INFO) << "An active ODBC transaction was dismissed"; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
157 if (!SQL_SUCCEEDED(SQLEndTran(SQL_HANDLE_DBC, db_.GetHandle(), SQL_ROLLBACK))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
158 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
159 LOG(ERROR) << "Cannot rollback transaction"; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
160 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
161 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
162 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
163 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
164 virtual bool IsImplicit() const ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
165 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
166 return false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
167 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
168 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
169 virtual void Commit() ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
170 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
171 EndTransaction(SQL_COMMIT); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
172 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
173 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
174 virtual void Rollback() ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
175 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
176 EndTransaction(SQL_ROLLBACK); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
177 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
178 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
179 virtual bool DoesTableExist(const std::string& name) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
180 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
181 return db_.DoesTableExist(name.c_str()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
182 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
183 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
184 virtual bool DoesTriggerExist(const std::string& name) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
185 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
186 return false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
187 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
188 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
189 virtual void ExecuteMultiLines(const std::string& query) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
190 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
191 db_.ExecuteMultiLines(query); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
192 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
193 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
194 virtual IResult* Execute(IPrecompiledStatement& statement, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
195 const Dictionary& parameters) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
196 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
197 return dynamic_cast<OdbcPreparedStatement&>(statement).Execute(parameters); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
198 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
199 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
200 virtual void ExecuteWithoutResult(IPrecompiledStatement& statement, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
201 const Dictionary& parameters) ORTHANC_OVERRIDE |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
202 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
203 std::unique_ptr<IResult> result(Execute(statement, parameters)); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
204 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
205 }; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
206 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
207 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
208 static bool ParseThreePartsVersion(unsigned int& majorVersion, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
209 const std::string& version) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
210 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
211 std::vector<std::string> tokens; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
212 Orthanc::Toolbox::TokenizeString(tokens, version, '.'); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
213 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
214 try |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
215 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
216 if (tokens.size() == 3u) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
217 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
218 int tmp = boost::lexical_cast<int>(tokens[0]); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
219 if (tmp >= 0) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
220 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
221 majorVersion = static_cast<unsigned int>(tmp); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
222 return true; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
223 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
224 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
225 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
226 catch (boost::bad_lexical_cast&) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
227 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
228 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
229 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
230 return false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
231 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
232 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
233 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
234 OdbcDatabase::OdbcDatabase(OdbcEnvironment& environment, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
235 const std::string& connectionString) : |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
236 dbmsMajorVersion_(0) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
237 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
238 LOG(INFO) << "Creating an ODBC connection: " << connectionString; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
239 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
240 /* Allocate a connection handle */ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
241 if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_DBC, environment.GetHandle(), &handle_))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
242 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
243 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
244 "Cannot create ODBC connection"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
245 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
246 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
247 /* Connect to the DSN mydsn */ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
248 SQLCHAR* tmp = const_cast<SQLCHAR*>(reinterpret_cast<const SQLCHAR*>(connectionString.c_str())); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
249 SQLCHAR outBuffer[2048]; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
250 SQLSMALLINT outSize = 0; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
251 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
252 bool success = true; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
253 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
254 if (SQL_SUCCEEDED(SQLDriverConnect(handle_, NULL, tmp, SQL_NTS /* null-terminated string */, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
255 outBuffer, sizeof(outBuffer), &outSize, SQL_DRIVER_COMPLETE))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
256 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
257 LOG(INFO) << "Returned connection string: " << outBuffer; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
258 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
259 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
260 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
261 success = false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
262 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
263 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
264 if (!SQL_SUCCEEDED(SQLSetConnectAttr(handle_, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER) SQL_TXN_SERIALIZABLE, SQL_NTS))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
265 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
266 /** |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
267 * Switch to the "serializable" isolation level that is expected |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
268 * by Orthanc. This is already the default for MySQL and MSSQL, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
269 * but is needed for PostgreSQL. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
270 * https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/transaction-isolation-levels |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
271 **/ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
272 success = false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
273 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
274 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
275 SQLCHAR versionBuffer[2048]; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
276 SQLSMALLINT versionSize; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
277 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
278 if (success && |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
279 SQL_SUCCEEDED(SQLGetInfo(handle_, SQL_DBMS_NAME, outBuffer, sizeof(outBuffer) - 1, &outSize)) && |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
280 SQL_SUCCEEDED(SQLGetInfo(handle_, SQL_DBMS_VER, versionBuffer, sizeof(versionBuffer) - 1, &versionSize))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
281 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
282 std::string dbms(reinterpret_cast<const char*>(outBuffer), outSize); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
283 std::string version(reinterpret_cast<const char*>(versionBuffer), versionSize); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
284 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
285 LOG(WARNING) << "DBMS Name: " << dbms; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
286 LOG(WARNING) << "DBMS Version: " << version; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
287 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
288 if (dbms == "PostgreSQL") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
289 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
290 dialect_ = Dialect_PostgreSQL; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
291 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
292 else if (dbms == "SQLite") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
293 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
294 dialect_ = Dialect_SQLite; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
295 ExecuteMultiLines("PRAGMA FOREIGN_KEYS=ON"); // Necessary for cascaded delete to work |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
296 ExecuteMultiLines("PRAGMA ENCODING=\"UTF-8\""); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
297 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
298 // The following lines speed up SQLite |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
299 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
300 /*ExecuteMultiLines("PRAGMA SYNCHRONOUS=NORMAL;"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
301 ExecuteMultiLines("PRAGMA JOURNAL_MODE=WAL;"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
302 ExecuteMultiLines("PRAGMA LOCKING_MODE=EXCLUSIVE;"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
303 ExecuteMultiLines("PRAGMA WAL_AUTOCHECKPOINT=1000;");*/ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
304 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
305 else if (dbms == "MySQL") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
306 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
307 dialect_ = Dialect_MySQL; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
308 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
309 if (!ParseThreePartsVersion(dbmsMajorVersion_, version)) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
310 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
311 SQLFreeHandle(SQL_HANDLE_DBC, handle_); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
312 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, "Cannot parse the version of MySQL: " + version); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
313 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
314 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
315 else if (dbms == "Microsoft SQL Server") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
316 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
317 dialect_ = Dialect_MSSQL; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
318 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
319 if (!ParseThreePartsVersion(dbmsMajorVersion_, version)) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
320 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
321 SQLFreeHandle(SQL_HANDLE_DBC, handle_); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
322 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, "Cannot parse the version of SQL Server: " + version); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
323 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
324 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
325 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
326 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
327 SQLFreeHandle(SQL_HANDLE_DBC, handle_); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
328 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, "Unknown SQL dialect for DBMS: " + dbms); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
329 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
330 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
331 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
332 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
333 success = false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
334 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
335 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
336 if (!success) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
337 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
338 std::string error = FormatError(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
339 SQLFreeHandle(SQL_HANDLE_DBC, handle_); // Cannot call FormatError() below this point |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
340 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
341 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable, "Error in SQLDriverConnect():\n" + error); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
342 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
343 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
344 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
345 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
346 OdbcDatabase::~OdbcDatabase() |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
347 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
348 LOG(INFO) << "Destructing an ODBC connection"; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
349 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
350 if (!SQL_SUCCEEDED(SQLDisconnect(handle_))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
351 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
352 LOG(ERROR) << "Cannot disconnect from driver"; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
353 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
354 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
355 if (!SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_DBC, handle_))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
356 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
357 LOG(ERROR) << "Cannot destruct the ODBC connection"; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
358 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
359 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
360 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
361 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
362 std::string OdbcDatabase::FormatError() |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
363 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
364 return OdbcEnvironment::FormatError(handle_, SQL_HANDLE_DBC); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
365 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
366 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
367 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
368 void OdbcDatabase::ListTables(std::set<std::string>& target) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
369 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
370 target.clear(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
371 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
372 OdbcStatement statement(GetHandle()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
373 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
374 if (SQL_SUCCEEDED(SQLTables(statement.GetHandle(), NULL, 0, NULL, 0, NULL, 0, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
375 const_cast<SQLCHAR*>(reinterpret_cast<const SQLCHAR*>("'TABLE'")), SQL_NTS))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
376 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
377 OdbcResult result(statement, dialect_); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
378 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
379 while (!result.IsDone()) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
380 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
381 if (result.GetFieldsCount() < 5) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
382 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
383 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, "Invalid result for SQLTables()"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
384 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
385 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
386 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
387 if (result.GetField(2).GetType() == ValueType_Utf8String && |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
388 result.GetField(3).GetType() == ValueType_Utf8String && |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
389 dynamic_cast<const Utf8StringValue&>(result.GetField(3)).GetContent() == "TABLE") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
390 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
391 std::string name = dynamic_cast<const Utf8StringValue&>(result.GetField(2)).GetContent(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
392 Orthanc::Toolbox::ToLowerCase(name); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
393 target.insert(name); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
394 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
395 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
396 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
397 result.Next(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
398 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
399 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
400 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
401 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
402 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
403 bool OdbcDatabase::DoesTableExist(const std::string& name) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
404 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
405 std::set<std::string> tables; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
406 ListTables(tables); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
407 return (tables.find(name) != tables.end()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
408 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
409 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
410 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
411 void OdbcDatabase::ExecuteMultiLines(const std::string& query) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
412 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
413 OdbcStatement statement(GetHandle()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
414 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
415 std::vector<std::string> lines; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
416 Orthanc::Toolbox::TokenizeString(lines, query, ';'); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
417 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
418 for (size_t i = 0; i < lines.size(); i++) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
419 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
420 std::string line = Orthanc::Toolbox::StripSpaces(lines[i]); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
421 if (!line.empty()) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
422 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
423 LOG(INFO) << "Running ODBC SQL: " << line; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
424 SQLCHAR* tmp = const_cast<SQLCHAR*>(reinterpret_cast<const SQLCHAR*>(line.c_str())); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
425 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
426 SQLRETURN code = SQLExecDirect(statement.GetHandle(), tmp, SQL_NTS); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
427 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
428 if (code != SQL_NO_DATA && |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
429 code != SQL_SUCCESS && |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
430 code != SQL_SUCCESS_WITH_INFO) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
431 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
432 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
433 "Cannot execute multi-line SQL:\n" + statement.FormatError()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
434 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
435 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
436 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
437 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
438 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
439 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
440 IPrecompiledStatement* OdbcDatabase::Compile(const Query& query) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
441 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
442 return new OdbcPreparedStatement(GetHandle(), GetDialect(), query); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
443 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
444 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
445 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
446 ITransaction* OdbcDatabase::CreateTransaction(TransactionType type) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
447 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
448 /** |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
449 * In ODBC, there is no "START TRANSACTION". A transaction is |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
450 * automatically created with each connection, and the "READ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
451 * ONLY" status can only be set at the statement level |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
452 * (cf. SQL_CONCUR_READ_ONLY). One can only control the |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
453 * autocommit: https://stackoverflow.com/a/35351267/881731 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
454 **/ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
455 switch (type) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
456 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
457 case TransactionType_Implicit: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
458 return new OdbcImplicitTransaction(*this); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
459 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
460 case TransactionType_ReadWrite: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
461 case TransactionType_ReadOnly: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
462 return new OdbcExplicitTransaction(*this); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
463 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
464 default: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
465 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
466 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
467 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
468 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
469 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
470 unsigned int OdbcDatabase::GetDbmsMajorVersion() const |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
471 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
472 return dbmsMajorVersion_; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
473 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
474 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
475 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
476 IDatabaseFactory* OdbcDatabase::CreateDatabaseFactory(unsigned int maxConnectionRetries, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
477 unsigned int connectionRetryInterval, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
478 const std::string& connectionString, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
479 bool checkEncodings) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
480 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
481 class Factory : public RetryDatabaseFactory |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
482 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
483 private: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
484 OdbcEnvironment environment_; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
485 std::string connectionString_; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
486 bool checkEncodings_; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
487 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
488 bool LookupConnectionOption(std::string& value, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
489 const std::string& option) const |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
490 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
491 std::vector<std::string> tokens; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
492 Orthanc::Toolbox::TokenizeString(tokens, connectionString_, ';'); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
493 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
494 for (size_t i = 0; i < tokens.size(); i++) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
495 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
496 if (boost::starts_with(tokens[i], option + "=")) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
497 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
498 value = tokens[i]; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
499 return true; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
500 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
501 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
502 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
503 return false; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
504 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
505 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
506 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
507 void CheckMSSQLEncodings(OdbcDatabase& db) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
508 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
509 // https://en.wikipedia.org/wiki/History_of_Microsoft_SQL_Server |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
510 if (db.GetDbmsMajorVersion() <= 14) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
511 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
512 // Microsoft SQL Server up to 2017 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
513 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
514 std::string value; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
515 if (LookupConnectionOption(value, "AutoTranslate")) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
516 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
517 if (value != "AutoTranslate=no") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
518 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
519 LOG(WARNING) << "For UTF-8 to work properly, it is strongly advised to set \"AutoTranslate=no\" in the " |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
520 << "ODBC connection string when connecting to Microsoft SQL Server with version <= 2017"; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
521 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
522 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
523 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
524 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
525 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
526 "Your Microsoft SQL Server has version <= 2017, and thus doesn't support UTF-8; " |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
527 "Please upgrade or add \"AutoTranslate=no\" to your ODBC connection string"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
528 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
529 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
530 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
531 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
532 std::string value; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
533 if (LookupConnectionOption(value, "AutoTranslate") && |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
534 value != "AutoTranslate=yes") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
535 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
536 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
537 "Your Microsoft SQL Server has version >= 2019, and thus fully supports UTF-8; " |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
538 "Please set \"AutoTranslate=yes\" in your ODBC connection string"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
539 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
540 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
541 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
542 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
543 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
544 void CheckMySQLEncodings(OdbcDatabase& db) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
545 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
546 // https://dev.mysql.com/doc/connector-odbc/en/connector-odbc-configuration-connection-parameters.html |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
547 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
548 std::string value; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
549 if (LookupConnectionOption(value, "charset")) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
550 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
551 if (value != "charset=utf8") |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
552 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
553 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
554 "For compatibility with UTF-8 in Orthanc, your connection string to MySQL " |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
555 "must *not* set the \"charset\" option to another value than \"utf8\""); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
556 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
557 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
558 else if (db.GetDbmsMajorVersion() < 8) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
559 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
560 // MySQL up to 5.7 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
561 LOG(WARNING) << "It is advised to set the \"charset=utf8\" option in your connection string if using MySQL <= 5.7"; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
562 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
563 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
564 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
565 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
566 "For compatibility with UTF-8 in Orthanc, your connection string to MySQL >= 8.0 " |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
567 "*must* set the \"charset=utf8\" in your connection string"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
568 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
569 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
570 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
571 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
572 protected: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
573 IDatabase* TryOpen() |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
574 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
575 std::unique_ptr<OdbcDatabase> db(new OdbcDatabase(environment_, connectionString_)); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
576 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
577 if (checkEncodings_) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
578 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
579 switch (db->GetDialect()) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
580 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
581 case Dialect_MSSQL: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
582 CheckMSSQLEncodings(*db); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
583 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
584 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
585 case Dialect_MySQL: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
586 CheckMySQLEncodings(*db); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
587 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
588 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
589 case Dialect_SQLite: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
590 case Dialect_PostgreSQL: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
591 // Nothing specific to be checked wrt. encodings |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
592 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
593 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
594 default: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
595 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
596 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
597 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
598 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
599 if (db->GetDbmsMajorVersion() >= 15) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
600 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
601 /** |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
602 * SQL Server 2019 introduces support for UTF-8. Note that |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
603 * "ALTER" cannot be run inside a transaction, and must be |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
604 * done *before* the creation of the tables. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
605 * https://docs.microsoft.com/en-US/sql/relational-databases/collations/collation-and-unicode-support#utf8 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
606 * |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
607 * Furthermore, this call must be done by both |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
608 * "odbc-index" and "odbc-storage" plugins, because |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
609 * altering collation is an operation that requires |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
610 * exclusive lock: If "odbc-storage" is the first plugin |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
611 * to be loaded and doesn't set the UTF-8 collation, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
612 * "odbc-index" cannot start because it doesn't have |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
613 * exclusive access. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
614 **/ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
615 db->ExecuteMultiLines("ALTER DATABASE CURRENT COLLATE LATIN1_GENERAL_100_CI_AS_SC_UTF8"); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
616 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
617 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
618 return db.release(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
619 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
620 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
621 public: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
622 Factory(unsigned int maxConnectionRetries, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
623 unsigned int connectionRetryInterval, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
624 const std::string& connectionString, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
625 bool checkEncodings) : |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
626 RetryDatabaseFactory(maxConnectionRetries, connectionRetryInterval), |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
627 connectionString_(connectionString), |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
628 checkEncodings_(checkEncodings) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
629 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
630 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
631 }; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
632 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
633 return new Factory(maxConnectionRetries, connectionRetryInterval, connectionString, checkEncodings); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
634 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
635 } |