Code

store: Record the data-type of an expression.
[sysdb.git] / src / core / store_expr.c
1 /*
2  * SysDB - src/core/store_expr.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 /*
29  * This module implements expressions which may be executed in the store.
30  */
32 #if HAVE_CONFIG_H
33 #       include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "sysdb.h"
37 #include "core/store-private.h"
38 #include "core/data.h"
39 #include "core/object.h"
41 #include <assert.h>
42 #include <stdlib.h>
43 #include <string.h>
45 /*
46  * private data types
47  */
49 static int
50 expr_init(sdb_object_t *obj, va_list ap)
51 {
52         int type = va_arg(ap, int);
53         sdb_store_expr_t *left  = va_arg(ap, sdb_store_expr_t *);
54         sdb_store_expr_t *right = va_arg(ap, sdb_store_expr_t *);
55         const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
57         sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
59         if (type <= 0) {
60                 if (! value)
61                         return -1;
62         } else {
63                 if (value)
64                         return -1;
65                 if ((! left) || (! right))
66                         return -1;
67         }
69         if (value)
70                 expr->data = *value;
72         sdb_object_ref(SDB_OBJ(left));
73         sdb_object_ref(SDB_OBJ(right));
75         expr->type  = type;
76         expr->left  = left;
77         expr->right = right;
79         /* unknown for now */
80         expr->data_type = -1;
81         return 0;
82 } /* expr_init */
84 static void
85 expr_destroy(sdb_object_t *obj)
86 {
87         sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
88         sdb_object_deref(SDB_OBJ(expr->left));
89         sdb_object_deref(SDB_OBJ(expr->right));
91         if (expr->data.type)
92                 sdb_data_free_datum(&expr->data);
93 } /* expr_destroy */
95 static sdb_type_t expr_type = {
96         /* size = */ sizeof(sdb_store_expr_t),
97         /* init = */ expr_init,
98         /* destroy = */ expr_destroy,
99 };
101 /*
102  * public API
103  */
105 sdb_store_expr_t *
106 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right)
108         sdb_data_t value = SDB_DATA_INIT;
109         sdb_store_expr_t *e;
111         if ((op < 0) || (SDB_DATA_CONCAT < op) || (! left) || (! right))
112                 return NULL;
114         if (left->type || right->type) {
115                 e = SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
116                                         op, left, right, NULL));
117                 e->data_type = sdb_data_expr_type(op, left->type, right->type);
118                 return e;
119         }
120         /* else: both expressions are constant values; evaluate now */
122         if (sdb_data_expr_eval(op, &left->data, &right->data, &value))
123                 return NULL;
124         e = SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
125                                 0, NULL, NULL, &value));
126         e->data_type = value.type;
127         return e;
128 } /* sdb_store_expr_create */
130 sdb_store_expr_t *
131 sdb_store_expr_fieldvalue(int field)
133         sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = field } };
134         sdb_store_expr_t *e;
136         if ((field < 0) || (SDB_FIELD_BACKEND < field))
137                 return NULL;
138         e = SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type,
139                                 FIELD_VALUE, NULL, NULL, &value));
140         e->data_type = SDB_FIELD_TYPE(field);
141         return e;
142 } /* sdb_store_expr_fieldvalue */
144 sdb_store_expr_t *
145 sdb_store_expr_attrvalue(const char *name)
147         sdb_data_t value = { SDB_TYPE_STRING, { .string = NULL} };
148         sdb_store_expr_t *expr;
150         value.data.string = strdup(name);
151         if (! value.data.string)
152                 return NULL;
154         expr = SDB_STORE_EXPR(sdb_object_create("store-attrvalue", expr_type,
155                                 ATTR_VALUE, NULL, NULL, &value));
156         if (! expr)
157                 free(value.data.string);
158         expr->data_type = -1;
159         return expr;
160 } /* sdb_store_expr_attrvalue */
162 sdb_store_expr_t *
163 sdb_store_expr_constvalue(const sdb_data_t *value)
165         sdb_data_t data = SDB_DATA_INIT;
166         sdb_store_expr_t *e;
168         if (sdb_data_copy(&data, value))
169                 return NULL;
170         e = SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
171                                 0, NULL, NULL, &data));
172         e->data_type = data.type;
173         return e;
174 } /* sdb_store_expr_constvalue */
176 int
177 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
178                 sdb_data_t *res, sdb_store_matcher_t *filter)
180         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
181         int status = 0;
183         if ((! expr) || (! res))
184                 return -1;
186         if (filter && obj && (! sdb_store_matcher_matches(filter, obj, NULL)))
187                 obj = NULL; /* this object does not exist */
189         if (! expr->type)
190                 return sdb_data_copy(res, &expr->data);
191         else if (expr->type == FIELD_VALUE)
192                 return sdb_store_get_field(obj, (int)expr->data.data.integer, res);
193         else if (expr->type == ATTR_VALUE) {
194                 status = sdb_store_get_attr(obj, expr->data.data.string, res, filter);
195                 if ((status < 0) && obj) {
196                         /* attribute does not exist => NULL */
197                         status = 0;
198                         res->type = SDB_TYPE_STRING;
199                         res->data.string = NULL;
200                 }
201                 return status;
202         }
204         if (sdb_store_expr_eval(expr->left, obj, &v1, filter))
205                 return -1;
206         if (sdb_store_expr_eval(expr->right, obj, &v2, filter)) {
207                 sdb_data_free_datum(&v1);
208                 return -1;
209         }
211         if (sdb_data_expr_eval(expr->type, &v1, &v2, res))
212                 status = -1;
213         sdb_data_free_datum(&v1);
214         sdb_data_free_datum(&v2);
215         return status;
216 } /* sdb_store_expr_eval */
218 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */