2
0
Эх сурвалжийг харах

str structure is supported now.

Jan Janak 23 жил өмнө
parent
commit
225486fc8e
3 өөрчлөгдсөн 32 нэмэгдсэн , 12 устгасан
  1. 4 0
      db/db_val.h
  2. 10 0
      db/doc/db-api.txt
  3. 18 12
      db/example/dbexample.c

+ 4 - 0
db/db_val.h

@@ -6,6 +6,7 @@
 #define DB_VAL_H
 
 #include <time.h>
+#include "../str.h"
 
 
 /*
@@ -15,6 +16,7 @@ typedef enum {
 	DB_INT,
         DB_DOUBLE,
 	DB_STRING,
+	DB_STR,
 	DB_DATETIME
 } db_type_t;
 
@@ -30,6 +32,7 @@ typedef struct {
 		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 */
 	} val;                           /* union of all possible types */
 } db_val_t;
 
@@ -44,6 +47,7 @@ typedef struct {
 #define VAL_DOUBLE(dv) ((dv)->val.double_val)
 #define VAL_TIME(dv)   ((dv)->val.time_val)
 #define VAL_STRING(dv) ((dv)->val.string_val)
+#define VAL_STR(dv)    ((dv)->val.str_val)
 
 
 /*

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

@@ -79,6 +79,7 @@ information, see the next section.
        DB_INT,       /* Integer number */
        DB_DOUBLE,    /* Decimal number */
        DB_STRING,    /* String */
+       DB_STR,       /* str structure */
        DB_DATETIME   /* Date and time */
    } db_type_t;
 
@@ -97,6 +98,7 @@ recognized and converted by the database API:
 DB_INT      - Value in the database represents an integer number
 DB_DOUBLE   - Value in the database represents a decimal number
 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
 
 These datatypes are automaticaly recognized, converted from internal database
@@ -169,6 +171,14 @@ Example: if (VAL_TYPE(val) == DB_STRING) {
              printf("%s", VAL_STRING(val));
          }
 
+1.4.3.6 VAL_STR(value) Macro
+
+Use this macro if you need to access str structure in the db_val_t structure.
+
+Example: if (VAL_TYPE(val) == DB_STR) {
+             printf("%.*s", VAL_STR(val).len, VAL_STR(val).s);
+         }
+
 
 1.5 Type db_row_t
 

+ 18 - 12
db/example/dbexample.c

@@ -66,6 +66,12 @@ static int print_res(db_res_t* _r)
 				break;
 			case DB_STRING:
 				printf("%s ", RES_ROWS(_r)[i].values[j].val.string_val);
+				break;
+			case DB_STR:
+				printf("%.*s ", 
+				       RES_ROWS(_r)[i].values[j].val.str_val.len,
+				       RES_ROWS(_r)[i].values[j].val.str_val.s);
+				break;
 			}
 			
 		}
@@ -90,24 +96,24 @@ struct module_exports* mod_register()
 	db_key_t keys4[] = {"contact", "q"};
 
 	db_val_t vals1[] = { 
-		{ DB_STRING  , 0, { .string_val = "[email protected]" }      },
-		{ DB_STRING  , 0, { .string_val = "[email protected]" } },
-		{ DB_DOUBLE  , 0, { .double_val = 1.2 }                 },
-		{ DB_DATETIME, 0, { .time_val = 439826493 }            }
+		{ DB_STRING  , 0, { .string_val = "[email protected]" }              },
+		{ DB_STR     , 0, { .str_val    = { "[email protected]", 18 } } },
+		{ DB_DOUBLE  , 0, { .double_val = 1.2 }                        },
+		{ DB_DATETIME, 0, { .time_val   = 439826493 }                  }
 	};
 
 	db_val_t vals2[] = { 
-		{ DB_STRING  , 0, { .string_val = "[email protected]" }      },
-		{ DB_STRING  , 0, { .string_val = "[email protected]" } },
-		{ DB_DOUBLE  , 0, { .double_val = 1.3 }                   },
-		{ DB_DATETIME, 0, { .time_val = 12345 }                  }
+		{ DB_STRING  , 0, { .string_val = "[email protected]" }              },
+		{ DB_STR     , 0, { .str_val    = { "[email protected]", 18 } } },
+		{ DB_DOUBLE  , 0, { .double_val = 1.3 }                          },
+		{ DB_DATETIME, 0, { .time_val   = 12345 }                        }
 	};
 
 	db_val_t vals3[] = { 
-		{ DB_STRING  , 0, { .string_val = "[email protected]" }      },
-		{ DB_STRING  , 0, { .string_val = "[email protected]" } },
-		{ DB_DOUBLE  , 0, { .double_val = 1.5 }                  },
-		{ DB_DATETIME, 0, { .time_val = 123456 }                 }
+		{ DB_STRING  , 0, { .string_val = "[email protected]" }              },
+		{ DB_STR     , 0, { .str_val    = { "[email protected]", 18 } } },
+		{ DB_DOUBLE  , 0, { .double_val = 1.5 }                          },
+		{ DB_DATETIME, 0, { .time_val   = 123456 }                       }
 	};
 
 	db_val_t vals4[] = {