I am building a simple mysql connector in c++ to do something very specific and return the result to PHP. Along the way I encountered a problem that I cannot solve nicely.
The function I am writing gets the results of a query and builds an array to return to PHP. I want to assign the right data types to the returned values in this array.
In the example below, you can see that I fetch the data from the field with getString. This returns an SQLString. I want to make getString dynamic (getInt, getUInt,...) to have the right type. I can access the field type with res_meta->getColumnTypeName(i). My first thought was to make a switch statement with the column name to execute the right method. This seems to be a non-elegant way to me. Are there any other options?
// Select all columns/rows from example_table
res = stmt->executeQuery(query);
sql::ResultSetMetaData *res_meta = res -> getMetaData();
int columns = res_meta -> getColumnCount();
//Loop for each row
int key = 0;
Php::Value array;
while (res->next()) {
array[key] = "";
for (int i = 1; i <= columns; i++) {
string column = res_meta->getColumnName(i);
string columnTypeName = res_meta->getColumnTypeName(i);
Php::value value = res->getString(i);
array[key][column] = value;
}
key++;
}
Aucun commentaire:
Enregistrer un commentaire