db_res.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. * Copyright (C) 2007-2008 1&1 Internet AG
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * \file lib/srdb1/db_res.h
  25. * \brief Data structure that represents a result from a query.
  26. *
  27. * Data structure that represents a result from a database query,
  28. * it also provides some convenience macros and some memory management
  29. * functions for result structures.
  30. * \ingroup db1
  31. */
  32. #ifndef DB1_RES_H
  33. #define DB1_RES_H
  34. #include "db_key.h"
  35. #include "db_val.h"
  36. struct db_row;
  37. /**
  38. * This type represents a result returned by db_query function (see below). The
  39. * result can consist of zero or more rows (see db_row_t description).
  40. *
  41. * Note: A variable of type db1_res_t returned by db_query function uses dynamicaly
  42. * allocated memory, don't forget to call db_free_result if you don't need the
  43. * variable anymore. You will encounter memory leaks if you fail to do this!
  44. *
  45. * In addition to zero or more rows, each db1_res_t object contains also an array
  46. * of db_key_t objects. The objects represent keys (names of columns). *
  47. */
  48. typedef struct db1_res {
  49. struct {
  50. db_key_t* names; /**< Column names */
  51. db_type_t* types; /**< Column types */
  52. int n; /**< Number of columns */
  53. } col;
  54. struct db_row* rows; /**< Rows */
  55. int n; /**< Number of rows in current fetch */
  56. int res_rows; /**< Number of total rows in query */
  57. int last_row; /**< Last row */
  58. void* ptr; /**< For use by DB modules */
  59. } db1_res_t;
  60. /** Return the column names */
  61. #define RES_NAMES(re) ((re)->col.names)
  62. /** Return the column types */
  63. #define RES_TYPES(re) ((re)->col.types)
  64. /** Return the number of columns */
  65. #define RES_COL_N(re) ((re)->col.n)
  66. /** Return the result rows */
  67. #define RES_ROWS(re) ((re)->rows)
  68. /** Return the number of current result rows */
  69. #define RES_ROW_N(re) ((re)->n)
  70. /** Return the last row of the result */
  71. #define RES_LAST_ROW(re) ((re)->last_row)
  72. /** Return the number of total result rows */
  73. #define RES_NUM_ROWS(re) ((re)->res_rows)
  74. /** Return the module-specific pointer */
  75. #define RES_PTR(re) ((re)->ptr)
  76. /**
  77. * Release memory used by rows in a result structure.
  78. * \param _r the result that should be released
  79. * \return zero on success, negative on errors
  80. */
  81. int db_free_rows(db1_res_t* _r);
  82. /**
  83. * Release memory used by columns. This methods assumes that the string values
  84. * holding the column names are in memory allocated from the database driver,
  85. * and thus must be not freed here.
  86. * \param _r the result that should be released
  87. * \return zero on success, negative on errors
  88. */
  89. int db_free_columns(db1_res_t* _r);
  90. /**
  91. * Create a new result structure and initialize it.
  92. * \return a pointer to the new result on success, NULL on errors
  93. */
  94. db1_res_t* db_new_result(void);
  95. /**
  96. * Release memory used by a result structure.
  97. * \return zero on success, negative on errors
  98. */
  99. int db_free_result(db1_res_t* _r);
  100. /**
  101. * Allocate storage for column names and type in existing result structure.
  102. * If no more memory is available for the allocation of the types then the
  103. * already allocated memory for the names is freed.
  104. * \param _r filled result set
  105. * \param cols number of columns
  106. * \return zero on success, negative on errors
  107. */
  108. int db_allocate_columns(db1_res_t* _r, const unsigned int cols);
  109. /**
  110. * Allocate memory for rows.
  111. * \param _res result set
  112. * \return zero on success, negative on errors
  113. */
  114. int db_allocate_rows(db1_res_t* _res);
  115. /**
  116. * Reallocate memory for rows.
  117. * \param _res result set
  118. * \param _nsize new number of rows in result set
  119. * \return zero on success, negative on errors
  120. */
  121. int db_reallocate_rows(db1_res_t* _res, int _nsize);
  122. #endif /* DB1_RES_H */