Browse code

db_text : support 'order by' in raw query

Luis Azedo authored on 18/06/2015 15:06:58
Showing 2 changed files
... ...
@@ -41,6 +41,7 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
41 41
     char* fields_end_ptr = NULL;
42 42
     char* fields_ptr = NULL;
43 43
     char* where_ptr = NULL;
44
+    char* order_start_ptr = NULL;
44 45
     char** tokens = NULL;
45 46
     str table;
46 47
     dbt_table_p _tbc = NULL;
... ...
@@ -52,6 +53,10 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
52 52
 	db_key_t* _k = NULL;
53 53
 	db_op_t* _op = NULL;
54 54
 	db_val_t* _v = NULL;
55
+	str order;
56
+	db_key_t k_order = NULL;
57
+
58
+	LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
55 59
 
56 60
     fields_end_ptr = strcasestr(_s->s, " from ");
57 61
     if(fields_end_ptr == NULL)
... ...
@@ -63,6 +68,11 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
63 63
     fields_ptr[len] = '\0';
64 64
     fields_ptr = dbt_trim(fields_ptr);
65 65
 
66
+    order_start_ptr = strcasestr(_s->s, " order by ");
67
+    if(order_start_ptr != NULL) {
68
+    	*order_start_ptr = '\0';
69
+    	order_start_ptr += 10;
70
+    }
66 71
 
67 72
 
68 73
     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)
80 80
 
81 81
     table.s = table_ptr;
82 82
     table.len = len;
83
+    LM_DBG("using table '%.*s'\n", table.len, table.s);
83 84
 
84 85
 	if(dbt_use_table(_h, &table) != 0) {
85 86
 		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)
120 120
 
121 121
 
122 122
 	dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
123
-    _tbc = NULL;
124
-
125
-	res = dbt_query(_h, _k, _op, _v, result_cols, nc, cols, NULL, _r);
123
+	_tbc = NULL;
124
+	if(order_start_ptr != NULL) {
125
+		order.s = order_start_ptr;
126
+		order.len = strlen(order_start_ptr);
127
+		k_order = ℴ
128
+	}
129
+	res = dbt_query(_h, _k, _op, _v, result_cols, nc, cols, k_order, _r);
126 130
 
127 131
 error:
128 132
 
... ...
@@ -31,7 +31,8 @@
31 31
 
32 32
 #include "dbt_raw_util.h"
33 33
 
34
-static const char* _regexp = "\\s*(and|or)?\\s*(\\w*)\\s*(>=|<=|<>|=|>|<)\\s*(')?([a-zA-Z0-9_-]*)(')?";
34
+//static const char* _regexp = "\\s*(and|or)?\\s*(\\w*)\\s*(>=|<=|<>|=|>|<)\\s*(')?([a-zA-Z0-9_-]*)(')?";
35
+static const char* _regexp = "\\s*(and|or)?\\s*(\\w*)\\s*(>=|<=|<>|=|>|<)\\s*(['\"])?([^'\"]*)(['\"])?";
35 36
 
36 37
 char** dbt_str_split(char* a_str, const char a_delim, int* c)
37 38
 {