ed0589bc28d1a37c32c6ea93400331410b01c532
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;
78 return 0;
79 } /* expr_init */
81 static void
82 expr_destroy(sdb_object_t *obj)
83 {
84 sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
85 sdb_object_deref(SDB_OBJ(expr->left));
86 sdb_object_deref(SDB_OBJ(expr->right));
88 if (expr->data.type)
89 sdb_data_free_datum(&expr->data);
90 } /* expr_destroy */
92 static sdb_type_t expr_type = {
93 /* size = */ sizeof(sdb_store_expr_t),
94 /* init = */ expr_init,
95 /* destroy = */ expr_destroy,
96 };
98 /*
99 * public API
100 */
102 sdb_store_expr_t *
103 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right)
104 {
105 sdb_data_t value = SDB_DATA_INIT;
107 if ((op < 0) || (SDB_DATA_CONCAT < op) || (! left) || (! right))
108 return NULL;
110 if (left->type || right->type)
111 return SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
112 op, left, right, NULL));
113 /* else: both expressions are constant values; evaluate now */
115 if (sdb_data_expr_eval(op, &left->data, &right->data, &value))
116 return NULL;
117 return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
118 0, NULL, NULL, &value));
119 } /* sdb_store_expr_create */
121 sdb_store_expr_t *
122 sdb_store_expr_fieldvalue(int field)
123 {
124 sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = field } };
125 if ((field < 0) || (SDB_FIELD_BACKEND < field))
126 return NULL;
127 return SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type,
128 FIELD_VALUE, NULL, NULL, &value));
129 } /* sdb_store_expr_fieldvalue */
131 sdb_store_expr_t *
132 sdb_store_expr_attrvalue(const char *name)
133 {
134 sdb_data_t value = { SDB_TYPE_STRING, { .string = NULL} };
135 sdb_store_expr_t *expr;
137 value.data.string = strdup(name);
138 if (! value.data.string)
139 return NULL;
141 expr = SDB_STORE_EXPR(sdb_object_create("store-attrvalue", expr_type,
142 ATTR_VALUE, NULL, NULL, &value));
143 if (! expr)
144 free(value.data.string);
145 return expr;
146 } /* sdb_store_expr_attrvalue */
148 sdb_store_expr_t *
149 sdb_store_expr_constvalue(const sdb_data_t *value)
150 {
151 sdb_data_t data = SDB_DATA_INIT;
152 if (sdb_data_copy(&data, value))
153 return NULL;
154 return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
155 0, NULL, NULL, &data));
156 } /* sdb_store_expr_constvalue */
158 int
159 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
160 sdb_data_t *res, sdb_store_matcher_t *filter)
161 {
162 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
163 int status = 0;
165 if ((! expr) || (! res))
166 return -1;
168 if (filter && obj && (! sdb_store_matcher_matches(filter, obj, NULL)))
169 obj = NULL; /* this object does not exist */
171 if (! expr->type)
172 return sdb_data_copy(res, &expr->data);
173 else if (expr->type == FIELD_VALUE)
174 return sdb_store_get_field(obj, (int)expr->data.data.integer, res);
175 else if (expr->type == ATTR_VALUE) {
176 status = sdb_store_get_attr(obj, expr->data.data.string, res, filter);
177 if ((status < 0) && obj) {
178 /* attribute does not exist => NULL */
179 status = 0;
180 res->type = SDB_TYPE_STRING;
181 res->data.string = NULL;
182 }
183 return status;
184 }
186 if (sdb_store_expr_eval(expr->left, obj, &v1, filter))
187 return -1;
188 if (sdb_store_expr_eval(expr->right, obj, &v2, filter)) {
189 sdb_data_free_datum(&v1);
190 return -1;
191 }
193 if (sdb_data_expr_eval(expr->type, &v1, &v2, res))
194 status = -1;
195 sdb_data_free_datum(&v1);
196 sdb_data_free_datum(&v2);
197 return status;
198 } /* sdb_store_expr_eval */
200 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */