00001 /* 00002 * $Id: db_res.h 5362 2008-12-15 16:33:22Z 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_res.h 00026 * \brief Data structure that represents a result from a query. 00027 * 00028 * Data structure that represents a result from a database query, 00029 * it also provides some convenience macros and some memory management 00030 * functions for result structures. 00031 * \ingroup db 00032 */ 00033 00034 #ifndef DB_RES_H 00035 #define DB_RES_H 00036 00037 00038 #include "db_key.h" 00039 #include "db_val.h" 00040 00041 struct db_row; 00042 00043 /** 00044 * This type represents a result returned by db_query function (see below). The 00045 * result can consist of zero or more rows (see db_row_t description). 00046 * 00047 * Note: A variable of type db_res_t returned by db_query function uses dynamicaly 00048 * allocated memory, don't forget to call db_free_result if you don't need the 00049 * variable anymore. You will encounter memory leaks if you fail to do this! 00050 * 00051 * In addition to zero or more rows, each db_res_t object contains also an array 00052 * of db_key_t objects. The objects represent keys (names of columns). * 00053 */ 00054 typedef struct db_res { 00055 struct { 00056 db_key_t* names; /**< Column names */ 00057 db_type_t* types; /**< Column types */ 00058 int n; /**< Number of columns */ 00059 } col; 00060 struct db_row* rows; /**< Rows */ 00061 int n; /**< Number of rows in current fetch */ 00062 int res_rows; /**< Number of total rows in query */ 00063 int last_row; /**< Last row */ 00064 } db_res_t; 00065 00066 00067 /** Return the column names */ 00068 #define RES_NAMES(re) ((re)->col.names) 00069 /** Return the column types */ 00070 #define RES_TYPES(re) ((re)->col.types) 00071 /** Return the number of columns */ 00072 #define RES_COL_N(re) ((re)->col.n) 00073 /** Return the result rows */ 00074 #define RES_ROWS(re) ((re)->rows) 00075 /** Return the number of current result rows */ 00076 #define RES_ROW_N(re) ((re)->n) 00077 /** Return the last row of the result */ 00078 #define RES_LAST_ROW(re) ((re)->last_row) 00079 /** Return the number of total result rows */ 00080 #define RES_NUM_ROWS(re) ((re)->res_rows) 00081 00082 00083 /** 00084 * Release memory used by rows in a result structure. 00085 * \param _r the result that should be released 00086 * \return zero on success, negative on errors 00087 */ 00088 inline int db_free_rows(db_res_t* _r); 00089 00090 00091 /** 00092 * Release memory used by columns. This methods assumes that the string values 00093 * holding the column names are in memory allocated from the database driver, 00094 * and thus must be not freed here. 00095 * \param _r the result that should be released 00096 * \return zero on success, negative on errors 00097 */ 00098 inline int db_free_columns(db_res_t* _r); 00099 00100 00101 /** 00102 * Create a new result structure and initialize it. 00103 * \return a pointer to the new result on success, NULL on errors 00104 */ 00105 inline db_res_t* db_new_result(void); 00106 00107 /** 00108 * Release memory used by a result structure. 00109 * \return zero on success, negative on errors 00110 */ 00111 inline int db_free_result(db_res_t* _r); 00112 00113 /** 00114 * Allocate storage for column names and type in existing result structure. 00115 * If no more memory is available for the allocation of the types then the 00116 * already allocated memory for the names is freed. 00117 * \param _r filled result set 00118 * \param cols number of columns 00119 * \return zero on success, negative on errors 00120 */ 00121 inline int db_allocate_columns(db_res_t* _r, const unsigned int cols); 00122 00123 00124 /** 00125 * Allocate memory for rows. 00126 * \param _res result set 00127 * \return zero on success, negative on errors 00128 */ 00129 inline int db_allocate_rows(db_res_t* _res); 00130 00131 #endif /* DB_RES_H */
1.5.6