Пример #1
0
func StmtClose(conn Conn, stmt *sqlx.Stmt) error {
	if stmt == nil {
		return nil
	}

	switch conn.(type) {
	case *DB:
		return nil
	default:
		return stmt.Close()
	}
}
Пример #2
0
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
}
Пример #3
0
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
}
Пример #4
0
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
}
Пример #5
0
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
}
Пример #6
0
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()
}
Пример #7
0
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
}
Пример #8
0
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)
}