diff options
-rw-r--r-- | db.c | 21 | ||||
-rw-r--r-- | schema.sql | 9 |
2 files changed, 10 insertions, 20 deletions
@@ -131,21 +131,16 @@ struct db_room_list db_list_rooms(i64 userid){ i64 db_create_user(const char *name,const char *pass){ sqlite3_stmt *stmt; - SQLITE(prepare_v2,database,"insert into Users (username, pass) values (?, ?)",-1,&stmt,NULL); + SQLITE(prepare_v2,database,"insert into Users (name, pass) values (?, ?)",-1,&stmt,NULL); SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC); SQLITE(bind_text,stmt,2,pass,-1,SQLITE_STATIC); - if(sqlite3_step(stmt)!=SQLITE_DONE)die_sqlite("sqlite3_step"); - SQLITE(finalize,stmt); - - i64 userid=sqlite3_last_insert_rowid(database); - - SQLITE(prepare_v2,database,"insert into UserNames (name, user) values (?, ?)",-1,&stmt,NULL); - SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC); - SQLITE(bind_int64,stmt,2,userid); - if(sqlite3_step(stmt)!=SQLITE_DONE)die_sqlite("sqlite3_step"); + bool success=sqlite3_step(stmt)==SQLITE_DONE; SQLITE(finalize,stmt); - - return userid; + if(success){ + return sqlite3_last_insert_rowid(database); + } else { + return -1; + } } char* db_get_pass(i64 userid){ @@ -164,7 +159,7 @@ char* db_get_pass(i64 userid){ i64 db_find_user(const char *name){ sqlite3_stmt *stmt; - SQLITE(prepare_v2,database,"select user from UserNames where name = ?",-1,&stmt,NULL); + SQLITE(prepare_v2,database,"select id from Users where name = ?",-1,&stmt,NULL); SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC); i64 userid=-1; if(sqlite3_step(stmt)==SQLITE_ROW){ @@ -16,15 +16,10 @@ create table Members ( create table Users ( id integer primary key, - username text, + name text, pass text ); - -create table UserNames ( - name text primary key, - user integer, - foreign key(user) references Users(id) on delete cascade -); +create unique index users_name_index on Users(name); create table Messages ( id integer primary key, |