func NewMsgRepository(db *sql.DB) *MsgRepository { mr := &MsgRepository{} mr.db = db mr.urIDs = make(map[types.ID]types.ID, 10240) gsql.MustExec(mr.db, `create table if not exists read_msgs( user_id bigint primary key, msg_id bigint not null default 0 )`) for i := 0; i < userMsgTableCount; i++ { mr.CreateUserMsgTable(types.ID(i)) } for i := 0; i < groupMsgTableCount; i++ { mr.CreateGroupMsgTable(types.ID(i)) } var rows *sql.Rows rows, err := mr.db.Query("select user_id, msg_id from read_msgs") if err != nil { log.Panic(err) } defer rows.Close() var uid, msgID types.ID for rows.Next() { err = rows.Scan(&uid, &msgID) if err != nil { panic(err) } mr.urIDs[uid] = msgID } return mr }
func initIdentity() { gsql.MustExec(_db, `create table if not exists identities( account_id bigint primary key, type int not null default 0, number varchar(50) not null default '', photos varchar(1024) not null default '', created_at bigint not null, status int not null default 0)`) }
func NewGroupRepository(db *sql.DB) *GroupRepository { gr := &GroupRepository{} gr.db = db gsql.MustExec(gr.db, `create table if not exists groups( id bigint primary key, status tinyint not null default 0, created_at bigint not null )`) gsql.MustExec(gr.db, `create table if not exists members( group_id bigint not null, user_id bigint not null, role tinyint not null default 0, nick varchar(12) not null default '', primary key(group_id, user_id) )`) return gr }
func (mr *MsgRepository) CreateGroupMsgTable(groupID types.ID) { gsql.MustExec(mr.db, "create table if not exists "+getGroupMsgTable(groupID)+`( id bigint auto_increment primary key, msg_id bigint not null unique, local_id bigint not null, sender_id bigint not null, receiver_id bigint not null, content varchar(512) not null default '', at_ids varchar(512) not null default '', created_at bigint not null, unique(local_id,sender_id,receiver_id) )`) }
func initOpenAccount() { gsql.MustExec(_db, `create table if not exists open_accounts( type tinyint not null, open_id varchar(64) not null, access_token varchar(64) not null, refresh_token varchar(64) not null, expired_at bigint not null default 0, nick varchar(50) not null default '', avatar varchar(255) not null default '', updated_at bigint not null default 0, info varchar(1024) not null default '', primary key(type, open_id) )`) }
func initAccounts() { gsql.MustExec(_db, `create table if not exists accounts( id bigint primary key, open_id char(70) not null default '', mobile varchar(20) not null default '', email varchar(40) not null default '', password char(40) not null, login_token char(40) unique, nick varchar(20) unique, gender tinyint not null default 0, avatar varchar(255) not null default '', birthdate char(10) not null default '', created_at bigint not null default 0, status tinyint not null default 0)`) }