3 #include <murphy/common/debug.h>
4 #include <murphy/common/mm.h>
5 #include <murphy/common/hashtbl.h>
6 #include <murphy/common/utils.h>
8 #include <murphy-db/mqi.h>
14 * client-side tables, common table routines
17 static void purge_pep_table(mrp_pep_table_t *t)
22 mrp_free((char *)t->name);
24 for (i = 0; i < t->ncolumn; i++)
25 mrp_free((char *)t->columns[i].name);
32 static void free_column_definitions(mqi_column_def_t *columns, int ncolumn)
36 if (columns != NULL) {
37 for (i = 0; i < ncolumn; i++)
38 mrp_free((char *)columns[i].name);
45 static int copy_column_definitions(mqi_column_def_t *src, int nsrc,
46 mqi_column_def_t **dstcol, int *ndst)
48 mqi_column_def_t *dst;
52 if (src[nsrc - 1].name == NULL)
57 dst = mrp_allocz_array(mqi_column_def_t, n + 1);
62 for (i = 0; i < n; i++) {
63 dst[i].type = src[i].type;
64 dst[i].length = src[i].length;
65 dst[i].name = mrp_strdup(src[i].name);
67 if (dst[i].name == NULL)
84 free_column_definitions(dst, n);
89 static void free_column_descriptors(mqi_column_desc_t *coldesc)
95 static int setup_column_descriptors(mqi_column_def_t *columns, int ncolumn,
96 mqi_column_desc_t **coldesc)
98 #define SETUP_TYPE(type, member) \
101 desc->offset = (void *)&col->member - (void *)NULL; \
104 mqi_column_def_t *def;
105 mqi_column_desc_t *desc;
106 mrp_pep_value_t *col;
109 *coldesc = mrp_allocz_array(mqi_column_desc_t, ncolumn + 1);
111 if (coldesc != NULL) {
116 for (i = 0; i < ncolumn; i++) {
118 SETUP_TYPE(integer , s32);
119 SETUP_TYPE(unsignd , u32);
120 SETUP_TYPE(floating, dbl);
121 SETUP_TYPE(string , str);
140 free_column_descriptors(*coldesc);
149 static int check_columns(mqi_column_def_t *p, int np,
150 mqi_column_def_t *q, int nq)
155 for (i = 0; i < np; i++, p++, q++) {
156 if (p->type != q->type || p->length != q->length)
158 if (strcmp(p->name, q->name))
169 int copy_pep_table(mrp_pep_table_t *src, mrp_pep_table_t *dst)
173 dst->name = mrp_strdup(src->name);
174 if (dst->name == NULL)
177 if (src->columns[src->ncolumn - 1].name != NULL)
178 ncolumn = src->ncolumn;
180 ncolumn = src->ncolumn - 1;
182 dst->columns = mrp_allocz_array(typeof(*dst->columns), ncolumn + 1);
183 if (dst->columns == NULL) {
184 mrp_free((char *)dst->name);
189 dst->ncolumn = ncolumn;
192 for (i = 0; i < ncolumn; i++) {
193 dst->columns[i].type = src->columns[i].type;
194 dst->columns[i].length = src->columns[i].length;
195 dst->columns[i].name = mrp_strdup(src->columns[i].name);
197 if (dst->columns[i].name == NULL)
200 if (src->columns[i].flags != 0) {
201 if (dst->idx_col == -1)
207 dst->columns[i].flags = 0;
213 purge_pep_table(dst);
219 int copy_pep_tables(mrp_pep_table_t *src, mrp_pep_table_t *dst, int n)
223 for (i = 0; i < n; i++) {
224 if (!copy_pep_table(src + i, dst + i)) {
225 for (i--; i >= 0; i--)
226 purge_pep_table(dst + i);
236 void free_pep_table(mrp_pep_table_t *t)
243 void free_pep_tables(mrp_pep_table_t *tables, int n)
247 for (i = 0; i < n; i++)
248 purge_pep_table(tables + i);