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