Conn.hpp 4.79 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
22
#include "common/exception/Exception.hpp"
#include "rdbms/AutocommitMode.hpp"
23
24
#include "rdbms/ConnAndStmts.hpp"
#include "rdbms/Stmt.hpp"
25

Steven Murray's avatar
Steven Murray committed
26
#include <list>
27
28
#include <memory>

29
namespace cta {
30
namespace rdbms {
31

32
33
class ConnPool;

34
/**
35
36
 * A smart database connection that will automatically return the underlying
 * database connection to its parent connection pool when it goes out of scope.
37
 */
38
class Conn {
39
40
public:

41
42
43
44
45
  /**
   * Constructor.
   */
  Conn();

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  /**
   * Constructor.
   *
   * @param connAndStmts The database connection and its pool of prepared
   * statements.
   * @param pool The database connection pool to which the connection
   * should be returned.
   */
  Conn(std::unique_ptr<ConnAndStmts> connAndStmts, ConnPool *const pool);

  /**
   * Deletion of the copy constructor.
   */
  Conn(Conn &) = delete;

  /**
   * Move constructor.
   *
   * @param other The other object.
   */
  Conn(Conn &&other);

68
69
  /**
   * Destructor.
70
71
   *
   * Returns the database connection back to its pool.
72
   */
73
  ~Conn() noexcept;
74
75

  /**
76
   * Deletion of the copy assignment operator.
77
   */
78
79
80
81
82
83
84
85
86
  Conn &operator=(const Conn &) = delete;

  /**
   * Move assignment operator.
   *
   * @param rhs The object on the right-hand side of the operator.
   * @return This object.
   */
  Conn &operator=(Conn &&rhs);
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  /**
   * Thrown when a requested autocommit mode is not supported.
   */
  struct AutocommitModeNotSupported: public exception::Exception {
    AutocommitModeNotSupported(const std::string &context = "", const bool embedBacktrace = true):
      Exception(context, embedBacktrace) {}
  };

  /**
   * 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);

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

112
113
114
  /**
   * Creates a prepared statement.
   *
115
   * @param sql The SQL statement.
116
117
   * @return The prepared statement.
   */
118
  Stmt createStmt(const std::string &sql);
119

120
  /**
121
122
123
124
   * Convenience method that parses the specified string of multiple SQL
   * statements and calls executeNonQuery() for each individual statement found.
   *
   * Please note that each statement should be a non-query terminated by a
125
   * semicolon.
126
127
128
   *
   * @param sqlStmts The SQL statements to be executed.
   */
129
  void executeNonQueries(const std::string &sqlStmts);
130
131

  /**
132
   * Executes the statement.
133
   *
134
   * @param sql The SQL statement.
135
   */
136
  void executeNonQuery(const std::string &sql);
137

138
139
140
  /**
   * Commits the current transaction.
   */
141
  void commit();
142
143
144
145

  /**
   * Rolls back the current transaction.
   */
146
  void rollback();
147

Steven Murray's avatar
Steven Murray committed
148
149
150
151
152
153
154
  /**
   * 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.
   */
155
  std::list<std::string> getTableNames() const;
Steven Murray's avatar
Steven Murray committed
156

157
  /**
158
159
160
161
   * Closes the underlying cached database statements and their connection.
   *
   * This method should only be used in extreme cases where the closure of thei
   * underlying database connection needs to be forced.
162
   */
163
  void closeUnderlyingStmtsAndConn();
164

165
  /**
166
   * Returns true if this connection is open.
167
   */
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  bool isOpen() const;

  /**
   * 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();

private:

  /**
   * The database connection and its pool of prepared statements.
   */
  std::unique_ptr<ConnAndStmts> m_connAndStmts;

  /**
   * The database connection pool to which the m_conn should be returned.
   */
  ConnPool *m_pool;
193

194
}; // class Conn
195

196
} // namespace rdbms
197
} // namespace cta