SqliteConn.hpp 6.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  CERN
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

21
#include "common/threading/Mutex.hpp"
22
#include "rdbms/wrapper/ConnWrapper.hpp"
23

24
25
26
#include <sqlite3.h>

namespace cta {
27
namespace rdbms {
28
namespace wrapper {
29

30
/**
31
 * Forward declaration to avoid a circular dependency between SqliteConn and
32
33
34
35
 * SqliteStmt.
 */
class SqliteStmt;

36
/**
37
 * A convenience wrapper around a connection to an SQLite database.
38
 */
39
class SqliteConn: public ConnWrapper {
40
41
public:

42
43
44
45
46
47
  /**
   * The SqliteStmt class can lock the m_mutex member of the SqliteConn class
   * and it can read the pointer to the SQLite connection.
   */
  friend SqliteStmt;

48
49
50
  /**
   * Constructor.
   *
51
   * @param filename The filename to be passed to the sqlite3_open() function.
52
   */
53
  SqliteConn(const std::string &filename);
54
55
56
57

  /**
   * Destructor.
   */
58
  ~SqliteConn() override;
59

60
61
62
  /**
   * Idempotent close() method.  The destructor calls this method.
   */
63
  void close() override;
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  /**
   * Sets the autocommit mode of the connection.
   *
   * @param autocommitMode The autocommit mode of the connection.
   * @throw AutocommitModeNotSupported If the specified autocommit mode is not
   * supported.
   */
  void setAutocommitMode(const AutocommitMode autocommitMode) override;

  /**
   * Returns the autocommit mode of the connection.
   *
   * @return The autocommit mode of the connection.
   */
  AutocommitMode getAutocommitMode() const noexcept override;

81
82
83
84
85
  /**
   * Executes the statement.
   *
   * @param sql The SQL statement.
   */
86
  void executeNonQuery(const std::string &sql) override;
87

88
  /**
89
   * Creates a prepared statement.
90
   *
91
   * @param sql The SQL statement.
92
93
   * @return The prepared statement.
   */
94
  std::unique_ptr<StmtWrapper> createStmt(const std::string &sql) override;
95

96
97
98
  /**
   * Commits the current transaction.
   */
99
  void commit() override;
100
101
102
103

  /**
   * Rolls back the current transaction.
   */
104
  void rollback() override;
105

106
107
108
109
110
111
112
113
114
  /**
   * Returns the names of all the column and their type as a map for the given 
   * table in the database schema.
   *
   * @param tableName The table name to get the columns.
   * @return The map of types by name of all the columns for the given table in the database schema.
   */
  std::map<std::string, std::string> getColumns(const std::string &tableName) override;
  
Steven Murray's avatar
Steven Murray committed
115
116
117
118
119
120
121
  /**
   * Returns the names of all the tables in the database schema in alphabetical
   * order.
   *
   * @return The names of all the tables in the database schema in alphabetical
   * order.
   */
122
  std::list<std::string> getTableNames() override;
123
124
125
126
127
128
129
130
131
132
  
  
  /**
   * Returns the names of all the indices in the database schema in alphabetical
   * order.
   *
   * @return The names of all the indices in the database schema in alphabetical
   * order.
   */
  std::list<std::string> getIndexNames() override;
133
134
135
136
137

  /**
   * Returns true if this connection is open.
   */
  bool isOpen() const override;
Steven Murray's avatar
Steven Murray committed
138

139
140
141
142
143
144
145
146
147
148
149
150
  /**
   * Returns the names of all the sequences in the database schema in
   * alphabetical order.
   *
   * If the underlying database technologies does not supported sequences then
   * this method simply returns an empty list.
   *
   * @return The names of all the sequences in the database schema in
   * alphabetical order.
   */
  std::list<std::string> getSequenceNames() override;

151
152
153
154
155
156
157
158
159
160
161
162
  /**
   * Returns the names of all the triggers in the database schema in
   * alphabetical order.
   *
   * If the underlying database technologies does not supported triggers then
   * this method simply returns an empty list.
   *
   * @return The names of all the triggers in the database schema in
   * alphabetical order.
   */
  std::list<std::string> getTriggerNames() override;
  
163
164
165
166
167
168
169
170
171
172
173
174
  /**
   * Returns the names of all the tables that have been set as PARALLEL
   * in alphabetical order.
   * 
   * If the underlying database technologies does not support PARALLEL
   * them this method simply returns an empty list.
   * 
   * @return the names of all the tables that have been set as PARALLEL
   * in alphabetical order. 
   */
  std::list<std::string> getParallelTableNames() override;
  
175
176
177
178
179
180
181
  /**
   * Returns the Constraint names of a given table in the database schema
   * @param tableName the table name to get the constraint names from
   * @return the list of the names of the constraints that the given table has.
   */
  std::list<std::string> getConstraintNames(const std::string &tableName) override;
  
182
183
184
185
186
187
188
189
190
191
192
  /**
   * 
   * Returns the stored procedure names of the database
   * 
   * If the underlying database technologies does not support stored procedures informations
   * this method simply returns an empty list.
   * 
   * @return the list of the names of the stored procedures in the database
   */
  std::list<std::string> getStoredProcedureNames() override;

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  /**
   * Returns the synonym names of the database
   * 
   * If the underlying database technologies does not support synonym informations
   * this method simply returns an empty list.
   * 
   * @return the list of the names of the synonyms in the database
   */
  std::list<std::string> getSynonymNames() override;
  
  /**
   * Returns the type names of the database
   * 
   * If the underlying database technologies does not support type informations
   * this method simply returns an empty list.
   * 
   * @return the list of the names of the types in the database
   */
  std::list<std::string> getTypeNames() override;
212
  
213
  /**
214
   * This is an SqliteConn specific method that prints the database schema to
215
   * the specified output stream.
216
   *
217
218
219
220
   * Please note that this method is intended to be used for debugging.  The
   * output is subjectively not pretty.
   *
   * @param os The output stream.
221
   */
222
  void printSchema(std::ostream &os);
223

224
225
private:

226
  /**
227
   * Mutex used to serialize access to the database connection.
228
   */
229
  threading::Mutex m_mutex;
230

231
  /**
232
   * The database connection.
233
   */
234
  sqlite3 *m_sqliteConn;
235

236
}; // class SqliteConn
237

238
} // namespace wrapper
239
} // namespace rdbms
240
} // namespace cta