* Convert rows from Berkeley DB to db API representation */
| 84 | * Convert rows from Berkeley DB to db API representation |
| 85 | */ |
| 86 | int bdb_convert_row(db_res_t* _res, char *bdb_result, int* _lres) |
| 87 | { |
| 88 | int col, len, i, j; |
| 89 | char **row_buf, *s; |
| 90 | col = len = i = j = 0; |
| 91 | struct db_row* row = NULL; |
| 92 | |
| 93 | if (!_res) { |
| 94 | LM_ERR("invalid parameter\n"); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | /* Save the number of rows in the current fetch */ |
| 99 | RES_ROW_N(_res) = 1; |
| 100 | row = RES_ROWS(_res); |
| 101 | |
| 102 | /* Save the number of columns in the ROW structure */ |
| 103 | ROW_N(row) = RES_COL_N(_res); |
| 104 | |
| 105 | /* |
| 106 | * Allocate an array of pointers one per column. |
| 107 | * It that will be used to hold the address of the string |
| 108 | * representation of each column. |
| 109 | */ |
| 110 | len = sizeof(char *) * RES_COL_N(_res); |
| 111 | row_buf = (char **)pkg_malloc(len); |
| 112 | if (!row_buf) { |
| 113 | LM_ERR("no private memory left\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | LM_DBG("allocate for %d columns %d bytes in row buffer at %p\n", |
| 117 | RES_COL_N(_res), len, row_buf); |
| 118 | memset(row_buf, 0, len); |
| 119 | |
| 120 | /*populate the row_buf with bdb_result*/ |
| 121 | /*bdb_result is memory from our callers stack so we copy here*/ |
| 122 | |
| 123 | |
| 124 | LM_DBG("Found: [%s]\n",bdb_result); |
| 125 | |
| 126 | s = strsep(&bdb_result, DELIM); |
| 127 | while( s!=NULL) |
| 128 | { |
| 129 | if(_lres) { |
| 130 | /*only requested cols (_c was specified)*/ |
| 131 | for(i=0; i<ROW_N(row); i++) |
| 132 | { if (col == _lres[i]) { |
| 133 | len = strlen(s); |
| 134 | row_buf[i] = pkg_malloc(len+1); |
| 135 | if (!row_buf[i]) { |
| 136 | LM_ERR("no private memory left\n"); |
| 137 | goto error; |
| 138 | } |
| 139 | LM_DBG("allocated %d bytes for row_buf[%d] at %p\n", len, i, row_buf[i]); |
| 140 | memcpy(row_buf[i], s, len+1); |
| 141 | } |
| 142 | |
| 143 | } |
no test coverage detected