4e8bae7cad7c980941145938597be86e1bb770f3
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 /*
50 * expression types:
51 */
52 enum {
53 ATTR_VALUE = -2, /* attr name stored in data.data.string */
54 FIELD_VALUE = -1, /* field type stored in data.data.integer */
55 /* 0: const value (stored in data) */
56 /* >0: operator id */
57 };
59 struct sdb_store_expr {
60 sdb_object_t super;
62 int type; /* see above */
64 sdb_store_expr_t *left;
65 sdb_store_expr_t *right;
67 sdb_data_t data;
68 };
70 /*
71 * private data types
72 */
74 static int
75 expr_init(sdb_object_t *obj, va_list ap)
76 {
77 int type = va_arg(ap, int);
78 sdb_store_expr_t *left = va_arg(ap, sdb_store_expr_t *);
79 sdb_store_expr_t *right = va_arg(ap, sdb_store_expr_t *);
80 const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
82 sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
84 if (type <= 0) {
85 if (! value)
86 return -1;
87 } else {
88 if (value)
89 return -1;
90 if ((! left) || (! right))
91 return -1;
92 }
94 if (value)
95 expr->data = *value;
97 sdb_object_ref(SDB_OBJ(left));
98 sdb_object_ref(SDB_OBJ(right));
100 expr->type = type;
101 expr->left = left;
102 expr->right = right;
103 return 0;
104 } /* expr_init */
106 static void
107 expr_destroy(sdb_object_t *obj)
108 {
109 sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
110 sdb_object_deref(SDB_OBJ(expr->left));
111 sdb_object_deref(SDB_OBJ(expr->right));
113 if (expr->data.type)
114 sdb_data_free_datum(&expr->data);
115 } /* expr_destroy */
117 static sdb_type_t expr_type = {
118 /* size = */ sizeof(sdb_store_expr_t),
119 /* init = */ expr_init,
120 /* destroy = */ expr_destroy,
121 };
123 /*
124 * public API
125 */
127 sdb_store_expr_t *
128 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right)
129 {
130 sdb_data_t value = SDB_DATA_INIT;
132 if ((op < 0) || (SDB_DATA_CONCAT < op) || (! left) || (! right))
133 return NULL;
135 if (left->type || right->type)
136 return SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
137 op, left, right, NULL));
138 /* else: both expressions are constant values; evaluate now */
140 if (sdb_data_expr_eval(op, &left->data, &right->data, &value))
141 return NULL;
142 return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
143 0, NULL, NULL, &value));
144 } /* sdb_store_expr_create */
146 sdb_store_expr_t *
147 sdb_store_expr_fieldvalue(int field)
148 {
149 sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = field } };
150 if ((field < 0) || (SDB_FIELD_BACKEND < field))
151 return NULL;
152 return SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type,
153 FIELD_VALUE, NULL, NULL, &value));
154 } /* sdb_store_expr_fieldvalue */
156 sdb_store_expr_t *
157 sdb_store_expr_attrvalue(const char *name)
158 {
159 sdb_data_t value = { SDB_TYPE_STRING, { .string = NULL} };
160 sdb_store_expr_t *expr;
162 value.data.string = strdup(name);
163 if (! value.data.string)
164 return NULL;
166 expr = SDB_STORE_EXPR(sdb_object_create("store-attrvalue", expr_type,
167 ATTR_VALUE, NULL, NULL, &value));
168 if (! expr)
169 free(value.data.string);
170 return expr;
171 } /* sdb_store_expr_attrvalue */
173 sdb_store_expr_t *
174 sdb_store_expr_constvalue(const sdb_data_t *value)
175 {
176 sdb_data_t data = SDB_DATA_INIT;
177 if (sdb_data_copy(&data, value))
178 return NULL;
179 return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
180 0, NULL, NULL, &data));
181 } /* sdb_store_expr_constvalue */
183 int
184 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
185 sdb_data_t *res, sdb_store_matcher_t *filter)
186 {
187 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
188 int status = 0;
190 if ((! expr) || (! res))
191 return -1;
193 if (filter && obj && (! sdb_store_matcher_matches(filter, obj, NULL)))
194 obj = NULL; /* this object does not exist */
196 if (! expr->type)
197 return sdb_data_copy(res, &expr->data);
198 else if (expr->type == FIELD_VALUE)
199 return sdb_store_get_field(obj, (int)expr->data.data.integer, res);
200 else if (expr->type == ATTR_VALUE)
201 return sdb_store_get_attr(obj, expr->data.data.string, res, filter);
203 if (sdb_store_expr_eval(expr->left, obj, &v1, filter))
204 return -1;
205 if (sdb_store_expr_eval(expr->right, obj, &v2, filter)) {
206 sdb_data_free_datum(&v1);
207 return -1;
208 }
210 if (sdb_data_expr_eval(expr->type, &v1, &v2, res))
211 status = -1;
212 sdb_data_free_datum(&v1);
213 sdb_data_free_datum(&v2);
214 return status;
215 } /* sdb_store_expr_eval */
217 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */