OpenDNSSEC-enforcer  1.3.16
di_string.c
Go to the documentation of this file.
1 /*
2  * $Id: di_string.c 731 2009-05-18 08:24:19Z 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  * di_string.c - Database INSERT String
31  *
32  * Description:
33  * Holds miscellaneous utility functions used when constructing SQL INSERT
34  * statements.
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  * DisInit - Create Basic Query
48  *
49  * Description:
50  * Creates the basic sql string comprising:
51  *
52  * INSERT INTO <table> VALUES (NULL,
53  *
54  * The initial insert is due to the fact that the table is assumed to
55  * have as its first column an autonumber field (which is automatically
56  * set when the data is inserted).
57  *
58  * Arguments:
59  * const char* table
60  * Name of the table from where the data is inserted.
61  *
62  * Returns:
63  * char*
64  * Query string. This must be freed via a call to DisEnd
65 -*/
66 
67 char* DisInit(const char* table)
68 {
69  char* sql;
70 
71  sql = StrStrdup("INSERT INTO ");
72  StrAppend(&sql, table);
73  StrAppend(&sql, " VALUES (NULL");
74 
75  return sql;
76 }
77 
78 /*+
79  * DisSpecifyInit - Create Basic Query
80  *
81  * Description:
82  * Creates the basic sql string comprising:
83  *
84  * INSERT INTO <table> VALUES (NULL,
85  *
86  * The initial insert is due to the fact that the table is assumed to
87  * have as its first column an autonumber field (which is automatically
88  * set when the data is inserted).
89  *
90  * Arguments:
91  * const char* table
92  * Name of the table from where the data is inserted.
93  * const char* cols
94  * List of columns that we are inserting into
95  *
96  * Returns:
97  * char*
98  * Query string. This must be freed via a call to DisEnd
99 -*/
100 
101 char* DisSpecifyInit(const char* table, const char* cols)
102 {
103  char* sql;
104 
105  sql = StrStrdup("INSERT INTO ");
106  StrAppend(&sql, table);
107  StrAppend(&sql, " (id, ");
108  StrAppend(&sql, cols);
109  StrAppend(&sql, ")");
110  StrAppend(&sql, " VALUES (NULL");
111 
112  return sql;
113 }
114 
115 
116 /*+
117  * DisAppendInt - Append Integer Field
118  * DisAppendString - Append String Field
119  *
120  * Description:
121  * Appends an integer or string field to the sql.
122  *
123  * Arguments:
124  * char** sql
125  * Query to modify.
126  *
127  * int/const char* what
128  * Data to append. If a string, it is assumed NOT to contain the
129  * apostrophe character. Also, if a string and specified as NULL,
130  * then the keyword NULL is inserted.
131 -*/
132 
133 void DisAppendInt(char** sql, int what)
134 {
135  char buffer[KSM_INT_STR_SIZE]; /* Enough to hold any integer */
136 
137  StrAppend(sql, ", ");
138  snprintf(buffer, KSM_INT_STR_SIZE, "%d", what);
139  StrAppend(sql, buffer);
140 
141  return;
142 }
143 
144 void DisAppendString(char** sql, const char* what)
145 {
146  if (what) {
147  StrAppend(sql, ", '");
148  StrAppend(sql, what); /* TODO make sure 'what' is safe to insert (quote quotes?) */
149  StrAppend(sql, "'");
150  }
151  else {
152  StrAppend(sql, ", NULL");
153  }
154 
155  return;
156 }
157 
158 
159 
160 /*+
161  * DisEnd - End Up SQL Statement
162  *
163  * Description:
164  * Appends the trailing bracket to the SQL sql string.
165  *
166  * Arguments:
167  * char** sql
168  * Query string. If not NULL, is freed. On return, the pointer
169  * is invalid.
170 -*/
171 
172 void DisEnd(char** sql)
173 {
174  StrAppend(sql, ")");
175 
176  return;
177 }
178 
179 
180 
181 /*+
182  * DisFree - Free Query Resources
183  *
184  * Description:
185  * Frees up resources allocated for the sql string.
186  *
187  * Arguments:
188  * char* sql
189  * Query string. If not NULL, is freed. On return, the pointer
190  * is invalid.
191 -*/
192 
193 void DisFree(char* sql)
194 {
195  if (sql) {
196  StrFree(sql);
197  }
198 
199  return;
200 }
#define StrFree(x)
Definition: string_util.h:68
char * DisSpecifyInit(const char *table, const char *cols)
Definition: di_string.c:101
char * StrStrdup(const char *string)
Definition: string_util.c:126
void DisAppendString(char **sql, const char *what)
Definition: di_string.c:144
void DisEnd(char **sql)
Definition: di_string.c:172
void StrAppend(char **str1, const char *str2)
Definition: string_util2.c:78
void DisFree(char *sql)
Definition: di_string.c:193
char * DisInit(const char *table)
Definition: di_string.c:67
#define KSM_INT_STR_SIZE
Definition: ksm.h:67
void DisAppendInt(char **sql, int what)
Definition: di_string.c:133