int db_dbi_open(MDB_txn *const txn, DB_schema *const schema, unsigned int opts, DB_column const *const cols, count_t const ncols, strarg_t const name, MDB_dbi *const dbi) { int rc; uint64_t const dbname_id = db_string_id(txn, schema, name); if(!dbname_id) return -1; DB_VAL(dbinfo_val, 2); db_bind(dbinfo_val, 0); db_bind(dbinfo_val, dbname_id); DB_VAL(info_val, 1); db_bind(info_val, 0xff & opts); mdb_put(txn, schema->schema, dbinfo_val, info_val, MDB_NOOVERWRITE); // TODO: Check opts MDB_cursor *cur = NULL; mdb_cursor_open(txn, schema->schema, &cur); DB_VAL(dbcols_val, 2); db_bind(dbcols_val, 1); db_bind(dbcols_val, dbname_id); MDB_val col_val; mdb_cursor_get(cur, &dbcols_val, &col_val, MDB_GET); for(; MDB_SUCCESS == rc; rc = mdb_cursor_get(cur, &dbcols_val, &col_val, MDB_NEXT_DUP)) { uint64_t const col = db_column(col_val, 0); uint64_t const type = db_column(col_val, 1); strarg_t const colname = db_column_text(txn, schema, col_val, 2); if(col >= ncols) break; // Extra columns are not an error. if(type != cols[i].type || 0 != strcmp(colname, cols[i].name)) { mdb_cursor_close(cur); cur = NULL; return -1; } } mdb_cursor_close(cur); cur = NULL; for(index_t i = 0; i < ncols; ++i) { uint64_t const colname_id = db_string_id(txn, schema, cols[i].name); if(!colname_id) return -1; DB_VAL(col_val, 3); db_bind(col_val, i); db_bind(col_val, cols[i].type); db_bind(col_val, colname_id); rc = mdb_put(txn, schema->schema, dbcols_val, col_val, MDB_NODUPDATA); if(MDB_SUCCESS != rc && MDB_KEYEXIST != rc) return -1; } mdb_dbi_open(txn, name, MDB_CREATE | opts, dbi); return 0; }
int do_binds(char *sql) { char *txt="X"; int ok; ok = db_compile(db,sql) && db_bind(db,":txt",dbChar,0,txt,-1) && db_open(db) && db_exec(db); if (!ok) { printf("compile failed: %s, SQL:%s\n",db->error,sql); } ok = db_fetch(db); printf("compiled ok, fetch = %d\n",ok); if (!ok) return 0; printf("text:'%s'\n",db_text(db->out.cols)); return 1; }
int onDbVarGet(Socket *sock, vssHttp *req, SocketMap *map) { // Генерация статистики по серверу char buf[1024],id[80],name[80]; httpSrv *srv = (void*)sock->pool; strSetLength(&srv->buf,0); // ClearResulted vss r=req->B; vssGetVar2Buffer(req->args,"id",id,sizeof(id)); vssGetVar2Buffer(req->args,"name",name,sizeof(name)); //strCat(&srv->buf,r.data,r.len); char *sql = srv->buf; printf("DB->REQ: get a var '%s' for id '%s'\n",name,id); if (db_compile(db, var_get_sql) && db_bind(db,"id",dbChar,0,id,strlen(id)) && db_bind(db,"name",dbChar,0,name,strlen(name)) && db_open(db) && db_exec(db) && db_fetch(db) ) { // ok - respons a data printf("Yes!\n"); char *t = db_text(db->out.cols); printf("Resp:%s\n",t); SocketSendHttp(sock,req,t,-1); // send result of first column } else { //error printf("ERR:%s\n",db->error); SocketPrintHttp(sock,req,"-%s",db->error); } return 1; }