1 /*
2  * Copyright (c) 2014 Jerry Lundström <lundstrom.jerry@gmail.com>
3  * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
4  * Copyright (c) 2014 OpenDNSSEC AB (svb)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef __db_clause_h
31 #define __db_clause_h
32 
33 /**
34  * The clause operation to make on the value.
35  */
36 typedef enum {
37     /**
38      * Empty, not set or unknown.
39      */
40     DB_CLAUSE_UNKNOWN,
41     /**
42      * ==
43      */
44     DB_CLAUSE_EQUAL,
45     /**
46      * !=
47      */
48     DB_CLAUSE_NOT_EQUAL,
49     /**
50      * <
51      */
52     DB_CLAUSE_LESS_THEN,
53     /**
54      * <=
55      */
56     DB_CLAUSE_LESS_OR_EQUAL,
57     /**
58      * >=
59      */
60     DB_CLAUSE_GREATER_OR_EQUAL,
61     /**
62      * >
63      */
64     DB_CLAUSE_GREATER_THEN,
65     /**
66      * Is null.
67      */
68     DB_CLAUSE_IS_NULL,
69     /**
70      * Is not null.
71      */
72     DB_CLAUSE_IS_NOT_NULL,
73     /**
74      * This adds a nested clause as in wrapping the content with ( ).
75      */
76     DB_CLAUSE_NESTED
77 } db_clause_type_t;
78 
79 #define DB_CLAUSE_EQ DB_CLAUSE_EQUAL
80 #define DB_CLAUSE_NE DB_CLAUSE_NOT_EQUAL
81 #define DB_CLAUSE_LT DB_CLAUSE_LESS_THEN
82 #define DB_CLAUSE_LE DB_CLAUSE_LESS_OR_EQUAL
83 #define DB_CLAUSE_GE DB_CLAUSE_GREATER_OR_EQUAL
84 #define DB_CLAUSE_GT DB_CLAUSE_GREATER_THEN
85 
86 /**
87  * The operator to do between the previous clause and this one.
88  */
89 typedef enum {
90     /**
91      * Empty, not set or unknown.
92      */
93     DB_CLAUSE_OPERATOR_UNKNOWN,
94     /**
95      * ||
96      */
97     DB_CLAUSE_OPERATOR_AND,
98     /**
99      * &&
100      */
101     DB_CLAUSE_OPERATOR_OR
102 } db_clause_operator_t;
103 
104 #define DB_CLAUSE_OP_AND DB_CLAUSE_OPERATOR_AND
105 #define DB_CLAUSE_OP_OR  DB_CLAUSE_OPERATOR_OR
106 
107 struct db_clause;
108 struct db_clause_list;
109 typedef struct db_clause db_clause_t;
110 typedef struct db_clause_list db_clause_list_t;
111 
112 #include "db_value.h"
113 
114 /**
115  * A database clause, describes the comparison of a database object field and a
116  * value.
117  */
118 struct db_clause {
119     db_clause_t* next;
120     char* table;
121     char* field;
122     db_clause_type_t type;
123     db_value_t value;
124     db_clause_operator_t clause_operator;
125     db_clause_list_t* clause_list;
126 };
127 
128 /**
129  * Create a new database clause.
130  * \return a db_clause_t pointer or NULL on error.
131  */
132 extern db_clause_t* db_clause_new(void);
133 
134 /**
135  * Delete a database clause.
136  * \param[in] clause a db_clause_t pointer.
137  */
138 extern void db_clause_free(db_clause_t* clause);
139 
140 /**
141  * Get the field name of a database clause.
142  * \param[in] a db_clause_t pointer.
143  * \return a character pointer or NULL on error or if no field name has been set.
144  */
145 extern const char* db_clause_field(const db_clause_t* clause);
146 
147 /**
148  * Get the database clause type of a database clause.
149  * \param[in] a db_clause_t pointer.
150  * \return a db_clause_type_t.
151  */
152 extern db_clause_type_t db_clause_type(const db_clause_t* clause);
153 
154 /**
155  * Get the database value of a database value.
156  * \param[in] a db_clause_t pointer.
157  * \return a db_value_t pointer or NULL on error.
158  */
159 extern const db_value_t* db_clause_value(const db_clause_t* clause);
160 
161 /**
162  * Get the database clause operator of a database clause.
163  * \param[in] a db_clause_t pointer.
164  * \return a db_clause_operator_t.
165  */
166 extern db_clause_operator_t db_clause_operator(const db_clause_t* clause);
167 
168 /**
169  * Get the database clause list of a database clause, this is used for nested
170  * database clauses.
171  * \param[in] a db_clause_t pointer.
172  * \return a db_clause_list_t pointer or NULL on error or if no database clause
173  * list has been set.
174  */
175 extern const db_clause_list_t* db_clause_list(const db_clause_t* clause);
176 
177 /**
178  * Set the field name of a database clause.
179  * \param[in] a db_clause_t pointer.
180  * \param[in] field a character pointer.
181  * \return DB_ERROR_* on failure, otherwise DB_OK.
182  */
183 extern int db_clause_set_field(db_clause_t* clause, const char* field);
184 
185 /**
186  * Set the database clause type of a database clause.
187  * \param[in] a db_clause_t pointer.
188  * \param[in] type a db_clause_type_t.
189  * \return DB_ERROR_* on failure, otherwise DB_OK.
190  */
191 extern int db_clause_set_type(db_clause_t* clause, db_clause_type_t type);
192 
193 /**
194  * Set the database clause operator of a database clause.
195  * \param[in] a db_clause_t pointer.
196  * \param[in] clause_operator a db_clause_operator_t.
197  * \return DB_ERROR_* on failure, otherwise DB_OK.
198  */
199 extern int db_clause_set_operator(db_clause_t* clause, db_clause_operator_t clause_operator);
200 
201 /**
202  * Check if the database clause is not empty.
203  * \param[in] a db_clause_t pointer.
204  * \return DB_ERROR_* if empty, otherwise DB_OK.
205  */
206 extern int db_clause_not_empty(const db_clause_t* clause);
207 
208 /**
209  * Return the next database clause connected in a database clause list.
210  * \param[in] a db_clause_t pointer.
211  * \return a db_clause_t pointer or NULL on error or if there are no more
212  * database clauses in the list.
213  */
214 extern const db_clause_t* db_clause_next(const db_clause_t* clause);
215 
216 /**
217  * Get the writable database value of a database clause.
218  * \param[in] a db_clause_t pointer.
219  * \return a db_value_t pointer or NULL on error.
220  */
221 extern db_value_t* db_clause_get_value(db_clause_t* clause);
222 
223 /**
224  * A list of database clauses.
225  */
226 struct db_clause_list {
227     db_clause_t* begin;
228     db_clause_t* end;
229 };
230 
231 /**
232  * Create a new database clause list.
233  * \return a db_clause_list_t pointer or NULL on error.
234  */
235 extern db_clause_list_t* db_clause_list_new(void);
236 
237 /**
238  * Delete a database clause list and all database clauses in the list.
239  * \param[in] clause_list a db_clause_list_t pointer.
240  */
241 extern void db_clause_list_free(db_clause_list_t* clause_list);
242 
243 /**
244  * Add a database clause to a database clause list, this takes over the
245  * ownership of the database clause.
246  * \param[in] clause_list a db_clause_list_t pointer.
247  * \param[in] a db_clause_t pointer.
248  * \return DB_ERROR_* on failure, otherwise DB_OK.
249  */
250 extern int db_clause_list_add(db_clause_list_t* clause_list, db_clause_t* clause);
251 
252 /**
253  * Return the first database clause of a database clause list.
254  * \param[in] clause_list a db_clause_list_t pointer.
255  * \return a db_clause_t pointer or NULL on error or if the list is empty.
256  */
257 extern const db_clause_t* db_clause_list_begin(const db_clause_list_t* clause_list);
258 
259 #endif
260