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