Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 6 Statement::bind() overrides using size_t for blobs #431

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ class SQLITECPP_API Statement
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/
void bind(const int aIndex, const void* apValue, const int aSize);
void bind(const int aIndex, const void* apValue, const size_t aSize)
{
bind(aIndex, apValue, static_cast<int>(aSize));
}
/**
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1).
*
Expand All @@ -178,6 +182,10 @@ class SQLITECPP_API Statement
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/
void bindNoCopy(const int aIndex, const void* apValue, const int aSize);
void bindNoCopy(const int aIndex, const void* apValue, const size_t aSize)
{
bindNoCopy(aIndex, apValue, static_cast<int>(aSize));
}
/**
* @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
Expand Down Expand Up @@ -240,6 +248,10 @@ class SQLITECPP_API Statement
{
bind(getIndex(apName), apValue, aSize);
}
void bind(const char* apName, const void* apValue, const size_t aSize)
{
bind(getIndex(apName), apValue, aSize);
}
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
Expand Down Expand Up @@ -271,6 +283,10 @@ class SQLITECPP_API Statement
{
bindNoCopy(getIndex(apName), apValue, aSize);
}
void bindNoCopy(const char* apName, const void* apValue, const size_t aSize)
{
bindNoCopy(getIndex(apName), apValue, aSize);
}
/**
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
Expand Down Expand Up @@ -337,6 +353,10 @@ class SQLITECPP_API Statement
{
bind(aName.c_str(), apValue, aSize);
}
void bind(const std::string& aName, const void* apValue, const size_t aSize)
{
bind(aName.c_str(), apValue, aSize);
}
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
Expand Down Expand Up @@ -368,6 +388,10 @@ class SQLITECPP_API Statement
{
bindNoCopy(aName.c_str(), apValue, aSize);
}
void bindNoCopy(const std::string& aName, const void* apValue, const size_t aSize)
{
bindNoCopy(aName.c_str(), apValue, aSize);
}
/**
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Statement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,24 @@ TEST(Statement, bindings)
EXPECT_EQ(7, query.getColumn(0).getInt64());
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
}

// reset() with clearbindings() and new bindings
insert.reset();
insert.clearBindings();

// Height row with a vector of unsigned char as blob
{
const std::vector<char> blob = { 'b','l','\0','b' };
insert.bind(3, blob.data(), blob.size());
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());

// Check the result
query.executeStep();
EXPECT_TRUE(query.hasRow());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(0, memcmp(blob.data(), &query.getColumn(3).getString()[0], blob.size()));
}
}

TEST(Statement, bindNoCopy)
Expand Down Expand Up @@ -447,6 +465,24 @@ TEST(Statement, bindNoCopy)
EXPECT_EQ(0, memcmp(&txt2[0], &query.getColumn(2).getString()[0], txt2.size()));
EXPECT_EQ(0, memcmp(blob, &query.getColumn(3).getString()[0], sizeof(blob)));
}

// reset() with clearbindings() and new bindings
insert.reset();
insert.clearBindings();

// Insert a second row with a vector of unsigned char as blob
{
const std::vector<char> blob = { 'b','l','\0','b' };
insert.bindNoCopy(3, blob.data(), blob.size());
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());

// Check the result
query.executeStep();
EXPECT_TRUE(query.hasRow());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(0, memcmp(blob.data(), &query.getColumn(3).getString()[0], blob.size()));
}
}

TEST(Statement, bindByName)
Expand Down