OpenDNSSEC-enforcer  1.4.5
du_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  * du_string.c - Database UPDATE String
29  *
30  * Description:
31  * Holds miscellaneous utility functions used when constructing SQL UPDATE
32  * statments 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  * DusInit - Create Basic Update
45  *
46  * Description:
47  * Creates the basic sql string comprising:
48  *
49  * UPDATE <table> SET
50  *
51  * Arguments:
52  * const char* table
53  * Name of the table from where the data is inserted.
54  *
55  * Returns:
56  * char*
57  * Query string. This must be freed via a call to DusEnd
58 -*/
59 
60 char* DusInit(const char* table)
61 {
62  char* sql;
63 
64  sql = StrStrdup("UPDATE ");
65  StrAppend(&sql, table);
66  StrAppend(&sql, " SET ");
67 
68  return sql;
69 }
70 
71 
72 /*+
73  * DusSetInt - Integer Set
74  * DusSetString - String Set
75  *
76  * Description:
77  * Appends an integer or string field to the sql of the form:
78  *
79  * keyword = value
80  *
81  * Arguments:
82  * char** sql
83  * Query to modify.
84  *
85  * const char* field
86  * Field to modify.
87  *
88  * int/const char* data
89  * Data to append. If a string, it is assumed NOT to contain the
90  * apostrophe character. Also, if a string and specified as NULL,
91  * then the keyword NULL is inserted.
92  *
93  * int clause
94  * If 0, no comma is prepended to the string.
95 -*/
96 
97 void DusSetInt(char** sql, const char* field, int data, int clause)
98 {
99  char buffer[KSM_INT_STR_SIZE]; /* Enough to hold any integer */
100 
101  if (clause) {
102  StrAppend(sql, ", ");
103  }
104  StrAppend(sql, field);
105  StrAppend(sql, " = ");
106 
107  snprintf(buffer, KSM_INT_STR_SIZE, "%d", data);
108  StrAppend(sql, buffer);
109 
110  return;
111 }
112 
113 void DusSetString(char** sql, const char* field, const char* data, int clause)
114 {
115  if (clause) {
116  StrAppend(sql, ", ");
117  }
118 
119  StrAppend(sql, field);
120  StrAppend(sql, " = ");
121 
122  if (data) {
123  StrAppend(sql, "'");
124  StrAppend(sql, data);
125  StrAppend(sql, "'");
126  }
127  else {
128  StrAppend(sql, "NULL");
129  }
130 
131  return;
132 }
133 
134 
135 /*+
136  * DusConditionInt - Append Integer Condition to Query
137  * DusConditionString - Append String Condition to Query
138  * DusConditionKeyword - Append Keyword Condition to Query
139  *
140  * Description:
141  * Appends a condition to the basic query.
142  *
143  * -Int Appends a comparison with an integer
144  * -String Appends a comparison with a string, quoting the string
145  * -Keyword Appends more complicated condition
146  *
147  * Note: These simply call the corresponding Dqs functions.
148  *
149  * Arguments:
150  * char** query
151  * Query to modify.
152  *
153  * const char* field
154  * Name of field to be comparison value
155  *
156  * DQS_COMPARISON compare
157  * Code for the compaison.
158  *
159  * int value/char* value
160  * Value to compare against.
161  *
162  * int clause
163  * Condition clause. If 0, a WHERE is appended in front of the
164  * condition as it is the first one. Otherwise an AND in appended.
165  *
166  * N.B. This is a different variable to the clause in the DusSetXxx
167  * functions.
168 -*/
169 
170 void DusConditionInt(char** query, const char* field, DQS_COMPARISON compare,
171  int value, int clause)
172 {
173  DqsConditionInt(query, field, compare, value, clause);
174 }
175 
176 void DusConditionString(char** query, const char* field, DQS_COMPARISON compare,
177  const char* value, int clause)
178 {
179  DqsConditionString(query, field, compare, value, clause);
180 }
181 
182 void DusConditionKeyword(char** query, const char* field,
183  DQS_COMPARISON compare, const char* value, int clause)
184 {
185  DqsConditionKeyword(query, field, compare, value, clause);
186 }
187 
188 
189 
190 /*+
191  * DusEnd - End Up SQL Statement
192  *
193  * Description:
194  * Appends the trailing bracket to the SQL sql string.
195  *
196  * Arguments:
197  * char** sql
198  * Query string. If not NULL, is freed. On return, the pointer
199  * is invalid. ???
200 -*/
201 
202 void DusEnd(char** sql)
203 {
204  /* Unused parameter */
205  (void)sql;
206  return;
207 }
208 
209 
210 
211 /*+
212  * DusFree - Free Query Resources
213  *
214  * Description:
215  * Frees up resources allocated for the sql string.
216  *
217  * Arguments:
218  * char* sql
219  * Query string. If not NULL, is freed. On return, the pointer
220  * is invalid.
221 -*/
222 
223 void DusFree(char* sql)
224 {
225  if (sql) {
226  StrFree(sql);
227  }
228 
229  return;
230 }