aboutsummaryrefslogtreecommitdiff
path: root/src/mysql.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mysql.cpp')
-rw-r--r--src/mysql.cpp32
1 files changed, 11 insertions, 21 deletions
diff --git a/src/mysql.cpp b/src/mysql.cpp
index 040670e..381898b 100644
--- a/src/mysql.cpp
+++ b/src/mysql.cpp
@@ -20,6 +20,7 @@
*/
#include <plugin.h>
+#include "tools.h"
#include <iostream>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
@@ -62,11 +63,12 @@ mysql::ResultSet* MySQLConnection::Query(char* sql) {
return result;
}
-MYFLT MySQLConnection::Scalar(char* sql, int row=0, int col=0) {
+MYFLT MySQLConnection::Scalar(char* sql, int row, int col) {
mysql::ResultSet* res = Query(sql);
mysql::ResultSetMetaData* meta = res->getMetaData();
int colCount = meta->getColumnCount();
if (col > colCount - 1) {
+ delete res;
throw std::runtime_error("column number out of range");
}
@@ -78,12 +80,13 @@ MYFLT MySQLConnection::Scalar(char* sql, int row=0, int col=0) {
return result;
}
-char* MySQLConnection::ScalarString(char* sql, int row=0, int col=0) {
+char* MySQLConnection::ScalarString(char* sql, csnd::Csound* csound, int row, int col) {
mysql::ResultSet* res = Query(sql);
mysql::ResultSetMetaData* meta = res->getMetaData();
int colCount = meta->getColumnCount();
if (col > colCount - 1) {
+ delete res;
throw std::runtime_error("column number out of range");
}
@@ -91,7 +94,7 @@ char* MySQLConnection::ScalarString(char* sql, int row=0, int col=0) {
for (int rowIndex = 0; rowIndex <= row; rowIndex++) {
res->next();
}
- char* result = res->getString(col + 1).c_str();
+ char* result = csound->strdup((char*) res->getString(col + 1).c_str());
delete res;
@@ -101,31 +104,18 @@ char* MySQLConnection::ScalarString(char* sql, int row=0, int col=0) {
void MySQLConnection::ToArray(mysql::ResultSet* result, csnd::Csound* csound, ARRAYDAT* array, bool asString) {
mysql::ResultSetMetaData* meta = result->getMetaData();
- int colNum = meta->getColumnCount();
- int rowNum = result->rowsCount();
- int totalResults = colNum * rowNum;
- array->sizes = csound->calloc(sizeof(int32_t) * 2);
- array->sizes[0] = rowNum;
- array->sizes[1] = colNum;
- array->dimensions = 2;
- CS_VARIABLE *var = array->arrayType->createVariable(csound, NULL);
- array->arrayMemberSize = var->memBlockSize;
- array->data = csound->calloc(var->memBlockSize * totalResults);
- STRINGDAT* strings;
- if (asString) {
- strings = (STRINGDAT*) array->data;
- }
+ int cols = meta->getColumnCount();
+ int rows = result->rowsCount();
+ STRINGDAT* strings = arrayInit(csound, array, rows, cols);
int colIndex;
int index = 0;
while (result->next()) {
colIndex = 0;
- while (colIndex < colNum) {
+ while (colIndex < cols) {
if (asString) {
- char* item = result->getString(colIndex + 1).c_str();
- strings[index].size = strlen(item) + 1;
- strings[index].data = csound->strdup(item);
+ insertArrayStringItem(csound, strings, index, (char*) result->getString(colIndex + 1).c_str());
} else {
array->data[index] = (MYFLT) result->getDouble(colIndex + 1);
}