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 if (sdb_data_copy(&expr->data, value))
90 return -1;
92 sdb_object_ref(SDB_OBJ(left));
93 sdb_object_ref(SDB_OBJ(right));
95 expr->type = type;
96 expr->left = left;
97 expr->right = right;
98 return 0;
99 } /* expr_init */
101 static void
102 expr_destroy(sdb_object_t *obj)
103 {
104 sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
105 sdb_object_deref(SDB_OBJ(expr->left));
106 sdb_object_deref(SDB_OBJ(expr->right));
108 if (expr->data.type)
109 sdb_data_free_datum(&expr->data);
110 } /* expr_destroy */
112 static sdb_type_t expr_type = {
113 /* size = */ sizeof(sdb_store_expr_t),
114 /* init = */ expr_init,
115 /* destroy = */ expr_destroy,
116 };
118 /*
119 * public API
120 */
122 sdb_store_expr_t *
123 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right)
124 {
125 if ((op < 0) || (SDB_DATA_CONCAT < op))
126 return NULL;
127 return SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
128 op, left, right, NULL));
129 } /* sdb_store_expr_create */
131 sdb_store_expr_t *
132 sdb_store_expr_fieldvalue(int field)
133 {
134 sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = field } };
135 if ((field < 0) || (SDB_FIELD_BACKEND < field))
136 return NULL;
137 return SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type,
138 -1, NULL, NULL, &value));
139 } /* sdb_store_expr_fieldvalue */
141 sdb_store_expr_t *
142 sdb_store_expr_constvalue(const sdb_data_t *value)
143 {
144 return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
145 0, NULL, NULL, value));
146 } /* sdb_store_expr_constvalue */
148 int
149 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
150 sdb_data_t *res)
151 {
152 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
154 if ((! expr) || (! res))
155 return -1;
157 if (! expr->type)
158 return sdb_data_copy(res, &expr->data);
159 else if (expr->type < 0)
160 return sdb_store_get_field(obj, (int)expr->data.data.integer, res);
162 if (sdb_store_expr_eval(expr->left, obj, &v1))
163 return -1;
164 if (sdb_store_expr_eval(expr->right, obj, &v2)) {
165 sdb_data_free_datum(&v1);
166 return -1;
167 }
169 if (sdb_data_expr_eval(expr->type, &v1, &v2, res)) {
170 sdb_data_free_datum(&v1);
171 sdb_data_free_datum(&v2);
172 return -1;
173 }
174 return 0;
175 } /* sdb_store_expr_eval */
177 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */