summaryrefslogtreecommitdiff
path: root/libodbcpp/column.cpp
blob: 58f9b3144b8d8b799cd90c8b03192063c9ef922b (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "column.h"
#include "error.h"
#include "timetypepair.h"

ODBC::Column::Column(String n, unsigned int i) :
	colNo(i),
	name(n),
	fresh(false)
{
}

ODBC::Column::~Column()
{
}

#define ODBC_DEFAULT_COLUMN_CAST(ctype, rtype) \
	ODBC::Column::operator rtype() const { \
		return (dynamic_cast<const _Column<ctype>& >(*this)).value; \
	}

ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, unsigned int);
ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, unsigned long long);
ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, long long);
ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, int);
ODBC_DEFAULT_COLUMN_CAST(SQLDOUBLE, double);
ODBC_DEFAULT_COLUMN_CAST(SQLDOUBLE, float);
ODBC_DEFAULT_COLUMN_CAST(SQLCHAR*, const unsigned char *);
ODBC_DEFAULT_COLUMN_CAST(SQLCHAR*, String);
ODBC::Column::operator std::string() const {
	return (const char*)((dynamic_cast<const _Column<SQLCHAR*>& >(*this)).value);
}
ODBC::Column::operator const char * () const {
	return (const char*)((dynamic_cast<const _Column<SQLCHAR*>& >(*this)).value);
}
ODBC::Column::operator const struct tm & () const {
	const _Column<TimeTypePair>& c = dynamic_cast<const _Column<TimeTypePair>& >(*this);
	if (c.fresh) {
		c.value.sql2c();
		c.fresh = false;
	}
	return c.value.c();
}

void
ODBC::Column::bind(SQLHANDLE hStmt, SQLUINTEGER col, SQLSMALLINT ctype, void * buf, size_t size)
{
	bindSize = size;
	RETCODE rc = SQLBindCol(hStmt, col, ctype, buf, bindSize, &bindLen);
	if (rc != SQL_SUCCESS) {
		throw Error(rc, SQL_HANDLE_STMT, hStmt, "%s: Bind column %lu", __FUNCTION__, col);
	}
}