Statement Class
(CDP2SQL::Statement)Class for handling SQL statements, including placeholder values. This can be used for insert, update, select etc. More...
Header: | #include <CDP2SQL/Statement.h> |
Inherits: | HasDatabaseConnection |
Public Functions
Statement() | |
Statement(Database &db) | |
Statement(Database &db, const std::string &statementString) | |
Statement(const Statement &other) = delete | |
~Statement() override | |
void | Bind(int param, int value) |
void | Bind(int param, int64_t value) |
void | Bind(int param, const std::string &value) |
void | Bind(int param, const char *value) |
void | Bind(int param, const unsigned char *blobValue, int len) |
void | Bind(int param, double value) |
void | Bind(int param, bool value) |
void | BindNull(int param) |
void | BindTimestamp(int param, std::time_t tTimestamp) |
void | Close() |
void | Compile(const std::string &statementString) |
void | Execute() |
void | Reset() |
- 2 public functions inherited from CDP2SQL::HasDatabaseConnection
Detailed Description
Class for handling SQL statements, including placeholder values. This can be used for insert, update, select etc.
While you can modify a database by creating custom SQL strings and calling Database::Execute directly, it is not a recommended approach due to potential for memory-overhead, escaping-mishaps etc.
Instead you can use a Statement object to create a precompiled statement which will handling data escaping/quoting and can be reused if you are inserting multiple rows.
Example usage:
#include <CDP2SQL/CDP2SQL.h> #include <CppSQLite3/SQLite3Factory.h> ... using namespace CDP2SQL; try { Database db(SQLite3Factory().Create(), "mysqlite3databasefile.db"); Statement st(db, "insert into employee values (?, ?, ?);"); st.Bind(1, 123); st.Bind(2, "John Doe"); st.Bind(3, 64000.99); st.Execute(); } catch (const SQLException& e) { printf("Error occurred: %s\n", e.Message()); }
Member Function Documentation
Statement::Statement()
Default constructor. Only use this if you have to delay connecting it to a Database object.
Statement::Statement(Database &db)
Constructor taking a Database reference. Must be connected.
Statement::Statement(Database &db, const std::string &statementString)
Constructor taking a Database reference and an SQL statement string.
Precompiles the statement.
[delete]
Statement::Statement(const Statement &other)
Copy constructor.
Statement::~Statement()
Destroys the instance of Statement.
void Statement::Bind(int param, int value)
Binds placeholder at position param to integer value value. The numbering of param starts with 1
.
void Statement::Bind(int param, int64_t value)
Binds placeholder at position param to int64_t value value. The numbering of param starts with 1
.
void Statement::Bind(int param, const std::string &value)
Binds placeholder at position param to string value value. The numbering of param starts with 1
.
void Statement::Bind(int param, const char *value)
Binds placeholder at position param to string value value. The numbering of param starts with 1
.
void Statement::Bind(int param, const unsigned char *blobValue, int len)
Binds placeholder at position param to blobValue with length len. The numbering of param starts with 1
.
void Statement::Bind(int param, double value)
Binds placeholder at position param to floating point value value. The numbering of param starts with 1
.
void Statement::Bind(int param, bool value)
Binds placeholder at position param to boolean value value. The numbering of param starts with 1
.
void Statement::BindNull(int param)
Binds placeholder at position param to SQL NULL. The numbering of param starts with 1
.
void Statement::BindTimestamp(int param, std::time_t tTimestamp)
Binds placeholder at position param to a timestamp string calculated from the dTimestamp given as time_t (seconds since epoch).
The SQL statement will still need to convert this to an SQL TIMESTAMP type, such as TIME, DATE, DATETIME etc, as this merely creates the ISO-8601 timestamp text string.
Example:
Statement st(db, "INSERT INTO mytimetable VALUES(datetime(?));"); st.BindTimestamp(1, std::time(NULL) );
The numbering of param starts with 1
.
void Statement::Close()
Closes and finalizes the statement. Done automatically by the destructor.
void Statement::Compile(const std::string &statementString)
Compiles an SQL statement. Must be called before you can Bind() and Execute() the statement.
void Statement::Execute()
Executes the statement. All placeholders must have been bound to values.
void Statement::Reset()
Resets the statement for reuse. Remember to re-Bind all placeholder values before calling Execute().
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.