Пример #1
0
func (f Feed) DeleteExcludes() {
	el := f.Excludes()
	for _, e := range el {
		e = Escape_guid(e)
		if len(e) < 1 {
			continue
		}
		var query = "delete from ttrss_entries where feed_id=" + u.Tostr(f.ID) + " and title like '%" + e + "%'"
		var stmt, err = u.Sth(db, query)
		if err != nil {
			glog.Errorf("u.Sth(db,%s): %s", query, err)
			return
		}
		if _, err := stmt.Exec(); err != nil {
			glog.Errorf("DeleteExcludes.stmt.Exec: %s", err)
		}
	}
	ed := f.ExcludesData()
	for _, e := range ed {
		e = Escape_guid(e)
		if len(e) < 1 {
			continue
		}
		var query = "delete from ttrss_entries where feed_id=" + u.Tostr(f.ID) + " and content like '%" + e + "%'"
		var stmt, err = u.Sth(db, query)
		if err != nil {
			glog.Errorf("u.Sth(db,%s): %s", query, err)
			return
		}
		if _, err := stmt.Exec(); err != nil {
			glog.Errorf("stmt.Exec: %s", err)
		}
	}
	f.ClearCache()
}
Пример #2
0
func GetGamesByIDS(id_list []int, user auth.User) (gl []Game, err error) {
	if len(id_list) == 1 {
		g, err := GetGame(id_list[0], user)
		gl = append(gl, g)
		return gl, err
	}
	if len(id_list) == 0 {
		return gl, err
	}
	var id_list_string []string
	for _, id := range id_list {
		id_list_string = append(id_list_string, strconv.Itoa(id))
	}
	ids := strings.Join(id_list_string, ",")
	sql := fmt.Sprintf("select games.id, games.name,games.console_name,IFNULL(publisher,''),IFNULL(year,0),IF(ebay_ends > NOW(),ebay_price,0.0),IFNULL(ebay_updated,''),IFNULL(ebay_ends,''),IFNULL(ebay_url,''),IFNULL(has,false),IFNULL(manual,false),IFNULL(box,false),IFNULL(rating,0),IFNULL(review,''),IFNULL(want,false) from  games left join user_games on games.id=user_games.game_id where games.id in (%s) OR (games.id in (%s) AND user_games.user_id=%v) ;", ids, ids, user.ID)
	if user.ID < 1 {
		sql = fmt.Sprintf("select games.id, games.name,games.console_name,IFNULL(publisher,''),IFNULL(year,0),IFNULL(ebay_price,0.0),IFNULL(ebay_updated,''),IFNULL(ebay_ends,''),IFNULL(ebay_url,''),false,false,false,0,'',false from games where id in (%s)", ids)
	}
	sth, err := u.Sth(db, sql)
	if err != nil {
		glog.Errorf("u.Sth(db, %s): %s", sql, err)
	}
	rows, err := sth.Query()
	if err != nil {
		glog.Errorf("sth.Query(): %s", err)
	}
	for rows.Next() {
		var g Game
		rows.Scan(&g.ID, &g.Name, &g.ConsoleName, &g.Publisher, &g.Year, &g.EbayPrice, &g.EbayUpdated, &g.EbayEnds, &g.EbayURL, &g.Has, &g.HasManual, &g.HasBox, &g.Rating, &g.Review, &g.Want)
		g.User = user
		gl = append(gl, g)
	}
	return gl, err
}
Пример #3
0
func (f Feed) MarkEntriesRead(ids []string) (err error) {
	if len(ids) == 0 {
		err = fmt.Errorf("Ids is null")
		return err
	} else {
		// make sure they're all integers
		var id_list []string
		for _, i := range ids {
			if _, err := strconv.Atoi(i); err == nil {
				id_list = append(id_list, i)
			}
		}
		if len(id_list) < 1 {
			return fmt.Errorf("Not enough valid ids passed")
		}
		j := strings.Join(id_list, ",")
		sql := "update ttrss_entries set unread='0' where feed_id=" + u.Tostr(f.ID) + " and id in (" + j + ")"
		stmtUpdateMarkEntries, err := u.Sth(db, sql)
		if err != nil {
			glog.Errorf("u.Sth(db,%s): %s", sql, err)
			return err
		}
		if _, err = stmtUpdateMarkEntries.Exec(); err != nil {
			glog.Errorf("stmtUpdateMarkEntries.Exec(%s): %s", sql, err)
		}
		mc.Decrement("Category"+u.Tostr(f.CategoryID)+"_UnreadCount", uint64(len(ids)))
		mc.Decrement("Feed"+u.Tostr(f.ID)+"_UnreadCount", uint64(len(ids)))
		mc.Delete("Category" + u.Tostr(f.CategoryID) + "_unreadentries")
		mc.Delete("Feed" + u.Tostr(f.ID) + "_unreadentries")
		mc.Delete("Category" + u.Tostr(f.CategoryID) + "_readentries")
		mc.Delete("Feed" + u.Tostr(f.ID) + "_readentries")
	}
	return err
}
Пример #4
0
func DB(d *sql.DB) {
	db = d
	var err error
	userDB()
	stmtCookieIns, err = u.Sth(db, "INSERT INTO sessions (user_id,session_hash) VALUES( ? ,?  )")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtCookieIns) %s", err)
	}
	stmtInsertUser, err = u.Sth(db, "insert into users (email) values (?) ")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtInsertUser) %s", err)
	}
	stmtSessionExists, err = u.Sth(db, "select user_id from sessions where session_hash=?")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtSessionExists) %s", err)
	}
}
Пример #5
0
func DB(d *sql.DB) {
	db = d
	var err error
	stmtCookieIns, err = u.Sth(db, "INSERT INTO sessions (user_id,session_hash) VALUES( ? ,?  )")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtCookieIns) %s", err)
	}
	stmtGetUserID, err = u.Sth(db, "select id from users where email = ?")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtGetUserID) %s", err)
	}
	stmtInsertUser, err = u.Sth(db, "insert into users (id,email) values (?,?) ")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtInsertUser) %s", err)
	}
	stmtGetUserBySession, err = u.Sth(db, "select users.id, users.email from users, sessions where users.id=sessions.user_id and sessions.session_hash=?")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtGetUserBySession) %s", err)
	}
	stmtSessionExists, err = u.Sth(db, "select user_id from sessions where session_hash=?")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtSessionExists) %s", err)
	}
	stmtLogoutSession, err = u.Sth(db, "delete from sessions where session_hash=? limit 1")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtLogoutSession) %s", err)
	}
	stmtGetAllUsers, err = u.Sth(db, "select id, email from users where 1")
	if err != nil {
		glog.Fatalf("sth(db, select id, email from users where 1): %s", err)
	}
}
Пример #6
0
func ConsoleDB(d *sql.DB) {
	var err error
	db = d
	stmtGetConsoles, err = u.Sth(db, "select c.name,IFNULL(c.manufacturer,''),IFNULL(c.year,0),IFNULL(c.picture,'') from consoles as c")
	if err != nil {
		glog.Fatalf("u.Sth(db,select id, IFNULL(name,''),IFNULL(manufacturer,''),IFNULL(year,0) from consoles): %s", err)
	}
	stmtGetConsole, err = u.Sth(db, "select consoles.name,IFNULL(manufacturer,''),IFNULL(year,0),IFNULL(picture,'') from consoles where consoles.name=?")
	if err != nil {
		glog.Fatalf("u.Sth(db,select id, IFNULL(name,''),IFNULL(manufacturer,''),IFNULL(year,0) from consoles where id=?): %s", err)
	}
	stmt := "select IFNULL(has,false),IFNULL(manual,false),IFNULL(box,false),IFNULL(rating,0),IFNULL(review,''),IFNULL(user_consoles.want,false),IFNULL(user_consoles.wantgames,false) from consoles, user_consoles where user_id=? and user_consoles.name=?"
	stmtGetUserConsole, err = u.Sth(db, stmt)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", stmt, err)
	}
	stmtUpdateConsole, err = u.Sth(db, "replace into consoles (name,manufacturer,year,picture) values (?,?,?,?)")
	if err != nil {
		glog.Errorf("u.Sth(db,replace into consoles (name,manufacturer,year) values (?,?,?): %s", err)
	}
	stmtUpdateHasConsole, err = u.Sth(db, "replace into user_consoles (name,user_id,has,manual,box,rating,review,want,wantgames) values (?,?,?,?,?,?,?,?,?)")
	if err != nil {
		glog.Errorf("u.Sth(db,replace into user_consoles (name,user_id,has,manual,box,rating,review) values (?,?,?,?,?,?,?): %s", err)
	}
	stmtGetUserConsoles, err = u.Sth(db, "select name,IFNULL(has,false),IFNULL(manual,false),IFNULL(box,false),IFNULL(rating,0),IFNULL(review,''),IFNULL(want,false),IFNULL(wantgames,false) from user_consoles where user_id=?")
	if err != nil {
		glog.Errorf("u.Sth(db,select IFNULL(name,0),IFNULL(has,false),IFNULL(manual,false),IFNULL(box,false),IFNULL(rating,0),IFNULL(review,'') from user_consoles where user_id=?): %s", err)
	}
	delcon := "delete from consoles where name=? limit 1"
	stmtDeleteConsole, err = u.Sth(db, delcon)
	if err != nil {
		glog.Fatalf("u.Sth(db,%s): %s", delcon, err)
	}

}
Пример #7
0
func Categoryinit(dbh *sql.DB, mch *easymemcache.Client) {
	var err error
	db = dbh
	mc = mch
	stmtGetCat, err = u.Sth(db, "select name,user_name,IFNULL(description,''),id, exclude from ttrss_categories where id = ?")
	if err != nil {
		glog.Fatalf("sth(): %s", err)
	}
	stmtGetCats, err = u.Sth(db, "select name,user_name,IFNULL(description,''),id,exclude from ttrss_categories where user_name= ?")
	if err != nil {
		glog.Fatalf("sth(): %s", err)
	}
	stmtGetAllCats, err = u.Sth(db, "select name,user_name,IFNULL(description,''),id,exclude from ttrss_categories")
	if err != nil {
		glog.Fatalf("sth(): %s", err)
	}
	stmtSaveCat, err = u.Sth(db, "update ttrss_categories set name=?,description=?, exclude=? where id=? limit 1")
	if err != nil {
		glog.Fatalf("sth(): %s", err)
	}
	stmtAddCat, err = u.Sth(db, "insert into ttrss_categories (user_name,name) values (?,?)")
	if err != nil {
		glog.Fatalf("sth(): %s", err)
	}
	stmtResetCategories, err = u.Sth(db, "update ttrss_feeds set category_id=NULL where category_id= ?")
	if err != nil {
		glog.Fatalf("sth(): %s", err)
	}
	stmtDeleteCategory, err = u.Sth(db, "delete from ttrss_categories where id=? limit 1")
	if err != nil {
		glog.Fatalf("sth(): %s", err)
	}
}
Пример #8
0
func getEntriesFromSql(s string) []Entry {
	var el []Entry
	var stmt, err = u.Sth(db, s)
	if err != nil {
		glog.Errorf("Error preparing statment '%s': %s", s, err)
		return el
	}
	el, err = getEntriesFromSth(stmt)
	return el
}
Пример #9
0
func Entryinit() {
	var err error
	entrySelectString = " id,IFNULL(title,''),IFNULL(link,''),IFNULL(updated,''),IFNULL(marked,0),IFNULL(unread,1),feed_id,content,guid "
	stmtAddEntry, err = u.Sth(db, "insert into ttrss_entries (updated,title,link,feed_id,marked,content,content_hash,unread,guid,user_name) values (NOW(),?,?,?,?,?,?,1,?,?)")
	if err != nil {
		glog.Fatalf("stmt: %s", err)
	}
	stmtUpdateMarkEntry, err = u.Sth(db, "update ttrss_entries set marked=? where id=?")
	if err != nil {
		glog.Fatalf("stmtUpdateMarkEntry: %s", err)
	}
	stmtUpdateReadEntry, err = u.Sth(db, "update ttrss_entries set unread=? where id=?")
	if err != nil {
		glog.Fatalf("stmtUpdateReadEntry: %s", err)
	}
	stmtSaveEntry, err = u.Sth(db, "update ttrss_entries set title=?,link=?,updated=?,feed_id=?,marked=?,unread=?,content=? where id=? limit 1")
	if err != nil {
		glog.Fatalf("stmtSaveEntry: %s", err)
	}
	stmtGetEntryCount, err = u.Sth(db, "select count(id) from ttrss_entries")
	if err != nil {
		glog.Fatalf("stmtGetEntryCount: %s", err)
	}
	stmtGetEntry, err = u.Sth(db, "select "+entrySelectString+"from ttrss_entries where id=?")
	if err != nil {
		glog.Fatalf("stmtGetEntry: %s", err)
	}
}
Пример #10
0
func init() {
	var err error
	db, err = sql.Open("mysql", "feedinator_test:feedinator_test@tcp(localhost:3306)/feedinator_test")
	if err != nil {
		glog.Fatalf("error: %s", err)
	}
	mc = &tmc
	initDB()
	if d_ca, err = u.Sth(db, "delete from ttrss_categories"); err != nil {
		glog.Fatalf("delete from categories: %s", err)
	}
	if p_ca, err = u.Sth(db, "insert into ttrss_categories(id, name, user_name) values (1,'cat1','test'),(2,'cat2','test'),(3,'cat3','test'),(4,'cat4','test'),(5,'cat5','other'),(6,'cat6','other'),(7,'cat7','other'),(8,'cat8','other')"); err != nil {
		glog.Fatalf("seed categories: %s", err)
	}
	if d_fe, err = u.Sth(db, "delete from ttrss_feeds"); err != nil {
		glog.Fatalf("delete from feeds: %s", err)
	}
	if p_fe, err = u.Sth(db, "insert into ttrss_feeds (id,title,feed_url,user_name,category_id,view_mode) values (1,'test1','http://blah','test',1,'default'),(2,'test2','http://blah','test',1,'default'),(3,'test3','http://blah','other',2,'default'),(4,'test4','http://blah','other',NULL,'default'),(5,'feed','http://blah','test',NULL,'default'),(6,'feed2','http://blah','test',NULL,'default'),(7,'feed3','http://blah','other',NULL,'default'),(8,'feed4','http://blah','other',NULL,'default')"); err != nil {
		glog.Fatalf("Seed feeds: %s", err)
	}
	if d_en, err = u.Sth(db, "delete from ttrss_entries"); err != nil {
		glog.Fatalf("delete from entries: %s", err)
	}
	if p_en, err = u.Sth(db, "insert into ttrss_entries(id,feed_id,guid,content_hash,title,user_name,link,content) values (1,1,1,1,'asdf','test','link','content1'),(2,2,2,2,'asdf','test','link','content2'),(3,1,3,3,'another','other','link','content3'),(4,1,4,4,'another','other','link','content4'),(5,5,5,5,'title','yetanother','link','content5')"); err != nil {
		glog.Fatalf("insert into entries: %s", err)
	}
	Categoryinit(db, mc)
	Feedinit()
	Entryinit()
}
Пример #11
0
func Feedinit() {
	var selecttxt string = " id, IFNULL(title,'--untitled--'), IFNULL(feed_url,''), IFNULL(last_updated,''), IFNULL(user_name,''), IFNULL(public,''),  IFNULL(category_id,0), IFNULL(view_mode,''), IFNULL(autoscroll_px,0), IFNULL(exclude,''),IFNULL(exclude_data,''), IFNULL(error_string,''), IFNULL(expirey,'') "
	var err error
	stmtInsertFeed, err = u.Sth(db, "insert into ttrss_feeds (feed_url,user_name,title) values (?,?,?)")
	if err != nil {
		glog.Fatalf("stmtInsertFeed: %s", err)
	}
	stmtGetFeeds, err = u.Sth(db, "select "+selecttxt+" from ttrss_feeds where user_name = ?")
	if err != nil {
		glog.Fatalf("stmtGetFeeds: %s", err)
	}
	stmtGetAllFeeds, err = u.Sth(db, "select "+selecttxt+"	from ttrss_feeds")
	if err != nil {
		glog.Fatalf("stmtGetAllFeeds: %s", err)
	}
	stmtGetFeed, err = u.Sth(db, "select "+selecttxt+"	from ttrss_feeds where id = ?")
	if err != nil {
		glog.Fatalf("stmtGetFeed: %s", err)
	}
	stmtFeedUnread, err = u.Sth(db, "select count(ttrss_entries.id) as unread from ttrss_entries where ttrss_entries.feed_id=? and ttrss_entries.unread='1'")
	if err != nil {
		glog.Fatalf("stmtFeedUnread: %s", err)
	}
	stmtGetFeedsWithoutCats, err = u.Sth(db, "select "+selecttxt+" from ttrss_feeds where user_name=? and (category_id is NULL or category_id=0) order by id ASC")
	if err != nil {
		glog.Fatalf("stmtGetFeedsWithoutCats: %s", err)
	}
	stmtSaveFeed, err = u.Sth(db, "update ttrss_feeds set title=?, feed_url=?,public=?,category_id=?,view_mode=?,autoscroll_px=?,exclude=?,exclude_data=?,expirey=?, error_string=? where id=? limit 1")
	if err != nil {
		glog.Fatalf("stmtSaveFeed: %s", err)
	}
	stmtDeleteFeedEntries, err = u.Sth(db, "delete from ttrss_entries where feed_id=?")
	if err != nil {
		glog.Fatalf("stmtDeleteFeedEntries: %s", err)
	}
	stmtDeleteFeed, err = u.Sth(db, "delete from ttrss_feeds where id=? limit 1")
	if err != nil {
		glog.Fatalf("stmtDeleteFeed: %s", err)
	}
}
Пример #12
0
func init() {
	var err error
	db, err = sql.Open("mysql", "feedinator_test:feedinator_test@tcp(localhost:3306)/feedinator_test")
	if err != nil {
		glog.Fatalf("error: %s", err)
	}
	initDB()
	DB(db)
	mc := &tmc
	del_us, err = u.Sth(db, "delete from users where 1")
	if err != nil {
		glog.Fatalf("delete from users: %s", err)
	}
	pop_us, err = u.Sth(db, "insert into users (id, email) values (1,'user1'),(2,'user2'),(3,'user3'),(4,'user4')")
	if err != nil {
		glog.Fatalf("insert into users: %s", err)
	}
	del_ss, err = u.Sth(db, "delete from sessions where 1")
	if err != nil {
		glog.Fatalf("delete from sessions: %s", err)
	}
	pop_ss, err = u.Sth(db, "insert into sessions (user_id, session_hash) values (1,'user1'),(2,'user2')")
	if err != nil {
		glog.Fatalf("insert into sessions: %s", err)
	}
	if del_cc, err = u.Sth(db, "delete from ttrss_categories"); err != nil {
		glog.Fatalf("delete from categories: %s", err)
	}
	if pop_cc, err = u.Sth(db, "insert into ttrss_categories(id, name, user_name) values (1,'cat1','user1'),(2,'cat2','user1'),(3,'cat3','user1'),(4,'cat4','user1'),(5,'cat5','other'),(6,'cat6','other'),(7,'cat7','other'),(8,'cat8','other')"); err != nil {
		glog.Fatalf("seed categories: %s", err)
	}
	if del_ff, err = u.Sth(db, "delete from ttrss_feeds"); err != nil {
		glog.Fatalf("delete from feeds: %s", err)
	}
	if pop_ff, err = u.Sth(db, "insert into ttrss_feeds (id,title,feed_url,user_name,category_id) values (1,'test1','http://blah','user1',1),(2,'test2','http://blah','user1',1),(3,'test3','http://blah','other',2),(4,'test4','http://blah','other',NULL),(5,'feed','http://blah','user1',NULL),(6,'feed2','http://blah','user1',NULL),(7,'feed3','http://blah','other',NULL),(8,'feed4','http://blah','other',NULL)"); err != nil {
		glog.Fatalf("Seed feeds: %s", err)
	}
	feed.Categoryinit(db, mc)
	feed.Feedinit()

}
Пример #13
0
func (c Category) MarkEntriesRead(ids []string) (err error) {
	if len(ids) == 0 {
		err = fmt.Errorf("Ids is null")
	} else {
		// make sure they're all integers
		var id_list []string
		for _, i := range ids {
			_, err := strconv.Atoi(i)
			if err != nil {
				glog.Errorf("Non int passed to markEntriesRead: %s", err)
			} else {
				id_list = append(id_list, i)
			}
		}
		if len(id_list) < 1 {
			err = fmt.Errorf("Not enough valid ids passed")
			return err
		}
		j := strings.Join(id_list, ",")
		sql := "update ttrss_entries set unread='0' where id in (" + j + ")"
		stmtUpdateMarkEntries, err := u.Sth(db, sql)
		if err != nil {
			glog.Errorf("Sth(db, %s): %s", sql, err)
			return err
		}
		_, err = stmtUpdateMarkEntries.Exec()
		if err != nil {
			glog.Errorf("stmtUpdateMarkEntries.Exec: %s\n%s\n", err, sql)
			return err
		}
		mc.Decrement("Category"+u.Tostr(c.ID)+"_UnreadCount", uint64(len(ids)))
		mc.Delete("Category" + u.Tostr(c.ID) + "_unreadentries")
		mc.Delete("Category" + u.Tostr(c.ID) + "_readentries")
		for _, f := range c.Feeds() {
			mc.Delete("Feed" + u.Tostr(f.ID) + "_readentries")
			mc.Delete("Feed" + u.Tostr(f.ID) + "_UnreadCount")
			mc.Delete("Feed" + u.Tostr(f.ID) + "_unreadentries")
		}
	}
	return err
}
Пример #14
0
func GameDB(d *sql.DB) {
	var err error
	db = d
	gameselect := "games.id, games.name,games.console_name,IFNULL(publisher,''),IFNULL(year,0),IF(ebay_ends > NOW(),ebay_price,0.0),IFNULL(ebay_updated,''),IFNULL(ebay_ends,''),IFNULL(ebay_url,''),IFNULL(user_id,0),IFNULL(has,false),IFNULL(manual,false),IFNULL(box,false),IFNULL(rating,0),IFNULL(review,''),IFNULL(want,false)"
	sgg := "select " + gameselect + " from games left join user_games on games.id=user_games.game_id where games.id=? OR (games.id=? AND user_games.user_id=?)"
	stmtGetGame, err = u.Sth(db, sgg)
	if err != nil {
		glog.Fatalf("%s: %s", sgg, err)
	}

	sug := "update games set name=?, console_name=?, publisher=?, year=?, ebay_price=?, ebay_ends=?, ebay_url=?,ebay_updated=? where id=? limit 1"
	stmtUpdateGame, err = u.Sth(db, sug)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sug, err)
	}

	sig := "insert into games (name, console_name, publisher, year) values (?,?,?,?)"
	stmtInsertGame, err = u.Sth(db, sig)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sig, err)
	}

	suug := "replace into user_games (game_id, user_id, has, manual, box, rating, review,want) values (?,?,?,?,?,?,?,?)"
	stmtUpdateUserGame, err = u.Sth(db, suug)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", suug, err)
	}

	sggbs := "select id from games where console_name=?"
	stmtGetGameByConsole, err = u.Sth(db, sggbs)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sggbs, err)
	}

	sgbsn := "select id  from games where name like ?"
	stmtSearchGameNames, err = u.Sth(db, sgbsn)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sgbsn, err)
	}

	sdelgame := "delete from games where id=? limit 1"
	stmtDeleteGame, err = u.Sth(db, sdelgame)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sdelgame, err)
	}

	sgetowners := "select count(*) from user_games where game_id=? and has=true"
	stmtGetOwners, err = u.Sth(db, sgetowners)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sgetowners, err)
	}

	sgug := "select game_id from user_games where user_id=?"
	stmtGetUserGames, err = u.Sth(db, sgug)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sgug, err)
	}

	sql := "select distinct(g.id) from games as g, consoles as c, user_consoles as uc where (uc.wantgames=1 and uc.name=g.console_name)"
	stmtGetWantedGamesByConsole, err = u.Sth(db, sql)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sql, err)
	}

	sql = "select distinct(g.game_id) from user_games as g where want=1"
	stmtGetWantedGamesByGame, err = u.Sth(db, sql)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sql, err)
	}

	sql = "select distinct(g.id) from games as g, user_consoles as uc, user_games as ug where (uc.wantgames=1 and uc.name=g.console_name and uc.user_id=?) or (ug.user_id=? and ug.want=1 and ug.game_id=g.id)"
	stmtUserWantedGames, err = u.Sth(db, sql)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sql, err)
	}

}
Пример #15
0
func userDB() {
	var err error

	scst := "create table  if not exists `sessions` ( `user_id` varchar(255) NOT NULL, `session_hash` char(255) NOT NULL, PRIMARY KEY (`session_hash`)) ;"
	_, err = db.Exec(scst)
	if err != nil {
		glog.Fatalf("db.Exec(%s): %s", scst, err)
	}

	scut := "create table if not exists `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `email` varchar(128) DEFAULT NULL, `admin` bool DEFAULT false, `share_token` char(128) DEFAULT NULL, `login_token` char(128) DEFAULT NULL, PRIMARY KEY (`id`))"
	_, err = db.Exec(scut)
	if err != nil {
		glog.Fatalf("db.Exec(%s): %s", scut, err)
	}

	sius := "update users set share_token=? where id=?"
	stmtInsertUserShare, err = u.Sth(db, sius)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", sius, err)
	}

	sgus := "select ifnull(share_token,'') from users where id = ?"
	stmtGetUserShare, err = u.Sth(db, sgus)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", sgus, err)
	}

	siul := "update users set login_token=? where id=?"
	stmtInsertUserLogin, err = u.Sth(db, siul)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", siul, err)
	}

	sgul := "select ifnull(login_token,'') from users where id=?"
	stmtGetUserLogin, err = u.Sth(db, sgul)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", sgul, err)
	}

	sguid := "select id,admin from users where email = ?"
	stmtGetUserID, err = u.Sth(db, sguid)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", sguid, err)
	}

	sls := "delete from sessions where session_hash=? limit 1"
	stmtLogoutSession, err = u.Sth(db, sls)
	if err != nil {
		glog.Fatalf(" DB(): u.sth(%s) %s", sls, err)
	}

	sgubs := "select users.id, users.email, IFNULL(users.admin,false) from users, sessions where users.id=sessions.user_id and sessions.session_hash=?"
	stmtGetUserBySession, err = u.Sth(db, sgubs)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", sgubs, err)
	}

	sgubsh := "select id, email, IFNULL(admin,false) from users where share_token = ?"
	stmtGetUserByShared, err = u.Sth(db, sgubsh)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", sgubsh, err)
	}

	sgubid := "select ID,Email,admin from users where id=?"
	stmtGetUserByID, err = u.Sth(db, sgubid)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", sgubid, err)
	}

	sgublc := "select id, email, admin from users where login_token = ?"
	stmtGetUserByLoginCode, err = u.Sth(db, sgublc)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", sgublc, err)
	}

	ssa := "update users set admin=? where id=? limit 1"
	stmtSetAdmin, err = u.Sth(db, ssa)
	if err != nil {
		glog.Fatalf("u.Sth(%s): %s", ssa, err)
	}

	sga := "select id, email, admin from users where 1"
	stmtGetAllUsers, err = u.Sth(db, sga)
	if err != nil {
		glog.Fatalf("u.Sth(db, %s): %s", sga, err)
	}

}