Code

data: Added sdb_data_expr_type().
[sysdb.git] / src / core / data.c
1 /*
2  * SysDB - src/core/data.c
3  * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
34 #include "core/data.h"
35 #include "utils/error.h"
37 #include <assert.h>
39 #include <errno.h>
41 #include <inttypes.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include <math.h>
49 /*
50  * Operator support maxtrix.
51  * <type1> <op> <type2> -> op_matrix[<op>][<type1>][<type2>]
52  */
54 /* add, sub, mul, div, mod, concat */
56 /* integer, decimal, string, datetime, binary, regex */
58 static int op_matrix[6][6][6] = {
59         /* SDB_DATA_ADD */
60         {
61                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
62                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
63                 { -1, -1, -1, -1, -1, -1 },
64                 { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
65                 { -1, -1, -1, -1, -1, -1 },
66                 { -1, -1, -1, -1, -1, -1 },
67         },
69         /* SDB_DATA_SUB */
70         {
71                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
72                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
73                 { -1, -1, -1, -1, -1, -1 },
74                 { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
75                 { -1, -1, -1, -1, -1, -1 },
76                 { -1, -1, -1, -1, -1, -1 },
77         },
79         /* SDB_DATA_MUL */
80         {
81                 { SDB_TYPE_INTEGER, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
82                 { -1, SDB_TYPE_DECIMAL, -1, SDB_TYPE_DATETIME, -1, -1 },
83                 { -1, -1, -1, -1, -1, -1 },
84                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
85                 { -1, -1, -1, -1, -1, -1 },
86                 { -1, -1, -1, -1, -1, -1 },
87         },
89         /* SDB_DATA_DIV */
90         {
91                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
92                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
93                 { -1, -1, -1, -1, -1, -1 },
94                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
95                 { -1, -1, -1, -1, -1, -1 },
96                 { -1, -1, -1, -1, -1, -1 },
97         },
99         /* SDB_DATA_MOD */
100         {
101                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
102                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
103                 { -1, -1, -1, -1, -1, -1 },
104                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
105                 { -1, -1, -1, -1, -1, -1 },
106                 { -1, -1, -1, -1, -1, -1 },
107         },
109         /* SDB_DATA_CONCAT */
110         {
111                 { -1, -1, -1, -1, -1, -1 },
112                 { -1, -1, -1, -1, -1, -1 },
113                 { -1, -1, SDB_TYPE_STRING, -1, -1, -1 },
114                 { -1, -1, -1, -1, -1, -1 },
115                 { -1, -1, -1, -1, SDB_TYPE_BINARY, -1 },
116                 { -1, -1, -1, -1, -1, -1 },
117         },
118 };
120 /*
121  * private helper functions
122  */
124 /* this function supports in-place copies */
125 static int
126 copy_array_values(sdb_data_t *dst, const sdb_data_t *src, size_t elem_size)
128         int type = src->type & 0xff;
130         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)) {
131                 if (dst != src)
132                         memcpy(dst->data.array.values, src->data.array.values,
133                                         src->data.array.length * elem_size);
134         }
135         else if (type == SDB_TYPE_STRING) {
136                 char **s = src->data.array.values;
137                 char **d = dst->data.array.values;
138                 size_t i;
140                 for (i = 0; i < src->data.array.length; ++i) {
141                         d[i] = strdup(s[i]);
142                         if (! d[i])
143                                 return -1;
144                 }
145         }
146         else {
147                 /* TODO */
148                 errno = ENOTSUP;
149                 return -1;
150         }
151         return 0;
152 } /* copy_array_values */
154 static void
155 free_array_values(sdb_data_t *datum)
157         int type = datum->type & 0xff;
159         if (type == SDB_TYPE_STRING) {
160                 char **v = datum->data.array.values;
161                 size_t i;
163                 for (i = 0; i < datum->data.array.length; ++i) {
164                         if (v[i])
165                                 free(v[i]);
166                         v[i] = NULL;
167                 }
168         }
169 } /* free_array_values */
171 /* compare two arrays element-by-element returning how the first non-equal
172  * elements compare to each other */
173 static int
174 array_cmp(const sdb_data_t *a1, const sdb_data_t *a2)
176         int type = a1->type & 0xff;
177         size_t len, i;
179         assert((a1->type == a2->type) && (a1->type & SDB_TYPE_ARRAY));
181         len = SDB_MIN(a1->data.array.length, a2->data.array.length);
183         if (type == SDB_TYPE_INTEGER) {
184                 int64_t *v1 = a1->data.array.values;
185                 int64_t *v2 = a2->data.array.values;
187                 for (i = 0; i < len; ++i)
188                         if (v1[i] != v2[i])
189                                 return SDB_CMP(v1[i], v2[i]);
190         }
191         else if (type == SDB_TYPE_DECIMAL) {
192                 double *v1 = a1->data.array.values;
193                 double *v2 = a2->data.array.values;
195                 for (i = 0; i < len; ++i)
196                         if (v1[i] != v2[i])
197                                 return SDB_CMP(v1[i], v2[i]);
198         }
199         else if (type == SDB_TYPE_STRING) {
200                 char **v1 = a1->data.array.values;
201                 char **v2 = a2->data.array.values;
203                 for (i = 0; i < len; ++i) {
204                         int diff = strcasecmp(v1[i], v2[i]);
205                         if (diff)
206                                 return diff;
207                 }
208         }
209         else {
210                 /* TODO */
211                 errno = ENOTSUP;
212                 /* but fall through to ensure stable sorting: */
213         }
214         return SDB_CMP(a1->data.array.length, a2->data.array.length);
215 } /* array_cmp */
217 /* Calculate the linear function 'd1 + n * d2'. */
218 static int
219 data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res)
221         if (d1->type != d2->type)
222                 return -1;
224         if (d1->type == SDB_TYPE_INTEGER)
225                 res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer;
226         else if (d1->type == SDB_TYPE_DECIMAL)
227                 res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal;
228         else if (d1->type ==  SDB_TYPE_DATETIME)
229                 res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime;
230         else
231                 return -1;
232         res->type = d1->type;
233         return 0;
234 } /* data_lin */
236 /* Multiply d1 with d2. */
237 static int
238 data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
240         if (d1->type == SDB_TYPE_INTEGER) {
241                 if (d2->type == SDB_TYPE_INTEGER)
242                         res->data.integer = d1->data.integer * d2->data.integer;
243                 else if (d2->type == SDB_TYPE_DATETIME) {
244                         res->data.datetime = (sdb_time_t)d1->data.integer
245                                 * d2->data.datetime;
246                         res->type = SDB_TYPE_DATETIME;
247                         return 0;
248                 }
249                 else
250                         return -1;
251         }
252         else if (d1->type == SDB_TYPE_DECIMAL) {
253                 if (d2->type == SDB_TYPE_DECIMAL)
254                         res->data.decimal = d1->data.decimal * d2->data.decimal;
255                 else if (d2->type == SDB_TYPE_DATETIME) {
256                         res->data.datetime = (sdb_time_t)(d1->data.decimal
257                                         * (double)d2->data.datetime);
258                         res->type = SDB_TYPE_DATETIME;
259                         return 0;
260                 }
261                 else
262                         return -1;
263         }
264         else if (d1->type == SDB_TYPE_DATETIME) {
265                 if (d2->type == SDB_TYPE_DATETIME)
266                         res->data.datetime = d1->data.datetime
267                                 * d2->data.datetime;
268                 else if (d2->type == SDB_TYPE_INTEGER)
269                         res->data.datetime = d1->data.datetime
270                                 * (sdb_time_t)d2->data.integer;
271                 else if (d2->type == SDB_TYPE_DECIMAL)
272                         res->data.datetime = (sdb_time_t)((double)d1->data.datetime
273                                         * d2->data.decimal);
274                 else
275                         return -1;
276         }
277         else
278                 return -1;
280         res->type = d1->type;
281         return 0;
282 } /* data_mul */
284 /* Device d1 by d2 and return the result and the remainder. */
285 static int
286 data_div(const sdb_data_t *d1, const sdb_data_t *d2,
287                 sdb_data_t *res, sdb_data_t *rem)
289         if (d1->type == SDB_TYPE_INTEGER) {
290                 if (d2->type != SDB_TYPE_INTEGER)
291                         return -1;
292                 if (res)
293                         res->data.integer = d1->data.integer / d2->data.integer;
294                 if (rem)
295                         rem->data.integer = d1->data.integer % d2->data.integer;
296         }
297         else if (d1->type == SDB_TYPE_DECIMAL) {
298                 if (d2->type != SDB_TYPE_DECIMAL)
299                         return -1;
300                 if (res)
301                         res->data.decimal = d1->data.decimal / d2->data.decimal;
302                 if (rem)
303                         rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
304         }
305         else if (d1->type == SDB_TYPE_DATETIME) {
306                 if (d2->type == SDB_TYPE_DECIMAL) {
307                         if (res)
308                                 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
309                                                 / d2->data.decimal);
310                         if (rem) {
311                                 double tmp = fmod((double)d1->data.datetime, d2->data.decimal);
312                                 rem->data.datetime = (sdb_time_t)tmp;
313                         }
314                 }
315                 else {
316                         sdb_time_t a, b;
317                         if (d2->type == SDB_TYPE_DATETIME) {
318                                 a = d1->data.datetime;
319                                 b = d2->data.datetime;
320                         }
321                         else if (d2->type == SDB_TYPE_INTEGER) {
322                                 a = d1->data.datetime;
323                                 b = (sdb_time_t)d2->data.integer;
324                         }
325                         else
326                                 return -1;
327                         if (res)
328                                 res->data.datetime = a / b;
329                         if (rem)
330                                 rem->data.datetime = a % b;
331                 }
332         }
333         else
334                 return -1;
336         if (res)
337                 res->type = d1->type;
338         if (rem)
339                 rem->type = d1->type;
340         return 0;
341 } /* data_div */
343 /* Concatenate d1 and d2. */
344 static int
345 data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
347         unsigned char *new;
348         unsigned char *s1, *s2;
349         size_t len1, len2;
351         /* TODO: support array plus element */
352         if (d1->type != d2->type)
353                 return -1;
355         if (d1->type == SDB_TYPE_STRING) {
356                 s1 = (unsigned char *)d1->data.string;
357                 s2 = (unsigned char *)d2->data.string;
358                 len1 = s1 ? strlen((char *)s1) : 0;
359                 len2 = s2 ? strlen((char *)s2) : 0;
360         }
361         else if (d1->type == SDB_TYPE_BINARY) {
362                 s1 = d1->data.binary.datum;
363                 s2 = d2->data.binary.datum;
364                 len1 = d1->data.binary.length;
365                 len2 = d2->data.binary.length;
366         }
367         else if (d1->type & SDB_TYPE_ARRAY) {
368                 size_t elem_size = sdb_data_sizeof(d1->type & 0xff);
369                 s1 = (unsigned char *)d1->data.array.values;
370                 s2 = (unsigned char *)d2->data.array.values;
371                 len1 = d1->data.array.length * elem_size;
372                 len2 = d2->data.array.length * elem_size;
373         }
374         else
375                 return -1;
377         assert(s1 && s2);
379         new = malloc(len1 + len2 + 1);
380         if (! new)
381                 return -1;
383         if (len1)
384                 memcpy(new, s1, len1);
385         if (len2)
386                 memcpy(new + len1, s2, len2);
387         new[len1 + len2] = '\0';
389         res->type = d1->type;
390         if (res->type == SDB_TYPE_STRING) {
391                 res->data.string = (char *)new;
392         }
393         else if (res->type == SDB_TYPE_BINARY) {
394                 res->data.binary.datum = new;
395                 res->data.binary.length = len1 + len2;
396         }
397         else if (d1->type & SDB_TYPE_ARRAY) {
398                 res->data.array.values = new;
399                 res->data.array.length = d1->data.array.length + d2->data.array.length;
400                 if (copy_array_values(res, res, sdb_data_sizeof(res->type & 0xff))) {
401                         /* this leaks already copied values but there's not much we can
402                          * do and this should only happen if we're in trouble anyway */
403                         free(new);
404                         res->data.array.values = NULL;
405                         res->data.array.length = 0;
406                         return -1;
407                 }
408         }
409         return 0;
410 } /* data_concat */
412 /*
413  * public API
414  */
416 const sdb_data_t SDB_DATA_NULL = SDB_DATA_INIT;
418 int
419 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
421         sdb_data_t tmp;
423         if ((! dst) || (! src))
424                 return -1;
426         tmp = *src;
427         if (src->type == SDB_TYPE_STRING) {
428                 if (src->data.string) {
429                         tmp.data.string = strdup(src->data.string);
430                         if (! tmp.data.string)
431                                 return -1;
432                 }
433         }
434         else if (src->type == SDB_TYPE_BINARY) {
435                 if (src->data.binary.datum) {
436                         tmp.data.binary.datum = malloc(src->data.binary.length);
437                         if (! tmp.data.binary.datum)
438                                 return -1;
439                         memcpy(tmp.data.binary.datum, src->data.binary.datum,
440                                         src->data.binary.length);
441                 }
442         }
443         else if (src->type == SDB_TYPE_REGEX) {
444                 if (src->data.re.raw) {
445                         tmp.data.re.raw = strdup(src->data.re.raw);
446                         if (! tmp.data.re.raw)
447                                 return -1;
448                         /* we need to recompile because the regex might point to
449                          * dynamically allocated memory */
450                         if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
451                                                 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
452                                 free(tmp.data.re.raw);
453                                 return -1;
454                         }
455                 }
456                 else
457                         memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
458         }
459         else if (src->type & SDB_TYPE_ARRAY) {
460                 if (src->data.array.values) {
461                         size_t elem_size = sdb_data_sizeof(src->type & 0xff);
462                         tmp.data.array.values = calloc(src->data.array.length, elem_size);
463                         if (! tmp.data.array.values)
464                                 return -1;
465                         if (copy_array_values(&tmp, src, elem_size)) {
466                                 sdb_data_free_datum(&tmp);
467                                 return -1;
468                         }
469                 }
470         }
472         sdb_data_free_datum(dst);
473         *dst = tmp;
474         return 0;
475 } /* sdb_data_copy */
477 void
478 sdb_data_free_datum(sdb_data_t *datum)
480         if (! datum)
481                 return;
483         if (datum->type == SDB_TYPE_STRING) {
484                 if (datum->data.string)
485                         free(datum->data.string);
486                 datum->data.string = NULL;
487         }
488         else if (datum->type == SDB_TYPE_BINARY) {
489                 if (datum->data.binary.datum)
490                         free(datum->data.binary.datum);
491                 datum->data.binary.datum = NULL;
492                 datum->data.binary.length = 0;
493         }
494         else if (datum->type == SDB_TYPE_REGEX) {
495                 if (datum->data.re.raw) {
496                         free(datum->data.re.raw);
497                         regfree(&datum->data.re.regex);
498                 }
499                 datum->data.re.raw = NULL;
500                 memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
501         }
502         else if (datum->type & SDB_TYPE_ARRAY) {
503                 free_array_values(datum);
504                 if (datum->data.array.values)
505                         free(datum->data.array.values);
506                 datum->data.array.values = NULL;
507                 datum->data.array.length = 0;
508         }
509 } /* sdb_data_free_datum */
511 int
512 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
514 #define CMP_NULL(a, b) \
515         do { \
516                 if (!(a) && !(b)) return 0; \
517                 if (!(a)) return -1; \
518                 if (!(b)) return 1; \
519         } while (0)
521         CMP_NULL(d1, d2);
523         if (d1->type != d2->type)
524                 return SDB_CMP(d1->type, d2->type);
526         if (d1->type == SDB_TYPE_INTEGER)
527                 return SDB_CMP(d1->data.integer, d2->data.integer);
528         else if (d1->type == SDB_TYPE_DECIMAL)
529                 return SDB_CMP(d1->data.decimal, d2->data.decimal);
530         else if (d1->type == SDB_TYPE_STRING) {
531                 CMP_NULL(d1->data.string, d2->data.string);
532                 return strcasecmp(d1->data.string, d2->data.string);
533         }
534         else if (d1->type == SDB_TYPE_DATETIME)
535                 return SDB_CMP(d1->data.datetime, d2->data.datetime);
536         else if (d1->type == SDB_TYPE_BINARY) {
537                 int diff;
539                 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
541                 /* on a common prefix, the shorter datum sorts less */
542                 if (d1->data.binary.length < d2->data.binary.length) {
543                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
544                                         d1->data.binary.length);
545                         diff = diff ? diff : -1;
546                 }
547                 else if (d1->data.binary.length > d2->data.binary.length) {
548                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
549                                         d2->data.binary.length);
550                         diff = diff ? diff : 1;
551                 }
552                 else
553                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
554                                         d1->data.binary.length);
556                 return diff;
557         }
558         else if (d1->type == SDB_TYPE_REGEX) {
559                 CMP_NULL(d1->data.re.raw, d2->data.re.raw);
560                 return strcmp(d1->data.re.raw, d2->data.re.raw);
561         }
562         else if (d1->type & SDB_TYPE_ARRAY) {
563                 CMP_NULL(d1->data.array.values, d2->data.array.values);
564                 return array_cmp(d1, d2);
565         }
566         return -1;
567 } /* sdb_data_cmp */
569 int
570 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
572         char d1_str[sdb_data_strlen(d1) + 1];
573         char d2_str[sdb_data_strlen(d2) + 1];
575         if ((d1->type & SDB_TYPE_ARRAY) || (d2->type & SDB_TYPE_ARRAY)) {
576                 /* TODO */
577                 errno = ENOTSUP;
578                 return -1;
579         }
581         if (sdb_data_isnull(d1))
582                 d1 = NULL;
583         if (sdb_data_isnull(d2))
584                 d2 = NULL;
586         CMP_NULL(d1, d2);
588         if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
589                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
590         if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
591                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
593         return strcasecmp(d1_str, d2_str);
594 #undef CMP_NULL
595 } /* sdb_data_strcmp */
597 _Bool
598 sdb_data_isnull(const sdb_data_t *datum)
600         if (! datum)
601                 return 1;
602         if (datum->type == SDB_TYPE_NULL)
603                 return 1;
604         if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
605                 return 1;
606         if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
607                 return 1;
608         if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
609                 return 1;
610         if ((datum->type & SDB_TYPE_ARRAY) && (! datum->data.array.values))
611                 return 1;
612         return 0;
613 } /* sdb_data_isnull */
615 _Bool
616 sdb_data_inarray(const sdb_data_t *value, const sdb_data_t *array)
618         size_t i;
620         if (sdb_data_isnull(value) || sdb_data_isnull(array))
621                 return 0;
622         if ((value->type & SDB_TYPE_ARRAY) || (! (array->type & SDB_TYPE_ARRAY)))
623                 return 0;
624         if (value->type != (array->type & 0xff))
625                 return 0;
627         if (value->type == SDB_TYPE_INTEGER) {
628                 int64_t *v = array->data.array.values;
629                 for (i = 0; i < array->data.array.length; ++i)
630                         if (value->data.integer == v[i])
631                                 return 1;
632         }
633         else if (value->type == SDB_TYPE_DECIMAL) {
634                 double *v = array->data.array.values;
635                 for (i = 0; i < array->data.array.length; ++i)
636                         if (value->data.decimal == v[i])
637                                 return 1;
638         }
639         else if (value->type == SDB_TYPE_STRING) {
640                 char **v = array->data.array.values;
641                 for (i = 0; i < array->data.array.length; ++i)
642                         if (!strcasecmp(value->data.string, v[i]))
643                                 return 1;
644         }
645         else {
646                 /* TODO */
647                 errno = ENOTSUP;
648                 return 0;
649         }
650         return 0;
651 } /* sdb_data_inarray */
653 int
654 sdb_data_parse_op(const char *op)
656         if (! strcmp(op, "+"))
657                 return SDB_DATA_ADD;
658         else if (! strcmp(op, "-"))
659                 return SDB_DATA_SUB;
660         else if (! strcmp(op, "*"))
661                 return SDB_DATA_MUL;
662         else if (! strcmp(op, "/"))
663                 return SDB_DATA_DIV;
664         else if (! strcmp(op, "%"))
665                 return SDB_DATA_MOD;
666         else if (! strcmp(op, "||"))
667                 return SDB_DATA_CONCAT;
668         return -1;
669 } /* sdb_data_parse_op */
671 int
672 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
673                 sdb_data_t *res)
675         if ((! d1) || (! d2) || (! res))
676                 return -1;
677         if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) {
678                 *res = SDB_DATA_NULL;
679                 return 0;
680         }
681         switch (op) {
682                 case SDB_DATA_CONCAT:
683                         return data_concat(d1, d2, res);
684                 case SDB_DATA_ADD:
685                         return data_lin(d1, 1, d2, res);
686                 case SDB_DATA_SUB:
687                         return data_lin(d1, -1, d2, res);
688                 case SDB_DATA_MUL:
689                         return data_mul(d1, d2, res);
690                 case SDB_DATA_DIV:
691                         return data_div(d1, d2, res, NULL);
692                 case SDB_DATA_MOD:
693                         return data_div(d1, d2, NULL, res);
694         }
695         return -1;
696 } /* sdb_data_expr_eval */
698 int
699 sdb_data_expr_type(int op, int type1, int type2)
701         int types_num = (int)SDB_STATIC_ARRAY_LEN(op_matrix[0]);
703         assert(SDB_STATIC_ARRAY_LEN(op_matrix[0])
704                         == SDB_STATIC_ARRAY_LEN(op_matrix[0][0]));
706         if ((op <= 0) || (SDB_STATIC_ARRAY_LEN(op_matrix) < (size_t)op))
707                 return -1;
709         /* arrays only support concat; type has to match */
710         if ((type1 & SDB_TYPE_ARRAY) || (type2 & SDB_TYPE_ARRAY)) {
711                 if ((type1 != type2) || (op != SDB_DATA_CONCAT))
712                         return -1;
713                 return type1;
714         }
715         if ((type1 < 0) || (types_num < type1)
716                         || (type2 < 0) || (types_num < type2))
717                 return -1;
719         if ((type1 == SDB_TYPE_NULL) || (type2 == SDB_TYPE_NULL))
720                 return SDB_TYPE_NULL;
721         return op_matrix[op - 1][type1 - 1][type2 - 1];
722 } /* sdb_data_expr_type */
724 size_t
725 sdb_data_strlen(const sdb_data_t *datum)
727         if (! datum)
728                 return 0;
730         if (datum->type == SDB_TYPE_INTEGER) {
731                 /* log(64) */
732                 return 20;
733         }
734         else if (datum->type == SDB_TYPE_DECIMAL) {
735                 /* XXX: -d.dddddde+dd or -ddddd.dddddd */
736                 return 42;
737         }
738         else if (datum->type == SDB_TYPE_STRING) {
739                 if (! datum->data.string)
740                         return 6; /* NULL */
741                 /* in the worst case, each character needs to be escaped */
742                 return 2 * strlen(datum->data.string) + 2;
743         }
744         else if (datum->type == SDB_TYPE_DATETIME) {
745                 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
746                 return 27;
747         }
748         else if (datum->type == SDB_TYPE_BINARY) {
749                 if (! datum->data.binary.datum)
750                         return 6; /* NULL */
751                 /* "\xNN" */
752                 return 4 * datum->data.binary.length + 2;
753         }
754         else if (datum->type == SDB_TYPE_REGEX) {
755                 if (! datum->data.re.raw)
756                         return 6; /* NULL */
757                 /* "/.../" */
758                 return strlen(datum->data.re.raw) + 4;
759         }
760         else if (datum->type & SDB_TYPE_ARRAY) {
761                 /* TODO */
762                 errno = ENOTSUP;
763                 return 0;
764         }
765         return 0;
766 } /* sdb_data_strlen */
768 int
769 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
771         char tmp[sdb_data_strlen(datum) + 1];
772         char *data = NULL;
773         _Bool is_null = 0;
774         int ret = -1;
776         size_t i, pos;
778         if ((! datum) || (! buf))
779                 return -1;
781         if (datum->type == SDB_TYPE_INTEGER) {
782                 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
783         }
784         else if (datum->type == SDB_TYPE_DECIMAL) {
785                 ret = snprintf(buf, buflen, "%g", datum->data.decimal);
786         }
787         else if (datum->type == SDB_TYPE_STRING) {
788                 if (! datum->data.string)
789                         is_null = 1;
790                 else {
791                         pos = 0;
792                         for (i = 0; i < strlen(datum->data.string); ++i) {
793                                 char byte = datum->data.string[i];
795                                 if ((byte == '\\') || (byte == '"')) {
796                                         tmp[pos] = '\\';
797                                         ++pos;
798                                 }
799                                 tmp[pos] = byte;
800                                 ++pos;
801                         }
802                         tmp[pos] = '\0';
803                         data = tmp;
804                 }
805         }
806         else if (datum->type == SDB_TYPE_DATETIME) {
807                 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
808                                         datum->data.datetime))
809                         return -1;
810                 tmp[sizeof(tmp) - 1] = '\0';
811                 data = tmp;
812         }
813         else if (datum->type == SDB_TYPE_BINARY) {
814                 pos = 0;
815                 for (i = 0; i < datum->data.binary.length; ++i) {
816                         int byte = (int)datum->data.binary.datum[i];
817                         char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
818                                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
820                         tmp[pos] = '\\';
821                         tmp[pos + 1] = 'x';
822                         pos += 2;
824                         if (byte > 0xf) {
825                                 tmp[pos] = hex[byte >> 4];
826                                 ++pos;
827                         }
828                         tmp[pos] = hex[byte & 0xf];
829                         ++pos;
830                 }
831                 if (datum->data.binary.datum) {
832                         tmp[pos] = '\0';
833                         data = tmp;
834                 }
835                 else
836                         is_null = 1;
837         }
838         else if (datum->type == SDB_TYPE_REGEX) {
839                 if (! datum->data.re.raw)
840                         is_null = 1;
841                 else {
842                         snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
843                         data = tmp;
844                 }
845         }
846         else if (datum->type & SDB_TYPE_ARRAY) {
847                 /* TODO */
848                 errno = ENOTSUP;
849                 return -1;
850         }
852         if (is_null) {
853                 /* never quote NULL */
854                 strncpy(buf, "NULL", buflen);
855                 ret = (int)SDB_MIN(buflen, 4);
856         }
857         else if (data) {
858                 if (quoted == SDB_UNQUOTED)
859                         ret = snprintf(buf, buflen, "%s", data);
860                 else if (quoted == SDB_SINGLE_QUOTED)
861                         ret = snprintf(buf, buflen, "'%s'", data);
862                 else
863                         ret = snprintf(buf, buflen, "\"%s\"", data);
864         }
865         buf[buflen - 1] = '\0';
866         return ret;
867 } /* sdb_data_format */
869 int
870 sdb_data_parse(char *str, int type, sdb_data_t *data)
872         sdb_data_t tmp;
874         char *endptr = NULL;
876         errno = 0;
877         if (type == SDB_TYPE_INTEGER) {
878                 tmp.data.integer = strtoll(str, &endptr, 0);
879         }
880         else if (type == SDB_TYPE_DECIMAL) {
881                 tmp.data.decimal = strtod(str, &endptr);
882         }
883         else if (type == SDB_TYPE_STRING) {
884                 tmp.data.string = str;
885         }
886         else if (type == SDB_TYPE_DATETIME) {
887                 double datetime = strtod(str, &endptr);
888                 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
889         }
890         else if (type == SDB_TYPE_BINARY) {
891                 /* we don't support any binary information containing 0-bytes */
892                 tmp.data.binary.length = strlen(str);
893                 tmp.data.binary.datum = (unsigned char *)str;
894         }
895         else if (type == SDB_TYPE_REGEX) {
896                 tmp.data.re.raw = strdup(str);
897                 if (! tmp.data.re.raw)
898                         return -1;
899                 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
900                                         REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
901                         sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
902                                         "expression '%s'", tmp.data.re.raw);
903                         free(tmp.data.re.raw);
904                         return -1;
905                 }
906                 if (! data) {
907                         tmp.type = SDB_TYPE_REGEX;
908                         sdb_data_free_datum(&tmp);
909                 }
910         }
911         else if (type & SDB_TYPE_ARRAY) {
912                 /* TODO */
913                 errno = ENOTSUP;
914                 return -1;
915         }
916         else {
917                 errno = EINVAL;
918                 return -1;
919         }
921         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
922                         || (type == SDB_TYPE_DATETIME)) {
923                 if (errno || (str == endptr)) {
924                         char errbuf[1024];
925                         sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
926                                         "'%s' as numeric value (type %i): %s", str, type,
927                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
928                         return -1;
929                 }
930                 else if (endptr && (*endptr != '\0'))
931                         sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
932                                         "number while parsing numeric value (type %i): %s.",
933                                         type, endptr);
934         }
936         if (data) {
937                 *data = tmp;
938                 data->type = type;
939         }
940         return 0;
941 } /* sdb_data_parse */
943 size_t
944 sdb_data_sizeof(int type)
946         sdb_data_t v;
947         if (type == SDB_TYPE_INTEGER)
948                 return sizeof(v.data.integer);
949         else if (type == SDB_TYPE_DECIMAL)
950                 return sizeof(v.data.decimal);
951         else if (type == SDB_TYPE_STRING)
952                 return sizeof(v.data.string);
953         else if (type == SDB_TYPE_DATETIME)
954                 return sizeof(v.data.datetime);
955         else if (type == SDB_TYPE_BINARY)
956                 return sizeof(v.data.binary);
957         else if (type == SDB_TYPE_REGEX)
958                 return sizeof(v.data.re);
959         return 0;
960 } /* sdb_data_sizeof */
962 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */