当前位置: 首页>>代码示例>>Golang>>正文


Golang sqlx.Stmt类代码示例

本文整理汇总了Golang中github.com/jmoiron/sqlx.Stmt的典型用法代码示例。如果您正苦于以下问题:Golang Stmt类的具体用法?Golang Stmt怎么用?Golang Stmt使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Stmt类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: StmtClose

func StmtClose(conn Conn, stmt *sqlx.Stmt) error {
	if stmt == nil {
		return nil
	}

	switch conn.(type) {
	case *DB:
		return nil
	default:
		return stmt.Close()
	}
}
开发者ID:simonz05,项目名称:database,代码行数:12,代码来源:query.go

示例2: GetAndMarshal

func GetAndMarshal(query string, db Execer, destination interface{}, args ...interface{}) (err error) {
	var stmt *sqlx.Stmt

	var acid AcidTx = func(tx *sqlx.Tx) {
		stmt, err = tx.Preparex(query)
		if err != nil {
			panic(err)
		}
		if err = stmt.Get(destination, args...); err != nil {
			panic(err)
		}
	}
	AcidCtx(acid, db)

	return err
}
开发者ID:alligrader,项目名称:gradebook-backend,代码行数:16,代码来源:util.go

示例3: PrepAndExec

func PrepAndExec(query string, db Execer, args ...interface{}) (result sql.Result, err error) {

	var stmt *sqlx.Stmt
	var acid AcidTx = func(tx *sqlx.Tx) {
		stmt, err = tx.Preparex(query)
		if err != nil {
			panic(err)
		}
		if result, err = stmt.Exec(args...); err != nil {
			panic(err)
		}
	}
	AcidCtx(acid, db)

	return result, err
}
开发者ID:alligrader,项目名称:gradebook-backend,代码行数:16,代码来源:util.go

示例4: 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

示例5: 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

示例6: GetAPIStandings

func (d *dbInterface) GetAPIStandings(key XMLAPIKey, charID int) error {
	k := &evego.XMLKey{
		KeyID:            key.ID,
		VerificationCode: key.VerificationCode,
	}
	standings, err := d.xmlAPI.CharacterStandings(k, charID)
	if err != nil {
		return err
	}
	tx, err := d.db.Beginx()
	if err != nil {
		return err
	}
	// Clear standings before inserting the API's information.
	_, err = tx.Stmtx(d.apiKeyClearCorpStandingsStmt).Exec(charID)
	if err != nil {
		return err
	}
	_, err = tx.Stmtx(d.apiKeyClearFacStandingsStmt).Exec(charID)
	if err != nil {
		return err
	}
	var insertStmt *sqlx.Stmt
	for _, standing := range standings {
		switch standing.EntityType {
		case evego.NPCCorporation:
			insertStmt = d.apiKeyInsertCorpStandingsStmt
		case evego.NPCFaction:
			insertStmt = d.apiKeyInsertFacStandingsStmt
		default:
			// Agent standings - we don't handle those, so skip.
			continue
		}
		insertStmt = tx.Stmtx(insertStmt)
		_, err := insertStmt.Exec(charID, standing.ID, standing.Standing)
		if err != nil {
			tx.Rollback()
			return err
		}
	}
	return tx.Commit()
}
开发者ID:backerman,项目名称:eveindy,代码行数:42,代码来源:xmlapi.go

示例7: CreateWithId

func (db DB) CreateWithId(stmt *sqlx.Stmt, args ...interface{}) (int64, error) {
	var id int64

	if db.driver == "postgres" {
		err := stmt.QueryRow(args...).Scan(&id)
		if err != nil {
			return 0, err
		}
	} else {
		res, err := stmt.Exec(args...)
		if err != nil {
			return 0, err
		}

		id, err = res.LastInsertId()
		if err != nil {
			return 0, err
		}
	}

	return id, nil
}
开发者ID:yourchanges,项目名称:readeef,代码行数:22,代码来源:db.go

示例8: Create

func (maker *courseMaker) Create(course *Course) error {

	var (
		err                 error
		courseQuery         = queries["create_course"]
		courseMemberQuery   = queries["create_course_members"]
		courseTeachersQuery = queries["create_course_teachers"]
		acid                util.AcidTx
		result              sql.Result
		stmt                *sqlx.Stmt
	)

	acid = func(tx *sqlx.Tx) {
		// Add the course
		stmt, err = tx.Preparex(courseQuery)
		if err != nil {
			panic(err)
		}
		if result, err = stmt.Exec(course.Name); err != nil {
			panic(err)
		}
		course.ID, err = result.LastInsertId()
		if err != nil {
			panic(err)
		}

		// Then add a new record for each of the students to course_members
		for _, student := range course.Students {
			stmt, err = tx.Preparex(courseMemberQuery)
			if err != nil {
				panic(err)
			}
			if result, err = stmt.Exec(course.ID, student.ID); err != nil {
				panic(err)
			}
		}

		// Then add a new record for each of the teachers to course_teachers
		for _, teacher := range course.Teachers {
			stmt, err = tx.Preparex(courseTeachersQuery)
			if err != nil {
				panic(err)
			}
			if result, err = stmt.Exec(course.ID, teacher.ID); err != nil {
				panic(err)
			}
		}
	}
	return util.AcidCtx(acid, maker)
}
开发者ID:alligrader,项目名称:gradebook-backend,代码行数:50,代码来源:course_maker.go


注:本文中的github.com/jmoiron/sqlx.Stmt类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。