From 49286cb2d2987e4d2f6a07f6b9ed46d4de97d9ac Mon Sep 17 00:00:00 2001 From: randomdan Date: Sun, 18 Nov 2012 19:13:16 +0000 Subject: Add a basic MySQL connector, not fully functional, but will suffice for p2tv --- libmysqlpp/command.cpp | 159 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 libmysqlpp/command.cpp (limited to 'libmysqlpp/command.cpp') diff --git a/libmysqlpp/command.cpp b/libmysqlpp/command.cpp new file mode 100644 index 0000000..f33306f --- /dev/null +++ b/libmysqlpp/command.cpp @@ -0,0 +1,159 @@ +#include "command.h" +#include "connection.h" +#include +#include + +MySQL::Command::Command(const Connection * conn, const std::string & sql) : + DB::Command(sql), + c(conn), + stmt(mysql_stmt_init(&conn->conn)), + paramsNeedBinding(false) +{ + if (!stmt) { + fprintf(stderr, "here1\n"); + throw Error(mysql_error(&conn->conn)); + } + if (mysql_stmt_prepare(stmt, sql.c_str(), sql.length())) { + fprintf(stderr, "here2\n"); + throw Error(mysql_stmt_error(stmt)); + } + binds.resize(mysql_stmt_param_count(stmt)); + if (binds.size()) { + paramsNeedBinding = true; + for (Binds::iterator i = binds.begin(); i != binds.end(); ++i) { + memset(&*i, 0, sizeof(MYSQL_BIND)); + i->buffer_type = MYSQL_TYPE_NULL; + } + } +} + +MySQL::Command::~Command() +{ + for (Binds::const_iterator i = binds.begin(); i != binds.end(); ++i) { + free(i->buffer); + } + mysql_stmt_close(stmt); +} + +void * +MySQL::Command::realloc(void * buffer, size_t size) +{ + void * newBuffer = ::realloc(buffer, size); + if (buffer != newBuffer) { + paramsNeedBinding = true; + } + return newBuffer; +} + +void +MySQL::Command::bindParamI(unsigned int n, int v) +{ + binds[n].buffer_type = MYSQL_TYPE_LONG; + binds[n].buffer = realloc(binds[n].buffer, sizeof(int)); + *static_cast(binds[n].buffer) = v; + binds[n].is_unsigned = 0; +} +void +MySQL::Command::bindParamI(unsigned int n, long int v) +{ + binds[n].buffer_type = MYSQL_TYPE_LONGLONG; + binds[n].buffer = realloc(binds[n].buffer, sizeof(long long int)); + *static_cast(binds[n].buffer) = v; + binds[n].is_unsigned = 0; +} +void +MySQL::Command::bindParamI(unsigned int n, long long int v) +{ + binds[n].buffer_type = MYSQL_TYPE_LONGLONG; + binds[n].buffer = realloc(binds[n].buffer, sizeof(long long int)); + *static_cast(binds[n].buffer) = v; + binds[n].is_unsigned = 0; +} +void +MySQL::Command::bindParamI(unsigned int n, unsigned int v) +{ + binds[n].buffer_type = MYSQL_TYPE_LONG; + binds[n].buffer = realloc(binds[n].buffer, sizeof(int)); + *static_cast(binds[n].buffer) = v; + binds[n].is_unsigned = 1; +} +void +MySQL::Command::bindParamI(unsigned int n, long unsigned int v) +{ + binds[n].buffer_type = MYSQL_TYPE_LONGLONG; + binds[n].buffer = realloc(binds[n].buffer, sizeof(long long int)); + *static_cast(binds[n].buffer) = v; + binds[n].is_unsigned = 1; +} +void +MySQL::Command::bindParamI(unsigned int n, long long unsigned int v) +{ + binds[n].buffer_type = MYSQL_TYPE_LONGLONG; + binds[n].buffer = realloc(binds[n].buffer, sizeof(long long int)); + *static_cast(binds[n].buffer) = v; + binds[n].is_unsigned = 1; +} +void +MySQL::Command::bindParamF(unsigned int n, double v) +{ + binds[n].buffer_type = MYSQL_TYPE_DOUBLE; + binds[n].buffer = realloc(binds[n].buffer, sizeof(double)); + *static_cast(binds[n].buffer) = v; +} +void +MySQL::Command::bindParamF(unsigned int n, float v) +{ + binds[n].buffer_type = MYSQL_TYPE_FLOAT; + binds[n].buffer = realloc(binds[n].buffer, sizeof(float)); + *static_cast(binds[n].buffer) = v; +} +void +MySQL::Command::bindParamS(unsigned int n, const Glib::ustring & s) +{ + binds[n].buffer_type = MYSQL_TYPE_STRING; + binds[n].buffer = realloc(binds[n].buffer, s.bytes()); + s.copy(static_cast(binds[n].buffer), s.bytes()); + binds[n].buffer_length = s.bytes(); + binds[n].is_unsigned = 0; +} +void +MySQL::Command::bindParamT(unsigned int n, const tm * v) +{ + binds[n].buffer_type = MYSQL_TYPE_DATETIME; + binds[n].buffer = realloc(binds[n].buffer, sizeof(MYSQL_TIME)); + MYSQL_TIME & ts = *static_cast(binds[n].buffer); + ts.year = v->tm_year + 1900; + ts.month = v->tm_mon + 1; + ts.day = v->tm_mday; + ts.hour = v->tm_hour; + ts.minute = v->tm_min; + ts.second = v->tm_sec; + ts.neg = 0; +} +void +MySQL::Command::bindParamT(unsigned int n, time_t v) +{ + struct tm t; + gmtime_r(&v, &t); + bindParamT(n, &t); +} +void +MySQL::Command::bindNull(unsigned int n) +{ + binds[n].buffer_type = MYSQL_TYPE_NULL; + binds[n].buffer = NULL; + free(binds[n].buffer); +} + +void +MySQL::Command::bindParams() +{ + if (paramsNeedBinding) { + if (mysql_stmt_bind_param(stmt, &binds.front())) { + throw Error(mysql_stmt_error(stmt)); + paramsNeedBinding = false; + } + } +} + + -- cgit v1.2.3