コード例 #1
0
ファイル: language_go2sql.go プロジェクト: bom-d-van/go2sql
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
}
コード例 #2
0
ファイル: language_go2sql.go プロジェクト: bom-d-van/go2sql
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
}
コード例 #3
0
ファイル: language_go2sql.go プロジェクト: bom-d-van/go2sql
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
}
コード例 #4
0
ファイル: language_go2sql.go プロジェクト: bom-d-van/go2sql
func FindLanguage(optsx ...go2sql.QueryOption) (l *Language, 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
	}
	l = &Language{}
	var fields []interface{}
	var columns []string
	if sel, ok := opts.GetSelect(); ok {
		columns = []string(sel)
		for _, c := range columns {
			switch c {
			case LanguageColumnID:
				fields = append(fields, &l.ID)
			case LanguageColumnName:
				fields = append(fields, &l.Name)
			case LanguageColumnWordsCount:
				fields = append(fields, &l.WordsCount)
			default:
				err = fmt.Errorf("go2sql: unknown column %s", c)
				return
			}
		}
	} else {
		columns = []string{"id", "name", "words_stat", "field1", "field2", "field3", "field4", "field5", "field6", "field7"}
		fields = []interface{}{&l.ID, &l.Name, &l.WordsCount, &l.Field1, &l.Field2, &l.Field3, &l.Field4, &l.Field5, &l.Field6, &l.Field7}
	}

	var sql go2sql.SQL
	if opt, ok := opts.GetSQL(); ok && opt.Full {
		sql = opt
	} else {
		sql.SQL = fmt.Sprintf("select %s from languages %s", strings.Join(columns, ","), opt.SQL)
		sql.Args = opt.Args
	}

	err = db.QueryRow(sql.SQL, sql.Args...).Scan(fields...)
	if err != nil {
		return
	}

	if tables, ok := opts.GetTables(); ok {
		for _, table := range tables {
			switch table.Name {
			case LanguageColumnAuthor:
				err = l.FetchAuthor(go2sql.DB(db), table.Tables)
			case LanguageColumnKeywords:
				err = l.FetchKeywords(go2sql.DB(db), table.Tables)
			case LanguageColumnTeachers:
				err = l.FetchTeachers(go2sql.DB(db), table.Tables)
			}
			if err != nil {
				return
			}
		}
	}
	return
}
コード例 #5
0
ファイル: language_go2sql.go プロジェクト: bom-d-van/go2sql
func FindLanguages(optsx ...go2sql.QueryOption) (ls Languages, 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 columns []string
	if sel, ok := opts.GetSelect(); ok {
		columns = []string(sel)
	} else {
		columns = []string{"id", "name", "words_stat", "field1", "field2", "field3", "field4", "field5", "field6", "field7"}
	}

	var sql go2sql.SQL
	if opt, ok := opts.GetSQL(); ok && opt.Full {
		sql = opt
	} else {
		sql.SQL = fmt.Sprintf("select %s from languages %s", strings.Join(columns, ","), opt.SQL)
		sql.Args = opt.Args
	}

	rows, err := db.Query(sql.SQL, sql.Args...)
	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 l Language
		var fields []interface{}
		for _, c := range columns {
			switch c {
			case LanguageColumnID:
				fields = append(fields, &l.ID)
			case LanguageColumnName:
				fields = append(fields, &l.Name)
			case LanguageColumnWordsCount:
				fields = append(fields, &l.WordsCount)
			case "field1":
				fields = append(fields, &l.Field1)
			case "field2":
				fields = append(fields, &l.Field2)
			case "field3":
				fields = append(fields, &l.Field3)
			case "field4":
				fields = append(fields, &l.Field4)
			case "field5":
				fields = append(fields, &l.Field5)
			case "field6":
				fields = append(fields, &l.Field6)
			case "field7":
				fields = append(fields, &l.Field7)
			default:
				err = fmt.Errorf("go2sql: unknown column %s", c)
				return
			}
		}

		err = rows.Scan(fields...)
		if err != nil {
			return
		}
		ls = append(ls, &l)
	}

	if tables, ok := opts.GetTables(); ok {
		for _, table := range tables {
			switch table.Name {
			case LanguageColumnAuthor:
				err = ls.FetchAuthor(go2sql.DB(db), table.Tables)
			case LanguageColumnKeywords:
				err = ls.FetchKeywords(go2sql.DB(db), table.Tables)
			case LanguageColumnTeachers:
				err = ls.FetchTeachers(go2sql.DB(db), table.Tables)
			}
			if err != nil {
				return
			}
		}
	}

	return
}