int bdb_rrow_db2bdb(db_con_t* _h, db_key_t* _k, int _n, bdb_rrow_p *_r) { bdb_rrow_p r; bdb_table_p t; bdb_column_p c; int found; int i; int c_idx; *_r = NULL; if ((t = bdb_find_table(CON_TABLE(_h))) == NULL) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_rrow_db2bdb: no table in use\n"); #endif return -1; }; i = (_n == 0) ? BDB_CON_COL_NUM(_h) : _n; r = pkg_malloc(sizeof(*r) * i); memset(r, 0, sizeof(*r) * i); if (_n > 0) { for (i = 0; i < _n; i++) { found = 0; for (c = t->cols, c_idx = 0; c != NULL; c = c->next, c_idx++) { if (!strcmp(_k[i], c->name.s)) { #ifdef BDB_EXTRA_DEBUG LOG(L_NOTICE, "BDB:bdb_rrow_db2bdb: filling column '%.*s', c_idx = %0d\n", c->name.len, c->name.s, c_idx); #endif r[i] = c_idx; found = 1; break; } } if (!found) { LOG(L_ERR, "BDB:bdb_rrow_db2bdb: column '%s' does not exist\n", _k[i]); bdb_free_rrow(r); return -1; } } } else { /* return all columns */ for (c = t->cols, c_idx = 0; c != NULL; c = c->next, c_idx++) { #ifdef BDB_EXTRA_DEBUG LOG(L_NOTICE, "BDB:bdb_rrow_db2bdb: filling column '%.*s', c_idx = %0d\n", c->name.len, c->name.s, c_idx); #endif r[c_idx] = c_idx; } } *_r = r; return 0; };
int bdb_srow_db2bdb(db_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, int _n, bdb_srow_p *_r) { bdb_srow_p r; bdb_table_p t; bdb_column_p c; int found; int use_key, found_key, key_idx; int i; int c_idx; bdb_sval_p v; *_r = NULL; if ((t = bdb_find_table(CON_TABLE(_h))) == NULL) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_srow_db2bdb: no table in use\n"); #endif return -1; }; key_idx = -1; use_key = 0; /* check if all columns exist */ for (i = 0; i < _n; i++) { found = 0; /* key column is always first one */ for (c = t->cols, found_key = 1; c != NULL; c = c->next, found_key = 0) { if (!strcmp(_k[i], c->name.s)) { found = 1; break; } } if (found_key == 1) { /* use key only if it is used in clause and operator is '=' */ if (!use_key && (_op == NULL || (_op != NULL && !strcmp(_op[i], OP_EQ)))) { key_idx = i; use_key = 1; } } if (!found) { LOG(L_ERR, "BDB:bdb_srow_db2bdb: column '%s' does not exist\n", _k[i]); return -1; } } r = pkg_malloc(sizeof(*r)); memset(r, 0, sizeof(*r)); /* filling data into row */ for (c = t->cols, c_idx = 0; c != NULL; c = c->next, c_idx++) { for (i = 0; i < _n; i++) { if (!strcmp(_k[i], c->name.s)) { #ifdef BDB_EXTRA_DEBUG LOG(L_NOTICE, "BDB:bdb_srow_db2bdb: filling column '%.*s', c_idx = %0d\n", c->name.len, c->name.s, c_idx); #endif v = pkg_malloc(sizeof(*v)); memset(v, 0, sizeof(*v)); v->c_idx = c_idx; bdb_push_sfield(r, v); if (bdb_sfield_db2bdb(v, &_v[i], (!_op) ? NULL : _op[i]) < 0) { bdb_free_srow(r); return -1; }; if (use_key && i == key_idx) { bdb_set_skey(r, v); } } } }; *_r = r; return 0; };
int bdb_row_db2bdb(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n, bdb_row_p *_r) { bdb_row_p r; bdb_table_p t; bdb_column_p c; int found; int use_key, found_key, key_idx; int i; bdb_val_p v; *_r = NULL; if ((t = bdb_find_table(CON_TABLE(_h))) == NULL) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_row_db2bdb: table: no table in use\n"); #endif return -1; }; key_idx = -1; use_key = -1; /* check if all columns exist */ for (i = 0; i < _n; i++) { found = 0; /* key column is always first one */ for (c = t->cols, found_key = 1; c != NULL; c = c->next, found_key = 0) { if (!strcmp(_k[i], c->name.s)) { found = 1; break; } } if (found_key == 1) { key_idx = i; use_key++; /* set to 0 if used in clause only once */ } if (!found) { LOG(L_ERR, "BDB:bdb_row_db2bdb: column '%s' does not exist\n", _k[i]); return -1; } } if (use_key < 0) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_row_db2bdb: primary key value must be supplied\n"); #endif return -1; } if (use_key > 0) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_row_db2bdb: primary key value must be supplied only once\n"); #endif return -1; } r = pkg_malloc(sizeof(*r)); memset(r, 0, sizeof(*r)); /* filling data into row */ for (c = t->cols; c != NULL; c = c->next) { v = pkg_malloc(sizeof(*v)); memset(v, 0, sizeof(*v)); VAL_NULL(&(v->v)) = 1; /* default value is NULL */ bdb_push_field(r, v); for (i = 0; i < _n; i++) { if (!strcmp(_k[i], c->name.s)) { #ifdef BDB_EXTRA_DEBUG LOG(L_NOTICE, "BDB:bdb_row_db2bdb: filling column '%.*s'\n", c->name.len, c->name.s); #endif if (bdb_field_db2bdb(v, &_v[i]) < 0) { bdb_free_row(r); return -1; }; if (i == key_idx) { if (bdb_set_key(r, v) < 0) { bdb_free_row(r); return -1; }; } break; } } bdb_push_data(r, v); }; bdb_merge_tail(r); *_r = r; return 0; };
int bdb_urow_db2bdb(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n, bdb_urow_p *_r) { bdb_urow_p r; bdb_table_p t; bdb_column_p c; int found, found_key; int i, j; int c_idx; bdb_uval_p v; *_r = NULL; if (_n <= 0) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_urow_db2bdb: no defined keys to be updated\n"); #endif return -1; } if ((t = bdb_find_table(CON_TABLE(_h))) == NULL) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_urow_db2bdb: no table in use\n"); #endif return -1; }; /* check for dublicates in update set */ for (i = 1; i < _n; i++) { for (j = 0; j < i; j++) { if (!strcmp(_k[i], _k[j])) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_urow_db2bdb: dublicates keys in update set: '%s' and '%s'\n", _k[i], _k[j]); #endif return -1; } } } /* check if all columns exist */ for (i = 0; i < _n; i++) { found = 0; /* key column is always first one */ for (c = t->cols, found_key = 1; c != NULL; c = c->next, found_key = 0) { if (!strcmp(_k[i], c->name.s)) { found = 1; break; } } if (found_key == 1) { LOG(L_ERR, "BDB:bdb_urow_db2bdb: unable to update primary key value\n"); return -1; } if (!found) { LOG(L_ERR, "BDB:bdb_urow_db2bdb: column '%s' does not exist\n", _k[i]); return -1; } } r = pkg_malloc(sizeof(*r)); memset(r, 0, sizeof(*r)); /* filling data into row */ for (c = t->cols, c_idx = 0; c != NULL; c = c->next, c_idx++) { for (i = 0; i < _n; i++) { if (!strcmp(_k[i], c->name.s)) { #ifdef BDB_EXTRA_DEBUG LOG(L_NOTICE, "BDB:bdb_urow_db2bdb: filling column '%.*s', c_idx = %0d\n", c->name.len, c->name.s, c_idx); #endif v = pkg_malloc(sizeof(*v)); memset(v, 0, sizeof(*v)); v->c_idx = c_idx; bdb_push_ufield(r, v); if (bdb_ufield_db2bdb(v, &_v[i]) < 0) { bdb_free_urow(r); return -1; }; } } }; *_r = r; return 0; };