1 /*
2  * Copyright (C) 2001-2003 FhG Fokus
3  * Copyright (C) 2007-2008 1&1 Internet AG
4  *
5  * This file is part of Kamailio, a free SIP server.
6  *
7  * Kamailio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version
11  *
12  * Kamailio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 /**
23  * \file lib/srdb1/db_ut.h
24  * \brief Utility functions for database drivers.
25  *
26  * This utility methods are used from the database SQL driver to convert
27  * values and print SQL queries from the internal API representation.
28  * \ingroup db1
29 */
30 
31 #ifndef DB1_UT_H
32 #define DB1_UT_H
33 
34 #include "../../core/pvar.h"
35 
36 #include "db_key.h"
37 #include "db.h"
38 
39 
40 /**
41  * Converts a char into an integer value.
42  *
43  * \param _s source value
44  * \param _v target value
45  * \return zero on success, negative on conversion errors
46  */
47 int db_str2int(const char* _s, int* _v);
48 
49 
50 /**
51  * Converts a char into an unsigned integer value.
52  *
53  * \param _s source value
54  * \param _v target value
55  * \return zero on success, negative on conversion errors
56  */
57 int db_str2uint(const char* _s, unsigned int* _v);
58 
59 
60 /**
61  * Converts a char into a long long value.
62  *
63  * \param _s source value
64  * \param _v target value
65  * \return zero on success, negative on conversion errors
66  */
67 int db_str2longlong(const char* _s, long long* _v);
68 
69 
70 /**
71  * Converts a char into an unsigned long long value.
72  *
73  * \param _s source value
74  * \param _v target value
75  * \return zero on success, negative on conversion errors
76  */
77 int db_str2ulonglong(const char* _s, unsigned long long* _v);
78 
79 
80 /**
81  * Converts a char into a double value.
82  *
83  * \param _s source value
84  * \param _v target value
85  * \return zero on success, negative on conversion errors
86  */
87 int db_str2double(const char* _s, double* _v);
88 
89 
90 /**
91  * Converts an integer value in a char pointer.
92  *
93  * \param _v source value
94  * \param _s target value
95  * \param _l available length and target length
96  * \return zero on success, negative on conversion errors
97  */
98 int db_int2str(int _v, char* _s, int* _l);
99 
100 
101 /**
102  * Converts an unsigned integer value in a char pointer.
103  *
104  * \param _v source value
105  * \param _s target value
106  * \param _l available length and target length
107  * \return zero on success, negative on conversion errors
108  */
109 int db_uint2str(unsigned int _v, char* _s, int* _l);
110 
111 
112 /**
113  * Converts a long long value in a char pointer.
114  *
115  * \param _v source value
116  * \param _s target value
117  * \param _l available length and target length
118  * \return zero on success, negative on conversion errors
119  */
120 int db_longlong2str(long long _v, char* _s, int* _l);
121 
122 
123 /**
124  * Converts an unsigned long long value in a char pointer.
125  *
126  * \param _v source value
127  * \param _s target value
128  * \param _l available length and target length
129  * \return zero on success, negative on conversion errors
130  */
131 int db_ulonglong2str(unsigned long long _v, char* _s, int* _l);
132 
133 
134 /**
135  * Converts a double value into a char pointer.
136  *
137  * \param _v source value
138  * \param _s target value
139  * \param _l available length and target length
140  * \return zero on success, negative on conversion errors
141  */
142 int db_double2str(double _v, char* _s, int* _l);
143 
144 
145 /**
146  * Convert a time_t value to string.
147  *
148  * \param _v source value
149  * \param _s target value
150  * \param _l available length and target length
151  * \return zero on success, negative on conversion errors
152  * \todo This functions add quotes to the time value. This
153  * should be done in the val2str function, as some databases
154  * like db_berkeley don't need or like this at all.
155  */
156 int db_time2str(time_t _v, char* _s, int* _l);
157 
158 int db_time2str_ex(time_t _v, char* _s, int* _l, int _qmode);
159 
160 /**
161  * Converts a char into a time_t value.
162  *
163  * \param _s source value
164  * \param _v target value
165  * \return zero on success, negative on conversion errors
166  */
167 int db_str2time(const char* _s, time_t* _v);
168 
169 
170 /**
171  * Print columns for a SQL statement, separated by commas.
172  *
173  * \param _b target char
174  * \param _l length of the target
175  * \param _c keys that should be printed
176  * \param _n number of keys
177  * \param _tq char to quote special tokens or empty string
178  * \return the length of the printed result on success, negative on errors
179  */
180 int db_print_columns(char* _b, const int _l, const db_key_t* _c, const int _n, const char *_tq);
181 
182 
183 /**
184  * Print values for a SQL statement.
185  *
186  * \param _c structure representing database connection
187  * \param _b target char
188  * \param _l length of the target
189  * \param _v values that should be printed
190  * \param _n number of values
191  * \param  (*val2str) function pointer to a db specific conversion function
192  * \return the length of the printed result on success, negative on errors
193  */
194 int db_print_values(const db1_con_t* _c, char* _b, const int _l, const db_val_t* _v,
195 	const int _n, int (*val2str)(const db1_con_t*, const db_val_t*, char*, int*));
196 
197 
198 /**
199  * Print where clause for a SQL statement.
200  *
201  * \param _c structure representing database connection
202  * \param _b target char
203  * \param _l length of the target
204  * \param _k keys that should be printed
205  * \param _o optional operators
206  * \param _v values that should be printed
207  * \param _n number of key/value pairs
208  * \param  (*val2str) function pointer to a db specific conversion function
209  * \return the length of the printed result on success, negative on errors
210  */
211 int db_print_where(const db1_con_t* _c, char* _b, const int _l, const db_key_t* _k,
212 	const db_op_t* _o, const db_val_t* _v, const int _n, int (*val2str)
213 	(const 	db1_con_t*, const db_val_t*, char*, int*));
214 
215 
216 /**
217  * Print set clause for a SQL statement.
218  *
219  * \param _c structure representing database connection
220  * \param _b target char
221  * \param _l length of the target
222  * \param _k keys that should be printed
223  * \param _v vals that should be printed
224  * \param _n number of key/value pairs
225  * \param  (*val2str) function pointer to a db specific conversion function
226  * \return the length of the printed result on success, negative on errors
227  */
228 int db_print_set(const db1_con_t* _c, char* _b, const int _l,
229 	const db_key_t* _k, const db_val_t* _v, const int _n, int (*val2str)
230 	(const db1_con_t*, const db_val_t*, char*, int*));
231 
232 
233 /**
234  * Convert db_val_t to pv_spec_t
235  *
236  * \param msg sip msg structure
237  * \param dbval database value
238  * \param pvs pv_spec where to put the database value
239  * \return 0 on success, -1 on failure
240  */
241 int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs);
242 
243 #endif
244