src/lib/srdb1/db_res.h
9405692a
 /* 
  * Copyright (C) 2001-2003 FhG Fokus
bc4bf265
  * Copyright (C) 2007-2008 1&1 Internet AG
9405692a
  *
d520eaf5
  * This file is part of Kamailio, a free SIP server.
9405692a
  *
d520eaf5
  * Kamailio is free software; you can redistribute it and/or modify
9405692a
  * 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,
9405692a
  * 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.
  *
  * 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
9405692a
  */
 
bc4bf265
 /**
7d14b037
  * \file lib/srdb1/db_res.h
bc4bf265
  * \brief Data structure that represents a result from a query.
  *
  * Data structure that represents a result from a database query,
  * it also provides some convenience macros and some memory management
  * functions for result structures.
a33b9a87
  * \ingroup db1
bc4bf265
  */
9405692a
 
2d6bfb46
 #ifndef DB1_RES_H
 #define DB1_RES_H
9405692a
 
 
 #include "db_key.h"
 #include "db_val.h"
 
47c619ff
 struct db_row;
9405692a
 
fee36376
 /* -- column name flags -- */
 /* column name must be freed when db result is destroyed */
 #define DB1_FCOL_FREE	(1<<1)
 
bc4bf265
 /**
fee36376
  * This type represents a result returned by db_query function (see below). The
bc4bf265
  * result can consist of zero or more rows (see db_row_t description).
47c619ff
  *
20606ac1
  * Note: A variable of type db1_res_t returned by db_query function uses dynamicaly
bc4bf265
  * allocated memory, don't forget to call db_free_result if you don't need the
  * variable anymore. You will encounter memory leaks if you fail to do this!
  *
20606ac1
  * In addition to zero or more rows, each db1_res_t object contains also an array
47c619ff
  * of db_key_t objects. The objects represent keys (names of columns). *
bc4bf265
  */
13f9c1e6
 typedef struct db1_res {
9405692a
 	struct {
bc4bf265
 		db_key_t* names;   /**< Column names                    */
 		db_type_t* types;  /**< Column types                    */
 		int n;             /**< Number of columns               */
fee36376
 		int cflags;        /**< Flags of columns                */
9405692a
 	} col;
bc4bf265
 	struct db_row* rows;   /**< Rows                            */
 	int n;                 /**< Number of rows in current fetch */
 	int res_rows;          /**< Number of total rows in query   */
 	int last_row;          /**< Last row                        */
8366a26f
 	void* ptr;             /**< For use by DB modules           */
13f9c1e6
 } db1_res_t;
9405692a
 
 
bc4bf265
 /** Return the column names */
9405692a
 #define RES_NAMES(re) ((re)->col.names)
bc4bf265
 /** Return the column types */
9405692a
 #define RES_TYPES(re) ((re)->col.types)
bc4bf265
 /** Return the number of columns */
9405692a
 #define RES_COL_N(re) ((re)->col.n)
fee36376
 /** Return the flags of columns */
 #define RES_COL_FLAGS(re) ((re)->col.cflags)
bc4bf265
 /** Return the result rows */
9405692a
 #define RES_ROWS(re)  ((re)->rows)
bc4bf265
 /** Return the number of current result rows */
9405692a
 #define RES_ROW_N(re) ((re)->n)
bc4bf265
 /** Return the last row of the result */
83510117
 #define RES_LAST_ROW(re)  ((re)->last_row)
bc4bf265
 /** Return the number of total result rows */
83510117
 #define RES_NUM_ROWS(re) ((re)->res_rows)
8366a26f
 /** Return the module-specific pointer */
 #define RES_PTR(re) ((re)->ptr)
9405692a
 
bc4bf265
 
 /**
47c619ff
  * Release memory used by rows in a result structure.
bc4bf265
  * \param _r the result that should be released
  * \return zero on success, negative on errors
0eece1d2
  */
5beb495d
 int db_free_rows(db1_res_t* _r);
bc4bf265
 
 
 /**
47c619ff
  * Release memory used by columns. This methods assumes that the string values
  * holding the column names are in memory allocated from the database driver,
  * and thus must be not freed here.
bc4bf265
  * \param _r the result that should be released
  * \return zero on success, negative on errors
  */
5beb495d
 int db_free_columns(db1_res_t* _r);
bc4bf265
 
9405692a
 
bc4bf265
 /**
47c619ff
  * Create a new result structure and initialize it.
bc4bf265
  * \return a pointer to the new result on success, NULL on errors
676afffc
  */
5beb495d
 db1_res_t* db_new_result(void);
676afffc
 
dac7077b
 /**
  * Release memory used by a result structure.
  * \return zero on success, negative on errors
  */
5beb495d
 int db_free_result(db1_res_t* _r);
47c619ff
 
 /**
  * Allocate storage for column names and type in existing result structure.
  * If no more memory is available for the allocation of the types then the
  * already allocated memory for the names is freed.
  * \param _r filled result set
  * \param cols number of columns
  * \return zero on success, negative on errors
  */
5beb495d
 int db_allocate_columns(db1_res_t* _r, const unsigned int cols);
dac7077b
 
3d7f4c1e
 
 /**
  * Allocate memory for rows.
  * \param _res result set
  * \return zero on success, negative on errors
  */
5beb495d
 int db_allocate_rows(db1_res_t* _res);
3d7f4c1e
 
fea60616
 /**
  * Reallocate memory for rows.
  * \param _res result set
  * \param _nsize new number of rows in result set
  * \return zero on success, negative on errors
  */
 int db_reallocate_rows(db1_res_t* _res, int _nsize);
 
2d6bfb46
 #endif /* DB1_RES_H */