Merge pull request #192 from evanmiller/merge-166

Original author: @rogerswb 

* Dynamic allocation of MdbSQL->bound_values 

Was previously hardcoded to 256 columns max.
This commit is contained in:
Evan Miller 2020-11-01 08:31:50 -05:00 committed by GitHub
commit ad14a04dfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 47 deletions

View File

@ -44,8 +44,7 @@ typedef struct MdbSQL
MdbTableDef *cur_table;
MdbSargNode *sarg_tree;
GList *sarg_stack;
/* FIX ME */
void *bound_values[256];
GPtrArray *bound_values;
unsigned char *kludge_ttable_pg;
long max_rows;
char error_msg[1024];
@ -80,6 +79,7 @@ void mdb_sql_error(MdbSQL* sql, const char *fmt, ...);
MdbSQL *mdb_sql_init(void);
MdbSQLSarg *mdb_sql_alloc_sarg(void);
MdbHandle *mdb_sql_open(MdbSQL *sql, char *db_name);
void mdb_sql_free_tree(MdbSargNode *tree);
int mdb_sql_add_sarg(MdbSQL *sql, char *col_name, int op, char *constant);
void mdb_sql_all_columns(MdbSQL *sql);
void mdb_sql_sel_count(MdbSQL *sql);
@ -101,13 +101,13 @@ MdbSQL* mdb_sql_run_query (MdbSQL*, const gchar*);
void mdb_sql_set_maxrow(MdbSQL *sql, int maxrow);
int mdb_sql_eval_expr(MdbSQL *sql, char *const1, int op, char *const2);
void mdb_sql_bind_all(MdbSQL *sql);
void mdb_sql_unbind_all(MdbSQL *sql);
int mdb_sql_fetch_row(MdbSQL *sql, MdbTableDef *table);
int mdb_sql_add_temp_col(MdbSQL *sql, MdbTableDef *ttable, int col_num, char *name, int col_type, int col_size, int is_fixed);
void mdb_sql_bind_column(MdbSQL *sql, int colnum, void *varaddr, int *len_ptr);
int mdb_sql_add_limit(MdbSQL *sql, char *limit, int percent);
int mdb_sql_get_limit(MdbSQL *sql);
int parse_sql(MdbSQL * mdb, const gchar* str);
#ifdef __cplusplus

View File

@ -68,6 +68,7 @@ MdbSQL *sql;
sql = (MdbSQL *) g_malloc0(sizeof(MdbSQL));
sql->columns = g_ptr_array_new();
sql->tables = g_ptr_array_new();
sql->bound_values = g_ptr_array_new();
sql->sarg_tree = NULL;
sql->sarg_stack = NULL;
sql->max_rows = -1;
@ -144,6 +145,34 @@ static void mdb_sql_free_tables(GPtrArray *tables)
g_ptr_array_free(tables, TRUE);
}
/* This gives us a nice, uniform place to keep up with memory that needs to be freed */
static void mdb_sql_free(MdbSQL *sql)
{
/* Free MdbTableDef structures */
if (sql->cur_table) {
mdb_index_scan_free(sql->cur_table);
mdb_free_tabledef(sql->cur_table);
sql->cur_table = NULL;
}
/* Free MdbSQLColumns and MdbSQLTables */
mdb_sql_free_columns(sql->columns);
mdb_sql_free_tables(sql->tables);
/* Free sargs */
if (sql->sarg_tree) {
mdb_sql_free_tree(sql->sarg_tree);
sql->sarg_tree = NULL;
}
g_list_free(sql->sarg_stack);
sql->sarg_stack = NULL;
/* Free bindings */
mdb_sql_unbind_all(sql);
g_ptr_array_free(sql->bound_values, TRUE);
}
void
mdb_sql_close(MdbSQL *sql)
{
@ -541,13 +570,7 @@ void mdb_sql_dump(MdbSQL *sql)
}
void mdb_sql_exit(MdbSQL *sql)
{
/* Free the memory associated with the SQL engine */
mdb_sql_reset(sql);
g_ptr_array_free(sql->columns, TRUE);
g_ptr_array_free(sql->tables, TRUE);
/* If libmdb has been initialized, terminate it */
mdb_sql_free(sql);
if (sql->mdb)
mdb_close(sql->mdb);
@ -556,41 +579,18 @@ void mdb_sql_exit(MdbSQL *sql)
}
void mdb_sql_reset(MdbSQL *sql)
{
unsigned int i;
if (sql->cur_table) {
mdb_index_scan_free(sql->cur_table);
if (sql->cur_table->sarg_tree) {
mdb_sql_free_tree(sql->cur_table->sarg_tree);
sql->cur_table->sarg_tree = NULL;
}
mdb_free_tabledef(sql->cur_table);
sql->cur_table = NULL;
}
/* Reset bound values */
for (i=0;i<sql->num_columns;i++) {
g_free(sql->bound_values[i]);
sql->bound_values[i] = NULL;
}
mdb_sql_free(sql);
/* Reset columns */
mdb_sql_free_columns(sql->columns);
sql->num_columns = 0;
sql->columns = g_ptr_array_new();
/* Reset tables */
mdb_sql_free_tables(sql->tables);
/* Reset MdbSQL tables */
sql->num_tables = 0;
sql->tables = g_ptr_array_new();
/* Reset sargs */
if (sql->sarg_tree) {
mdb_sql_free_tree(sql->sarg_tree);
sql->sarg_tree = NULL;
}
g_list_free(sql->sarg_stack);
sql->sarg_stack = NULL;
/* Reset bindings */
sql->bound_values = g_ptr_array_new();
sql->all_columns = 0;
sql->sel_count = 0;
@ -884,12 +884,24 @@ void
mdb_sql_bind_all(MdbSQL *sql)
{
unsigned int i;
void *bound_value;
for (i=0;i<sql->num_columns;i++) {
sql->bound_values[i] = g_malloc0(MDB_BIND_SIZE);
mdb_sql_bind_column(sql, i+1, sql->bound_values[i], NULL);
bound_value = g_malloc0(sql->mdb->bind_size);
g_ptr_array_add(sql->bound_values, bound_value);
mdb_sql_bind_column(sql, i+1, bound_value, NULL);
}
}
void mdb_sql_unbind_all(MdbSQL *sql)
{
unsigned int i;
for (i=0;i<sql->bound_values->len;i++) {
g_free(g_ptr_array_index(sql->bound_values, i));
}
}
/*
* mdb_sql_fetch_row is now just a wrapper around mdb_fetch_row.
* It is left here only for backward compatibility.
@ -934,7 +946,7 @@ mdb_sql_dump_results(MdbSQL *sql)
while(mdb_fetch_row(sql->cur_table)) {
for (j=0;j<sql->num_columns;j++) {
sqlcol = g_ptr_array_index(sql->columns,j);
print_value(sql->bound_values[j],sqlcol->disp_size,!j);
print_value(g_ptr_array_index(sql->bound_values, j),sqlcol->disp_size,!j);
}
fprintf(stdout,"\n");
}
@ -946,10 +958,6 @@ mdb_sql_dump_results(MdbSQL *sql)
}
fprintf(stdout,"\n");
/* clean up */
for (j=0;j<sql->num_columns;j++) {
g_free(sql->bound_values[j]);
}
/* the column and table names are no good now */
mdb_sql_reset(sql);

View File

@ -230,11 +230,11 @@ dump_results(FILE *out, MdbSQL *sql, char *delimiter)
while(mdb_sql_fetch_row(sql, sql->cur_table)) {
for (j=0;j<sql->num_columns-1;j++) {
sqlcol = g_ptr_array_index(sql->columns,j);
fprintf(out, "%s%s", (char*)(sql->bound_values[j]),
fprintf(out, "%s%s", (char*)(g_ptr_array_index(sql->bound_values, j)),
delimiter ? delimiter : "\t");
}
sqlcol = g_ptr_array_index(sql->columns,sql->num_columns-1);
fprintf(out, "%s", (char*)(sql->bound_values[sql->num_columns-1]));
fprintf(out, "%s", (char*)(g_ptr_array_index(sql->bound_values, sql->num_columns-1)));
fprintf(out,"\n");
fflush(out);
}
@ -278,7 +278,7 @@ dump_results_pp(FILE *out, MdbSQL *sql)
while(mdb_sql_fetch_row(sql, sql->cur_table)) {
for (j=0;j<sql->num_columns;j++) {
sqlcol = g_ptr_array_index(sql->columns,j);
print_value(out, sql->bound_values[j],sqlcol->disp_size,!j);
print_value(out, (char *) g_ptr_array_index(sql->bound_values, j), sqlcol->disp_size,!j);
}
fprintf(out,"\n");
fflush(out);