comparison Framework/Common/DatabaseManager.h @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children 41543239072d
comparison
equal deleted inserted replaced
-1:000000000000 0:7cea966b6829
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
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/>.
19 **/
20
21
22 #pragma once
23
24 #include "IDatabaseFactory.h"
25 #include "StatementLocation.h"
26
27 #include <Core/Enumerations.h>
28
29 #include <boost/thread/recursive_mutex.hpp>
30 #include <memory>
31
32 namespace OrthancDatabases
33 {
34 class DatabaseManager : public boost::noncopyable
35 {
36 private:
37 typedef std::map<StatementLocation, IPrecompiledStatement*> CachedStatements;
38
39 boost::recursive_mutex mutex_;
40 std::auto_ptr<IDatabaseFactory> factory_;
41 std::auto_ptr<IDatabase> database_;
42 std::auto_ptr<ITransaction> transaction_;
43 CachedStatements cachedStatements_;
44 Dialect dialect_;
45
46 IDatabase& GetDatabase();
47
48 void CloseIfUnavailable(Orthanc::ErrorCode e);
49
50 IPrecompiledStatement* LookupCachedStatement(const StatementLocation& location) const;
51
52 IPrecompiledStatement& CacheStatement(const StatementLocation& location,
53 const Query& query);
54
55 ITransaction& GetTransaction();
56
57 public:
58 DatabaseManager(IDatabaseFactory* factory); // Takes ownership
59
60 ~DatabaseManager()
61 {
62 Close();
63 }
64
65 Dialect GetDialect() const
66 {
67 return dialect_;
68 }
69
70 void Open()
71 {
72 GetDatabase();
73 }
74
75 void Close();
76
77 void StartTransaction();
78
79 void CommitTransaction();
80
81 void RollbackTransaction();
82
83 class CachedStatement : public boost::noncopyable
84 {
85 private:
86 boost::recursive_mutex::scoped_lock lock_;
87 DatabaseManager& manager_;
88 StatementLocation location_;
89 IDatabase& database_;
90 ITransaction& transaction_;
91 IPrecompiledStatement* statement_;
92 std::auto_ptr<Query> query_;
93 std::auto_ptr<IResult> result_;
94
95 IResult& GetResult() const;
96
97 public:
98 CachedStatement(const StatementLocation& location,
99 DatabaseManager& manager,
100 const char* sql);
101
102 void SetReadOnly(bool readOnly);
103
104 void SetParameterType(const std::string& parameter,
105 ValueType type);
106
107 void Execute();
108
109 void Execute(const Dictionary& parameters);
110
111 IDatabase& GetDatabase()
112 {
113 return database_;
114 }
115
116 bool IsDone() const;
117
118 void Next();
119
120 size_t GetResultFieldsCount() const;
121
122 void SetResultFieldType(size_t field,
123 ValueType type);
124
125 const IValue& GetResultField(size_t index) const;
126 };
127 };
128 }