#define _GNU_SOURCE #include #include #include #include #include "db.h" #include "schema.sql.h" #define SQLITE(func,...) do{if(sqlite3_##func(__VA_ARGS__)!=SQLITE_OK){die_sqlite("sqlite3_" #func);}}while(0) sqlite3 *database=NULL; __attribute__((noreturn)) static void die_sqlite(const char *func){ die("%s: %s",func,sqlite3_errmsg(database)); } void db_init(void){ SQLITE(config,SQLITE_CONFIG_SERIALIZED); SQLITE(initialize); SQLITE(open_v2,"db.db",&database,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,NULL); char *str=malloc(schema_sql_len+1,char); memcpy(str,schema_sql,schema_sql_len); str[schema_sql_len]='\0'; sqlite3_exec(database,str,NULL,NULL,NULL); free(str); } void db_close(void){ sqlite3_close(database); SQLITE(shutdown); database=NULL; } static char* gen_room_name(void){ const int name_len=8; const char *alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int alpha_len=strlen(alphabet); char *name=malloc(name_len+1,char); for(int i=0;i