Browse Source

lib/srdb1: mechanism to be able to quote table and column names

- new field in db1_con_t with the char for quoting
Daniel-Constantin Mierla 10 years ago
parent
commit
ffbe9c2c00
4 changed files with 35 additions and 20 deletions
  1. 5 0
      lib/srdb1/db_con.h
  2. 17 12
      lib/srdb1/db_query.c
  3. 11 7
      lib/srdb1/db_ut.c
  4. 2 1
      lib/srdb1/db_ut.h

+ 5 - 0
lib/srdb1/db_con.h

@@ -37,12 +37,17 @@
  */
 typedef struct {
 	const str* table;      /*!< Default table that should be used              */
+	const char *tquote;    /*!< Char to quote special tokens (table/column names) */
 	unsigned long tail;    /*!< Variable length tail, database module specific */
 } db1_con_t;
 
 
 /** Return the table of the connection handle */
 #define CON_TABLE(cn)      ((cn)->table)
+/** Return the tquote of the connection handle */
+#define CON_TQUOTE(cn)     ((cn)->tquote)
+/** Return the tquote of the connection handle or empty str if null */
+#define CON_TQUOTESZ(cn)   (((cn)->tquote)?((cn)->tquote):"")
 /** Return the tail of the connection handle */
 #define CON_TAIL(cn)       ((cn)->tail)
 

+ 17 - 12
lib/srdb1/db_query.c

@@ -78,7 +78,8 @@ static int db_do_query_internal(const db1_con_t* _h, const db_key_t* _k, const d
 	}
 
 	if (!_c) {
-		ret = snprintf(sql_buf, sql_buffer_size, "select * from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+		ret = snprintf(sql_buf, sql_buffer_size, "select * from %s%.*s%s ",
+				CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h));
 		if (ret < 0 || ret >= sql_buffer_size) goto error;
 		off = ret;
 	} else {
@@ -86,11 +87,12 @@ static int db_do_query_internal(const db1_con_t* _h, const db_key_t* _k, const d
 		if (ret < 0 || ret >= sql_buffer_size) goto error;
 		off = ret;
 
-		ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _c, _nc);
+		ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _c, _nc, CON_TQUOTESZ(_h));
 		if (ret < 0) return -1;
 		off += ret;
 
-		ret = snprintf(sql_buf + off, sql_buffer_size - off, "from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+		ret = snprintf(sql_buf + off, sql_buffer_size - off, "from %s%.*s%s ",
+				CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h));
 		if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
 		off += ret;
 	}
@@ -203,15 +205,15 @@ int db_do_insert_cmd(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v
 	}
 
 	if(mode==1)
-		ret = snprintf(sql_buf, sql_buffer_size, "insert delayed into %.*s (",
-				CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+		ret = snprintf(sql_buf, sql_buffer_size, "insert delayed into %s%.*s%s (",
+				CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h));
 	else
-		ret = snprintf(sql_buf, sql_buffer_size, "insert into %.*s (",
-				CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+		ret = snprintf(sql_buf, sql_buffer_size, "insert into %s%.*s%s (",
+				CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h));
 	if (ret < 0 || ret >= sql_buffer_size) goto error;
 	off = ret;
 
-	ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n);
+	ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n, CON_TQUOTESZ(_h));
 	if (ret < 0) return -1;
 	off += ret;
 
@@ -266,7 +268,8 @@ int db_do_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
 		return -1;
 	}
 
-	ret = snprintf(sql_buf, sql_buffer_size, "delete from %.*s", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+	ret = snprintf(sql_buf, sql_buffer_size, "delete from %s%.*s%s",
+			CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h));
 	if (ret < 0 || ret >= sql_buffer_size) goto error;
 	off = ret;
 
@@ -309,7 +312,8 @@ int db_do_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
 		return -1;
 	}
 
-	ret = snprintf(sql_buf, sql_buffer_size, "update %.*s set ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+	ret = snprintf(sql_buf, sql_buffer_size, "update %s%.*s%s set ",
+			CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h));
 	if (ret < 0 || ret >= sql_buffer_size) goto error;
 	off = ret;
 
@@ -354,11 +358,12 @@ int db_do_replace(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
 		return -1;
 	}
 
-	ret = snprintf(sql_buf, sql_buffer_size, "replace %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+	ret = snprintf(sql_buf, sql_buffer_size, "replace %s%.*s%s (",
+			CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h));
 	if (ret < 0 || ret >= sql_buffer_size) goto error;
 	off = ret;
 
-	ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n);
+	ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n, CON_TQUOTESZ(_h));
 	if (ret < 0) return -1;
 	off += ret;
 

+ 11 - 7
lib/srdb1/db_ut.c

@@ -270,7 +270,7 @@ inline int db_time2str(time_t _v, char* _s, int* _l)
 /*
  * Print list of columns separated by comma
  */
-inline int db_print_columns(char* _b, const int _l, const db_key_t* _c, const int _n)
+int db_print_columns(char* _b, const int _l, const db_key_t* _c, const int _n, const char *_tq)
 {
 	int i, ret, len = 0;
 
@@ -281,11 +281,11 @@ inline int db_print_columns(char* _b, const int _l, const db_key_t* _c, const in
 
 	for(i = 0; i < _n; i++)	{
 		if (i == (_n - 1)) {
-			ret = snprintf(_b + len, _l - len, "%.*s ", _c[i]->len, _c[i]->s);
+			ret = snprintf(_b + len, _l - len, "%s%.*s%s ", _tq, _c[i]->len, _c[i]->s, _tq);
 			if (ret < 0 || ret >= (_l - len)) goto error;
 			len += ret;
 		} else {
-			ret = snprintf(_b + len, _l - len, "%.*s,", _c[i]->len, _c[i]->s);
+			ret = snprintf(_b + len, _l - len, "%s%.*s%s,", _tq, _c[i]->len, _c[i]->s, _tq);
 			if (ret < 0 || ret >= (_l - len)) goto error;
 			len += ret;
 		}
@@ -350,16 +350,19 @@ int db_print_where(const db1_con_t* _c, char* _b, const int _l, const db_key_t*
 				LM_ERR("Error while converting value to string\n");
 				return -1;
 			}
-			ret = snprintf(_b + len, _l - len, "%.*s&%.*s=%.*s", _k[i]->len, _k[i]->s, tmp_len, tmp_buf, tmp_len, tmp_buf);
+			ret = snprintf(_b + len, _l - len, "%s%.*s%s&%.*s=%.*s", CON_TQUOTESZ(_c),
+					_k[i]->len, _k[i]->s, CON_TQUOTESZ(_c), tmp_len, tmp_buf, tmp_len, tmp_buf);
 			if (ret < 0 || ret >= (_l - len)) goto error;
 			len += ret;
 		} else {
 			if (_o) {
-				ret = snprintf(_b + len, _l - len, "%.*s%s", _k[i]->len, _k[i]->s, _o[i]);
+				ret = snprintf(_b + len, _l - len, "%s%.*s%s%s", CON_TQUOTESZ(_c),
+						_k[i]->len, _k[i]->s, CON_TQUOTESZ(_c), _o[i]);
 				if (ret < 0 || ret >= (_l - len)) goto error;
 				len += ret;
 			} else {
-				ret = snprintf(_b + len, _l - len, "%.*s=", _k[i]->len, _k[i]->s);
+				ret = snprintf(_b + len, _l - len, "%s%.*s%s=", CON_TQUOTESZ(_c),
+						_k[i]->len, _k[i]->s, CON_TQUOTESZ(_c));
 				if (ret < 0 || ret >= (_l - len)) goto error;
 				len += ret;
 			}
@@ -400,7 +403,8 @@ int db_print_set(const db1_con_t* _c, char* _b, const int _l, const db_key_t* _k
 	}
 
 	for(i = 0; i < _n; i++) {
-		ret = snprintf(_b + len, _l - len, "%.*s=", _k[i]->len, _k[i]->s);
+		ret = snprintf(_b + len, _l - len, "%s%.*s%s=",
+				CON_TQUOTESZ(_c), _k[i]->len, _k[i]->s, CON_TQUOTESZ(_c));
 		if (ret < 0 || ret >= (_l - len)) goto error;
 		len += ret;
 

+ 2 - 1
lib/srdb1/db_ut.h

@@ -131,9 +131,10 @@ int db_str2time(const char* _s, time_t* _v);
  * \param _l length of the target
  * \param _c keys that should be printed
  * \param _n number of keys
+ * \param _tq char to quote special tokens or empty string
  * \return the length of the printed result on success, negative on errors
  */
-int db_print_columns(char* _b, const int _l, const db_key_t* _c, const int _n);
+int db_print_columns(char* _b, const int _l, const db_key_t* _c, const int _n, const char *_tq);
 
 
 /**