OpenDNSSEC-enforcer
1.3.8
|
00001 /* 00002 * $Id: di_string.c 731 2009-05-18 08:24:19Z sion $ 00003 * 00004 * Copyright (c) 2008-2009 Nominet UK. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00029 /*+ 00030 * di_string.c - Database INSERT String 00031 * 00032 * Description: 00033 * Holds miscellaneous utility functions used when constructing SQL INSERT 00034 * statements. 00035 -*/ 00036 00037 #include <stdio.h> 00038 00039 #include "ksm/ksm.h" 00040 #include "ksm/database_statement.h" 00041 #include "ksm/string_util.h" 00042 #include "ksm/string_util2.h" 00043 00044 00045 00046 /*+ 00047 * DisInit - Create Basic Query 00048 * 00049 * Description: 00050 * Creates the basic sql string comprising: 00051 * 00052 * INSERT INTO <table> VALUES (NULL, 00053 * 00054 * The initial insert is due to the fact that the table is assumed to 00055 * have as its first column an autonumber field (which is automatically 00056 * set when the data is inserted). 00057 * 00058 * Arguments: 00059 * const char* table 00060 * Name of the table from where the data is inserted. 00061 * 00062 * Returns: 00063 * char* 00064 * Query string. This must be freed via a call to DisEnd 00065 -*/ 00066 00067 char* DisInit(const char* table) 00068 { 00069 char* sql; 00070 00071 sql = StrStrdup("INSERT INTO "); 00072 StrAppend(&sql, table); 00073 StrAppend(&sql, " VALUES (NULL"); 00074 00075 return sql; 00076 } 00077 00078 /*+ 00079 * DisSpecifyInit - Create Basic Query 00080 * 00081 * Description: 00082 * Creates the basic sql string comprising: 00083 * 00084 * INSERT INTO <table> VALUES (NULL, 00085 * 00086 * The initial insert is due to the fact that the table is assumed to 00087 * have as its first column an autonumber field (which is automatically 00088 * set when the data is inserted). 00089 * 00090 * Arguments: 00091 * const char* table 00092 * Name of the table from where the data is inserted. 00093 * const char* cols 00094 * List of columns that we are inserting into 00095 * 00096 * Returns: 00097 * char* 00098 * Query string. This must be freed via a call to DisEnd 00099 -*/ 00100 00101 char* DisSpecifyInit(const char* table, const char* cols) 00102 { 00103 char* sql; 00104 00105 sql = StrStrdup("INSERT INTO "); 00106 StrAppend(&sql, table); 00107 StrAppend(&sql, " (id, "); 00108 StrAppend(&sql, cols); 00109 StrAppend(&sql, ")"); 00110 StrAppend(&sql, " VALUES (NULL"); 00111 00112 return sql; 00113 } 00114 00115 00116 /*+ 00117 * DisAppendInt - Append Integer Field 00118 * DisAppendString - Append String Field 00119 * 00120 * Description: 00121 * Appends an integer or string field to the sql. 00122 * 00123 * Arguments: 00124 * char** sql 00125 * Query to modify. 00126 * 00127 * int/const char* what 00128 * Data to append. If a string, it is assumed NOT to contain the 00129 * apostrophe character. Also, if a string and specified as NULL, 00130 * then the keyword NULL is inserted. 00131 -*/ 00132 00133 void DisAppendInt(char** sql, int what) 00134 { 00135 char buffer[KSM_INT_STR_SIZE]; /* Enough to hold any integer */ 00136 00137 StrAppend(sql, ", "); 00138 snprintf(buffer, KSM_INT_STR_SIZE, "%d", what); 00139 StrAppend(sql, buffer); 00140 00141 return; 00142 } 00143 00144 void DisAppendString(char** sql, const char* what) 00145 { 00146 if (what) { 00147 StrAppend(sql, ", '"); 00148 StrAppend(sql, what); /* TODO make sure 'what' is safe to insert (quote quotes?) */ 00149 StrAppend(sql, "'"); 00150 } 00151 else { 00152 StrAppend(sql, ", NULL"); 00153 } 00154 00155 return; 00156 } 00157 00158 00159 00160 /*+ 00161 * DisEnd - End Up SQL Statement 00162 * 00163 * Description: 00164 * Appends the trailing bracket to the SQL sql string. 00165 * 00166 * Arguments: 00167 * char** sql 00168 * Query string. If not NULL, is freed. On return, the pointer 00169 * is invalid. 00170 -*/ 00171 00172 void DisEnd(char** sql) 00173 { 00174 StrAppend(sql, ")"); 00175 00176 return; 00177 } 00178 00179 00180 00181 /*+ 00182 * DisFree - Free Query Resources 00183 * 00184 * Description: 00185 * Frees up resources allocated for the sql string. 00186 * 00187 * Arguments: 00188 * char* sql 00189 * Query string. If not NULL, is freed. On return, the pointer 00190 * is invalid. 00191 -*/ 00192 00193 void DisFree(char* sql) 00194 { 00195 if (sql) { 00196 StrFree(sql); 00197 } 00198 00199 return; 00200 }