diff options
Diffstat (limited to 'libodbcpp/odbc-command.cpp')
-rw-r--r-- | libodbcpp/odbc-command.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/libodbcpp/odbc-command.cpp b/libodbcpp/odbc-command.cpp new file mode 100644 index 0000000..d96234d --- /dev/null +++ b/libodbcpp/odbc-command.cpp @@ -0,0 +1,40 @@ +#include "odbc-command.h" +#include "odbc-error.h" +#include "odbc-param.h" +#include <sqlext.h> + +ODBC::Command::Command(const Connection & c, const std::string & s) : + DB::Command(s), + connection(c) +{ + RETCODE rc = SQLAllocHandle(SQL_HANDLE_STMT, c.conn, &hStmt); + if (!SQL_SUCCEEDED(rc)) { + throw Error(rc, SQL_HANDLE_STMT, hStmt, "Allocate statement handle"); + } + rc = SQLSetStmtAttr(hStmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER)SQL_CURSOR_DYNAMIC, 0); + if (!SQL_SUCCEEDED(rc)) { + throw ConnectionError(rc, SQL_HANDLE_STMT, hStmt, "Set scrollable cursor"); + } + rc = SQLPrepare(hStmt, (SQLCHAR*)sql.c_str(), sql.length()); + if (!SQL_SUCCEEDED(rc)) { + SQLFreeHandle(SQL_HANDLE_STMT, hStmt); + throw Error(rc, SQL_HANDLE_STMT, hStmt, "Prepare statement"); + } + SQLSMALLINT pcount; + rc = SQLNumParams(hStmt, &pcount); + if (!SQL_SUCCEEDED(rc)) { + SQLFreeHandle(SQL_HANDLE_STMT, hStmt); + throw Error(rc, SQL_HANDLE_STMT, hStmt, "Parameter count"); + } + params.resize(pcount); +} + +ODBC::Command::~Command() +{ + for (Params::iterator i = params.begin(); i != params.end(); ++i) { + if (*i) { + delete *i; + } + } +} + |