OpenDNSSEC-enforcer  1.3.4
/build/buildd/opendnssec-1.3.4/enforcer/ksm/dq_string.c
Go to the documentation of this file.
00001 /*
00002  * $Id: dq_string.c 3776 2010-08-24 14:55:39Z 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  * dq_string.c - Database QUERY String
00031  *
00032  * Description:
00033  *      Holds miscellaneous utility functions used when constructing queries
00034  *      (SELECT) 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 /*+
00047  * DqsInit - Create Basic Query - DEPRECATED
00048  *
00049  * Description:
00050  *      Creates the basic query string comprising:
00051  *
00052  *          SELECT * FROM <table>
00053  *
00054  * Arguments:
00055  *      const char* table
00056  *          Name of the table from where the data is retrieved.
00057  *
00058  * Returns:
00059  *      char*
00060  *          Query string.  This must be freed via a call to DqsFree
00061 -*/
00062 
00063 char* DqsInit(const char* table)
00064 {
00065     char*   query;
00066 
00067     query = StrStrdup("SELECT * FROM ");
00068     StrAppend(&query, table);
00069 
00070     return query;
00071 }
00072 
00073 
00074 
00075 /*+
00076  * DqsCountInit - Create Basic Count Query
00077  *
00078  * Description:
00079  *      Creates the basic query string comprising:
00080  *
00081  *          SELECT COUNT(*) FROM <table>
00082  *
00083  * Arguments:
00084  *      const char* table
00085  *          Name of the table from where the data is retrieved.
00086  *
00087  * Returns:
00088  *      const char*
00089  *          Query string.  This must be freed via a call to DqsFree
00090 -*/
00091 
00092 char* DqsCountInit(const char* table)
00093 {
00094     char*   query;
00095 
00096     query = StrStrdup("SELECT COUNT(*) FROM ");
00097     StrAppend(&query, table);
00098 
00099     return query;
00100 }
00101 
00102 /*+
00103  * DqsSpecifyInit - Create Query
00104  *
00105  * Description:
00106  *      Creates the basic query string comprising:
00107  *
00108  *          SELECT x, y, z FROM <table>
00109  *
00110  * Arguments:
00111  *      const char* table
00112  *          Name of the table from where the data is retrieved.
00113  *
00114  * Returns:
00115  *      char*
00116  *          Query string.  This must be freed via a call to DqsEnd
00117 -*/
00118 
00119 char* DqsSpecifyInit(const char* table, const char* fields)
00120 {
00121     char*   query;
00122     char* query1;
00123 
00124     query = StrStrdup("SELECT ");
00125     StrAppend(&query, fields);
00126     query1 = StrStrdup(" FROM ");
00127     StrAppend(&query, query1);
00128     StrAppend(&query, table);
00129     StrFree(query1);
00130     return query;
00131 }
00132 
00133 /*+
00134  * DqsAppendComparison - Append Comparison Operator
00135  *
00136  * Description:
00137  *      Depending on the value of the comparsion code, append the appropriate
00138  *      operator to the string.
00139  *
00140  * Arguments:
00141  *      char** query
00142  *          Query to modify.
00143  *
00144  *      DQS_COMPARISON compare
00145  *          One of the KSM comparison codes.  If invalid, the string " ??"
00146  *          is appended, which will cause the query to fail.
00147 -*/
00148 
00149 static void DqsAppendComparison(char** query, DQS_COMPARISON compare)
00150 {
00151     switch (compare) {
00152     case DQS_COMPARE_LT:
00153         StrAppend(query, " < ");
00154         break;
00155 
00156     case DQS_COMPARE_LE:
00157         StrAppend(query, " <= ");
00158         break;
00159 
00160     case DQS_COMPARE_EQ:
00161         StrAppend(query, " = ");
00162         break;
00163 
00164     case DQS_COMPARE_NE:
00165         StrAppend(query, " != ");
00166         break;
00167 
00168     case DQS_COMPARE_GE:
00169         StrAppend(query, " >= ");
00170         break;
00171 
00172     case DQS_COMPARE_GT:
00173         StrAppend(query, " > ");
00174         break;
00175 
00176     case DQS_COMPARE_IN:
00177         StrAppend(query, " IN ");
00178         break;
00179 
00180     case DQS_COMPARE_NOT_IN:
00181         StrAppend(query, " NOT IN ");
00182         break;
00183 
00184     case DQS_COMPARE_IS:
00185         StrAppend(query, " IS ");
00186         break;
00187 
00188     default:
00189         StrAppend(query, " ?? ");
00190     }
00191 
00192     return;
00193 }
00194 
00195 
00196 /*+
00197  * DqsConditionInt - Append Integer Condition to Query
00198  * DqsConditionString - Append String Condition to Query
00199  * DqsConditionKeyword - Append Keyword Condition to Query
00200  *
00201  * Description:
00202  *      Appends a condition to the basic query.
00203  *
00204  *      -Int        Appends a comparison with an integer
00205  *      -String     Appends a comparison with a string, quoting the string
00206  *      -Keyword    Appends more complicated condition
00207  *
00208  * Arguments:
00209  *      char** query
00210  *          Query to modify.
00211  *
00212  *      const char* field
00213  *          Name of field to be comparison value
00214  *
00215  *      DQS_COMPARISON compare
00216  *          Code for the compaison.
00217  *
00218  *      int value/char* value
00219  *          Value to compare against.
00220  *
00221  *      int index
00222  *          Condition index.  If 0, a WHERE is appended in front of the
00223  *          condition as it is the first one.  Otherwise an AND in appended.
00224 -*/
00225 
00226 void DqsConditionInt(char** query, const char* field, DQS_COMPARISON compare,
00227     int value, int index)
00228 {
00229     char    stringval[KSM_INT_STR_SIZE];  /* For Integer to String conversion */
00230 
00231     StrAppend(query, (index == 0) ? " WHERE " : " AND ");
00232     StrAppend(query, field);
00233     DqsAppendComparison(query, compare);
00234     snprintf(stringval, KSM_INT_STR_SIZE, "%d", value);
00235     StrAppend(query, stringval);
00236 
00237     return;
00238 }
00239 
00240 void DqsConditionString(char** query, const char* field, DQS_COMPARISON compare,
00241     const char* value, int index)
00242 {
00243     StrAppend(query, (index == 0) ? " WHERE " : " AND ");
00244     StrAppend(query, field);
00245     DqsAppendComparison(query, compare);
00246     StrAppend(query, "\"");
00247     StrAppend(query, value);
00248     StrAppend(query, "\"");
00249 
00250     return;
00251 }
00252 
00253 void DqsConditionKeyword(char** query, const char* field,
00254     DQS_COMPARISON compare, const char* value, int index)
00255 {
00256     StrAppend(query, (index == 0) ? " WHERE " : " AND ");
00257     StrAppend(query, field);
00258     DqsAppendComparison(query, compare);
00259     StrAppend(query, value);
00260 
00261     return;
00262 }
00263 
00264 
00265 /*+
00266  * DqsOrderBy - Add Order By Clause
00267  *
00268  * Description:
00269  *      Adds an ORDER BY clause to the query.
00270  *
00271  * Arguments:
00272  *      char** query
00273  *          Query to modify.
00274  *
00275  *      const char* field
00276  *          Field on which to order.
00277 -*/
00278 
00279 void DqsOrderBy(char** query, const char* field)
00280 {
00281     StrAppend(query, " ORDER BY ");
00282     StrAppend(query, field);
00283 
00284     return;
00285 }
00286 
00287 
00288 /*+
00289  * DqsEnd - End Query String Creation
00290 
00291  *
00292  * Description:
00293  *      Closes down the creation of the query string.  At present, this is a
00294  *      no-op.
00295  *
00296  * Arguments:
00297  *      char** query
00298  *          Query string.
00299 -*/
00300 
00301 void DqsEnd(char** query)
00302 {
00303     /* Unused parameter */
00304     (void)query;
00305     return;
00306 }
00307 
00308 
00309 
00310 /*+
00311  * DqsFree - Free Query Resources
00312  *
00313  * Description:
00314  *      Frees up resources allocated for the query string.
00315  *
00316  * Arguments:
00317  *      char* query
00318  *          Query string.  If not NULL, is freed.  On return, the pointer
00319  *          is invalid.
00320 -*/
00321 
00322 void DqsFree(char* query)
00323 {
00324     if (query) {
00325         StrFree(query);
00326     }
00327 
00328     return;
00329 }