db_res.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "db_res.h"
00034
00035 #include "db_row.h"
00036 #include "../dprint.h"
00037 #include "../mem/mem.h"
00038
00039 #include <string.h>
00040
00041
00042
00043
00044 inline int db_free_rows(db_res_t* _r)
00045 {
00046 int i;
00047
00048 if (!_r) {
00049 LM_ERR("invalid parameter value\n");
00050 return -1;
00051 }
00052 LM_DBG("freeing %d rows\n", RES_ROW_N(_r));
00053
00054 for(i = 0; i < RES_ROW_N(_r); i++) {
00055 db_free_row(&(RES_ROWS(_r)[i]));
00056 }
00057 RES_ROW_N(_r) = 0;
00058
00059 if (RES_ROWS(_r)) {
00060 LM_DBG("freeing rows at %p\n", RES_ROWS(_r));
00061 pkg_free(RES_ROWS(_r));
00062 RES_ROWS(_r) = NULL;
00063 }
00064 return 0;
00065 }
00066
00067
00068
00069
00070
00071 inline int db_free_columns(db_res_t* _r)
00072 {
00073 int col;
00074
00075 if (!_r) {
00076 LM_ERR("invalid parameter value\n");
00077 return -1;
00078 }
00079 LM_DBG("freeing %d columns\n", RES_COL_N(_r));
00080
00081 for(col = 0; col < RES_COL_N(_r); col++) {
00082 if (RES_NAMES(_r)[col]!=NULL) {
00083 LM_DBG("freeing RES_NAMES[%d] at %p\n", col, RES_NAMES(_r)[col]);
00084 pkg_free((str *)RES_NAMES(_r)[col]);
00085 RES_NAMES(_r)[col] = NULL;
00086 }
00087 }
00088 RES_COL_N(_r) = 0;
00089
00090
00091 if (RES_NAMES(_r)) {
00092 LM_DBG("freeing result names at %p\n", RES_NAMES(_r));
00093 pkg_free(RES_NAMES(_r));
00094 RES_NAMES(_r) = NULL;
00095 }
00096 if (RES_TYPES(_r)) {
00097 LM_DBG("freeing result types at %p\n", RES_TYPES(_r));
00098 pkg_free(RES_TYPES(_r));
00099 RES_TYPES(_r) = NULL;
00100 }
00101 return 0;
00102 }
00103
00104
00105
00106
00107 inline db_res_t* db_new_result(void)
00108 {
00109 db_res_t* r = NULL;
00110 r = (db_res_t*)pkg_malloc(sizeof(db_res_t));
00111 if (!r) {
00112 LM_ERR("no private memory left\n");
00113 return 0;
00114 }
00115 LM_DBG("allocate %d bytes for result set at %p\n",
00116 (int)sizeof(db_res_t), r);
00117 memset(r, 0, sizeof(db_res_t));
00118 return r;
00119 }
00120
00121
00122
00123
00124 inline int db_free_result(db_res_t* _r)
00125 {
00126 if (!_r)
00127 {
00128 LM_ERR("invalid parameter\n");
00129 return -1;
00130 }
00131
00132 db_free_columns(_r);
00133 db_free_rows(_r);
00134 LM_DBG("freeing result set at %p\n", _r);
00135 pkg_free(_r);
00136 _r = NULL;
00137 return 0;
00138 }
00139
00140
00141
00142
00143
00144 inline int db_allocate_columns(db_res_t* _r, const unsigned int cols)
00145 {
00146 RES_NAMES(_r) = (db_key_t*)pkg_malloc(sizeof(db_key_t) * cols);
00147 if (!RES_NAMES(_r)) {
00148 LM_ERR("no private memory left\n");
00149 return -1;
00150 }
00151 LM_DBG("allocate %d bytes for result names at %p\n",
00152 (int)(sizeof(db_key_t) * cols),
00153 RES_NAMES(_r));
00154
00155 RES_TYPES(_r) = (db_type_t*)pkg_malloc(sizeof(db_type_t) * cols);
00156 if (!RES_TYPES(_r)) {
00157 LM_ERR("no private memory left\n");
00158 pkg_free(RES_NAMES(_r));
00159 return -1;
00160 }
00161 LM_DBG("allocate %d bytes for result types at %p\n",
00162 (int)(sizeof(db_type_t) * cols),
00163 RES_TYPES(_r));
00164
00165 return 0;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174 inline int db_allocate_rows(db_res_t* _res)
00175 {
00176 int len = sizeof(db_row_t) * RES_ROW_N(_res);
00177 RES_ROWS(_res) = (struct db_row*)pkg_malloc(len);
00178 if (!RES_ROWS(_res)) {
00179 LM_ERR("no private memory left\n");
00180 return -1;
00181 }
00182 LM_DBG("allocate %d bytes for rows at %p\n", len, RES_ROWS(_res));
00183 memset(RES_ROWS(_res), 0, len);
00184
00185 return 0;
00186 }