Code

data: Added basic support for arrays.
[sysdb.git] / src / include / core / data.h
index f1c0324a1e6b7cd9d736255ec25917b3bfa2325f..cfdf48db8ea7d63fd1c2ef718901706bd10ca2e0 100644 (file)
@@ -41,12 +41,16 @@ extern "C" {
 #endif
 
 enum {
-       SDB_TYPE_INTEGER = 1,
+       SDB_TYPE_NULL = 0,
+       SDB_TYPE_INTEGER,
        SDB_TYPE_DECIMAL,
        SDB_TYPE_STRING,
        SDB_TYPE_DATETIME,
        SDB_TYPE_BINARY,
        SDB_TYPE_REGEX, /* extended, case-insensitive POSIX regex */
+
+       /* flags: */
+       SDB_TYPE_ARRAY = 1 << 8,
 };
 
 #define SDB_TYPE_TO_STRING(t) \
@@ -57,28 +61,40 @@ enum {
                : ((t) == SDB_TYPE_BINARY) ? "BINARY" \
                : ((t) == SDB_TYPE_REGEX) ? "REGEX" : "UNKNOWN")
 
+union sdb_datum;
+typedef union sdb_datum sdb_datum_t;
+
+union sdb_datum {
+       int64_t     integer;  /* SDB_TYPE_INTEGER */
+       double      decimal;  /* SDB_TYPE_DECIMAL */
+       char       *string;   /* SDB_TYPE_STRING  */
+       sdb_time_t  datetime; /* SDB_TYPE_DATETIME */
+       struct {
+               size_t length;
+               unsigned char *datum;
+       } binary;             /* SDB_TYPE_BINARY */
+       struct {
+               char *raw;
+               regex_t regex;
+       } re;                 /* SDB_TYPE_REGEX */
+
+       struct {
+               size_t length;
+               void *values;
+       } array;
+};
+
 /*
  * sdb_data_t:
- * A datum retrieved from an arbitrary data source.
+ * An arbitrary value of a specified type.
  */
 typedef struct {
-       int type;
-       union {
-               int64_t     integer;  /* SDB_TYPE_INTEGER */
-               double      decimal;  /* SDB_TYPE_DECIMAL */
-               char       *string;   /* SDB_TYPE_STRING  */
-               sdb_time_t  datetime; /* SDB_TYPE_DATETIME */
-               struct {
-                       size_t length;
-                       unsigned char *datum;
-               } binary;             /* SDB_TYPE_BINARY */
-               struct {
-                       char *raw;
-                       regex_t regex;
-               } re;                 /* SDB_TYPE_REGEX */
-       } data;
+       int type;  /* type of the datum */
+       sdb_datum_t data;
 } sdb_data_t;
-#define SDB_DATA_INIT { 0, { .integer = 0 } }
+#define SDB_DATA_INIT { SDB_TYPE_NULL, { .integer = 0 } }
+
+extern const sdb_data_t SDB_DATA_NULL;
 
 /*
  * sdb_data_copy:
@@ -135,7 +151,8 @@ sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2);
 /*
  * sdb_data_isnull:
  * Determine whether a datum is NULL. A datum is considered to be NULL if
- * either datum is NULL or if the string or binary datum is NULL.
+ * either datum is NULL or if the type is SDB_TYPE_NULL or if the string or
+ * binary datum is NULL.
  */
 _Bool
 sdb_data_isnull(const sdb_data_t *datum);
@@ -179,6 +196,8 @@ sdb_data_parse_op(const char *op);
  * support the other operators. The result may be allocated dynamically and
  * has to be freed by the caller (using sdb_data_free_datum).
  *
+ * If any of the data points is a NULL value, the result is also NULL.
+ *
  * The data-types of d1 and d2 have to be the same, except for the following
  * cases:
  *  - <integer> or <decimal> <mul> <datetime>
@@ -239,6 +258,11 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted);
  * memory and also compiled to a regex. Use sdb_data_free_datum() to free the
  * dynamically allocated memory.
  *
+ * The input string may be stored in 'data', that is, the function may be used
+ * to do an inline cast from a string to any other type. It is the callers
+ * responsibility to free the memory used by the string in case the target
+ * type does not keep a reference to it.
+ *
  * Returns:
  *  - 0 on success
  *  - a negative value else
@@ -246,6 +270,17 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted);
 int
 sdb_data_parse(char *str, int type, sdb_data_t *data);
 
+/*
+ * sdb_data_sizeof:
+ * Return the size of the data-type identified by the specified type.
+ *
+ * Returns:
+ *  - the size of the data-type on success
+ *  - 0 else
+ */
+size_t
+sdb_data_sizeof(int type);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif