Browse Source

Preparation for implementation of UPDATE db statement.
First of all we need two sets of params in db_cmd.
First for match clause and the second as column-value pairs of changed columns.
Currently it uses three sets of parameters: result, match and vals.
- result - DB_GET output
- match - DB_GET, DB_DEL (and DB_UPD in the future)
- vals - DB_PUT (and DB_UPD)

Libor Chocholaty 18 years ago
parent
commit
9056cd458f
4 changed files with 51 additions and 25 deletions
  1. 33 13
      db/db_cmd.c
  2. 16 10
      db/db_cmd.h
  3. 1 1
      db/db_res.c
  4. 1 1
      db/db_res.h

+ 33 - 13
db/db_cmd.c

@@ -38,7 +38,7 @@
 
 
 db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table, 
-				 db_fld_t* result, db_fld_t* params)
+				 db_fld_t* result, db_fld_t* match, db_fld_t* values)
 {
 	char* fname;
     db_cmd_t* newp;
@@ -59,14 +59,21 @@ db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table,
 
 	newp->type = type;
 
+	/** FIXME: it is not clear now that this is necessary
+	 *         when we have match and value separate arrays */
 	if (result) {
 		newp->result = db_fld_copy(result);
 		if (newp->result == NULL) goto err;
 	}
 
-	if (params) {
-		newp->params = db_fld_copy(params);
-		if (newp->params == NULL) goto err;
+	if (match) {
+		newp->match = db_fld_copy(match);
+		if (newp->match == NULL) goto err;
+	}
+
+	if (values) {
+		newp->vals = db_fld_copy(values);
+		if (newp->vals == NULL) goto err;
 	}
 
 	for(i = 0; i < ctx->con_n; i++) {
@@ -81,18 +88,28 @@ db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table,
 			for(j = 0; !DB_FLD_LAST(newp->result[j]); j++) {
 				if (func && func(newp->result + j, table) < 0) goto err;
 			}
-			newp->res_fields = j;
+			newp->result_count = j;
+		}
+
+		if (!DB_FLD_EMPTY(newp->match)) {
+			for(j = 0; !DB_FLD_LAST(newp->match[j]); j++) {
+				if (func && func(newp->match + j, table) < 0) goto err;
+			}
+			newp->match_count = j;
 		}
 
-		if (!DB_FLD_EMPTY(newp->params)) {
-			for(j = 0; !DB_FLD_LAST(newp->params[j]); j++) {
-				if (func && func(newp->params + j, table) < 0) goto err;
+		if (!DB_FLD_EMPTY(newp->vals)) {
+			for(j = 0; !DB_FLD_LAST(newp->vals[j]); j++) {
+				if (func && func(newp->vals + j, table) < 0) goto err;
 			}
-			newp->param_fields = j;
+			newp->vals_count = j;
 		}
 
 		r = db_drv_call(&con->uri->scheme, "db_cmd", newp, i);
-		if (r < 0) goto err;
+		if (r < 0) {
+			ERR("db_drv_call(\"db_cmd\") failed\n");
+			goto err;
+		}
 		if (r > 0) {
 			ERR("DB driver %.*s does not implement mandatory db_cmd function\n",
 				con->uri->scheme.len, ZSW(con->uri->scheme.s));
@@ -106,6 +123,7 @@ db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table,
 			case DB_PUT: fname = "db_put"; break;
 			case DB_DEL: fname = "db_del"; break;
 			case DB_GET: fname = "db_get"; break;
+			case DB_UPD: fname = "db_upd"; break;
 			case DB_SQL: fname = "db_sql"; break;
 			default: ERR("db_cmd: Unsupported command type\n"); goto err;
 			}
@@ -143,8 +161,9 @@ db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table,
     ERR("db_cmd: Cannot create db_cmd structure\n");
     if (newp) {
 		db_gen_free(&newp->gen);
-		if (newp->result) db_fld_free(newp->result);
-		if (newp->params) db_fld_free(newp->params);
+		if (newp->result)  db_fld_free(newp->result);
+		if (newp->match)   db_fld_free(newp->match);
+		if (newp->vals)    db_fld_free(newp->vals);
 		if (newp->table.s) pkg_free(newp->table.s);
 		pkg_free(newp);
 	}
@@ -158,7 +177,8 @@ void db_cmd_free(db_cmd_t* cmd)
 	db_gen_free(&cmd->gen);
 
 	if (cmd->result) db_fld_free(cmd->result);
-	if (cmd->params) db_fld_free(cmd->params);
+	if (cmd->match)  db_fld_free(cmd->match);
+	if (cmd->vals)   db_fld_free(cmd->vals);
     if (cmd->table.s) pkg_free(cmd->table.s);
     pkg_free(cmd);
 }

+ 16 - 10
db/db_cmd.h

@@ -61,21 +61,27 @@ enum db_cmd_type {
 	DB_PUT,  /* Insert or update new record in database */
 	DB_DEL,  /* Delete all matching records from database */
 	DB_GET,  /* Get matching records from database */
+	DB_UPD,  /* Update matching records in database */
 	DB_SQL,  /* Raw SQL query */
 };
 
+/**
+ * Structure db_cmd describes command in DB-API
+ */
 typedef struct db_cmd {
-	db_gen_t gen;          /* Generic part of the structure, must be the 1st attribute */
-	enum db_cmd_type type; /* Type of the command to be executed */
-    struct db_ctx* ctx;   /* Context containing database connections to be used */
-    str table;            /* Name of the table to perform the command on */
-	db_exec_func_t exec[DB_PAYLOAD_MAX]; /* Array of exec functions provided by modules */
+	db_gen_t gen;          /**< Generic part of the structure, must be the 1st attribute */
+	enum db_cmd_type type; /**< Type of the command to be executed */
+    struct db_ctx* ctx;    /**< Context containing database connections to be used */
+    str table;             /**< Name of the table to perform the command on */
+	db_exec_func_t exec[DB_PAYLOAD_MAX]; /**< Array of exec functions provided by modules */
 	db_first_func_t first[DB_PAYLOAD_MAX];
 	db_next_func_t next[DB_PAYLOAD_MAX];
-	db_fld_t* result;        /* Fields to to be returned in result */
-	unsigned int res_fields; /* Number of fields in the result set */
-	db_fld_t* params;        /* Query parameters */
-	unsigned int param_fields; /* Number of fields in the parameter */
+	db_fld_t* result;       /**< Fields to to be returned in result */
+	db_fld_t* match;        /**< Fields describing WHERE clause */
+	db_fld_t* vals;         /**< Fields with values for UPDATE and INSERT statements */
+	unsigned int result_count; /* Number of fields in the result set */
+	unsigned int match_count;  /* Number of fields in the result set */
+	unsigned int vals_count;   /* Number of fields in the result set */
 } db_cmd_t;
 
 
@@ -85,7 +91,7 @@ typedef struct db_cmd {
 
 
 struct db_cmd* db_cmd(enum db_cmd_type type, struct db_ctx* ctx, char* table, 
-					  db_fld_t* result, db_fld_t* params);
+					  db_fld_t* result, db_fld_t* match, db_fld_t* value);
 void db_cmd_free(struct db_cmd* cmd);
 
 int db_exec(struct db_res** res, struct db_cmd* cmd);

+ 1 - 1
db/db_res.c

@@ -46,7 +46,7 @@ db_res_t* db_res(db_cmd_t* cmd)
 	memset(newp, '\0', sizeof(db_res_t));
 	if (db_gen_init(&newp->gen) < 0) goto err;
     newp->cmd = cmd;
-	newp->fields = cmd->res_fields;
+	newp->field_count = cmd->result_count;
 
 	ret = db_drv_call(&cmd->ctx->con[db_payload_idx]->uri->scheme, 
 					  "db_res", newp, db_payload_idx);

+ 1 - 1
db/db_res.h

@@ -43,7 +43,7 @@ extern "C" {
 
 typedef struct db_res {
 	db_gen_t gen;           /* Generic part of the structure */
-	unsigned int fields;    /* Number of fields in the result */
+	unsigned int field_count;    /* Number of fields in the result */
 	struct db_rec* cur_rec; /* Currently active record in the result */
     struct db_cmd* cmd;     /* Command that produced the result */
 } db_res_t;