OpenDNSSEC-enforcer  1.3.14
dq_string.c
Go to the documentation of this file.
1 /*
2  * $Id: dq_string.c 3776 2010-08-24 14:55:39Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * dq_string.c - Database QUERY String
31  *
32  * Description:
33  * Holds miscellaneous utility functions used when constructing queries
34  * (SELECT) of the KSM database.
35 -*/
36 
37 #include <stdio.h>
38 
39 #include "ksm/ksm.h"
40 #include "ksm/database_statement.h"
41 #include "ksm/string_util.h"
42 #include "ksm/string_util2.h"
43 
44 
45 
46 /*+
47  * DqsInit - Create Basic Query - DEPRECATED
48  *
49  * Description:
50  * Creates the basic query string comprising:
51  *
52  * SELECT * FROM <table>
53  *
54  * Arguments:
55  * const char* table
56  * Name of the table from where the data is retrieved.
57  *
58  * Returns:
59  * char*
60  * Query string. This must be freed via a call to DqsFree
61 -*/
62 
63 char* DqsInit(const char* table)
64 {
65  char* query;
66 
67  query = StrStrdup("SELECT * FROM ");
68  StrAppend(&query, table);
69 
70  return query;
71 }
72 
73 
74 
75 /*+
76  * DqsCountInit - Create Basic Count Query
77  *
78  * Description:
79  * Creates the basic query string comprising:
80  *
81  * SELECT COUNT(*) FROM <table>
82  *
83  * Arguments:
84  * const char* table
85  * Name of the table from where the data is retrieved.
86  *
87  * Returns:
88  * const char*
89  * Query string. This must be freed via a call to DqsFree
90 -*/
91 
92 char* DqsCountInit(const char* table)
93 {
94  char* query;
95 
96  query = StrStrdup("SELECT COUNT(*) FROM ");
97  StrAppend(&query, table);
98 
99  return query;
100 }
101 
102 /*+
103  * DqsSpecifyInit - Create Query
104  *
105  * Description:
106  * Creates the basic query string comprising:
107  *
108  * SELECT x, y, z FROM <table>
109  *
110  * Arguments:
111  * const char* table
112  * Name of the table from where the data is retrieved.
113  *
114  * Returns:
115  * char*
116  * Query string. This must be freed via a call to DqsEnd
117 -*/
118 
119 char* DqsSpecifyInit(const char* table, const char* fields)
120 {
121  char* query;
122  char* query1;
123 
124  query = StrStrdup("SELECT ");
125  StrAppend(&query, fields);
126  query1 = StrStrdup(" FROM ");
127  StrAppend(&query, query1);
128  StrAppend(&query, table);
129  StrFree(query1);
130  return query;
131 }
132 
133 /*+
134  * DqsAppendComparison - Append Comparison Operator
135  *
136  * Description:
137  * Depending on the value of the comparsion code, append the appropriate
138  * operator to the string.
139  *
140  * Arguments:
141  * char** query
142  * Query to modify.
143  *
144  * DQS_COMPARISON compare
145  * One of the KSM comparison codes. If invalid, the string " ??"
146  * is appended, which will cause the query to fail.
147 -*/
148 
149 static void DqsAppendComparison(char** query, DQS_COMPARISON compare)
150 {
151  switch (compare) {
152  case DQS_COMPARE_LT:
153  StrAppend(query, " < ");
154  break;
155 
156  case DQS_COMPARE_LE:
157  StrAppend(query, " <= ");
158  break;
159 
160  case DQS_COMPARE_EQ:
161  StrAppend(query, " = ");
162  break;
163 
164  case DQS_COMPARE_NE:
165  StrAppend(query, " != ");
166  break;
167 
168  case DQS_COMPARE_GE:
169  StrAppend(query, " >= ");
170  break;
171 
172  case DQS_COMPARE_GT:
173  StrAppend(query, " > ");
174  break;
175 
176  case DQS_COMPARE_IN:
177  StrAppend(query, " IN ");
178  break;
179 
180  case DQS_COMPARE_NOT_IN:
181  StrAppend(query, " NOT IN ");
182  break;
183 
184  case DQS_COMPARE_IS:
185  StrAppend(query, " IS ");
186  break;
187 
188  default:
189  StrAppend(query, " ?? ");
190  }
191 
192  return;
193 }
194 
195 
196 /*+
197  * DqsConditionInt - Append Integer Condition to Query
198  * DqsConditionString - Append String Condition to Query
199  * DqsConditionKeyword - Append Keyword Condition to Query
200  *
201  * Description:
202  * Appends a condition to the basic query.
203  *
204  * -Int Appends a comparison with an integer
205  * -String Appends a comparison with a string, quoting the string
206  * -Keyword Appends more complicated condition
207  *
208  * Arguments:
209  * char** query
210  * Query to modify.
211  *
212  * const char* field
213  * Name of field to be comparison value
214  *
215  * DQS_COMPARISON compare
216  * Code for the compaison.
217  *
218  * int value/char* value
219  * Value to compare against.
220  *
221  * int index
222  * Condition index. If 0, a WHERE is appended in front of the
223  * condition as it is the first one. Otherwise an AND in appended.
224 -*/
225 
226 void DqsConditionInt(char** query, const char* field, DQS_COMPARISON compare,
227  int value, int index)
228 {
229  char stringval[KSM_INT_STR_SIZE]; /* For Integer to String conversion */
230 
231  StrAppend(query, (index == 0) ? " WHERE " : " AND ");
232  StrAppend(query, field);
233  DqsAppendComparison(query, compare);
234  snprintf(stringval, KSM_INT_STR_SIZE, "%d", value);
235  StrAppend(query, stringval);
236 
237  return;
238 }
239 
240 void DqsConditionString(char** query, const char* field, DQS_COMPARISON compare,
241  const char* value, int index)
242 {
243  StrAppend(query, (index == 0) ? " WHERE " : " AND ");
244  StrAppend(query, field);
245  DqsAppendComparison(query, compare);
246  StrAppend(query, "\"");
247  StrAppend(query, value);
248  StrAppend(query, "\"");
249 
250  return;
251 }
252 
253 void DqsConditionKeyword(char** query, const char* field,
254  DQS_COMPARISON compare, const char* value, int index)
255 {
256  StrAppend(query, (index == 0) ? " WHERE " : " AND ");
257  StrAppend(query, field);
258  DqsAppendComparison(query, compare);
259  StrAppend(query, value);
260 
261  return;
262 }
263 
264 
265 /*+
266  * DqsOrderBy - Add Order By Clause
267  *
268  * Description:
269  * Adds an ORDER BY clause to the query.
270  *
271  * Arguments:
272  * char** query
273  * Query to modify.
274  *
275  * const char* field
276  * Field on which to order.
277 -*/
278 
279 void DqsOrderBy(char** query, const char* field)
280 {
281  StrAppend(query, " ORDER BY ");
282  StrAppend(query, field);
283 
284  return;
285 }
286 
287 
288 /*+
289  * DqsEnd - End Query String Creation
290 
291  *
292  * Description:
293  * Closes down the creation of the query string. At present, this is a
294  * no-op.
295  *
296  * Arguments:
297  * char** query
298  * Query string.
299 -*/
300 
301 void DqsEnd(char** query)
302 {
303  /* Unused parameter */
304  (void)query;
305  return;
306 }
307 
308 
309 
310 /*+
311  * DqsFree - Free Query Resources
312  *
313  * Description:
314  * Frees up resources allocated for the query string.
315  *
316  * Arguments:
317  * char* query
318  * Query string. If not NULL, is freed. On return, the pointer
319  * is invalid.
320 -*/
321 
322 void DqsFree(char* query)
323 {
324  if (query) {
325  StrFree(query);
326  }
327 
328  return;
329 }