Added a str_builder variant of compileWhere/compileLogic
This commit is contained in:
@@ -25,24 +25,37 @@ where_builder *createWhereBuilder(where_builder *initial_clause)
|
||||
return wb;
|
||||
}
|
||||
int compileWhereBuilder(where_builder *wb, char **str, size_t *str_len)
|
||||
{
|
||||
struct str_builder_t *sb;
|
||||
|
||||
if ((sb = strbld_create()) == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
compileWhereBuilder_sb(wb, sb);
|
||||
|
||||
return strbld_finalize_or_destroy(&sb, str, str_len);
|
||||
}
|
||||
void compileWhereBuilder_sb(where_builder *wb, str_builder *sb)
|
||||
{
|
||||
switch(wb->logic_type) {
|
||||
case CLAUSE:
|
||||
{
|
||||
return compileWhere((where_clause *)wb, str, str_len);
|
||||
compileWhere_sb((where_clause *)wb, sb);
|
||||
break;
|
||||
}
|
||||
case OR:
|
||||
case AND:
|
||||
{
|
||||
return compileLogic((where_logic *)wb, str, str_len);
|
||||
compileLogic_sb((where_logic *)wb, sb);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void destroyWhereBuilder(where_builder **wb_ptr)
|
||||
{
|
||||
where_builder *wb = finalizeWhere(*wb_ptr);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "db_column.h"
|
||||
#include "strext.h"
|
||||
|
||||
|
||||
enum e_where_logic_t {
|
||||
@@ -40,6 +41,7 @@ typedef struct where_builder_t where_builder;
|
||||
|
||||
where_builder *createWhereBuilder(where_builder *initial_clause);
|
||||
int compileWhereBuilder(where_builder *wb, char **str, size_t *str_len);
|
||||
void compileWhereBuilder_sb(where_builder *wb, str_builder *sb);
|
||||
void destroyWhereBuilder(where_builder **wb_ptr);
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "string.h"
|
||||
|
||||
#include "db_where-builder_p.h"
|
||||
#include "strext.h"
|
||||
|
||||
|
||||
where_logic *createLogic(e_where_logic type, size_t initial_size)
|
||||
@@ -36,26 +35,24 @@ where_logic *createLogic(e_where_logic type, size_t initial_size)
|
||||
int compileLogic(where_logic *logic, char **str, size_t *str_len)
|
||||
{
|
||||
struct str_builder_t *sb;
|
||||
char *tmp;
|
||||
size_t tmp_len;
|
||||
|
||||
sb = strbld_create();
|
||||
if (sb == 0) {
|
||||
*str = 0;
|
||||
*str_len = 0;
|
||||
if ((sb = strbld_create()) == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
compileLogic_sb(logic, sb);
|
||||
|
||||
return strbld_finalize_or_destroy(&sb, str, str_len);
|
||||
}
|
||||
void compileLogic_sb(where_logic *logic, str_builder *sb)
|
||||
{
|
||||
if (logic->n_clauses == 0) {
|
||||
return strbld_finalize_or_destroy(&sb, str, str_len);
|
||||
return;
|
||||
}
|
||||
|
||||
strbld_char(sb, '(');
|
||||
for (size_t i = 0; i < logic->n_clauses; i++) {
|
||||
if (compileWhereBuilder(logic->clauses[i], &tmp, &tmp_len) == 0) {
|
||||
strbld_str(sb, tmp, tmp_len);
|
||||
free(tmp);
|
||||
}
|
||||
compileWhereBuilder_sb(logic->clauses[i], sb);
|
||||
if (i < (logic->n_clauses - 1)) {
|
||||
if (logic->logic_type == OR) {
|
||||
strbld_str(sb, " OR ", 4);
|
||||
@@ -65,8 +62,6 @@ int compileLogic(where_logic *logic, char **str, size_t *str_len)
|
||||
}
|
||||
}
|
||||
strbld_char(sb, ')');
|
||||
|
||||
return strbld_finalize_or_destroy(&sb, str, str_len);
|
||||
}
|
||||
void freeLogic(where_logic **logic_ptr)
|
||||
{
|
||||
@@ -162,13 +157,19 @@ where_clause *createWhere(const char *tbl, const char *col, e_where_op op)
|
||||
int compileWhere(where_clause *clause, char **str, size_t *str_len)
|
||||
{
|
||||
struct str_builder_t *sb;
|
||||
size_t idx;
|
||||
|
||||
sb = strbld_create();
|
||||
if (sb == 0) {
|
||||
if ((sb = strbld_create()) == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
compileWhere_sb(clause, sb);
|
||||
|
||||
return strbld_finalize_or_destroy(&sb, str, str_len);
|
||||
}
|
||||
void compileWhere_sb(where_clause *clause, str_builder *sb)
|
||||
{
|
||||
size_t idx;
|
||||
|
||||
// Column
|
||||
if (clause->table != 0) {
|
||||
strbld_char(sb, '`');
|
||||
@@ -231,8 +232,6 @@ int compileWhere(where_clause *clause, char **str, size_t *str_len)
|
||||
strbld_char(sb, ' ');
|
||||
strbld_char(sb, ')');
|
||||
}
|
||||
|
||||
return strbld_finalize_or_destroy(&sb, str, str_len);
|
||||
}
|
||||
void freeWhere(where_clause **where_ptr)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define H__DB_WHERE_BUILDER_P__
|
||||
|
||||
#include "db_where-builder.h"
|
||||
#include "strext.h"
|
||||
|
||||
#ifndef DLL_LOCAL
|
||||
# if defined _WIN32 || defined __CYGWIN__
|
||||
@@ -53,6 +54,8 @@ where_logic *createLogic(e_where_logic type, size_t initial_size);
|
||||
DLL_LOCAL
|
||||
int compileLogic(where_logic *logic, char **str, size_t *str_len);
|
||||
DLL_LOCAL
|
||||
void compileLogic_sb(where_logic *logic, str_builder *sb);
|
||||
DLL_LOCAL
|
||||
void freeLogic(where_logic **logic_ptr);
|
||||
|
||||
DLL_LOCAL
|
||||
@@ -63,6 +66,8 @@ where_clause *createWhere(const char *tbl, const char *col, e_where_op op);
|
||||
DLL_LOCAL
|
||||
int compileWhere(where_clause *clause, char **str, size_t *str_len);
|
||||
DLL_LOCAL
|
||||
void compileWhere_sb(where_clause *clause, str_builder *sb);
|
||||
DLL_LOCAL
|
||||
void freeWhere(where_clause **where_ptr);
|
||||
|
||||
DLL_LOCAL
|
||||
|
||||
Reference in New Issue
Block a user