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