OpenDNSSEC-enforcer 1.3.0
/build/buildd/opendnssec-1.3.0/enforcer/ksm/du_string.c
Go to the documentation of this file.
00001 /*
00002  * $Id: du_string.c 5320 2011-07-12 10:42:26Z jakob $
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  * du_string.c - Database UPDATE String
00031  *
00032  * Description:
00033  *      Holds miscellaneous utility functions used when constructing SQL UPDATE
00034  *      statments of the KSM database.
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  * DusInit - Create Basic Update
00047  *
00048  * Description:
00049  *      Creates the basic sql string comprising:
00050  *
00051  *          UPDATE <table> SET
00052  *
00053  * Arguments:
00054  *      const char* table
00055  *          Name of the table from where the data is inserted.
00056  *
00057  * Returns:
00058  *      char*
00059  *          Query string.  This must be freed via a call to DusEnd
00060 -*/
00061 
00062 char* DusInit(const char* table)
00063 {
00064     char*   sql;
00065 
00066     sql = StrStrdup("UPDATE ");
00067     StrAppend(&sql, table);
00068     StrAppend(&sql, " SET ");
00069 
00070     return sql;
00071 }
00072 
00073 
00074 /*+
00075  * DusSetInt - Integer Set
00076  * DusSetString - String Set
00077  *
00078  * Description:
00079  *      Appends an integer or string field to the sql of the form:
00080  *
00081  *          keyword = value
00082  *
00083  * Arguments:
00084  *      char** sql
00085  *          Query to modify.
00086  *
00087  *      const char* field
00088  *          Field to modify.
00089  *
00090  *      int/const char* data
00091  *          Data to append.  If a string, it is assumed NOT to contain the
00092  *          apostrophe character. Also, if a string and specified as NULL,
00093  *          then the keyword NULL is inserted.
00094  *
00095  *      int clause
00096  *          If 0, no comma is prepended to the string.
00097 -*/
00098 
00099 void DusSetInt(char** sql, const char* field, int data, int clause)
00100 {
00101     char    buffer[KSM_INT_STR_SIZE];        /* Enough to hold any integer */
00102 
00103     if (clause) {
00104         StrAppend(sql, ", ");
00105     }
00106     StrAppend(sql, field);
00107     StrAppend(sql, " = ");
00108 
00109     snprintf(buffer, KSM_INT_STR_SIZE, "%d", data);
00110     StrAppend(sql, buffer);
00111 
00112     return;
00113 }
00114 
00115 void DusSetString(char** sql, const char* field, const char* data, int clause)
00116 {
00117     if (clause) {
00118         StrAppend(sql, ", ");
00119     }
00120 
00121     StrAppend(sql, field);
00122     StrAppend(sql, " = ");
00123 
00124     if (data) {
00125         StrAppend(sql, "\"");
00126         StrAppend(sql, data);
00127         StrAppend(sql, "\"");
00128     }
00129     else {
00130         StrAppend(sql, "NULL");
00131     }
00132 
00133     return;
00134 }
00135 
00136 
00137 /*+
00138  * DusConditionInt - Append Integer Condition to Query
00139  * DusConditionString - Append String Condition to Query
00140  * DusConditionKeyword - Append Keyword Condition to Query
00141  *
00142  * Description:
00143  *      Appends a condition to the basic query.
00144  *
00145  *      -Int        Appends a comparison with an integer
00146  *      -String     Appends a comparison with a string, quoting the string
00147  *      -Keyword    Appends more complicated condition
00148  *
00149  *      Note: These simply call the corresponding Dqs functions.
00150  *
00151  * Arguments:
00152  *      char** query
00153  *          Query to modify.
00154  *
00155  *      const char* field
00156  *          Name of field to be comparison value
00157  *
00158  *      DQS_COMPARISON compare
00159  *          Code for the compaison.
00160  *
00161  *      int value/char* value
00162  *          Value to compare against.
00163  *
00164  *      int clause
00165  *          Condition clause.  If 0, a WHERE is appended in front of the
00166  *          condition as it is the first one.  Otherwise an AND in appended.
00167  *
00168  *          N.B. This is a different variable to the clause in the DusSetXxx
00169  *          functions.
00170 -*/
00171 
00172 void DusConditionInt(char** query, const char* field, DQS_COMPARISON compare,
00173     int value, int clause)
00174 {
00175     DqsConditionInt(query, field, compare, value, clause);
00176 }
00177 
00178 void DusConditionString(char** query, const char* field, DQS_COMPARISON compare,
00179     const char* value, int clause)
00180 {
00181     DqsConditionString(query, field, compare, value, clause);
00182 }
00183 
00184 void DusConditionKeyword(char** query, const char* field,
00185     DQS_COMPARISON compare, const char* value, int clause)
00186 {
00187     DqsConditionKeyword(query, field, compare, value, clause);
00188 }
00189 
00190 
00191 
00192 /*+
00193  * DusEnd - End Up SQL Statement
00194  *
00195  * Description:
00196  *      Appends the trailing bracket to the SQL sql string.
00197  *
00198  * Arguments:
00199  *      char** sql
00200  *          Query string.  If not NULL, is freed.  On return, the pointer
00201  *          is invalid. ???
00202 -*/
00203 
00204 void DusEnd(char** sql)
00205 {
00206     /* Unused parameter */
00207     (void)sql;
00208     return;
00209 }
00210 
00211 
00212 
00213 /*+
00214  * DusFree - Free Query Resources
00215  *
00216  * Description:
00217  *      Frees up resources allocated for the sql string.
00218  *
00219  * Arguments:
00220  *      char* sql
00221  *          Query string.  If not NULL, is freed.  On return, the pointer
00222  *          is invalid.
00223 -*/
00224 
00225 void DusFree(char* sql)
00226 {
00227     if (sql) {
00228         StrFree(sql);
00229     }
00230 
00231     return;
00232 }