Skip to content

Commit cb421a8

Browse files
authored
Merge pull request #242 from KOLANICH/efficiency_1
- Added a `getIndex` method and used it. - Improved code reuse. - Moved some functions from sources into headers.
2 parents 97b2a07 + b98eabb commit cb421a8

File tree

2 files changed

+51
-102
lines changed

2 files changed

+51
-102
lines changed

include/SQLiteCpp/Statement.h

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class Statement
121121
// instead of being copied.
122122
// => if you know what you are doing, use bindNoCopy() instead of bind()
123123

124+
int getIndex(const char * const apName);
125+
124126
/**
125127
* @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
126128
*/
@@ -206,11 +208,17 @@ class Statement
206208
/**
207209
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
208210
*/
209-
void bind(const char* apName, const int aValue);
211+
void bind(const char* apName, const int aValue)
212+
{
213+
bind(getIndex(apName), aValue);
214+
}
210215
/**
211216
* @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
212217
*/
213-
void bind(const char* apName, const unsigned aValue);
218+
void bind(const char* apName, const unsigned aValue)
219+
{
220+
bind(getIndex(apName), aValue);
221+
}
214222

215223
#if (LONG_MAX == INT_MAX) // 4 bytes "long" type means the data model is ILP32 or LLP64 (Win64 Visual C++ and MinGW)
216224
/**
@@ -232,57 +240,84 @@ class Statement
232240
/**
233241
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
234242
*/
235-
void bind(const char* apName, const long long aValue);
243+
void bind(const char* apName, const long long aValue)
244+
{
245+
bind(getIndex(apName), aValue);
246+
}
236247
/**
237248
* @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
238249
*/
239-
void bind(const char* apName, const double aValue);
250+
void bind(const char* apName, const double aValue)
251+
{
252+
bind(getIndex(apName), aValue);
253+
}
240254
/**
241255
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
242256
*
243257
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
244258
*/
245-
void bind(const char* apName, const std::string& aValue);
259+
void bind(const char* apName, const std::string& aValue)
260+
{
261+
bind(getIndex(apName), aValue);
262+
}
246263
/**
247264
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
248265
*
249266
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
250267
*/
251-
void bind(const char* apName, const char* apValue);
268+
void bind(const char* apName, const char* apValue)
269+
{
270+
bind(getIndex(apName), apValue);
271+
}
252272
/**
253273
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
254274
*
255275
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
256276
*/
257-
void bind(const char* apName, const void* apValue, const int aSize);
277+
void bind(const char* apName, const void* apValue, const int aSize)
278+
{
279+
bind(getIndex(apName), apValue, aSize);
280+
}
258281
/**
259282
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
260283
*
261284
* The string can contain null characters as it is binded using its size.
262285
*
263286
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
264287
*/
265-
void bindNoCopy(const char* apName, const std::string& aValue);
288+
void bindNoCopy(const char* apName, const std::string& aValue)
289+
{
290+
bindNoCopy(getIndex(apName), aValue);
291+
}
266292
/**
267293
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
268294
*
269295
* Main usage is with null-terminated literal text (aka in code static strings)
270296
*
271297
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
272298
*/
273-
void bindNoCopy(const char* apName, const char* apValue);
299+
void bindNoCopy(const char* apName, const char* apValue)
300+
{
301+
bindNoCopy(getIndex(apName), apValue);
302+
}
274303
/**
275304
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
276305
*
277306
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
278307
*/
279-
void bindNoCopy(const char* apName, const void* apValue, const int aSize);
308+
void bindNoCopy(const char* apName, const void* apValue, const int aSize)
309+
{
310+
bindNoCopy(getIndex(apName), apValue, aSize);
311+
}
280312
/**
281313
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
282314
*
283315
* @see clearBindings() to set all bound parameters to NULL.
284316
*/
285-
void bind(const char* apName); // bind NULL value
317+
void bind(const char* apName) // bind NULL value
318+
{
319+
bind(getIndex(apName));
320+
}
286321

287322

288323
/**

src/Statement.cpp

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ void Statement::clearBindings()
6363
check(ret);
6464
}
6565

66+
int Statement::getIndex(const char * const apName)
67+
{
68+
return sqlite3_bind_parameter_index(mStmtPtr, apName);
69+
}
70+
6671
// Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
6772
void Statement::bind(const int aIndex, const int aValue)
6873
{
@@ -143,97 +148,6 @@ void Statement::bind(const int aIndex)
143148
}
144149

145150

146-
// Bind an int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
147-
void Statement::bind(const char* apName, const int aValue)
148-
{
149-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
150-
const int ret = sqlite3_bind_int(mStmtPtr, index, aValue);
151-
check(ret);
152-
}
153-
154-
// Bind a 32bits unsigned int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
155-
void Statement::bind(const char* apName, const unsigned aValue)
156-
{
157-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
158-
const int ret = sqlite3_bind_int64(mStmtPtr, index, aValue);
159-
check(ret);
160-
}
161-
162-
// Bind a 64bits int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
163-
void Statement::bind(const char* apName, const long long aValue)
164-
{
165-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
166-
const int ret = sqlite3_bind_int64(mStmtPtr, index, aValue);
167-
check(ret);
168-
}
169-
170-
// Bind a double (64bits float) value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
171-
void Statement::bind(const char* apName, const double aValue)
172-
{
173-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
174-
const int ret = sqlite3_bind_double(mStmtPtr, index, aValue);
175-
check(ret);
176-
}
177-
178-
// Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
179-
void Statement::bind(const char* apName, const std::string& aValue)
180-
{
181-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
182-
const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(),
183-
static_cast<int>(aValue.size()), SQLITE_TRANSIENT);
184-
check(ret);
185-
}
186-
187-
// Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
188-
void Statement::bind(const char* apName, const char* apValue)
189-
{
190-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
191-
const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_TRANSIENT);
192-
check(ret);
193-
}
194-
195-
// Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
196-
void Statement::bind(const char* apName, const void* apValue, const int aSize)
197-
{
198-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
199-
const int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_TRANSIENT);
200-
check(ret);
201-
}
202-
203-
// Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
204-
void Statement::bindNoCopy(const char* apName, const std::string& aValue)
205-
{
206-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
207-
const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(),
208-
static_cast<int>(aValue.size()), SQLITE_STATIC);
209-
check(ret);
210-
}
211-
212-
// Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
213-
void Statement::bindNoCopy(const char* apName, const char* apValue)
214-
{
215-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
216-
const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_STATIC);
217-
check(ret);
218-
}
219-
220-
// Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
221-
void Statement::bindNoCopy(const char* apName, const void* apValue, const int aSize)
222-
{
223-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
224-
const int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_STATIC);
225-
check(ret);
226-
}
227-
228-
// Bind a NULL value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
229-
void Statement::bind(const char* apName)
230-
{
231-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
232-
const int ret = sqlite3_bind_null(mStmtPtr, index);
233-
check(ret);
234-
}
235-
236-
237151
// Execute a step of the query to fetch one row of results
238152
bool Statement::executeStep()
239153
{

0 commit comments

Comments
 (0)