forked from parcio/julea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.c
279 lines (241 loc) · 10.1 KB
/
operators.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* JULEA - Flexible storage framework
* Copyright (C) 2019 Benjamin Warnke
* Copyright (C) 2019-2021 Michael Kuhn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
*
* Table (reffered as customersTable in this test case):
* customer_id first_name last_name email address city state zip
* 1 George Washington gwashington@usa.gov 3200 Mt Vernon Hwy Mount Vernon VA 22121
* 2 John Adams jadams@usa.gov 1250 Hancock St Quincy MA 02169
* 3 Thomas Jefferson tjefferson@usa.gov 931 Thomas Jefferson Pkwy Charlottesville VA 22902
* 4 James Madison jmadison@usa.gov 11350 Constitution Hwy Orange VA 22960
* 5 James Monroe jmonroe@usa.gov 2050 James Monroe Pkwy Charlottesville VA 22902
*
*
*/
#include <julea-config.h>
#include <glib.h>
#include <julea.h>
#include <julea-db.h>
#include "test.h"
static void
schema_create_customersTable(void)
{
g_autoptr(GError) error = NULL;
gboolean success = TRUE;
g_autoptr(JDBSchema) schema = NULL;
g_autoptr(JBatch) batch = j_batch_new_for_template(J_SEMANTICS_TEMPLATE_DEFAULT);
gchar const* idx_customerid[] = {
"customerid", NULL
};
schema = j_db_schema_new("namespace", "customersTable", &error);
g_assert_nonnull(schema);
g_assert_no_error(error);
success = j_db_schema_add_field(schema, "customerid", J_DB_TYPE_UINT64, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_add_field(schema, "firstname", J_DB_TYPE_STRING, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_add_field(schema, "lastname", J_DB_TYPE_STRING, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_add_field(schema, "email", J_DB_TYPE_STRING, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_add_field(schema, "address", J_DB_TYPE_STRING, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_add_field(schema, "city", J_DB_TYPE_STRING, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_add_field(schema, "state", J_DB_TYPE_STRING, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_add_field(schema, "zip", J_DB_TYPE_UINT64, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_add_index(schema, idx_customerid, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_schema_create(schema, batch, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_batch_execute(batch);
g_assert_true(success);
}
static void
entry_insert_customersTable(guint64 customerid, gchar const* firstname, gchar const* lastname, gchar const* email, gchar const* address, gchar const* city, gchar const* state, guint64 zip)
{
g_autoptr(GError) error = NULL;
gboolean success = TRUE;
g_autoptr(JDBSchema) schema = NULL;
g_autoptr(JDBEntry) entry = NULL;
g_autoptr(JBatch) batch = j_batch_new_for_template(J_SEMANTICS_TEMPLATE_DEFAULT);
schema = j_db_schema_new("namespace", "customersTable", &error);
g_assert_nonnull(schema);
g_assert_no_error(error);
success = j_db_schema_get(schema, batch, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_batch_execute(batch);
g_assert_true(success);
entry = j_db_entry_new(schema, &error);
g_assert_nonnull(entry);
g_assert_no_error(error);
success = j_db_entry_set_field(entry, "customerid", &customerid, sizeof(customerid), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_entry_set_field(entry, "firstname", firstname, strlen(firstname), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_entry_set_field(entry, "lastname", lastname, strlen(lastname), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_entry_set_field(entry, "email", email, strlen(email), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_entry_set_field(entry, "address", address, strlen(address), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_entry_set_field(entry, "city", city, strlen(city), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_entry_set_field(entry, "state", state, strlen(state), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_entry_set_field(entry, "zip", &zip, sizeof(zip), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_entry_insert(entry, batch, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_batch_execute(batch);
g_assert_true(success);
}
/*
* I want to run the following query;
* Query 1: SELECT _id, "city", "customerid", "state", "firstname", "lastname", "zip", "address", "email" FROM "namespace_customersTable"
* WHERE ( ("customerid" = 3 AND "zip" = 22121) OR ( "customerid" = 1 AND "zip" = 22902 );
*
* I could not find any way to achieve it and end up with the following query;
* Query 2: SELECT _id, "city", "customerid", "state", "firstname", "lastname", "zip", "address", "email" FROM "namespace_customersTable"
* WHERE ( "customerid" = 3 OR "zip" = 22121 OR ( "customerid" = 1 AND "zip" = 22902 ));
*
* Expected Output (Query 1):
* No rows
*
* Actual Output (Query 2):
* 1|Mount Vernon|1|VA|George|Washington|22121|3200 Mt Vernon Hwy|gwashington@usa.gov
* 3|Charlottesville|3|VA|Thomas|Jefferson|22902|931 Thomas Jefferson Pkwy|tjefferson@usa.gov
*/
static void
try_or_operator_between_two_conditionsWithANDOperator(void)
{
g_autoptr(GError) error = NULL;
gboolean success = TRUE;
g_autoptr(JDBSchema) schema_customersTable = NULL;
g_autoptr(JDBSelector) selector_customersTable = NULL;
g_autoptr(JDBSelector) selector2_customersTable = NULL;
g_autoptr(JDBIterator) iterator = NULL;
g_autoptr(JBatch) batch = j_batch_new_for_template(J_SEMANTICS_TEMPLATE_DEFAULT);
guint64 custid_1 = 3;
guint64 zip_1 = 22121;
guint64 custid_2 = 1;
guint64 zip_2 = 22902;
JDBType type;
guint64 len;
schema_customersTable = j_db_schema_new("namespace", "customersTable", &error);
g_assert_nonnull(schema_customersTable);
g_assert_no_error(error);
success = j_db_schema_get(schema_customersTable, batch, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_batch_execute(batch);
g_assert_true(success);
selector_customersTable = j_db_selector_new(schema_customersTable, J_DB_SELECTOR_MODE_OR, &error);
g_assert_nonnull(selector_customersTable);
g_assert_no_error(error);
success = j_db_selector_add_field(selector_customersTable, "customerid", J_DB_SELECTOR_OPERATOR_EQ, &custid_1, sizeof(custid_1), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_selector_add_field(selector_customersTable, "zip", J_DB_SELECTOR_OPERATOR_EQ, &zip_1, sizeof(zip_1), &error);
g_assert_true(success);
g_assert_no_error(error);
selector2_customersTable = j_db_selector_new(schema_customersTable, J_DB_SELECTOR_MODE_AND, &error);
g_assert_nonnull(selector2_customersTable);
g_assert_no_error(error);
success = j_db_selector_add_field(selector2_customersTable, "customerid", J_DB_SELECTOR_OPERATOR_EQ, &custid_2, sizeof(custid_2), &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_selector_add_field(selector2_customersTable, "zip", J_DB_SELECTOR_OPERATOR_EQ, &zip_2, sizeof(zip_2), &error);
g_assert_true(success);
g_assert_no_error(error);
j_db_selector_add_selector(selector_customersTable, selector2_customersTable, NULL);
iterator = j_db_iterator_new(schema_customersTable, selector_customersTable, &error);
g_assert_nonnull(iterator);
g_assert_no_error(error);
j_db_iterator_next(iterator, NULL);
{
g_assert_null(iterator);
/*g_autofree gchar* firstname = NULL;
g_autofree gchar* lastname = NULL;
success = j_db_iterator_get_field(iterator, "firstname", &type, (gpointer*)&firstname, &len, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_db_iterator_get_field(iterator, "lastname", &type, (gpointer*)&lastname, &len, &error);
g_assert_true(success);
g_assert_no_error(error);
g_assert_cmpstr(firstname, ==, "George");
g_assert_cmpstr(lastname, ==, "Washington");
*/
}
}
static void
schema_delete_customersTable(void)
{
g_autoptr(GError) error = NULL;
gboolean success;
g_autoptr(JDBSchema) schema = NULL;
g_autoptr(JBatch) batch = j_batch_new_for_template(J_SEMANTICS_TEMPLATE_DEFAULT);
schema = j_db_schema_new("namespace", "customersTable", &error);
g_assert_nonnull(schema);
g_assert_no_error(error);
success = j_db_schema_delete(schema, batch, &error);
g_assert_true(success);
g_assert_no_error(error);
success = j_batch_execute(batch);
g_assert_true(success);
}
static void
test_or_operator_between_two_conditionsWithANDOperator(void)
{
schema_create_customersTable();
entry_insert_customersTable(1, "George", "Washington", "gwashington@usa.gov", "3200 Mt Vernon Hwy", "Mount Vernon", "VA", 22121);
entry_insert_customersTable(2, "John", "Adams", "jadams@usa.gov", "1250 Hancock St", "Quincy", "MA", 12169);
entry_insert_customersTable(3, "Thomas", "Jefferson", "tjefferson@usa.gov", "931 Thomas Jefferson Pkwy", "Charlottesville", "VA", 22902);
entry_insert_customersTable(4, "James", "Madison", "jmadison@usa.gov", "11350 Constitution Hwy", "Orange", "VA", 22960);
entry_insert_customersTable(5, "James", "Monroe", "jmonroe@usa.gov", "2050 James Monroe Pkwy", "Charlottesville", "VA", 22902);
try_or_operator_between_two_conditionsWithANDOperator();
//schema_delete_customersTable();
}
void
test_db_operators(void)
{
g_test_add_func("/db/operators/or_operator_1", test_or_operator_between_two_conditionsWithANDOperator);
}