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>
43 /*
44 * private data types
45 */
47 struct sdb_store_expr {
48 sdb_object_t super;
50 /*
51 * type:
52 * -1: field value (field type store in data.data.integer)
53 * 0: const value (stored in data)
54 * >0: operator id
55 */
56 int type;
58 sdb_store_expr_t *left;
59 sdb_store_expr_t *right;
61 sdb_data_t data;
62 };
64 /*
65 * private data types
66 */
68 static int
69 expr_init(sdb_object_t *obj, va_list ap)
70 {
71 int type = va_arg(ap, int);
72 sdb_store_expr_t *left = va_arg(ap, sdb_store_expr_t *);
73 sdb_store_expr_t *right = va_arg(ap, sdb_store_expr_t *);
74 const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
76 sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
78 if (type <= 0) {
79 if (! value)
80 return -1;
81 } else {
82 if (value)
83 return -1;
84 if ((! left) || (! right))
85 return -1;
86 }
88 if (value)
89 expr->data = *value;
91 sdb_object_ref(SDB_OBJ(left));
92 sdb_object_ref(SDB_OBJ(right));
94 expr->type = type;
95 expr->left = left;
96 expr->right = right;
97 return 0;
98 } /* expr_init */
100 static void
101 expr_destroy(sdb_object_t *obj)
102 {
103 sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
104 sdb_object_deref(SDB_OBJ(expr->left));
105 sdb_object_deref(SDB_OBJ(expr->right));
107 if (expr->data.type)
108 sdb_data_free_datum(&expr->data);
109 } /* expr_destroy */
111 static sdb_type_t expr_type = {
112 /* size = */ sizeof(sdb_store_expr_t),
113 /* init = */ expr_init,
114 /* destroy = */ expr_destroy,
115 };
117 /*
118 * public API
119 */
121 sdb_store_expr_t *
122 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right)
123 {
124 sdb_data_t value = SDB_DATA_INIT;
126 if ((op < 0) || (SDB_DATA_CONCAT < op) || (! left) || (! right))
127 return NULL;
129 if (left->type || right->type)
130 return SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
131 op, left, right, NULL));
132 /* else: both expressions are constant values; evaluate now */
134 if (sdb_data_expr_eval(op, &left->data, &right->data, &value))
135 return NULL;
136 return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
137 0, NULL, NULL, &value));
138 } /* sdb_store_expr_create */
140 sdb_store_expr_t *
141 sdb_store_expr_fieldvalue(int field)
142 {
143 sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = field } };
144 if ((field < 0) || (SDB_FIELD_BACKEND < field))
145 return NULL;
146 return SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type,
147 -1, NULL, NULL, &value));
148 } /* sdb_store_expr_fieldvalue */
150 sdb_store_expr_t *
151 sdb_store_expr_constvalue(const sdb_data_t *value)
152 {
153 sdb_data_t data = SDB_DATA_INIT;
154 if (sdb_data_copy(&data, value))
155 return NULL;
156 return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
157 0, NULL, NULL, &data));
158 } /* sdb_store_expr_constvalue */
160 int
161 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
162 sdb_data_t *res)
163 {
164 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
165 int status = 0;
167 if ((! expr) || (! res))
168 return -1;
170 if (! expr->type)
171 return sdb_data_copy(res, &expr->data);
172 else if (expr->type < 0)
173 return sdb_store_get_field(obj, (int)expr->data.data.integer, res);
175 if (sdb_store_expr_eval(expr->left, obj, &v1))
176 return -1;
177 if (sdb_store_expr_eval(expr->right, obj, &v2)) {
178 sdb_data_free_datum(&v1);
179 return -1;
180 }
182 if (sdb_data_expr_eval(expr->type, &v1, &v2, res))
183 status = -1;
184 sdb_data_free_datum(&v1);
185 sdb_data_free_datum(&v2);
186 return status;
187 } /* sdb_store_expr_eval */
189 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */