src/lib/srdb1/db_ut.h
cda1e991
 /*
  * Copyright (C) 2001-2003 FhG Fokus
bc4bf265
  * Copyright (C) 2007-2008 1&1 Internet AG
cda1e991
  *
d520eaf5
  * This file is part of Kamailio, a free SIP server.
cda1e991
  *
d520eaf5
  * Kamailio is free software; you can redistribute it and/or modify
cda1e991
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
d520eaf5
  * Kamailio is distributed in the hope that it will be useful,
cda1e991
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
b3da13a1
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
cda1e991
  */
 
bc4bf265
 /**
7d14b037
  * \file lib/srdb1/db_ut.h
bc4bf265
  * \brief Utility functions for database drivers.
  *
  * This utility methods are used from the database SQL driver to convert
  * values and print SQL queries from the internal API representation.
a33b9a87
  * \ingroup db1
bc4bf265
 */
cda1e991
 
2d6bfb46
 #ifndef DB1_UT_H
 #define DB1_UT_H
cda1e991
 
cf83221d
 #include "../../core/pvar.h"
0a8e9ea7
 
0eece1d2
 #include "db_key.h"
29ec8148
 #include "db.h"
94b24ee2
 
bc4bf265
 
 /**
  * Converts a char into an integer value.
  *
  * \param _s source value
  * \param _v target value
f75d7375
  * \return zero on success, negative on conversion errors
bc4bf265
  */
cda1e991
 int db_str2int(const char* _s, int* _v);
 
bc4bf265
 
7746b450
 /**
e62355f2
  * Converts a char into an unsigned integer value.
  *
  * \param _s source value
  * \param _v target value
  * \return zero on success, negative on conversion errors
  */
 int db_str2uint(const char* _s, unsigned int* _v);
 
 
 /**
  * Converts a char into a long long value.
7746b450
  *
  * \param _s source value
  * \param _v target value
f75d7375
  * \return zero on success, negative on conversion errors
7746b450
  */
 int db_str2longlong(const char* _s, long long* _v);
 
 
e62355f2
 /**
  * Converts a char into an unsigned long long value.
  *
  * \param _s source value
  * \param _v target value
  * \return zero on success, negative on conversion errors
  */
 int db_str2ulonglong(const char* _s, unsigned long long* _v);
 
 
bc4bf265
 /**
  * Converts a char into a double value.
  *
  * \param _s source value
  * \param _v target value
f75d7375
  * \return zero on success, negative on conversion errors
bc4bf265
  */
cda1e991
 int db_str2double(const char* _s, double* _v);
 
bc4bf265
 
 /**
e62355f2
  * Converts an integer value in a char pointer.
bc4bf265
  *
  * \param _v source value
  * \param _s target value
  * \param _l available length and target length
f75d7375
  * \return zero on success, negative on conversion errors
bc4bf265
  */
cda1e991
 int db_int2str(int _v, char* _s, int* _l);
 
bc4bf265
 
e62355f2
 /**
  * Converts an unsigned integer value in a char pointer.
  *
  * \param _v source value
  * \param _s target value
  * \param _l available length and target length
  * \return zero on success, negative on conversion errors
  */
 int db_uint2str(unsigned int _v, char* _s, int* _l);
 
 
7746b450
 /**
  * Converts a long long value in a char pointer.
  *
  * \param _v source value
  * \param _s target value
  * \param _l available length and target length
f75d7375
  * \return zero on success, negative on conversion errors
7746b450
  */
 int db_longlong2str(long long _v, char* _s, int* _l);
 
 
e62355f2
 /**
  * Converts an unsigned long long value in a char pointer.
  *
  * \param _v source value
  * \param _s target value
  * \param _l available length and target length
  * \return zero on success, negative on conversion errors
  */
 int db_ulonglong2str(unsigned long long _v, char* _s, int* _l);
 
 
bc4bf265
 /**
  * Converts a double value into a char pointer.
  *
  * \param _v source value
  * \param _s target value
  * \param _l available length and target length
f75d7375
  * \return zero on success, negative on conversion errors
bc4bf265
  */
94b24ee2
 int db_double2str(double _v, char* _s, int* _l);
 
bc4bf265
 
 /**
  * Convert a time_t value to string.
  *
  * \param _v source value
  * \param _s target value
  * \param _l available length and target length
f75d7375
  * \return zero on success, negative on conversion errors
bc4bf265
  * \todo This functions add quotes to the time value. This
  * should be done in the val2str function, as some databases
  * like db_berkeley don't need or like this at all.
  */
94b24ee2
 int db_time2str(time_t _v, char* _s, int* _l);
cda1e991
 
b3da13a1
 int db_time2str_ex(time_t _v, char* _s, int* _l, int _qmode);
bc4bf265
 
 /**
  * Converts a char into a time_t value.
  *
  * \param _s source value
  * \param _v target value
f75d7375
  * \return zero on success, negative on conversion errors
bc4bf265
  */
94b24ee2
 int db_str2time(const char* _s, time_t* _v);
0eece1d2
 
 
bc4bf265
 /**
  * Print columns for a SQL statement, separated by commas.
  *
  * \param _b target char
  * \param _l length of the target
  * \param _c keys that should be printed
  * \param _n number of keys
ffbe9c2c
  * \param _tq char to quote special tokens or empty string
bc4bf265
  * \return the length of the printed result on success, negative on errors
  */
ffbe9c2c
 int db_print_columns(char* _b, const int _l, const db_key_t* _c, const int _n, const char *_tq);
bc4bf265
 
 
 /**
  * Print values for a SQL statement.
  *
  * \param _c structure representing database connection
  * \param _b target char
  * \param _l length of the target
  * \param _v values that should be printed
  * \param _n number of values
  * \param  (*val2str) function pointer to a db specific conversion function
  * \return the length of the printed result on success, negative on errors
  */
a5071c50
 int db_print_values(const db1_con_t* _c, char* _b, const int _l, const db_val_t* _v,
 	const int _n, int (*val2str)(const db1_con_t*, const db_val_t*, char*, int*));
bc4bf265
 
 
 /**
  * Print where clause for a SQL statement.
  *
  * \param _c structure representing database connection
  * \param _b target char
  * \param _l length of the target
  * \param _k keys that should be printed
  * \param _o optional operators
  * \param _v values that should be printed
  * \param _n number of key/value pairs
  * \param  (*val2str) function pointer to a db specific conversion function
  * \return the length of the printed result on success, negative on errors
  */
a5071c50
 int db_print_where(const db1_con_t* _c, char* _b, const int _l, const db_key_t* _k,
bc4bf265
 	const db_op_t* _o, const db_val_t* _v, const int _n, int (*val2str)
a5071c50
 	(const 	db1_con_t*, const db_val_t*, char*, int*));
bc4bf265
 
 
 /**
  * Print set clause for a SQL statement.
  *
  * \param _c structure representing database connection
  * \param _b target char
  * \param _l length of the target
  * \param _k keys that should be printed
  * \param _v vals that should be printed
  * \param _n number of key/value pairs
  * \param  (*val2str) function pointer to a db specific conversion function
  * \return the length of the printed result on success, negative on errors
  */
a5071c50
 int db_print_set(const db1_con_t* _c, char* _b, const int _l,
bc4bf265
 	const db_key_t* _k, const db_val_t* _v, const int _n, int (*val2str)
a5071c50
 	(const db1_con_t*, const db_val_t*, char*, int*));
29ec8148
 
ed78c096
 
 /**
b4af74a1
  * Convert db_val_t to pv_spec_t
b3da13a1
  *
ed78c096
  * \param msg sip msg structure
  * \param dbval database value
33654b5c
  * \param pvs pv_spec where to put the database value
  * \return 0 on success, -1 on failure
ed78c096
  */
 int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs);
 
cda1e991
 #endif