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_res.h 24 * \brief Data structure that represents a result from a query. 25 * 26 * Data structure that represents a result from a database query, 27 * it also provides some convenience macros and some memory management 28 * functions for result structures. 29 * \ingroup db1 30 */ 31 32 #ifndef DB1_RES_H 33 #define DB1_RES_H 34 35 36 #include "db_key.h" 37 #include "db_val.h" 38 39 struct db_row; 40 41 /* -- column name flags -- */ 42 /* column name must be freed when db result is destroyed */ 43 #define DB1_FCOL_FREE (1<<1) 44 45 /** 46 * This type represents a result returned by db_query function (see below). The 47 * result can consist of zero or more rows (see db_row_t description). 48 * 49 * Note: A variable of type db1_res_t returned by db_query function uses dynamicaly 50 * allocated memory, don't forget to call db_free_result if you don't need the 51 * variable anymore. You will encounter memory leaks if you fail to do this! 52 * 53 * In addition to zero or more rows, each db1_res_t object contains also an array 54 * of db_key_t objects. The objects represent keys (names of columns). * 55 */ 56 typedef struct db1_res { 57 struct { 58 db_key_t* names; /**< Column names */ 59 db_type_t* types; /**< Column types */ 60 int n; /**< Number of columns */ 61 int cflags; /**< Flags of columns */ 62 } col; 63 struct db_row* rows; /**< Rows */ 64 int n; /**< Number of rows in current fetch */ 65 int res_rows; /**< Number of total rows in query */ 66 int last_row; /**< Last row */ 67 void* ptr; /**< For use by DB modules */ 68 } db1_res_t; 69 70 71 /** Return the column names */ 72 #define RES_NAMES(re) ((re)->col.names) 73 /** Return the column types */ 74 #define RES_TYPES(re) ((re)->col.types) 75 /** Return the number of columns */ 76 #define RES_COL_N(re) ((re)->col.n) 77 /** Return the flags of columns */ 78 #define RES_COL_FLAGS(re) ((re)->col.cflags) 79 /** Return the result rows */ 80 #define RES_ROWS(re) ((re)->rows) 81 /** Return the number of current result rows */ 82 #define RES_ROW_N(re) ((re)->n) 83 /** Return the last row of the result */ 84 #define RES_LAST_ROW(re) ((re)->last_row) 85 /** Return the number of total result rows */ 86 #define RES_NUM_ROWS(re) ((re)->res_rows) 87 /** Return the module-specific pointer */ 88 #define RES_PTR(re) ((re)->ptr) 89 90 91 /** 92 * Release memory used by rows in a result structure. 93 * \param _r the result that should be released 94 * \return zero on success, negative on errors 95 */ 96 int db_free_rows(db1_res_t* _r); 97 98 99 /** 100 * Release memory used by columns. This methods assumes that the string values 101 * holding the column names are in memory allocated from the database driver, 102 * and thus must be not freed here. 103 * \param _r the result that should be released 104 * \return zero on success, negative on errors 105 */ 106 int db_free_columns(db1_res_t* _r); 107 108 109 /** 110 * Create a new result structure and initialize it. 111 * \return a pointer to the new result on success, NULL on errors 112 */ 113 db1_res_t* db_new_result(void); 114 115 /** 116 * Release memory used by a result structure. 117 * \return zero on success, negative on errors 118 */ 119 int db_free_result(db1_res_t* _r); 120 121 /** 122 * Allocate storage for column names and type in existing result structure. 123 * If no more memory is available for the allocation of the types then the 124 * already allocated memory for the names is freed. 125 * \param _r filled result set 126 * \param cols number of columns 127 * \return zero on success, negative on errors 128 */ 129 int db_allocate_columns(db1_res_t* _r, const unsigned int cols); 130 131 132 /** 133 * Allocate memory for rows. 134 * \param _res result set 135 * \return zero on success, negative on errors 136 */ 137 int db_allocate_rows(db1_res_t* _res); 138 139 /** 140 * Reallocate memory for rows. 141 * \param _res result set 142 * \param _nsize new number of rows in result set 143 * \return zero on success, negative on errors 144 */ 145 int db_reallocate_rows(db1_res_t* _res, int _nsize); 146 147 #endif /* DB1_RES_H */ 148