2023-12-10 23:23:15 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-12-08 22:26:16 +00:00
|
|
|
#include "statement.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
static uint64_t TIME_LENGTH = sizeof(MYSQL_TIME);
|
|
|
|
|
|
|
|
MySQL::Statement::Statement(MYSQL* connection, const char* statement):
|
|
|
|
stmt(mysql_stmt_init(connection)),
|
2023-12-20 22:42:13 +00:00
|
|
|
param()
|
2023-12-08 22:26:16 +00:00
|
|
|
{
|
|
|
|
int result = mysql_stmt_prepare(stmt.get(), statement, strlen(statement));
|
|
|
|
if (result != 0)
|
|
|
|
throw std::runtime_error(std::string("Error preparing statement: ") + mysql_stmt_error(stmt.get()));
|
|
|
|
}
|
|
|
|
|
2023-12-20 22:42:13 +00:00
|
|
|
void MySQL::Statement::bind(void* value, enum_field_types type, bool usigned) {
|
2023-12-08 22:26:16 +00:00
|
|
|
MYSQL_BIND& result = param.emplace_back();
|
|
|
|
std::memset(&result, 0, sizeof(result));
|
|
|
|
|
|
|
|
result.buffer_type = type;
|
|
|
|
result.buffer = value;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MYSQL_TYPE_STRING:
|
|
|
|
case MYSQL_TYPE_VAR_STRING:
|
2023-12-20 22:42:13 +00:00
|
|
|
result.buffer_length = strlen(static_cast<char*>(value));
|
2023-12-08 22:26:16 +00:00
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_DATE:
|
2023-12-20 22:42:13 +00:00
|
|
|
result.buffer_length = TIME_LENGTH;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_LONG:
|
|
|
|
case MYSQL_TYPE_LONGLONG:
|
|
|
|
case MYSQL_TYPE_SHORT:
|
|
|
|
case MYSQL_TYPE_TINY:
|
|
|
|
result.is_unsigned = usigned;
|
2023-12-08 22:26:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("Type: " + std::to_string(type) + " is not yet supported in bind");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MySQL::Statement::execute() {
|
|
|
|
int result = mysql_stmt_bind_param(stmt.get(), param.data());
|
|
|
|
if (result != 0)
|
|
|
|
throw std::runtime_error(std::string("Error binding statement: ") + mysql_stmt_error(stmt.get()));
|
|
|
|
|
|
|
|
result = mysql_stmt_execute(stmt.get());
|
|
|
|
if (result != 0)
|
|
|
|
throw std::runtime_error(std::string("Error executing statement: ") + mysql_stmt_error(stmt.get()));
|
|
|
|
}
|