Browse Source

Return values changed, blob support added

Jan Janak 23 năm trước cách đây
mục cha
commit
18029bcbcc
3 tập tin đã thay đổi với 32 bổ sung12 xóa
  1. 5 2
      db/db_val.h
  2. 12 1
      db/doc/db-api.txt
  3. 15 9
      db/example/dbexample.c

+ 5 - 2
db/db_val.h

@@ -17,7 +17,8 @@ typedef enum {
         DB_DOUBLE,
         DB_DOUBLE,
 	DB_STRING,
 	DB_STRING,
 	DB_STR,
 	DB_STR,
-	DB_DATETIME
+	DB_DATETIME,
+	DB_BLOB
 } db_type_t;
 } db_type_t;
 
 
 
 
@@ -33,6 +34,7 @@ typedef struct {
 		time_t       time_val;   /* unix time value */
 		time_t       time_val;   /* unix time value */
 		const char*  string_val; /* NULL terminated string */
 		const char*  string_val; /* NULL terminated string */
 		str          str_val;    /* str string value */
 		str          str_val;    /* str string value */
+		str          blob_val;   /* Blob data */
 	} val;                           /* union of all possible types */
 	} val;                           /* union of all possible types */
 } db_val_t;
 } db_val_t;
 
 
@@ -48,12 +50,13 @@ typedef struct {
 #define VAL_TIME(dv)   ((dv)->val.time_val)
 #define VAL_TIME(dv)   ((dv)->val.time_val)
 #define VAL_STRING(dv) ((dv)->val.string_val)
 #define VAL_STRING(dv) ((dv)->val.string_val)
 #define VAL_STR(dv)    ((dv)->val.str_val)
 #define VAL_STR(dv)    ((dv)->val.str_val)
+#define VAL_BLOB(dv)   ((dv)->val.blob_val)
 
 
 
 
 /*
 /*
  * Convert string to given type
  * Convert string to given type
  */
  */
-int str2val(db_type_t _t, db_val_t* _v, const char* _s);
+int str2val(db_type_t _t, db_val_t* _v, const char* _s, int _l);
 
 
 
 
 /*
 /*

+ 12 - 1
db/doc/db-api.txt

@@ -81,6 +81,7 @@ information, see the next section.
        DB_STRING,    /* String */
        DB_STRING,    /* String */
        DB_STR,       /* str structure */
        DB_STR,       /* str structure */
        DB_DATETIME   /* Date and time */
        DB_DATETIME   /* Date and time */
+       DB_BLOB       /* Binary large object */
    } db_type_t;
    } db_type_t;
 
 
 1.3.3 Macros
 1.3.3 Macros
@@ -100,6 +101,7 @@ DB_DOUBLE   - Value in the database represents a decimal number
 DB_STRING   - Value in the database represents a string
 DB_STRING   - Value in the database represents a string
 DB_STR      - Value in the database represents a string
 DB_STR      - Value in the database represents a string
 DB_DATETIME - Value in the database represents date and time
 DB_DATETIME - Value in the database represents date and time
+DB_BLOB     - Value in the database represents binary large object
 
 
 These datatypes are automaticaly recognized, converted from internal database
 These datatypes are automaticaly recognized, converted from internal database
 representation and stored in the variable of corresponding type.
 representation and stored in the variable of corresponding type.
@@ -114,6 +116,8 @@ representation and stored in the variable of corresponding type.
              double double_val;       /* Double value */
              double double_val;       /* Double value */
              time_t time_val;         /* Unix time_t value */
              time_t time_val;         /* Unix time_t value */
              const char* string_val;  /* Zero terminated string */
              const char* string_val;  /* Zero terminated string */
+	     str str_val;             /* str structure */
+             str blob_val;            /* Structure describing blob */
          } val;
          } val;
     } db_val_t;
     } db_val_t;
 
 
@@ -171,7 +175,7 @@ Example: if (VAL_TYPE(val) == DB_STRING) {
              printf("%s", VAL_STRING(val));
              printf("%s", VAL_STRING(val));
          }
          }
 
 
-1.4.3.6 VAL_STR(value) Macro
+1.4.3.7 VAL_STR(value) Macro
 
 
 Use this macro if you need to access str structure in the db_val_t structure.
 Use this macro if you need to access str structure in the db_val_t structure.
 
 
@@ -179,6 +183,13 @@ Example: if (VAL_TYPE(val) == DB_STR) {
              printf("%.*s", VAL_STR(val).len, VAL_STR(val).s);
              printf("%.*s", VAL_STR(val).len, VAL_STR(val).s);
          }
          }
 
 
+1.4.3.8 VAL_BLOB(value) Macro
+
+Use this macro if you need to access blob value in the db_val_t structure.
+
+Example: if (VAL_TYPE(val) == DB_BLOB) {
+	     printf("%.*s", VAL_BLOB(val).len, VAL_BLOB(val).s);
+         }
 
 
 1.5 Type db_row_t
 1.5 Type db_row_t
 
 

+ 15 - 9
db/example/dbexample.c

@@ -72,6 +72,12 @@ static int print_res(db_res_t* _r)
 				       RES_ROWS(_r)[i].values[j].val.str_val.len,
 				       RES_ROWS(_r)[i].values[j].val.str_val.len,
 				       RES_ROWS(_r)[i].values[j].val.str_val.s);
 				       RES_ROWS(_r)[i].values[j].val.str_val.s);
 				break;
 				break;
+
+			case DB_BLOB:
+				printf("%.*s ",
+				       RES_ROWS(_r)[i].values[j].val.blob_val.len,
+				       RES_ROWS(_r)[i].values[j].val.blob_val.s);
+				break;
 			}
 			}
 			
 			
 		}
 		}
@@ -162,7 +168,7 @@ struct module_exports* mod_register()
 	      * Specify a table name, that will
 	      * Specify a table name, that will
 	      * be used for manipulations
 	      * be used for manipulations
 	      */
 	      */
-	if (db_use_table(h, DB_TABLE) == FALSE) {
+	if (db_use_table(h, DB_TABLE) < 0) {
 		fprintf(stderr, "Error while calling db_use_table\n");
 		fprintf(stderr, "Error while calling db_use_table\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}
@@ -170,22 +176,22 @@ struct module_exports* mod_register()
 	     /* If you do not specify any keys and values to be
 	     /* If you do not specify any keys and values to be
 	      * matched, all rows will be deleted
 	      * matched, all rows will be deleted
 	      */
 	      */
-	if (db_delete(h, NULL, NULL, 0) == FALSE) {
+	if (db_delete(h, NULL, NULL, 0) < 0) {
 		fprintf(stderr, "Error while flushing table\n");
 		fprintf(stderr, "Error while flushing table\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}
 
 
-	if (db_insert(h, keys1, vals1, 4) == FALSE) {
+	if (db_insert(h, keys1, vals1, 4) < 0) {
 		fprintf(stderr, "Error while inserting line 1\n");
 		fprintf(stderr, "Error while inserting line 1\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}
 
 
-	if (db_insert(h, keys1, vals2, 4) == FALSE) {
+	if (db_insert(h, keys1, vals2, 4) < 0) {
 		fprintf(stderr, "Error while inserting line 2\n");
 		fprintf(stderr, "Error while inserting line 2\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}
 
 
-	if (db_insert(h, keys1, vals3, 4) == FALSE) {
+	if (db_insert(h, keys1, vals3, 4) < 0) {
 		fprintf(stderr, "Error while inserting line 3\n");
 		fprintf(stderr, "Error while inserting line 3\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}
@@ -194,7 +200,7 @@ struct module_exports* mod_register()
 	      * Let's delete middle line with
 	      * Let's delete middle line with
 	      * user = [email protected] and q = 1.3
 	      * user = [email protected] and q = 1.3
 	      */
 	      */
-	if (db_delete(h, keys2, vals4, 2) == FALSE) {
+	if (db_delete(h, keys2, vals4, 2) < 0) {
 		fprintf(stderr, "Error while deleting line\n");
 		fprintf(stderr, "Error while deleting line\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}
@@ -202,7 +208,7 @@ struct module_exports* mod_register()
 	     /*
 	     /*
 	      * Modify last line
 	      * Modify last line
 	      */
 	      */
-	if (db_update(h, keys3, vals5, keys4, vals6, 2, 2) == FALSE) {
+	if (db_update(h, keys3, vals5, keys4, vals6, 2, 2) < 0) {
 		fprintf(stderr, "Error while modifying table\n");
 		fprintf(stderr, "Error while modifying table\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}
@@ -211,7 +217,7 @@ struct module_exports* mod_register()
 	      * Last but not least, dump the result of db_query
 	      * Last but not least, dump the result of db_query
 	      */
 	      */
 
 
-	if (db_query(h, NULL, NULL, NULL, 0, 0, NULL, &res) == FALSE) {
+	if (db_query(h, NULL, NULL, NULL, 0, 0, NULL, &res) < 0) {
 		fprintf(stderr, "Error while querying table\n");
 		fprintf(stderr, "Error while querying table\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}
@@ -223,7 +229,7 @@ struct module_exports* mod_register()
 	      * Free the result because we don't need it
 	      * Free the result because we don't need it
 	      * anymore
 	      * anymore
 	      */
 	      */
-	if (db_free_query(h, res) == FALSE) {
+	if (db_free_query(h, res) < 0) {
 		fprintf(stderr, "Error while freeing result of query\n");
 		fprintf(stderr, "Error while freeing result of query\n");
 		return &dbex_exports;
 		return &dbex_exports;
 	}
 	}