func readFromDb(queries string) webData {
	//Reading from database
	log.Println("Refreshing database read")
	tempData := newWebData()
	var (
		url            string
		title          string
		id             int
		state          string
		body           string
		creation_date  int64
		last_edit_time sql.NullInt64
		owner          sql.NullInt64
		name           sql.NullString
		pic            sql.NullString
		link           sql.NullString
	)
	//Select all questions in the database and read into a new data object
	query := "SELECT * FROM questions LEFT JOIN user ON questions.user=user.id"
	if queries != "" {
		query = query + " WHERE state='unanswered' OR " + queries
	}
	rows, err := db.Query(query)
	if err != nil {
		log.Fatal("query failed:\t", err)
	}

	defer rows.Close()
	//Iterate through each row and add to the correct cache
	for rows.Next() {
		err := rows.Scan(&id, &title, &url, &state, &owner, &body, &creation_date, &last_edit_time, &owner, &name, &pic, &link)
		currentQ := stackongo.Question{
			Question_id:   id,
			Title:         title,
			Link:          url,
			Body:          body,
			Creation_date: creation_date,
		}
		if last_edit_time.Valid {
			currentQ.Last_edit_date = last_edit_time.Int64
		}
		if err != nil {
			log.Fatal("query failed:\t", err)
		}

		var tagToAdd string
		//Get tags for that question, based on the ID
		tagRows, err := db.Query("SELECT tag from question_tag where question_id = ?", currentQ.Question_id)
		if err != nil {
			log.Fatal("Tag retrieval failed!\t", err)
		}
		defer tagRows.Close()
		for tagRows.Next() {
			err := tagRows.Scan(&tagToAdd)
			if err != nil {
				log.Fatal("Could not scan for tag!\t", err)
			}
			currentQ.Tags = append(currentQ.Tags, tagToAdd)
		}
		//Switch on the state as read from the database to ensure question is added to correct cace
		tempData.Caches[state] = append(tempData.Caches[state], currentQ)

		if owner.Valid {
			user := stackongo.User{
				User_id:       int(owner.Int64),
				Display_name:  name.String,
				Profile_image: pic.String,
			}
			tempData.Qns[id] = user
			if _, ok := tempData.Users[user.User_id]; !ok {
				tempData.Users[user.User_id] = newUser(user, "")
			}
			tempData.Users[user.User_id].Caches[state] = append(tempData.Users[user.User_id].Caches[state], currentQ)
		}
	}

	for cacheType, _ := range tempData.Caches {
		sort.Sort(byCreationDate(tempData.Caches[cacheType]))
	}

	mostRecentUpdate = time.Now().Unix()
	return tempData
}
Exemple #2
0
// Returns questions and user data from the db filtered by parameters
func readFromDb(ctx context.Context, params string) (webData, int64, error) {
	log.Infof(ctx, "Refreshing database read")

	tempData := newWebData()
	var (
		url            string
		title          string
		id             int
		state          string
		body           string
		creation_date  int64
		last_edit_time sql.NullInt64
		owner          sql.NullInt64
		name           sql.NullString
		pic            sql.NullString
		link           sql.NullString
	)

	//Select all questions in the database and read into a new data object
	query := "SELECT * FROM questions LEFT JOIN user ON questions.user=user.id"
	if params != "" {
		query += " WHERE " + params
	}
	log.Infof(ctx, "query: %v", query)

	rows, err := db.Query(query)
	if err != nil {
		return tempData, 0, fmt.Errorf("query failed: %v", err.Error())
	}

	defer rows.Close()
	//Iterate through each row and add to the correct cache
	for rows.Next() {
		err := rows.Scan(&id, &title, &url, &state, &owner, &body, &creation_date, &last_edit_time, &owner, &name, &pic, &link)
		if err != nil {
			log.Errorf(ctx, "query failed: %v", err)
			continue
		}

		currentQ := stackongo.Question{
			Question_id:   id,
			Title:         title,
			Link:          url,
			Body:          body,
			Creation_date: creation_date,
		}
		if last_edit_time.Valid {
			currentQ.Last_edit_date = last_edit_time.Int64
		}

		var tagToAdd string
		//Get tags for that question, based on the ID
		tagRows, err := db.Query("SELECT tag FROM question_tag WHERE question_id = ?", currentQ.Question_id)
		if err != nil {
			log.Errorf(ctx, "Tag retrieval failed: %v", err.Error())
			continue
		}
		defer tagRows.Close()
		for tagRows.Next() {
			err := tagRows.Scan(&tagToAdd)
			if err != nil {
				log.Errorf(ctx, "Could not scan for tag: %v", err.Error())
				continue
			}
			currentQ.Tags = append(currentQ.Tags, tagToAdd)
		}
		//Switch on the state as read from the database to ensure question is added to correct cace
		tempData.Caches[state] = append(tempData.Caches[state], currentQ)

		if owner.Valid {
			user := stackongo.User{
				User_id:       int(owner.Int64),
				Display_name:  name.String,
				Profile_image: pic.String,
			}
			tempData.Qns[id] = user
			if _, ok := tempData.Users[user.User_id]; !ok {
				tempData.Users[user.User_id] = newUser(user)
			}
			tempData.Users[user.User_id].Caches[state] = append(tempData.Users[user.User_id].Caches[state], currentQ)
		}
	}

	for cacheType, _ := range tempData.Caches {
		sort.Sort(byCreationDate(tempData.Caches[cacheType]))
	}

	return tempData, time.Now().Unix(), nil
}