Skip to content
Closed
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
25 changes: 22 additions & 3 deletions include/SQLiteCpp/Column.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class Column
* thus you must copy it before using it beyond its scope (to a std::string for instance).
*/
const void* getBlob() const noexcept; // nothrow
/**
* @brief Return a std::string for a TEXT or BLOB column.
*
* Note this correctly handles strings that contain null bytes.
*
*/
std::string getString() const noexcept; // nothrow

/**
* @brief Return the type of the value of the column
Expand Down Expand Up @@ -158,6 +165,11 @@ class Column
{
return getInt();
}
/// @brief Inline cast operator to 32bits unsigned integer
inline operator uint32_t() const
{
return static_cast<uint32_t>(getInt64());
}
/// @brief Inline cast operator to 64bits integer
inline operator sqlite3_int64() const
{
Expand Down Expand Up @@ -193,12 +205,19 @@ class Column
// but is not working under Microsoft Visual Studio 2010, 2012 and 2013
// (error C2440: 'initializing' : cannot convert from 'SQLite::Column' to 'std::basic_string<_Elem,_Traits,_Ax>'
// [...] constructor overload resolution was ambiguous)
/// Inline cast operator to std::string
inline operator const std::string() const
/**
* @brief Inline cast operator to std::string
*
* Handles BLOB or TEXT, which may contain null bytes within
*
* @see getString
*/
inline operator std::string() const
{
return getText();
return getString();
}
#endif

// NOTE : the following is required by GCC and Clang to cast a Column result in a long/int64_t
/// @brief Inline cast operator to long as 64bits integer
inline operator long() const
Expand Down
91 changes: 82 additions & 9 deletions include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <sqlite3.h>
#include <string>
#include <map>
#include <stdint.h>

#include <SQLiteCpp/Exception.h>

Expand Down Expand Up @@ -67,6 +68,17 @@ class Statement
*/
Statement(Database& aDatabase, const std::string& aQuery);

// Require C++11
#if ( __cplusplus>= 201103L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) )
#define Statement_CAN_MOVE 1
/**
* @breif Move Constructor
*
* (Allows inserting into STL containers that may need to move memory when growing.)
*/
Statement(Statement &&other);
#endif

/**
* @brief Finalize and unregister the SQL query from the SQLite Database Connection.
*/
Expand Down Expand Up @@ -105,21 +117,33 @@ class Statement
/**
* @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
void bind(const int aIndex, const int& aValue);
void bind(const int aIndex, const int aValue);
/**
* @brief Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
void bind(const int aIndex, const sqlite3_int64& aValue);
void bind(const int aIndex, const sqlite3_int64 aValue);
/**
* @brief Bind a 32bits unsigned int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
void bind(const int aIndex, const uint32_t aValue);
/**
* @brief Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
void bind(const int aIndex, const double& aValue);
void bind(const int aIndex, const double aValue);
/**
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/
void bind(const int aIndex, const std::string& aValue);

/**
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data. It must exist while executing the statement.
*/
void bindNoCopy(const int aIndex, const std::string& aValue);

/**
* @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
Expand All @@ -132,6 +156,14 @@ class Statement
* @note This 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);

/**
* @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data. It must exist while executing the statement.
*/
void bindNoCopy(const int aIndex, const void* apValue, const int aSize);

/**
* @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
Expand All @@ -140,21 +172,31 @@ class Statement
/**
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
void bind(const char* apName, const int& aValue);
void bind(const char* apName, const int aValue);
/**
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
void bind(const char* apName, const sqlite3_int64& aValue);
void bind(const char* apName, const sqlite3_int64 aValue);
/**
* @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
void bind(const char* apName, const uint32_t aValue);
/**
* @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
void bind(const char* apName, const double& aValue);
void bind(const char* apName, const double aValue);
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/
void bind(const char* apName, const std::string& aValue);
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data.
*/
void bindNoCopy(const char* apName, const std::string& aValue);
/**
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
Expand All @@ -167,6 +209,12 @@ class Statement
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/
void bind(const char* apName, const void* apValue, const int aSize);
/**
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, making a copy of the data.
*/
void bindNoCopy(const char* apName, const void* apValue, const int aSize);
/**
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
Expand All @@ -176,21 +224,28 @@ class Statement
/**
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
inline void bind(const std::string& aName, const int& aValue)
inline void bind(const std::string& aName, const int aValue)
{
bind(aName.c_str(), aValue);
}
/**
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
inline void bind(const std::string& aName, const sqlite3_int64& aValue)
inline void bind(const std::string& aName, const sqlite3_int64 aValue)
{
bind(aName.c_str(), aValue);
}
/**
* @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
inline void bind(const std::string& aName, const uint32_t aValue)
{
bind(aName.c_str(), aValue);
}
/**
* @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/
inline void bind(const std::string& aName, const double& aValue)
inline void bind(const std::string& aName, const double aValue)
{
bind(aName.c_str(), aValue);
}
Expand All @@ -203,6 +258,15 @@ class Statement
{
bind(aName.c_str(), aValue);
}
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data.
*/
inline void bindNoCopy(const std::string& aName, const std::string& aValue)
{
bindNoCopy(aName.c_str(), aValue);
}
/**
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
Expand All @@ -221,6 +285,15 @@ class Statement
{
bind(aName.c_str(), apValue, aSize);
}
/**
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data.
*/
inline void bindNoCopy(const std::string& aName, const void* apValue, const int 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
15 changes: 14 additions & 1 deletion src/Column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <SQLiteCpp/Column.h>

#include <iostream>
#include <string>


namespace SQLite
Expand Down Expand Up @@ -69,12 +70,24 @@ const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcep
return (pText?pText:apDefaultValue);
}

// Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0
// Return a pointer to the blob value (*not* NULL terminated) of the column specified by its index starting at 0
const void* Column::getBlob() const noexcept // nothrow
{
return sqlite3_column_blob(mStmtPtr, mIndex);
}

// Return a std::string to a TEXT or BLOB column
std::string Column::getString() const noexcept // nothrow
{
// Note: using sqlite3_column_blob and not sqlite3_column_text
// - no need for sqlite3_column_text to add a \0 on the end, as we're getting the bytes length directly
const char *data = static_cast<const char *>(sqlite3_column_blob(mStmtPtr, mIndex));

// SQLite docs: "The safest policy is to invoke… sqlite3_column_blob() followed by sqlite3_column_bytes()"
// Note: std::string is ok to pass nullptr as first arg, if length is 0
return std::string(data, sqlite3_column_bytes(mStmtPtr, mIndex));
}

// Return the type of the value of the column
int Column::getType() const noexcept // nothrow
{
Expand Down
Loading