Module: kamailio Branch: master Commit: 2a42a17d4ff434da065a988be1c8b9cdaa1938ed URL: https://github.com/kamailio/kamailio/commit/2a42a17d4ff434da065a988be1c8b9cd...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: 2015-06-19T10:04:48+02:00
Merge pull request #216 from kamailio/db_text_raw_query
db_text : support 'order by' in raw query
---
Modified: modules/db_text/dbt_raw_query.c Modified: modules/db_text/dbt_raw_util.c
---
Diff: https://github.com/kamailio/kamailio/commit/2a42a17d4ff434da065a988be1c8b9cd... Patch: https://github.com/kamailio/kamailio/commit/2a42a17d4ff434da065a988be1c8b9cd...
---
diff --git a/modules/db_text/dbt_raw_query.c b/modules/db_text/dbt_raw_query.c index 143a585..ef62503 100644 --- a/modules/db_text/dbt_raw_query.c +++ b/modules/db_text/dbt_raw_query.c @@ -41,6 +41,7 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r) char* fields_end_ptr = NULL; char* fields_ptr = NULL; char* where_ptr = NULL; + char* order_start_ptr = NULL; char** tokens = NULL; str table; dbt_table_p _tbc = NULL; @@ -52,6 +53,10 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r) db_key_t* _k = NULL; db_op_t* _op = NULL; db_val_t* _v = NULL; + str order; + db_key_t k_order = NULL; + + LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
fields_end_ptr = strcasestr(_s->s, " from "); if(fields_end_ptr == NULL) @@ -63,6 +68,11 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r) fields_ptr[len] = '\0'; fields_ptr = dbt_trim(fields_ptr);
+ order_start_ptr = strcasestr(_s->s, " order by "); + if(order_start_ptr != NULL) { + *order_start_ptr = '\0'; + order_start_ptr += 10; + }
where_ptr = strcasestr(_s->s, " where "); @@ -80,6 +90,7 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
table.s = table_ptr; table.len = len; + LM_DBG("using table '%.*s'\n", table.len, table.s);
if(dbt_use_table(_h, &table) != 0) { LM_ERR("use table is invalid %.*s\n", table.len, table.s); @@ -120,9 +131,13 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); - _tbc = NULL; - - res = dbt_query(_h, _k, _op, _v, result_cols, nc, cols, NULL, _r); + _tbc = NULL; + if(order_start_ptr != NULL) { + order.s = order_start_ptr; + order.len = strlen(order_start_ptr); + k_order = ℴ + } + res = dbt_query(_h, _k, _op, _v, result_cols, nc, cols, k_order, _r);
error:
diff --git a/modules/db_text/dbt_raw_util.c b/modules/db_text/dbt_raw_util.c index f000f3b..97012cc 100644 --- a/modules/db_text/dbt_raw_util.c +++ b/modules/db_text/dbt_raw_util.c @@ -31,7 +31,8 @@
#include "dbt_raw_util.h"
-static const char* _regexp = "\s*(and|or)?\s*(\w*)\s*(>=|<=|<>|=|>|<)\s*(')?([a-zA-Z0-9_-]*)(')?"; +//static const char* _regexp = "\s*(and|or)?\s*(\w*)\s*(>=|<=|<>|=|>|<)\s*(')?([a-zA-Z0-9_-]*)(')?"; +static const char* _regexp = "\s*(and|or)?\s*(\w*)\s*(>=|<=|<>|=|>|<)\s*(['"])?([^'"]*)(['"])?";
char** dbt_str_split(char* a_str, const char a_delim, int* c) {