|
@@ -167,29 +167,315 @@ error:
|
|
|
|
|
|
int dbt_raw_query_update(db1_con_t* _h, str* _s, db1_res_t** _r)
|
|
|
{
|
|
|
- int res = -1;
|
|
|
+ int res = -1;
|
|
|
+ int len;
|
|
|
+ char* table_ptr = NULL;
|
|
|
+ char* fields_end_ptr = NULL;
|
|
|
+ char* fields_start_ptr = NULL;
|
|
|
+ char* fields_ptr = NULL;
|
|
|
+ char* where_ptr = NULL;
|
|
|
+ char* table_start_ptr = NULL;
|
|
|
+ str table;
|
|
|
+ dbt_table_p _tbc = NULL;
|
|
|
+ int ncols = 0;
|
|
|
+ int nkeys = 0;
|
|
|
+ db_key_t* _k = NULL;
|
|
|
+ db_op_t* _op1 = NULL;
|
|
|
+ db_val_t* _kv = NULL;
|
|
|
|
|
|
+ db_key_t* _c = NULL;
|
|
|
+ db_op_t* _op2 = NULL;
|
|
|
+ db_val_t* _cv = NULL;
|
|
|
|
|
|
+ LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
|
|
|
+
|
|
|
+ table_start_ptr = _s->s + 6;
|
|
|
+ fields_start_ptr = strcasestr(_s->s, " set ");
|
|
|
+ if(fields_start_ptr == NULL)
|
|
|
+ return res;
|
|
|
+
|
|
|
+ len = fields_start_ptr - table_start_ptr;
|
|
|
+ table_ptr = pkg_malloc(len);
|
|
|
+ strncpy(table_ptr, table_start_ptr, len);
|
|
|
+ table_ptr[len] = '\0';
|
|
|
+ dbt_trim(table_ptr);
|
|
|
+ table.s = table_ptr;
|
|
|
+ table.len = len;
|
|
|
+
|
|
|
+ where_ptr = strcasestr(_s->s, " where ");
|
|
|
+ fields_end_ptr = where_ptr;
|
|
|
+ len = fields_end_ptr - ( fields_start_ptr + 4) + 1;
|
|
|
+ fields_ptr = pkg_malloc(len);
|
|
|
+ strncpy(fields_ptr, fields_start_ptr + 4, len);
|
|
|
+ fields_ptr[len] = '\0';
|
|
|
+ fields_ptr = dbt_trim(fields_ptr);
|
|
|
+
|
|
|
+ ncols = dbt_build_where(fields_ptr, &_c, &_op2, &_cv);
|
|
|
+ if(ncols <0) {
|
|
|
+ LM_ERR("unexpected error buuilding fields\n");
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(where_ptr == NULL) {
|
|
|
+ LM_ERR("specify where clause to determine keys\n");
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+ nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv);
|
|
|
+ if(nkeys < 1) {
|
|
|
+ LM_ERR("needsa at least one key\n");
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+ _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+ if(!_tbc)
|
|
|
+ {
|
|
|
+ LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+ _tbc = NULL;
|
|
|
+ res = dbt_update(_h, _k, _op1, _kv, _c, _cv, nkeys, ncols);
|
|
|
+
|
|
|
+error:
|
|
|
+
|
|
|
+ if(_tbc)
|
|
|
+ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+
|
|
|
+ if(fields_ptr)
|
|
|
+ pkg_free(fields_ptr);
|
|
|
+
|
|
|
+ if(table_ptr)
|
|
|
+ pkg_free(table_ptr);
|
|
|
+
|
|
|
+ dbt_clean_where(nkeys, _k, _op1, _kv);
|
|
|
+ dbt_clean_where(ncols, _c, _op2, _cv);
|
|
|
+
|
|
|
+ return res;
|
|
|
|
|
|
- return res;
|
|
|
}
|
|
|
|
|
|
int dbt_raw_query_delete(db1_con_t* _h, str* _s, db1_res_t** _r)
|
|
|
{
|
|
|
- int res = -1;
|
|
|
+ int res = -1;
|
|
|
+ int len;
|
|
|
+ char* table_ptr = NULL;
|
|
|
+ char* fields_end_ptr = NULL;
|
|
|
+ char* fields_ptr = NULL;
|
|
|
+ char* where_ptr = NULL;
|
|
|
+ str table;
|
|
|
+ dbt_table_p _tbc = NULL;
|
|
|
+ int nkeys = 0;
|
|
|
+ db_key_t* _k = NULL;
|
|
|
+ db_op_t* _op1 = NULL;
|
|
|
+ db_val_t* _kv = NULL;
|
|
|
|
|
|
+ LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
|
|
|
|
|
|
+ fields_end_ptr = strcasestr(_s->s, " from ");
|
|
|
+ if(fields_end_ptr == NULL)
|
|
|
+ return res;
|
|
|
|
|
|
- return res;
|
|
|
+ where_ptr = strcasestr(_s->s, " where ");
|
|
|
+ if(where_ptr == NULL) {
|
|
|
+ len = strlen(fields_end_ptr + 6);
|
|
|
+ } else {
|
|
|
+ len = where_ptr - (fields_end_ptr + 6);
|
|
|
+ nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv);
|
|
|
+ }
|
|
|
+
|
|
|
+ table_ptr = pkg_malloc(len);
|
|
|
+ strncpy(table_ptr, fields_end_ptr + 6, len);
|
|
|
+ table_ptr[len] = '\0';
|
|
|
+ dbt_trim(table_ptr);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+ _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+ if(!_tbc)
|
|
|
+ {
|
|
|
+ LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+ _tbc = NULL;
|
|
|
+ res = dbt_delete(_h, _k, _op1, _kv, nkeys);
|
|
|
+
|
|
|
+error:
|
|
|
+
|
|
|
+ if(_tbc)
|
|
|
+ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+
|
|
|
+ if(fields_ptr)
|
|
|
+ pkg_free(fields_ptr);
|
|
|
+
|
|
|
+ if(table_ptr)
|
|
|
+ pkg_free(table_ptr);
|
|
|
+
|
|
|
+ dbt_clean_where(nkeys, _k, _op1, _kv);
|
|
|
+
|
|
|
+ return res;
|
|
|
}
|
|
|
|
|
|
+int dbt_raw_query_insert(db1_con_t* _h, str* _s, db1_res_t** _r)
|
|
|
+{
|
|
|
+ int res = -1;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+int dbt_raw_query_replace(db1_con_t* _h, str* _s, db1_res_t** _r)
|
|
|
+{
|
|
|
+ int res = -1;
|
|
|
+ int i, len;
|
|
|
+ char* table_ptr = NULL;
|
|
|
+ char* fields_end_ptr = NULL;
|
|
|
+ char* fields_start_ptr = NULL;
|
|
|
+ char* fields_ptr = NULL;
|
|
|
+ char* where_ptr = NULL;
|
|
|
+ char* table_start_ptr = NULL;
|
|
|
+ str table;
|
|
|
+ dbt_table_p _tbc = NULL;
|
|
|
+ int cols;
|
|
|
+ int n = 0;
|
|
|
+ int ncols = 0;
|
|
|
+ int nkeys = 0;
|
|
|
+ db_key_t* _k = NULL;
|
|
|
+ db_op_t* _op1 = NULL;
|
|
|
+ db_val_t* _kv = NULL;
|
|
|
+
|
|
|
+ db_key_t* _c = NULL;
|
|
|
+ db_op_t* _op2 = NULL;
|
|
|
+ db_val_t* _cv = NULL;
|
|
|
+
|
|
|
+ db_key_t* _f = NULL;
|
|
|
+ db_val_t* _v = NULL;
|
|
|
+
|
|
|
+ LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
|
|
|
+
|
|
|
+ table_start_ptr = _s->s + 7;
|
|
|
+ fields_start_ptr = strcasestr(_s->s, " set ");
|
|
|
+ if(fields_start_ptr == NULL)
|
|
|
+ return res;
|
|
|
+
|
|
|
+ len = fields_start_ptr - table_start_ptr;
|
|
|
+ table_ptr = pkg_malloc(len);
|
|
|
+ strncpy(table_ptr, table_start_ptr, len);
|
|
|
+ table_ptr[len] = '\0';
|
|
|
+ dbt_trim(table_ptr);
|
|
|
+ table.s = table_ptr;
|
|
|
+ table.len = len;
|
|
|
+
|
|
|
+ where_ptr = strcasestr(_s->s, " where ");
|
|
|
+ fields_end_ptr = where_ptr;
|
|
|
+ len = fields_end_ptr - ( fields_start_ptr + 4) + 1;
|
|
|
+ fields_ptr = pkg_malloc(len);
|
|
|
+ strncpy(fields_ptr, fields_start_ptr + 4, len);
|
|
|
+ fields_ptr[len] = '\0';
|
|
|
+ fields_ptr = dbt_trim(fields_ptr);
|
|
|
+
|
|
|
+ ncols = dbt_build_where(fields_ptr, &_c, &_op2, &_cv);
|
|
|
+ if(ncols <0) {
|
|
|
+ LM_ERR("unexpected error buuilding fields\n");
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(where_ptr == NULL) {
|
|
|
+ LM_ERR("specify where clause to determine keys\n");
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+ nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv);
|
|
|
+ if(nkeys < 1) {
|
|
|
+ LM_ERR("needsa at least one key\n");
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+ _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+ if(!_tbc)
|
|
|
+ {
|
|
|
+ LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
|
|
|
+ goto error;
|
|
|
+ }
|
|
|
+
|
|
|
+ cols = nkeys + ncols;
|
|
|
+ _f = pkg_malloc(sizeof(db_key_t) * cols);
|
|
|
+ _v = pkg_malloc(sizeof(db_val_t) * cols);
|
|
|
+ for(n=0; n < nkeys; n++) {
|
|
|
+ _f[n] = _k[n];
|
|
|
+ _v[n] = _kv[n];
|
|
|
+ }
|
|
|
+ for(i=n; i < cols; i++) {
|
|
|
+ _f[i] = _c[i-n];
|
|
|
+ _v[i] = _cv[i-n];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+ _tbc = NULL;
|
|
|
+ res = dbt_replace(_h, _f, _v, cols, nkeys, 0);
|
|
|
+
|
|
|
+error:
|
|
|
+
|
|
|
+ if(_tbc)
|
|
|
+ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
|
|
|
+
|
|
|
+ if(fields_ptr)
|
|
|
+ pkg_free(fields_ptr);
|
|
|
+
|
|
|
+ if(table_ptr)
|
|
|
+ pkg_free(table_ptr);
|
|
|
+
|
|
|
+ dbt_clean_where(nkeys, _k, _op1, _kv);
|
|
|
+ dbt_clean_where(ncols, _c, _op2, _cv);
|
|
|
+
|
|
|
+ if(_f)
|
|
|
+ pkg_free(_f);
|
|
|
+ if(_v)
|
|
|
+ pkg_free(_v);
|
|
|
+
|
|
|
+ return res;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/*
|
|
|
* Raw SQL query -- is not the case to have this method
|
|
|
*/
|
|
|
int dbt_raw_query(db1_con_t* _h, str* _s, db1_res_t** _r)
|
|
|
{
|
|
|
*_r = NULL;
|
|
|
- int res = -1;
|
|
|
+ int res = -1;
|
|
|
|
|
|
if(!_h) {
|
|
|
LM_ERR("invalid connection\n");
|
|
@@ -203,19 +489,23 @@ int dbt_raw_query(db1_con_t* _h, str* _s, db1_res_t** _r)
|
|
|
|
|
|
if(_s->s == NULL) {
|
|
|
LM_ERR("sql query is null\n");
|
|
|
- return res;
|
|
|
+ return res;
|
|
|
}
|
|
|
|
|
|
- dbt_trim(_s->s);
|
|
|
- _s->len = strlen(_s->s);
|
|
|
-
|
|
|
- if(strncasecmp(_s->s, "select", 6) == 0) {
|
|
|
- return dbt_raw_query_select(_h, _s, _r);
|
|
|
- } else if(strncasecmp(_s->s, "update", 6) == 0) {
|
|
|
- return dbt_raw_query_update(_h, _s, _r);
|
|
|
- } else if(strncasecmp(_s->s, "delete", 6) == 0) {
|
|
|
- return dbt_raw_query_delete(_h, _s, _r);
|
|
|
- };
|
|
|
-
|
|
|
- return res;
|
|
|
+ dbt_trim(_s->s);
|
|
|
+ _s->len = strlen(_s->s);
|
|
|
+
|
|
|
+ if(strncasecmp(_s->s, "select", 6) == 0) {
|
|
|
+ return dbt_raw_query_select(_h, _s, _r);
|
|
|
+ } else if(strncasecmp(_s->s, "insert", 6) == 0) {
|
|
|
+ return dbt_raw_query_insert(_h, _s, _r);
|
|
|
+ } else if(strncasecmp(_s->s, "replace", 6) == 0) {
|
|
|
+ return dbt_raw_query_replace(_h, _s, _r);
|
|
|
+ } else if(strncasecmp(_s->s, "update", 6) == 0) {
|
|
|
+ return dbt_raw_query_update(_h, _s, _r);
|
|
|
+ } else if(strncasecmp(_s->s, "delete", 6) == 0) {
|
|
|
+ return dbt_raw_query_delete(_h, _s, _r);
|
|
|
+ };
|
|
|
+
|
|
|
+ return res;
|
|
|
}
|