Ejemplo n.º 1
0
// postlistQueryBuilder returns the same pointer passed as first argument, with new specified options setted
// If the user parameter is present, it's intentend to be the user browsing the website.
// So it will be used to fetch the following list -> so we can easily find the posts on a bord/project/home/ecc made by the users that "user" is following
func postlistQueryBuilder(query *igor.Database, options PostlistOptions, user ...*User) *igor.Database {
	query = query.Limit(int(AtMostPosts(uint64(options.N))))

	userOK := len(user) == 1 && user[0] != nil
	followersTable := UserFollower{}.TableName()

	if !options.Followers && options.Following && userOK { // from following + me
		query = query.Where(`"from" IN (SELECT "to" FROM `+followersTable+` WHERE "from" = ? UNION ALL SELECT ?)`, user[0].Counter, user[0].Counter)
	} else if !options.Following && options.Followers && userOK { //from followers + me
		query = query.Where(`"from" IN (SELECT "from" FROM `+followersTable+` WHERE "to" = ? UNION ALL SELECT ?)`, user[0].Counter, user[0].Counter)
	} else if options.Following && options.Followers && userOK { //from friends + me
		query = query.Where(`"from" IN (SELECT ? UNION ALL (SELECT "to" FROM (SELECT "to" FROM `+
			followersTable+
			` WHERE "from" = ?) AS f INNER JOIN (SELECT "from" FROM `+
			followersTable+
			` WHERE "to" = ?) AS e on f.to = e.from))`, user[0].Counter, user[0].Counter, user[0].Counter)
	}

	if options.Language != "" {
		query = query.Where(options.Model.TableName()+".lang = ?", options.Language)
	}

	messageTpl := Message{}.TableName()

	if options.Older != 0 && options.Newer != 0 {
		if options.Model.TableName() == messageTpl {
			query = query.Where(
				`"time" BETWEEN
				(SELECT "time" FROM `+options.NewerModel.TableName()+` WHERE hpid = ?)
				AND
				(SELECT "time" FROM `+options.OlderModel.TableName()+` WHERE hpid = ?)
				AND hpid NOT IN (?)`, options.Newer, options.Older, []uint64{options.Newer, options.Older})
		} else {
			query = query.Where("hpid BETWEEN ? AND ?", options.Newer, options.Older)
		}
	} else if options.Older != 0 {
		if options.Model.TableName() == messageTpl {
			query = query.Where(
				`"time" <= (
					SELECT "time" FROM `+options.OlderModel.TableName()+` WHERE hpid = ?
				) AND hpid <> ?`, options.Older, options.Older)
		} else {
			query = query.Where("hpid < ?", options.Older)
		}
	} else if options.Newer != 0 {
		if options.Model.TableName() == messageTpl {
			query = query.Where(
				`"time" >= (
					SELECT "time" FROM `+options.NewerModel.TableName()+` WHERE hpid = ?
				) AND hpid <> ?`, options.Newer, options.Newer)
		} else {
			query = query.Where("hpid > ?", options.Newer)
		}
	}

	return query
}
Ejemplo n.º 2
0
// pmsQueryBuilder returns the same pointer passed as first argument, with new specified options setted
func pmsQueryBuilder(query *igor.Database, options PmsOptions) *igor.Database {
	query = query.Limit(int(AtMostPms(uint64(options.N)))).Order("pmid DESC")
	if options.Older != 0 && options.Newer != 0 {
		query = query.Where("pmid BETWEEN ? AND ?", options.Newer, options.Older)
	} else if options.Older != 0 {
		query = query.Where("pmid < ?", options.Older)
	} else if options.Newer != 0 {
		query = query.Where("pmid > ?", options.Newer)
	}
	return query
}
Ejemplo n.º 3
0
// postlistQueryBuilder returns the same pointer passed as first argument, with new specified options setted
func commentlistQueryBuilder(query *igor.Database, options CommentlistOptions) *igor.Database {
	query = query.Limit(int(AtMostComments(uint64(options.N)))).Order("hcid DESC")

	if options.Older != 0 && options.Newer != 0 {
		query = query.Where("hcid BETWEEN ? AND ?", options.Newer, options.Older)
	} else if options.Older != 0 {
		query = query.Where("hcid < ?", options.Older)
	} else if options.Newer != 0 {
		query = query.Where("hcid > ?", options.Newer)
	}

	return query

}