summaryrefslogtreecommitdiff
path: root/libodbcpp/command.cpp
blob: 6c8bf3cd7550312075075daf698ffaa6d8dad52c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "command.h"
#include "error.h"
#include "param.h"
#include <sqlext.h>

ODBC::Command::Command(const Connection& c, String s) :
	sql(s)
{
	RETCODE rc = SQLAllocHandle(SQL_HANDLE_STMT, c.conn, &hStmt);
	if (rc != SQL_SUCCESS) {
		throw Error(rc, SQL_HANDLE_STMT, hStmt, "Allocate statement handle");
	}
    rc = SQLPrepare(hStmt, sql, sql.size());
    if (rc != SQL_SUCCESS) {
        SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
		throw Error(rc, SQL_HANDLE_STMT, hStmt, "Prepare statement");
    }
	SQLSMALLINT pcount;
    rc = SQLNumParams(hStmt, &pcount);
    if (rc != SQL_SUCCESS) {
        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;
		}
	}
}