예제 #1
0
func (l *Language) FetchTeachers(opts ...go2sql.QueryOption) (err error) {
	// rows, err := db.Query(`SELECT teachers.id, teachers.name, teachers.age FROM teachers
	// 	INNER JOIN languages_teachers_xref
	// 	ON teachers.id = languages_teachers_xref.teacher_id
	// 	WHERE languages_teachers_xref.language_id = ?`, l.ID)
	// if err != nil {
	// 	return
	// }

	// defer func() {
	// 	if er := rows.Close(); er != nil {
	// 		if err != nil {
	// 			log.Println(er)
	// 		} else {
	// 			err = er
	// 		}
	// 	}
	// }()

	// for rows.Next() {
	// 	var t Teacher
	// 	if err = rows.Scan(&t.ID, &t.Name, &t.Age); err != nil {
	// 		return
	// 	}
	// 	l.Teachers = append(l.Teachers, t)
	// }

	opts = append(opts, go2sql.NewSQL("where language_id = ?", l.ID))
	teachers, err := FindTeachers(opts...)
	l.Teachers = []*Teacher(teachers)

	return
}
예제 #2
0
func TestFindLanguages(t *testing.T) {
	resetDB()
	populateDB()

	var ls Languages
	var err error

	// normal
	ls, err = FindLanguages()
	if err != nil {
		t.Fatal(err)
	}
	if got, want := len(ls), 99; got != want {
		t.Error("len(ls) = %d; want %d", got, want)
	}
	if got, want := ls[0].Name, "Mr. Tester"; got != want {
		t.Errorf("ls[0].Name = %s; want %s", got, want)
	}

	// sql patial
	ls, err = FindLanguages(go2sql.NewSQL("order by id desc limit 10"))
	if err != nil {
		t.Fatal(err)
	}
	if got, want := len(ls), 10; got != want {
		t.Error("len(ls) = %d; want %d", got, want)
	}
	if got, want := ls[0].ID, uint(99); got != want {
		t.Errorf("ls[0].Name = %d; want %d", got, want)
	}

	// db specification
	go2sql.SetDefaultDB(nil)
	_, err = FindLanguages()
	if err == nil {
		t.Error("expect error for nil db")
	}
	ls, err = FindLanguages(go2sql.DB(db))
	if err != nil {
		t.Fatal(err)
	}
	if got, want := ls[0].Name, "Mr. Tester"; got != want {
		t.Errorf("ls[0].Name = %s; want %s", got, want)
	}
	go2sql.SetDefaultDB(db)

	// selects
	ls, err = FindLanguages(go2sql.Selects{"id", "words_stat"})
	if err != nil {
		t.Fatal(err)
	}
	if got, want := ls[0].Name, ""; got != want {
		t.Errorf("ls[0].Name = %s; want %s", got, want)
	}
	if got, want := ls[0].WordsCount, uint(1); got != want {
		t.Errorf("ls[0].Name = %d; want %d", got, want)
	}
}
예제 #3
0
func (ls *Languages) FetchAuthor(optsx ...go2sql.QueryOption) (err error) {
	if len(*ls) == 0 {
		return
	}
	opts := go2sql.QueryOptions(optsx)
	db, ok := opts.GetDB()
	if !ok {
		db = go2sql.DefaultDB.DB
	}
	if db == nil {
		err = errors.New("should specify *sql.DB by go2sql.DB or init go2sql.DefaultDB")
		return
	}

	var ids []string
	for _, l := range *ls {
		ids = append(ids, strconv.Itoa(int(l.AuthorID)))
	}
	// rows, err := db.Query(`SELECT id, name, email FROM people WHERE id IN (?)`, strings.Join(ids, ","))
	// if err != nil {
	// 	return
	// }

	// defer func() {
	// 	if er := rows.Close(); er != nil {
	// 		if err != nil {
	// 			log.Println(er)
	// 		} else {
	// 			err = er
	// 		}
	// 	}
	// }()

	// var people []*Person
	// for rows.Next() {
	// 	var person Person
	// 	if err = rows.Scan(&person.ID, &person.Name, &person.Email); err != nil {
	// 		return
	// 	}
	// 	people = append(people, &person)
	// }
	people, err := FindPeople(go2sql.DB(db), go2sql.NewSQL("WHERE id IN (?)", strings.Join(ids, ", ")))
	if err != nil {
		return
	}

	for index, l := range *ls {
		for _, person := range people {
			if person.ID == l.AuthorID {
				(*ls)[index].Author = person
			}
		}
	}

	return
}
예제 #4
0
func (ls *Languages) FetchKeywords(optsx ...go2sql.QueryOption) (err error) {
	opts := go2sql.QueryOptions(optsx)
	db, ok := opts.GetDB()
	if !ok {
		db = go2sql.DefaultDB.DB
	}
	if db == nil {
		err = errors.New("should specify *sql.DB by go2sql.DB or init go2sql.DefaultDB")
		return
	}

	var ids []string
	for _, l := range *ls {
		ids = append(ids, strconv.Itoa(int(l.ID)))
	}
	// rows, err := db.Query(`SELECT id, name, type, language_id FROM keywords WHERE language_id IN (?)`, strings.Join(ids, ","))
	// if err != nil {
	// 	return
	// }

	// defer func() {
	// 	if er := rows.Close(); er != nil {
	// 		if err != nil {
	// 			log.Println(er)
	// 		} else {
	// 			err = er
	// 		}
	// 	}
	// }()

	// var keywords []Keyword
	// for rows.Next() {
	// 	var k Keyword
	// 	if err = rows.Scan(&k.ID, &k.Name, &k.Type, &k.LanguageID); err != nil {
	// 		return
	// 	}
	// 	keywords = append(keywords, k)
	// }
	keywords, err := FindKeywords(go2sql.DB(db), go2sql.NewSQL("WHERE language_id IN (?)", strings.Join(ids, ",")))
	if err != nil {
		return
	}

	for i, l := range *ls {
		for _, keyword := range keywords {
			if keyword.LanguageID == l.ID {
				(*ls)[i].Keywords = append((*ls)[i].Keywords, keyword)
			}
		}
	}

	return
}
예제 #5
0
func (ls *Languages) FetchTeachers(optsx ...go2sql.QueryOption) (err error) {
	if len(*ls) == 0 {
		return
	}
	opts := go2sql.QueryOptions(optsx)
	// db, ok := opts.GetDB()
	// if !ok {
	// 	if go2sql.DefaultDB == nil {
	// 		err = errors.New("should specify *sql.DB by go2sql.DB or init go2sql.DefaultDB")
	// 		return
	// 	}
	// 	db = go2sql.DefaultDB.DB
	// }

	var ids []string
	for _, l := range *ls {
		ids = append(ids, strconv.Itoa(int(l.ID)))
	}
	// rows, err := db.Query(`SELECT
	// 		teachers.id,
	// 		teachers.name,
	// 		teachers.age,
	// 		languages_teachers_xref.language_id
	// 	FROM teachers
	// 	INNER JOIN languages_teachers_xref
	// 	ON teachers.id = languages_teachers_xref.teacher_id
	// 	WHERE languages_teachers_xref.language_id in (?)`, strings.Join(ids, ","))
	// if err != nil {
	// 	return
	// }

	// defer func() {
	// 	if er := rows.Close(); er != nil {
	// 		if err != nil {
	// 			log.Println(er)
	// 		} else {
	// 			err = er
	// 		}
	// 	}
	// }()

	// type teacher struct {
	// 	Teacher
	// 	languageID uint
	// }

	// var ts []teacher
	// for rows.Next() {
	// 	var t teacher
	// 	if err = rows.Scan(&t.ID, &t.Name, &t.Age, &t.languageID); err != nil {
	// 		return
	// 	}
	// 	ts = append(ts, t)
	// }

	// // TODO: support where override?
	// optsx = append(optsx, go2sql.NewSQL(`SELECT
	// 		teachers.id,
	// 		teachers.name,
	// 		teachers.age,
	// 	FROM teachers
	// 	INNER JOIN languages_teachers_xref
	// 	ON teachers.id = languages_teachers_xref.teacher_id
	// 	WHERE languages_teachers_xref.language_id in (?)`, strings.Join(ids, ",")))
	// teachers, err := FindTeachers(optsx...)
	// if err != nil {
	// 	return
	// }

	opts = append(opts, go2sql.NewSQL(`INNER JOIN languages_teachers_xref
	ON teachers.id = languages_teachers_xref.teacher_id
	WHERE languages_teachers_xref.language_id in (?)`, strings.Join(ids, ",")))
	teachers, err := FindTeachers(opts...)
	if err != nil {
		return
	}

	for i, l := range *ls {
		for _, t := range teachers {
			if t.LanguageID == l.ID {
				(*ls)[i].Teachers = append((*ls)[i].Teachers, t)
			}
		}
	}

	return
}
예제 #6
0
func (l *Language) FetchAuthor(opts ...go2sql.QueryOption) (err error) {
	// err = db.QueryRow("select id, name, email from people where id = ?", l.AuthorID).Scan(&l.Author.ID, &l.Author.Name, &l.Author.Email)
	opts = append(opts, go2sql.NewSQL("where language_id = ?", l.ID))
	l.Author, err = FindPerson(opts...)
	return
}
예제 #7
0
func (l *Language) FetchKeywords(opts ...go2sql.QueryOption) (err error) {
	opts = append(opts, go2sql.NewSQL("where language_id = ?", l.ID))
	l.Keywords, err = FindKeywords(opts...)

	return
}