Przeglądaj źródła

- DB_BITMAP data type added

Jan Janak 22 lat temu
rodzic
commit
8d63acb72a
3 zmienionych plików z 38 dodań i 16 usunięć
  1. 13 9
      db/db_val.h
  2. 11 0
      db/doc/db-api.txt
  3. 14 7
      db/example/dbexample.c

+ 13 - 9
db/db_val.h

@@ -42,7 +42,8 @@ typedef enum {
 	DB_STRING,
 	DB_STR,
 	DB_DATETIME,
-	DB_BLOB
+	DB_BLOB,
+	DB_BITMAP
 } db_type_t;
 
 
@@ -52,15 +53,17 @@ typedef enum {
 typedef struct {
 	db_type_t type;                /* Type of the value */
 	int nul;                       /* Means that the column in database
-									  has no value */
+					* has no value 
+					*/
 	union {
-		int          int_val;    /* integer value */
-		double       double_val; /* double value */
-		time_t       time_val;   /* unix time value */
-		const char*  string_val; /* NULL terminated string */
-		str          str_val;    /* str string value */
-		str          blob_val;   /* Blob data */
-	} val;                       /* union of all possible types */
+		int           int_val;    /* integer value */
+		double        double_val; /* double value */
+		time_t        time_val;   /* unix time value */
+		const char*   string_val; /* NULL terminated string */
+		str           str_val;    /* str string value */
+		str           blob_val;   /* Blob data */
+		unsigned int  bitmap_val; /* Bitmap data type, 32 flags, should be enough */ 
+	} val;                            /* union of all possible types */
 } db_val_t;
 
 
@@ -76,6 +79,7 @@ typedef struct {
 #define VAL_STRING(dv) ((dv)->val.string_val)
 #define VAL_STR(dv)    ((dv)->val.str_val)
 #define VAL_BLOB(dv)   ((dv)->val.blob_val)
+#define VAL_BITMAP(dv) ((dv)->val.bitmap_val)
 
 
 /*

+ 11 - 0
db/doc/db-api.txt

@@ -82,6 +82,7 @@ information, see the next section.
        DB_STR,       /* str structure */
        DB_DATETIME   /* Date and time */
        DB_BLOB       /* Binary large object */
+       DB_BITMAP     /* Bitmap, one-dimensional array of flags */
    } db_type_t;
 
 1.3.3 Macros
@@ -102,6 +103,7 @@ DB_STRING   - 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_BLOB     - Value in the database represents binary large object
+DB_BITMAP   - Value in the database represents an array of flags
 
 These datatypes are automaticaly recognized, converted from internal database
 representation and stored in the variable of corresponding type.
@@ -118,6 +120,7 @@ representation and stored in the variable of corresponding type.
              const char* string_val;  /* Zero terminated string */
 	     str str_val;             /* str structure */
              str blob_val;            /* Structure describing blob */
+             unsigned int bitmap_val; /* Array of flags */
          } val;
     } db_val_t;
 
@@ -191,6 +194,14 @@ Example: if (VAL_TYPE(val) == DB_BLOB) {
 	     printf("%.*s", VAL_BLOB(val).len, VAL_BLOB(val).s);
          }
 
+1.4.3.9 VAL_BITMAP(value) Macro
+
+Use this macro if you need to access bitmap value in the db_val_t structure.
+
+Example: if (VAL_TYPE(val) == DB_BITMAP) {
+	    printf("%d", VAL_BITMAP(val));
+	 }
+
 1.5 Type db_row_t
 
 1.5.1 Description

+ 14 - 7
db/example/dbexample.c

@@ -102,6 +102,10 @@ static int print_res(db_res_t* _r)
 				       RES_ROWS(_r)[i].values[j].val.blob_val.len,
 				       RES_ROWS(_r)[i].values[j].val.blob_val.s);
 				break;
+
+			case DB_BITMAP:
+				printf("%d ", RES_ROWS(_r)[i].values[j].val.bitmap_val);
+				break;
 			}
 			
 		}
@@ -120,7 +124,7 @@ struct module_exports* mod_register()
 	     /*
 	      * Column names of table location
 	      */
-	db_key_t keys1[] = {"username", "contact", "q", "expire", "opaque" };
+	db_key_t keys1[] = {"username", "contact", "q", "expire", "opaque", "flags" };
 	db_key_t keys2[] = {"username", "q"};
 	db_key_t keys3[] = {"username", "contact"};
 	db_key_t keys4[] = {"contact", "q"};
@@ -130,7 +134,8 @@ struct module_exports* mod_register()
 		{ DB_STR     , 0, { .str_val    = { "[email protected]", 18 } } },
 		{ DB_DOUBLE  , 0, { .double_val = 1.2 }                        },
 		{ DB_DATETIME, 0, { .time_val   = 439826493 }                  },
-		{ DB_BLOB    , 0, { .blob_val   = { "hdslgkhas\0glksf", 17 } } }
+		{ DB_BLOB    , 0, { .blob_val   = { "hdslgkhas\0glksf", 17 } } },
+		{ DB_BITMAP  , 0, { .bitmap_val = FLAG_NAT | FLAG_INVITE }     }
 	};
 
 	db_val_t vals2[] = { 
@@ -138,7 +143,8 @@ struct module_exports* mod_register()
 		{ DB_STR     , 0, { .str_val    = { "[email protected]", 18 } } },
 		{ DB_DOUBLE  , 0, { .double_val = 1.3 }                          },
 		{ DB_DATETIME, 0, { .time_val   = 12345 }                        },
-		{ DB_BLOB    , 0, { .blob_val   = { "\0a\0balkdfj", 10 }       } }
+		{ DB_BLOB    , 0, { .blob_val   = { "\0a\0balkdfj", 10 }       } },
+                { DB_BITMAP  , 0, { .bitmap_val = FLAG_NAT, FLAG_NOT_INVITE }    }
 	};
 
 	db_val_t vals3[] = { 
@@ -146,7 +152,8 @@ struct module_exports* mod_register()
 		{ DB_STR     , 0, { .str_val    = { "[email protected]", 18 } } },
 		{ DB_DOUBLE  , 0, { .double_val = 1.5 }                          },
 		{ DB_DATETIME, 0, { .time_val   = 123456 }                       },
-		{ DB_BLOB    , 0, { .blob_val   = { "halgkasdg\'", 10 }        } }
+		{ DB_BLOB    , 0, { .blob_val   = { "halgkasdg\'", 10 }        } },
+                { DB_BITMAP  , 0, { .blob_val   = FLAG_NAT }                     }
 	};
 
 	db_val_t vals4[] = {
@@ -208,17 +215,17 @@ struct module_exports* mod_register()
 		return &dbex_exports;
 	}
 
-	if (db_insert(h, keys1, vals1, 5) < 0) {
+	if (db_insert(h, keys1, vals1, 6) < 0) {
 		fprintf(stderr, "Error while inserting line 1\n");
 		return &dbex_exports;
 	}
 
-	if (db_insert(h, keys1, vals2, 5) < 0) {
+	if (db_insert(h, keys1, vals2, 6) < 0) {
 		fprintf(stderr, "Error while inserting line 2\n");
 		return &dbex_exports;
 	}
 
-	if (db_insert(h, keys1, vals3, 5) < 0) {
+	if (db_insert(h, keys1, vals3, 6) < 0) {
 		fprintf(stderr, "Error while inserting line 3\n");
 		return &dbex_exports;
 	}