00001 /* 00002 * $Id: db_val.h 5423 2009-01-05 18:29:35Z 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_val.h 00026 * \brief Data structures that represents values in the database. 00027 * 00028 * This file defines data structures that represents values in the database. 00029 * Several datatypes are recognized and converted by the database API. 00030 * Available types: DB_INT, DB_DOUBLE, DB_STRING, DB_STR, DB_DATETIME, DB_BLOB and DB_BITMAP 00031 * It also provides some macros for convenient access to this values, 00032 * and a function to convert a str to a value. 00033 * \ingroup db 00034 */ 00035 00036 00037 #ifndef DB_VAL_H 00038 #define DB_VAL_H 00039 00040 #include "db_con.h" 00041 #include <time.h> 00042 #include "../str.h" 00043 00044 00045 /** 00046 * Each cell in a database table can be of a different type. To distinguish 00047 * among these types, the db_type_t enumeration is used. Every value of the 00048 * enumeration represents one datatype that is recognized by the database 00049 * API. 00050 */ 00051 typedef enum { 00052 DB_INT, /**< represents an 32 bit integer number */ 00053 DB_BIGINT, /**< represents an 64 bit integer number */ 00054 DB_DOUBLE, /**< represents a floating point number */ 00055 DB_STRING, /**< represents a zero terminated const char* */ 00056 DB_STR, /**< represents a string of 'str' type */ 00057 DB_DATETIME, /**< represents date and time */ 00058 DB_BLOB, /**< represents a large binary object */ 00059 DB_BITMAP /**< an one-dimensional array of 32 flags */ 00060 } db_type_t; 00061 00062 00063 /** 00064 * This structure represents a value in the database. Several datatypes are 00065 * recognized and converted by the database API. These datatypes are automaticaly 00066 * recognized, converted from internal database representation and stored in the 00067 * variable of corresponding type. 00068 * 00069 * Module that want to use this values needs to copy them to another memory 00070 * location, because after the call to free_result there are not more available. 00071 * 00072 * If the structure holds a pointer to a string value that needs to be freed 00073 * because the module allocated new memory for it then the free flag must 00074 * be set to a non-zero value. A free flag of zero means that the string 00075 * data must be freed internally by the database driver. 00076 */ 00077 typedef struct { 00078 db_type_t type; /**< Type of the value */ 00079 int nul; /**< Means that the column in database has no value */ 00080 int free; /**< Means that the value should be freed */ 00081 /** Column value structure that holds the actual data in a union. */ 00082 union { 00083 int int_val; /**< integer value */ 00084 long long ll_val; /**< long long value */ 00085 double double_val; /**< double value */ 00086 time_t time_val; /**< unix time_t value */ 00087 const char* string_val; /**< zero terminated string */ 00088 str str_val; /**< str type string value */ 00089 str blob_val; /**< binary object data */ 00090 unsigned int bitmap_val; /**< Bitmap data type */ 00091 } val; 00092 } db_val_t; 00093 00094 00095 /** 00096 * Useful macros for accessing attributes of db_val structure. 00097 * All macros expect a reference to a db_val_t variable as parameter. 00098 */ 00099 00100 /** 00101 * Use this macro if you need to set/get the type of the value. 00102 */ 00103 #define VAL_TYPE(dv) ((dv)->type) 00104 00105 00106 /** 00107 * Use this macro if you need to set/get the null flag. A non-zero flag means that 00108 * the corresponding cell in the database contains no data (a NULL value in MySQL 00109 * terminology). 00110 */ 00111 #define VAL_NULL(dv) ((dv)->nul) 00112 00113 00114 /** 00115 * Use this macro if you need to set/ get the free flag. A non-zero flag means that 00116 * the corresponding cell in the database contains data that must be freed from the 00117 * DB API. 00118 */ 00119 #define VAL_FREE(dv) ((dv)->free) 00120 00121 00122 /** 00123 * Use this macro if you need to access the integer value in the db_val_t structure. 00124 */ 00125 #define VAL_INT(dv) ((dv)->val.int_val) 00126 00127 00128 /** 00129 * Use this macro if you need to access the long long value in the db_val_t structure. 00130 */ 00131 #define VAL_BIGINT(dv) ((dv)->val.ll_val) 00132 00133 00134 /** 00135 * Use this macro if you need to access the double value in the db_val_t structure. 00136 */ 00137 #define VAL_DOUBLE(dv) ((dv)->val.double_val) 00138 00139 00140 /** 00141 * Use this macro if you need to access the time_t value in the db_val_t structure. 00142 */ 00143 #define VAL_TIME(dv) ((dv)->val.time_val) 00144 00145 00146 /** 00147 * Use this macro if you need to access the string value in the db_val_t structure. 00148 */ 00149 #define VAL_STRING(dv) ((dv)->val.string_val) 00150 00151 00152 /** 00153 * Use this macro if you need to access the str structure in the db_val_t structure. 00154 */ 00155 #define VAL_STR(dv) ((dv)->val.str_val) 00156 00157 00158 /** 00159 * Use this macro if you need to access the blob value in the db_val_t structure. 00160 */ 00161 #define VAL_BLOB(dv) ((dv)->val.blob_val) 00162 00163 00164 /** 00165 * Use this macro if you need to access the bitmap value in the db_val_t structure. 00166 */ 00167 #define VAL_BITMAP(dv) ((dv)->val.bitmap_val) 00168 00169 00170 /*! 00171 * \brief Convert a str to a db value 00172 * 00173 * Convert a str to a db value, does not copy strings if _cpy is zero 00174 * \param _t destination value type 00175 * \param _v destination value 00176 * \param _s source string 00177 * \param _l string length 00178 * \param _cpy when set to zero does not copy strings, otherwise copy strings 00179 * \return 0 on success, negative on error 00180 */ 00181 int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l, 00182 const unsigned int _cpy); 00183 00184 00185 /*! 00186 * \brief Convert a numerical value to a string 00187 * 00188 * Convert a numerical value to a string, used when converting result from a query. 00189 * Implement common functionality needed from the databases, does parameter checking. 00190 * \param _c database connection 00191 * \param _v source value 00192 * \param _s target string 00193 * \param _len target string length 00194 * \return 0 on success, negative on error, 1 if value must be converted by other means 00195 */ 00196 int db_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len); 00197 00198 #endif /* DB_VAL_H */
1.5.6