From f1634688d62f2a6115739b65a311ab2b1cf79764 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 15 Mar 2017 08:53:25 +0100 Subject: Remove useless UserNames table --- db.c | 21 ++++++++------------- schema.sql | 9 ++------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/db.c b/db.c index 9efbd4a..068817c 100644 --- a/db.c +++ b/db.c @@ -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){ diff --git a/schema.sql b/schema.sql index 9f6da9e..321cb38 100644 --- a/schema.sql +++ b/schema.sql @@ -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, -- cgit v1.2.3-70-g09d2