Skip to content

Commit 5930530

Browse files
committed
Added a pure method. Should improve efficiency
1 parent 11fab0f commit 5930530

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

include/SQLiteCpp/Statement.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@
1616
#include <map>
1717
#include <climits> // For INT_MAX
1818

19+
// some macros are taken from https://github.com/nemequ/hedley/blob/master/hedley.h , it was public domain that time
20+
#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) ||\
21+
(defined(__INTEL_COMPILER) && __INTEL_COMPILER > 1600) ||\
22+
(defined(__ARMCC_VERSION) && __ARMCC_VERSION > 4010000) ||\
23+
(\
24+
defined(__TI_COMPILER_VERSION__) && (\
25+
__TI_COMPILER_VERSION__ > 8003000 ||\
26+
(__TI_COMPILER_VERSION__ > 7003000 && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))\
27+
)\
28+
)
29+
#if !defined(SQLITECPP_PURE_FUNC) and __has_attribute(const)
30+
#define SQLITECPP_PURE_FUNC __attribute__((const))
31+
#endif
32+
#endif
33+
#if !defined(SQLITECPP_PURE_FUNC)
34+
#define SQLITECPP_PURE_FUNC
35+
#endif
36+
37+
1938
// Forward declarations to avoid inclusion of <sqlite3.h> in a header
2039
struct sqlite3;
2140
struct sqlite3_stmt;
@@ -116,6 +135,9 @@ class Statement
116135
// instead of being copied.
117136
// => if you know what you are doing, use bindNoCopy() instead of bind()
118137

138+
SQLITECPP_PURE_FUNC
139+
int getIndex(const char * const apName);
140+
119141
/**
120142
* @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
121143
*/

src/Statement.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ void Statement::clearBindings()
8383
check(ret);
8484
}
8585

86+
int Statement::getIndex(const char * const apName)
87+
{
88+
return sqlite3_bind_parameter_index(mStmtPtr, apName);
89+
}
90+
8691
// Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
8792
void Statement::bind(const int aIndex, const int aValue)
8893
{
@@ -166,39 +171,39 @@ void Statement::bind(const int aIndex)
166171
// Bind an int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
167172
void Statement::bind(const char* apName, const int aValue)
168173
{
169-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
174+
const int index = getIndex(apName);
170175
const int ret = sqlite3_bind_int(mStmtPtr, index, aValue);
171176
check(ret);
172177
}
173178

174179
// Bind a 32bits unsigned int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
175180
void Statement::bind(const char* apName, const unsigned aValue)
176181
{
177-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
182+
const int index = getIndex(apName);
178183
const int ret = sqlite3_bind_int64(mStmtPtr, index, aValue);
179184
check(ret);
180185
}
181186

182187
// Bind a 64bits int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
183188
void Statement::bind(const char* apName, const long long aValue)
184189
{
185-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
190+
const int index = getIndex(apName);
186191
const int ret = sqlite3_bind_int64(mStmtPtr, index, aValue);
187192
check(ret);
188193
}
189194

190195
// Bind a double (64bits float) value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
191196
void Statement::bind(const char* apName, const double aValue)
192197
{
193-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
198+
const int index = getIndex(apName);
194199
const int ret = sqlite3_bind_double(mStmtPtr, index, aValue);
195200
check(ret);
196201
}
197202

198203
// Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
199204
void Statement::bind(const char* apName, const std::string& aValue)
200205
{
201-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
206+
const int index = getIndex(apName);
202207
const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(),
203208
static_cast<int>(aValue.size()), SQLITE_TRANSIENT);
204209
check(ret);
@@ -207,23 +212,23 @@ void Statement::bind(const char* apName, const std::string& aValue)
207212
// Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
208213
void Statement::bind(const char* apName, const char* apValue)
209214
{
210-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
215+
const int index = getIndex(apName);
211216
const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_TRANSIENT);
212217
check(ret);
213218
}
214219

215220
// Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
216221
void Statement::bind(const char* apName, const void* apValue, const int aSize)
217222
{
218-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
223+
const int index = getIndex(apName);
219224
const int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_TRANSIENT);
220225
check(ret);
221226
}
222227

223228
// Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
224229
void Statement::bindNoCopy(const char* apName, const std::string& aValue)
225230
{
226-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
231+
const int index = getIndex(apName);
227232
const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(),
228233
static_cast<int>(aValue.size()), SQLITE_STATIC);
229234
check(ret);
@@ -232,23 +237,23 @@ void Statement::bindNoCopy(const char* apName, const std::string& aValue)
232237
// Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
233238
void Statement::bindNoCopy(const char* apName, const char* apValue)
234239
{
235-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
240+
const int index = getIndex(apName);
236241
const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_STATIC);
237242
check(ret);
238243
}
239244

240245
// Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
241246
void Statement::bindNoCopy(const char* apName, const void* apValue, const int aSize)
242247
{
243-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
248+
const int index = getIndex(apName);
244249
const int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_STATIC);
245250
check(ret);
246251
}
247252

248253
// Bind a NULL value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
249254
void Statement::bind(const char* apName)
250255
{
251-
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
256+
const int index = getIndex(apName);
252257
const int ret = sqlite3_bind_null(mStmtPtr, index);
253258
check(ret);
254259
}

0 commit comments

Comments
 (0)