Skip to content

Commit 12a3886

Browse files
committed
Clean new classes code
1 parent b72479a commit 12a3886

File tree

10 files changed

+114
-118
lines changed

10 files changed

+114
-118
lines changed

include/SQLiteCpp/Column.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ class Column
5353
*
5454
* @throws Exception is thrown in case of error, then the Column object is NOT constructed.
5555
*/
56-
explicit Column(const StatementPtr::TStatementPtr& aStmtPtr, int aIndex);
56+
explicit Column(const StatementPtr::TRawStatementPtr& aStmtPtr, int aIndex);
57+
58+
Column(const Column&) = delete;
59+
Column& operator=(const Column&) = delete;
60+
61+
Column(Column&& aColumn) = default;
62+
Column& operator=(Column&& aColumn) = default;
5763

5864
/**
5965
* @brief Return a pointer to the named assigned to this result column (potentially aliased)
@@ -224,8 +230,8 @@ class Column
224230
}
225231

226232
private:
227-
StatementPtr::TStatementPtr mStmtPtr; ///< Shared Pointer to the prepared SQLite Statement Object
228-
int mIndex; ///< Index of the column in the row of result, starting at 0
233+
StatementPtr::TRawStatementPtr mStmtPtr; ///< Shared Pointer to the prepared SQLite Statement Object
234+
int mIndex; ///< Index of the column in the row of result, starting at 0
229235
};
230236

231237
/**

include/SQLiteCpp/Row.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Container for SQLite Statement Object step
55
*
6-
* Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com)
6+
* Copyright (c) 2022 Kacper Zielinski (KacperZ155@gmail.com)
77
* Copyright (c) 2015-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
88
*
99
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
@@ -26,7 +26,7 @@ namespace SQLite
2626
class Row
2727
{
2828
public:
29-
Row(TRowWeakPtr apStatement, std::size_t aID);
29+
Row(TStatementWeakPtr apStatement, std::size_t aID);
3030

3131
std::size_t getRowNumber() const
3232
{
@@ -53,7 +53,7 @@ class Row
5353
const char* getText(uint32_t aColumnID) const noexcept;
5454

5555
private:
56-
TRowWeakPtr mpStatement;
56+
TStatementWeakPtr mpStatement;
5757
std::size_t ID;
5858
};
5959

include/SQLiteCpp/Statement.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class Statement : public StatementExecutor
5858
*/
5959
Statement(const Database& aDatabase, const std::string& aQuery);
6060

61-
// Statement is non-copyable
6261
Statement(const Statement&) = delete;
6362
Statement& operator=(const Statement&) = delete;
6463

@@ -453,7 +452,7 @@ class Statement : public StatementExecutor
453452
template<typename T, const int... Is>
454453
T getColumns(const std::integer_sequence<int, Is...>)
455454
{
456-
return T{ Column(getStatement(), Is)... };
455+
return T{ Column(getStatementPtr(), Is)... };
457456
}
458457

459458
public:
@@ -530,7 +529,7 @@ class Statement : public StatementExecutor
530529
* - the statement is not a SELECT query
531530
* - the column at aIndex is not a table column but an expression or subquery
532531
*/
533-
const char * getColumnDeclaredType(const int aIndex) const;
532+
const char* getColumnDeclaredType(const int aIndex) const;
534533

535534
////////////////////////////////////////////////////////////////////////////
536535

@@ -540,7 +539,7 @@ class Statement : public StatementExecutor
540539
return mQuery;
541540
}
542541

543-
// Return a UTF-8 string containing the SQL text of prepared statement with bound parameters expanded.
542+
/// Return a UTF-8 string containing the SQL text of prepared statement with bound parameters expanded.
544543
std::string getExpandedSQL() const;
545544

546545
/// Return the number of bind parameters in the statement

include/SQLiteCpp/StatementExecutor.h

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Step executor for SQLite prepared Statement Object
55
*
6+
* Copyright (c) 2022 Kacper Zielinski (KacperZ155@gmail.com)
67
* Copyright (c) 2012-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
78
*
89
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
@@ -38,7 +39,7 @@ extern const int OK; ///< SQLITE_OK
3839
* 2) the SQLite "Serialized" mode is not supported by SQLiteC++,
3940
* because of the way it shares the underling SQLite precompiled statement
4041
* in a custom shared pointer (See the inner class "Statement::Ptr").
41-
* TODO Test Serialized mode after all changes to pointers
42+
* TODO: Test Serialized mode after all changes to pointers
4243
*/
4344
class StatementExecutor
4445
{
@@ -52,7 +53,12 @@ class StatementExecutor
5253
StatementExecutor(StatementExecutor&&) = default;
5354
StatementExecutor& operator=(StatementExecutor&&) = default;
5455

55-
/// Reset the statement to make it ready for a new execution. Throws an exception on error.
56+
/**
57+
* @brief Reset the statement to make it ready for a new execution.
58+
* This doesn't clear bindings!
59+
*
60+
* @throws SQLite::Exception in case of error
61+
*/
5662
void reset();
5763

5864
/// Reset the statement. Returns the sqlite result code instead of throwing an exception on error.
@@ -124,7 +130,7 @@ class StatementExecutor
124130
}
125131

126132
/// Get columns names with theirs ids
127-
const TColumnsMap& getColumnsNames() const
133+
const TColumnsMap& getColumnsNames() const noexcept
128134
{
129135
return mColumnNames;
130136
}
@@ -168,10 +174,10 @@ class StatementExecutor
168174
using difference_type = std::ptrdiff_t;
169175

170176
RowIterator() = default;
171-
RowIterator(TRowWeakPtr apStatement, uint16_t aID) :
177+
RowIterator(TStatementWeakPtr apStatement, uint16_t aID) :
172178
mpStatement(apStatement), mID(aID), mRow(apStatement, aID) {}
173179

174-
reference operator*() const
180+
reference operator*() const noexcept
175181
{
176182
return mRow;
177183
}
@@ -180,22 +186,22 @@ class StatementExecutor
180186
return &mRow;
181187
}
182188

183-
reference operator++() noexcept
189+
RowIterator& operator++() noexcept
184190
{
185191
mRow = Row(mpStatement, ++mID);
186192
advance();
187-
return mRow;
193+
return *this;
188194
}
189-
value_type operator++(int)
195+
/// Prefer to use prefix increment (++it)
196+
RowIterator operator++(int) noexcept
190197
{
191-
Row copy{ mRow };
192-
mRow = Row(mpStatement, ++mID);
198+
RowIterator copy{ *this };
193199
advance();
194200
return copy;
195201
}
196202

197-
bool operator==(const RowIterator& aIt) const;
198-
bool operator!=(const RowIterator& aIt) const
203+
bool operator==(const RowIterator& aIt) const noexcept;
204+
bool operator!=(const RowIterator& aIt) const noexcept
199205
{
200206
return !(*this == aIt);
201207
}
@@ -204,7 +210,7 @@ class StatementExecutor
204210
/// Executing next statement step
205211
void advance() noexcept;
206212

207-
TRowWeakPtr mpStatement{}; //!< Weak pointer to prepared Statement Object
213+
TStatementWeakPtr mpStatement{}; //!< Weak pointer to prepared Statement Object
208214
uint16_t mID{}; //!< Current row number
209215

210216
/// Internal row object storage
@@ -223,9 +229,12 @@ class StatementExecutor
223229
RowIterator begin();
224230

225231
/**
232+
* @brief Empty RowIterator without any connection to exisiting statements.
233+
* Use to find if RowIterator is out of steps.
234+
*
226235
* @return RowIterator to non-exisitng step
227236
*/
228-
RowIterator end();
237+
RowIterator end() noexcept;
229238

230239
protected:
231240
/**
@@ -239,40 +248,33 @@ class StatementExecutor
239248
explicit StatementExecutor(sqlite3* apSQLite, const std::string& aQuery);
240249

241250
/**
242-
* @brief Return a std::shared_ptr with SQLite Statement Object.
251+
* @brief Return a std::shared_ptr with prepared SQLite Statement Object.
243252
*
244-
* @return raw pointer to Statement Object
253+
* @return TRawStatementPtr with SQLite Statement Object
245254
*/
246-
StatementPtr::TStatementPtr getStatement() const noexcept
255+
StatementPtr::TRawStatementPtr getStatementPtr() const noexcept
247256
{
248257
return mpStatement->mpStatement;
249258
}
250259

251260
/**
252-
* @brief Return a prepared SQLite Statement Object.
261+
* @brief Return a pointer to prepared SQLite Statement Object.
253262
*
254-
* Throw an exception if the statement object was not prepared.
255-
* @return raw pointer to Prepared Statement Object
263+
* @return Raw pointer to SQLite Statement Object
256264
*/
257-
sqlite3_stmt* getPreparedStatement() const;
258-
259-
/**
260-
* @brief Return a prepared SQLite Statement Object.
261-
*
262-
* Throw an exception if the statement object was not prepared.
263-
* @return raw pointer to Prepared Statement Object
264-
*/
265-
TRowWeakPtr getExecutorWeakPtr() const
265+
sqlite3_stmt* getStatement() const noexcept
266266
{
267-
return mpStatement;
267+
return mpStatement->getStatement();
268268
}
269269

270270
////////////////////////////////////////////////////////////////////////////
271271

272272
/**
273273
* @brief Check if a return code equals SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
274274
*
275-
* @param[in] aRet SQLite return code to test against the SQLITE_OK expected value
275+
* @param[in] aRet SQLite return code to test against the SQLITE_OK expected value.
276+
*
277+
* @throws SQLite::Exception when aRet isn't SQLITE_OK.
276278
*/
277279
void check(const int aRet) const
278280
{
@@ -284,6 +286,8 @@ class StatementExecutor
284286

285287
/**
286288
* @brief Check if there is a row of result returned by executeStep(), else throw a SQLite::Exception.
289+
*
290+
* @throws SQLite::Exception when mbHasRow is false.
287291
*/
288292
void checkRow() const
289293
{
@@ -295,6 +299,8 @@ class StatementExecutor
295299

296300
/**
297301
* @brief Check if there is a Column index is in the range of columns in the result.
302+
*
303+
* @throws SQLite::Exception when aIndex is out of bounds.
298304
*/
299305
void checkIndex(const int aIndex) const
300306
{
@@ -305,17 +311,12 @@ class StatementExecutor
305311
}
306312

307313
private:
308-
/// Get column number and create map with columns names
314+
/// Get column number and create map with columns names.
309315
void createColumnInfo();
310316

311-
// xD
312-
bool checkReturnCode(int aReturnCode) const;
313-
// xD
314-
bool checkReturnCode(int aReturnCode, int aErrorCode) const;
315-
316317
/// Shared Pointer to this object.
317-
/// Allows RowIterator to execute next step
318-
TRowPtr mpStatement{};
318+
/// Allows RowIterator to execute next step.
319+
TStatementPtr mpStatement{};
319320

320321
int mColumnCount = 0; //!< Number of columns in the result of the prepared statement
321322
bool mbHasRow = false; //!< true when a row has been fetched with executeStep()

include/SQLiteCpp/StatementPtr.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Pointer for prepared SQLite Statement Object
55
*
6+
* Copyright (c) 2022 Kacper Zielinski (KacperZ155@gmail.com)
67
* Copyright (c) 2022 Sebastien Rombauts (sebastien.rombauts@gmail.com)
78
*
89
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
@@ -39,11 +40,11 @@ struct StatementPtr
3940
StatementPtr(sqlite3* apSQLite, const std::string& aQuery);
4041

4142
/// Shared pointer to SQLite prepared Statement Object
42-
using TStatementPtr = std::shared_ptr<sqlite3_stmt>;
43+
using TRawStatementPtr = std::shared_ptr<sqlite3_stmt>;
4344

44-
sqlite3* const mpConnection; //!< Pointer to SQLite Database Connection Handle
45-
TStatementPtr const mpStatement; //!< Shared Pointer to the prepared SQLite Statement Object
46-
std::size_t mCurrentStep = 0; //!< Current step of prepared Statement Object
45+
sqlite3* const mpConnection; //!< Pointer to SQLite Database Connection Handle
46+
TRawStatementPtr const mpStatement; //!< Shared Pointer to the prepared SQLite Statement Object
47+
std::size_t mCurrentStep = 0; //!< Current step of prepared Statement Object
4748

4849
/// Resets SQLite Statement Object
4950
int reset() noexcept;
@@ -57,19 +58,19 @@ struct StatementPtr
5758
*
5859
* @return Pointer to SQLite Statement Object
5960
*/
60-
sqlite3_stmt* getPreparedStatement() const;
61+
sqlite3_stmt* getStatement() const noexcept;
6162

6263
private:
6364
/// Create prepared SQLite Statement Object
64-
TStatementPtr prepareStatement(sqlite3* apConnection, const std::string& aQuery) const;
65+
TRawStatementPtr prepareStatement(sqlite3* apConnection, const std::string& aQuery) const;
6566
};
6667

6768

6869
/// Shared pointer to SQLite StatementPtr
69-
using TRowPtr = std::shared_ptr<StatementPtr>;
70+
using TStatementPtr = const std::shared_ptr<StatementPtr>;
7071

7172
/// Weak pointer to SQLite StatementPtr
72-
using TRowWeakPtr = std::weak_ptr<StatementPtr>;
73+
using TStatementWeakPtr = std::weak_ptr<StatementPtr>;
7374

7475

7576
} // namespace SQLite

src/Column.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const int Null = SQLITE_NULL;
2626

2727

2828
// Encapsulation of a Column in a row of the result pointed by the prepared Statement.
29-
Column::Column(const StatementPtr::TStatementPtr& aStmtPtr, int aIndex) :
29+
Column::Column(const StatementPtr::TRawStatementPtr& aStmtPtr, int aIndex) :
3030
mStmtPtr(aStmtPtr),
3131
mIndex(aIndex)
3232
{

src/Row.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Container for SQLite Statement Object step
55
*
6-
* Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com)
6+
* Copyright (c) 2022 Kacper Zielinski (KacperZ155@gmail.com)
77
* Copyright (c) 2015-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
88
*
99
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
@@ -19,7 +19,7 @@ namespace SQLite
1919
{
2020

2121

22-
Row::Row(TRowWeakPtr apStatement, std::size_t aID) :
22+
Row::Row(TStatementWeakPtr apStatement, std::size_t aID) :
2323
mpStatement(apStatement), ID(aID)
2424
{
2525
}
@@ -28,15 +28,15 @@ namespace SQLite
2828
{
2929
auto statement = mpStatement.lock();
3030

31-
return (SQLITE_NULL == sqlite3_column_type(statement->getPreparedStatement(), aIndex));
31+
return (SQLITE_NULL == sqlite3_column_type(statement->getStatement(), aIndex));
3232
}
3333

3434
const char* Row::getText(uint32_t aColumnID) const noexcept
3535
{
3636
auto statement = mpStatement.lock();
3737

3838

39-
auto pText = reinterpret_cast<const char*>(sqlite3_column_text(statement->getPreparedStatement(), aColumnID));
39+
auto pText = reinterpret_cast<const char*>(sqlite3_column_text(statement->getStatement(), aColumnID));
4040
return (pText ? pText : "");
4141
}
4242

0 commit comments

Comments
 (0)