當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Stmt.Select方法代碼示例

本文整理匯總了Golang中github.com/jmoiron/sqlx.Stmt.Select方法的典型用法代碼示例。如果您正苦於以下問題:Golang Stmt.Select方法的具體用法?Golang Stmt.Select怎麽用?Golang Stmt.Select使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/jmoiron/sqlx.Stmt的用法示例。


在下文中一共展示了Stmt.Select方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: GetByID

func (maker *courseMaker) GetByID(id int64) (*Course, error) {

	var (
		err                 error
		get_course          string  = queries["get_course"]
		get_course_members  string  = queries["get_course_members"]
		get_course_teachers string  = queries["get_course_teachers"]
		course              *Course = NewCourse()
		studentIDs          []int64 = []int64{}
		teacherIDs          []int64 = []int64{}
		stmt                *sqlx.Stmt
		acid                util.AcidTx
	)

	// Get the course's name.
	acid = func(tx *sqlx.Tx) {
		// Add the course
		stmt, err = tx.Preparex(get_course)
		PanicOnError(err)

		err = stmt.Get(course, id)
		PanicOnError(err)

		// Get the rows out of course_members
		stmt, err = tx.Preparex(get_course_members)
		PanicOnError(err)

		err = stmt.Select(&studentIDs, id)
		PanicOnError(err)

		// Get the rows out of course_teachers
		stmt, err = tx.Preparex(get_course_teachers)
		PanicOnError(err)

		stmt.Select(&teacherIDs, id)
		PanicOnError(err)

		for _, studentID := range studentIDs {
			student, err := StudentStore.GetByID(studentID)
			PanicOnError(err)
			course.Students = append(course.Students, student)
		}

		for _, teacherID := range teacherIDs {
			teacher, err := TeacherStore.GetByID(teacherID)

			PanicOnError(err)
			course.Teachers = append(course.Teachers, teacher)
		}
	}
	err = util.AcidCtx(acid, maker)
	if err != nil {
		return nil, err
	}

	return course, nil
}
開發者ID:alligrader,項目名稱:gradebook-backend,代碼行數:57,代碼來源:course_maker.go

示例2: Get

func (d Datastore) Get(table string, model interface{}, id interface{}) error {
	query := fmt.Sprintf("SELECT * FROM %v %v", table, string(table[0]))
	var stmt *sqlx.Stmt
	var err error
	if table == "orders" {
		return d.parseOrders(model, id)
	}
	if id != nil {
		query += fmt.Sprintf(" where %v.id = $1", string(table[0]))
		stmt, err = d.Db.Preparex(query)
		stmt.Select(model, id)
	} else {
		stmt, err = d.Db.Preparex(query)
		stmt.Select(model)
	}
	return err
}
開發者ID:TuukkaP,項目名稱:tyovuoro,代碼行數:17,代碼來源:datastore.go


注:本文中的github.com/jmoiron/sqlx.Stmt.Select方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。