00001 /* 00002 * $Id: db_ut.h 5159 2008-11-03 17:22:27Z henningw $ 00003 * 00004 * Copyright (C) 2001-2003 FhG Fokus 00005 * Copyright (C) 2007-2008 1&1 Internet AG 00006 * 00007 * This file is part of Kamailio, a free SIP server. 00008 * 00009 * Kamailio is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version 00013 * 00014 * Kamailio is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 /** 00025 * \file db/db_ut.h 00026 * \brief Utility functions for database drivers. 00027 * 00028 * This utility methods are used from the database SQL driver to convert 00029 * values and print SQL queries from the internal API representation. 00030 * \ingroup db 00031 */ 00032 00033 #ifndef DB_UT_H 00034 #define DB_UT_H 00035 00036 /** 00037 * maximal SQL buffer length for database drivers 00038 */ 00039 #define SQL_BUF_LEN 65536 00040 00041 /** 00042 * make strptime available 00043 * use 600 for 'Single UNIX Specification, Version 3' 00044 * _XOPEN_SOURCE creates conflict in swab definition in Solaris 00045 */ 00046 #ifndef __OS_solaris 00047 #define _XOPEN_SOURCE 600 /* glibc2 on linux, bsd */ 00048 #else 00049 #define _XOPEN_SOURCE_EXTENDED 1 /* solaris */ 00050 #endif 00051 00052 #include <time.h> 00053 00054 #undef _XOPEN_SOURCE 00055 #undef _XOPEN_SOURCE_EXTENDED 00056 00057 #include "db_key.h" 00058 #include "db.h" 00059 00060 00061 /** 00062 * Converts a char into an integer value. 00063 * 00064 * \param _s source value 00065 * \param _v target value 00066 * \return zero on sucess, negative on conversion errors 00067 */ 00068 int db_str2int(const char* _s, int* _v); 00069 00070 00071 /** 00072 * Converts a char into an long long value. 00073 * 00074 * \param _s source value 00075 * \param _v target value 00076 * \return zero on sucess, negative on conversion errors 00077 */ 00078 int db_str2longlong(const char* _s, long long* _v); 00079 00080 00081 /** 00082 * Converts a char into a double value. 00083 * 00084 * \param _s source value 00085 * \param _v target value 00086 * \return zero on sucess, negative on conversion errors 00087 */ 00088 int db_str2double(const char* _s, double* _v); 00089 00090 00091 /** 00092 * Converts a integer value in a char pointer. 00093 * 00094 * \param _v source value 00095 * \param _s target value 00096 * \param _l available length and target length 00097 * \return zero on sucess, negative on conversion errors 00098 */ 00099 int db_int2str(int _v, char* _s, int* _l); 00100 00101 00102 /** 00103 * Converts a long long value in a char pointer. 00104 * 00105 * \param _v source value 00106 * \param _s target value 00107 * \param _l available length and target length 00108 * \return zero on sucess, negative on conversion errors 00109 */ 00110 int db_longlong2str(long long _v, char* _s, int* _l); 00111 00112 00113 /** 00114 * Converts a double value into a char pointer. 00115 * 00116 * \param _v source value 00117 * \param _s target value 00118 * \param _l available length and target length 00119 * \return zero on sucess, negative on conversion errors 00120 */ 00121 int db_double2str(double _v, char* _s, int* _l); 00122 00123 00124 /** 00125 * Convert a time_t value to string. 00126 * 00127 * \param _v source value 00128 * \param _s target value 00129 * \param _l available length and target length 00130 * \return zero on sucess, negative on conversion errors 00131 * \todo This functions add quotes to the time value. This 00132 * should be done in the val2str function, as some databases 00133 * like db_berkeley don't need or like this at all. 00134 */ 00135 int db_time2str(time_t _v, char* _s, int* _l); 00136 00137 00138 /** 00139 * Converts a char into a time_t value. 00140 * 00141 * \param _s source value 00142 * \param _v target value 00143 * \return zero on sucess, negative on conversion errors 00144 */ 00145 int db_str2time(const char* _s, time_t* _v); 00146 00147 00148 /** 00149 * Print columns for a SQL statement, separated by commas. 00150 * 00151 * \param _b target char 00152 * \param _l length of the target 00153 * \param _c keys that should be printed 00154 * \param _n number of keys 00155 * \return the length of the printed result on success, negative on errors 00156 */ 00157 int db_print_columns(char* _b, const int _l, const db_key_t* _c, const int _n); 00158 00159 00160 /** 00161 * Print values for a SQL statement. 00162 * 00163 * \param _c structure representing database connection 00164 * \param _b target char 00165 * \param _l length of the target 00166 * \param _v values that should be printed 00167 * \param _n number of values 00168 * \param (*val2str) function pointer to a db specific conversion function 00169 * \return the length of the printed result on success, negative on errors 00170 */ 00171 int db_print_values(const db_con_t* _c, char* _b, const int _l, const db_val_t* _v, 00172 const int _n, int (*val2str)(const db_con_t*, const db_val_t*, char*, int*)); 00173 00174 00175 /** 00176 * Print where clause for a SQL statement. 00177 * 00178 * \param _c structure representing database connection 00179 * \param _b target char 00180 * \param _l length of the target 00181 * \param _k keys that should be printed 00182 * \param _o optional operators 00183 * \param _v values that should be printed 00184 * \param _n number of key/value pairs 00185 * \param (*val2str) function pointer to a db specific conversion function 00186 * \return the length of the printed result on success, negative on errors 00187 */ 00188 int db_print_where(const db_con_t* _c, char* _b, const int _l, const db_key_t* _k, 00189 const db_op_t* _o, const db_val_t* _v, const int _n, int (*val2str) 00190 (const db_con_t*, const db_val_t*, char*, int*)); 00191 00192 00193 /** 00194 * Print set clause for a SQL statement. 00195 * 00196 * \param _c structure representing database connection 00197 * \param _b target char 00198 * \param _l length of the target 00199 * \param _k keys that should be printed 00200 * \param _v vals that should be printed 00201 * \param _n number of key/value pairs 00202 * \param (*val2str) function pointer to a db specific conversion function 00203 * \return the length of the printed result on success, negative on errors 00204 */ 00205 int db_print_set(const db_con_t* _c, char* _b, const int _l, 00206 const db_key_t* _k, const db_val_t* _v, const int _n, int (*val2str) 00207 (const db_con_t*, const db_val_t*, char*, int*)); 00208 00209 #endif
1.5.6