Code

store_expr: Added support to include field values in an expression.
[sysdb.git] / src / include / core / data.h
index bd102669ce2d2873a2c2f0ef3b7bb64278483d13..2e9b90bcfd55af660d3cb576b1c5e3e21427730e 100644 (file)
@@ -75,13 +75,15 @@ typedef struct {
                } binary;             /* SDB_TYPE_BINARY */
        } data;
 } sdb_data_t;
+#define SDB_DATA_INIT { 0, { .integer = 0 } }
 
 /*
  * sdb_data_copy:
  * Copy the datum stored in 'src' to the memory location pointed to by 'dst'.
  * Any dynamic data (strings, binary data) is copied to newly allocated
  * memory. Use, for example, sdb_data_free_datum() to free any dynamic memory
- * stored in a datum.
+ * stored in a datum. On error, 'dst' is unchanged. Else, any dynamic memory
+ * in 'dst' will be freed.
  *
  * Returns:
  *  - 0 on success
@@ -111,7 +113,47 @@ sdb_data_free_datum(sdb_data_t *datum);
  *  - a value greater than zero if d1 compares greater than d2
  */
 int
-sdb_data_cmp(sdb_data_t *d1, sdb_data_t *d2);
+sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2);
+
+/*
+ * 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. The data-type
+ * of d1 and d2 have to be the same. String and binary data only support
+ * concatenation and all other data types only support the other operators.
+ *
+ * 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:
@@ -120,7 +162,7 @@ sdb_data_cmp(sdb_data_t *d1, sdb_data_t *d2);
  * account.
  */
 size_t
-sdb_data_strlen(sdb_data_t *datum);
+sdb_data_strlen(const sdb_data_t *datum);
 
 enum {
        SDB_UNQUOTED = 0,
@@ -143,7 +185,27 @@ enum {
  *  - a negative value else
  */
 int
-sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen, int quoted);
+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.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_data_parse(char *str, int type, sdb_data_t *data);
 
 #ifdef __cplusplus
 } /* extern "C" */