Bläddra i källkod

- Lookup db_fld function in drivers just once and then reuse the pointer
- The number of result fields and parameters added to db_cmd structure
- an array of strings that can map field types to field names added
- str element of the union in db_fld renamed to lstr, g++ for some
reason refuses to compile it
- number of fields in the result from db_cmd is now copied over to db_res

Jan Janak 18 år sedan
förälder
incheckning
a67659860d
7 ändrade filer med 42 tillägg och 14 borttagningar
  1. 13 5
      db/db_cmd.c
  2. 4 2
      db/db_cmd.h
  3. 14 0
      db/db_fld.c
  4. 3 1
      db/db_fld.h
  5. 3 3
      db/db_gen.h
  6. 1 0
      db/db_res.c
  7. 4 3
      db/db_res.h

+ 13 - 5
db/db_cmd.c

@@ -44,7 +44,8 @@ db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table,
     db_cmd_t* newp;
 	db_con_t* con;
 	int i, r, j;
-	
+	db_drv_func_t func;
+
     newp = (db_cmd_t*)pkg_malloc(sizeof(db_cmd_t));
     if (newp == NULL) goto err;
     memset(newp, '\0', sizeof(db_cmd_t));
@@ -52,9 +53,9 @@ db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table,
     newp->ctx = ctx;
 
 	newp->table.len = strlen(table);
-    newp->table.s = (char*)pkg_malloc(newp->table.len);
+    newp->table.s = (char*)pkg_malloc(newp->table.len + 1);
     if (newp->table.s == NULL) goto err;
-    memcpy(newp->table.s, table, newp->table.len);
+    memcpy(newp->table.s, table, newp->table.len + 1);
 
 	newp->type = type;
 
@@ -71,16 +72,23 @@ db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table,
 	for(i = 0; i < ctx->con_n; i++) {
 		con = ctx->con[i];
 
+		r = db_drv_func(&func, &con->uri->scheme, "db_fld");
+		if (r < 0) goto err;
+		if (r > 0) func = NULL;
+		db_payload_idx = i;
+
 		if (!DB_FLD_EMPTY(newp->result)) {
 			for(j = 0; !DB_FLD_LAST(newp->result[j]); j++) {
-				if (db_drv_call(&con->uri->scheme, "db_fld", newp->result + j, i) < 0) goto err;
+				if (func && func(newp->result + j, table) < 0) goto err;
 			}
+			newp->res_fields = j;
 		}
 
 		if (!DB_FLD_EMPTY(newp->params)) {
 			for(j = 0; !DB_FLD_LAST(newp->params[j]); j++) {
-				if (db_drv_call(&con->uri->scheme, "db_fld", newp->params + j, i) < 0) goto err;
+				if (func && func(newp->params + j, table) < 0) goto err;
 			}
+			newp->param_fields = j;
 		}
 
 		r = db_drv_call(&con->uri->scheme, "db_cmd", newp, i);

+ 4 - 2
db/db_cmd.h

@@ -72,8 +72,10 @@ typedef struct db_cmd {
 	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 */
-	db_fld_t* params;     /* Query parameters */
+	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_cmd_t;
 
 

+ 14 - 0
db/db_fld.c

@@ -36,6 +36,20 @@
 #include "db_fld.h"
 
 
+char* db_fld_str[] = {
+	"DB_NONE",
+	"DB_INT",
+	"DB_FLOAT",
+	"DB_DOUBLE",
+	"DB_CSTR",
+	"DB_STR",
+	"DB_DATETIME",
+	"DB_BLOB",
+	"DB_BITMAP"
+};
+
+
+
 int db_fld_init(db_fld_t* fld)
 {
 	int i;

+ 3 - 1
db/db_fld.h

@@ -54,6 +54,8 @@ enum db_fld_type {
     DB_BITMAP      /* Bitmap of flags */
 };
 
+extern char* db_fld_str[];
+
 enum db_fld_op {
 	DB_EQ = 0, /* The value of the field must be equal */
 	DB_LT,     /* The value of the field must be less than */
@@ -77,7 +79,7 @@ typedef struct db_fld {
 		double       dbl;    /* double value */
 		time_t       time;   /* unix time value */
 		char*  cstr;         /* NULL terminated string */
-		str          str;    /* str string value */
+                str          lstr;   /* String with known length */
 		str          blob;   /* Blob data */
 		unsigned int bitmap; /* Bitmap data type, 32 flags, should be enough */ 
 		long long    int8;   /* 8-byte integer */

+ 3 - 3
db/db_gen.h

@@ -41,7 +41,6 @@
 extern "C" {
 #endif /* __cplusplus */
 
-
 /*
  * Declare a list of DB API structures with given structure
  * name
@@ -150,7 +149,8 @@ struct db_drv;
  * all DB API structures share some common variables.
  */
 typedef struct db_gen {
-    DBLIST_ENTRY; /* All DB API structures can be arranged into lists */
+        DBLIST_ENTRY;
+
 	/* Array of pointers to driver-specific data. The database API
 	 * supports access to multiple databases at the same time and each
 	 * database driver may want to append some data to generic DB structures,
@@ -179,7 +179,7 @@ extern int db_payload_idx;
  * DB API structure
  */
 #define DB_SET_PAYLOAD(db_struct, drv_data) do { \
-    ((struct db_gen*)(db_struct))->data[db_payload_idx] = (void*)(drv_data); \
+    ((struct db_gen*)(db_struct))->data[db_payload_idx] = (struct db_drv*)(drv_data); \
 } while(0)
 
 

+ 1 - 0
db/db_res.c

@@ -46,6 +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;
 
 	ret = db_drv_call(&cmd->ctx->con[db_payload_idx]->uri->scheme, 
 					  "db_res", newp, db_payload_idx);

+ 4 - 3
db/db_res.h

@@ -42,9 +42,10 @@ extern "C" {
 #endif /* __cplusplus */
 
 typedef struct db_res {
-	db_gen_t gen;       /* Generic part of the structure */
-	struct db_rec* cur_rec;  /* Currently active record in the result */
-    struct db_cmd* cmd; /* Command that produced the result */
+	db_gen_t gen;           /* Generic part of the structure */
+	unsigned int fields;    /* 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;
 
 struct db_res* db_res(struct db_cmd* cmd);