/* * Insert a row into table */ int bdb_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) { bdb_row_p r; DBC *cursorp; int ret; if ((ret = bdb_row_db2bdb(_h, _k, _v, _n, &r)) < 0) { return -1; }; if (r->key.size == 0) { LOG(L_ERR, "BDB:bdb_insert: no primary key specified\n"); bdb_free_row(r); return -1; } BDB_CON_DB(_h)->cursor(BDB_CON_DB(_h), NULL, &cursorp, DB_WRITECURSOR); ret = cursorp->c_put(cursorp, &(r->key), &(r->data), DB_KEYLAST); if (ret < 0) { LOG(L_ERR, "BDB:bdb_insert: unable to insert record: %s\n", db_strerror(ret)); } if (cursorp != NULL) { cursorp->c_close(cursorp); } bdb_free_row(r); return ret; }
void bdb_free_row_list(bdb_row_p _r) { bdb_row_p curr, next; for (curr = _r; curr != NULL;) { next = curr->next; bdb_free_row(curr); curr = next; } };
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; };