예제 #1
0
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
}
예제 #2
0
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
}