Code

store: Added quaryable field ‘name’.
[sysdb.git] / src / include / core / data.h
index da653ce3f47e1f9692584b016da5e3d176ac4f80..87607f810d344fc87d7bbc5943d16caaaa7fe9d7 100644 (file)
@@ -33,6 +33,9 @@
 #include <inttypes.h>
 #include <stddef.h>
 
+#include <sys/types.h>
+#include <regex.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -43,20 +46,16 @@ enum {
        SDB_TYPE_STRING,
        SDB_TYPE_DATETIME,
        SDB_TYPE_BINARY,
+       SDB_TYPE_REGEX, /* extended, case-insensitive POSIX regex */
 };
 
 #define SDB_TYPE_TO_STRING(t) \
-       (((t) == SDB_TYPE_INTEGER) \
-               ? "INTEGER" \
-               : ((t) == SDB_TYPE_DECIMAL) \
-                       ? "DECIMAL" \
-                       : ((t) == SDB_TYPE_STRING) \
-                               ? "STRING" \
-                               : ((t) == SDB_TYPE_DATETIME) \
-                                       ? "DATETIME" \
-                                       : ((t) == SDB_TYPE_BINARY) \
-                                               ? "BINARY" \
-                                               : "UNKNOWN")
+       (((t) == SDB_TYPE_INTEGER) ? "INTEGER" \
+               : ((t) == SDB_TYPE_DECIMAL) ? "DECIMAL" \
+               : ((t) == SDB_TYPE_STRING) ? "STRING" \
+               : ((t) == SDB_TYPE_DATETIME) ? "DATETIME" \
+               : ((t) == SDB_TYPE_BINARY) ? "BINARY" \
+               : ((t) == SDB_TYPE_REGEX) ? "REGEX" : "UNKNOWN")
 
 /*
  * sdb_data_t:
@@ -73,8 +72,13 @@ typedef struct {
                        size_t length;
                        unsigned char *datum;
                } binary;             /* SDB_TYPE_BINARY */
+               struct {
+                       char *raw;
+                       regex_t regex;
+               } re;                 /* SDB_TYPE_REGEX */
        } data;
 } sdb_data_t;
+#define SDB_DATA_INIT { 0, { .integer = 0 } }
 
 /*
  * sdb_data_copy:
@@ -104,7 +108,7 @@ sdb_data_free_datum(sdb_data_t *datum);
 /*
  * sdb_data_cmp:
  * Compare two data points. A NULL datum is considered less than any non-NULL
- * datum. On data-type mismatch, the function always returns a negative value.
+ * datum. On data-type mismatch, the function always returns a non-zero value.
  *
  * Returns:
  *  - a value less than zero if d1 compares less than d2
@@ -114,6 +118,74 @@ sdb_data_free_datum(sdb_data_t *datum);
 int
 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2);
 
+/*
+ * sdb_data_strcmp:
+ * Compare the string values of two data points. A NULL datum is considered
+ * less than any non-NULL. This function works for arbitrary combination of
+ * data-types.
+ *
+ * Returns:
+ *  - a value less than zero if d1 compares less than d2
+ *  - zero if d1 compares equal to d2
+ *  - a value greater than zero if d1 compares greater than d2
+ */
+int
+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.
+ */
+_Bool
+sdb_data_isnull(const sdb_data_t *datum);
+
+/*
+ * Operators supported by sdb_data_eval_expr.
+ */
+enum {
+       SDB_DATA_ADD = 1, /* addition */
+       SDB_DATA_SUB,     /* substraction */
+       SDB_DATA_MUL,     /* multiplication */
+       SDB_DATA_DIV,     /* division */
+       SDB_DATA_MOD,     /* modulo */
+       SDB_DATA_CONCAT,  /* string / binary data concatenation */
+};
+
+#define SDB_DATA_OP_TO_STRING(op) \
+       (((op) == SDB_DATA_ADD) \
+               ? "+" \
+               : ((op) == SDB_DATA_SUB) \
+                       ? "-" \
+                       : ((op) == SDB_DATA_MUL) \
+                               ? "*" \
+                               : ((op) == SDB_DATA_DIV) \
+                                       ? "/" \
+                                       : ((op) == SDB_DATA_MOD) \
+                                               ? "%" \
+                                               : ((op) == SDB_DATA_CONCAT) \
+                                                       ? "||" : "UNKNOWN")
+
+/*
+ * sdb_data_expr_eval:
+ * Evaluate a simple arithmetic expression on two data points. String and
+ * binary data only support concatenation and all other data types only
+ * support the other operators. The result may be allocated dynamically and
+ * has to be freed by the caller (using sdb_data_free_datum).
+ *
+ * The data-types of d1 and d2 have to be the same, except for the following
+ * cases:
+ *  - <integer> or <decimal> <mul> <datetime>
+ *  - <datetime> <mul> or <div> or <mod> <integer> or <decimal>
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
+               sdb_data_t *res);
+
 /*
  * sdb_data_strlen:
  * Returns a (worst-case) estimate for the number of bytes required to format
@@ -146,6 +218,28 @@ enum {
 int
 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted);
 
+/*
+ * sdb_data_parse:
+ * Parse the specified string into a datum using the specified type. The
+ * string value is expected to be a raw value of the specified type. Integer
+ * and decimal numbers may be signed or unsigned octal (base 8, if the first
+ * character of the string is "0"), sedecimal (base 16, if the string includes
+ * the "0x" prefix), or decimal. Decimal numbers may also be "infinity" or
+ * "NaN" or may use a decimal exponent. Date-time values are expected to be
+ * specified as (floating point) number of seconds since the epoch. For string
+ * and binary data, the input string is passed to the datum. The function does
+ * not allocate new memory for that purpose. Use sdb_data_copy() if you want
+ * to do that. For regex data, the input string is copied to newly allocated
+ * memory and also compiled to a regex. Use sdb_data_free_datum() to free the
+ * dynamically allocated memory.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_data_parse(char *str, int type, sdb_data_t *data);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif