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


Golang DB.Get方法代码示例

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


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

示例1: validateMasterPassword

func validateMasterPassword(db *sqlx.DB) {
	c := dbConfig{}

	db.Get(&c, "SELECT `key`, `value` FROM `config` WHERE `key` = 'teststring'")

	if c.Key == "" {
		panic("Could not read the teststring from the config table. Your database is broken.")
	}

	// not yet initialized, so store the ciphertext
	if len(c.Value) == 0 {
		ciphertext, err := Encrypt([]byte(TestString))
		if err != nil {
			panic(err)
		}

		_, err = db.Exec("UPDATE `config` SET `value` = ? WHERE `key` = ?", ciphertext, c.Key)
		if err != nil {
			panic("Could not write initial password marker: " + err.Error())
		}
	} else {
		plaintext, err := Decrypt(c.Value)
		if err != nil {
			panic("The configured password is not usable for the configured database.")
		}

		// this should never happen: a wrong password should always yield an error in Decrypt()
		if TestString != string(plaintext) {
			panic("The configured password is not usable for the configured database.")
		}
	}
}
开发者ID:xrstf,项目名称:raziel,代码行数:32,代码来源:main.go

示例2: addTimeContext

// addTimeContext adds a time context to a Message if the word "then" is found.
func addTimeContext(db *sqlx.DB, in *dt.Msg) error {
	var addContext bool
	for _, stem := range in.Stems {
		if stem == "then" {
			addContext = true
			break
		}
	}
	if !addContext {
		return nil
	}
	var byt []byte
	var err error
	if in.User.ID > 0 {
		q := `SELECT value FROM states WHERE userid=$1 AND key=$2`
		err = db.Get(&byt, q, in.User.ID, keyContextTime)
	} else {
		q := `SELECT value FROM states
		      WHERE flexid=$1 AND flexidtype=$2 AND key=$3`
		err = db.Get(&byt, q, in.User.FlexID, in.User.FlexIDType,
			keyContextTime)
	}
	if err == sql.ErrNoRows {
		return nil
	}
	if err != nil {
		return err
	}
	var times []time.Time
	if err = json.Unmarshal(byt, &times); err != nil {
		return err
	}
	in.StructuredInput.Times = times
	return nil
}
开发者ID:itsabot,项目名称:abot,代码行数:36,代码来源:context.go

示例3: InsertAnswer

func InsertAnswer(db *sqlx.DB, userId int, questionId int, message string) (*Answer, error) {
	a := Answer{}
	a.AuthorId = userId
	a.QuestionId = questionId
	a.Message = message
	var err error

	if err = ValidateAnswer(a); err != nil {
		return nil, err
	}

	qry := sq.Insert("answers").
		Columns("author_id", "question_id", "message").
		Values(a.AuthorId, a.QuestionId, a.Message).
		Suffix("RETURNING *").
		PlaceholderFormat(sq.Dollar)

	sql, params, err := qry.ToSql()
	if err != nil {
		return nil, err
	}

	err = db.Get(&a, sql, params...)
	dbErr := dbError(err)
	if dbErr != nil {
		return nil, dbErr
	} else {
		return &a, nil
	}
}
开发者ID:cjwirth,项目名称:askme,代码行数:30,代码来源:answer.go

示例4: GetBySlug

func (p *Post) GetBySlug(db *sqlx.DB, slug string) error {

	err := db.Get(p, "SELECT * FROM posts WHERE status=1 AND slug=$1", slug)
	if err != nil {
		return err
	}
	return nil
}
开发者ID:xenamorph,项目名称:quick,代码行数:8,代码来源:models.go

示例5: GetById

func (p *Post) GetById(db *sqlx.DB, post_id int) error {

	err := db.Get(p, "SELECT * FROM posts WHERE id=$1", post_id)
	if err != nil {
		return err
	}
	return nil
}
开发者ID:xenamorph,项目名称:quick,代码行数:8,代码来源:models.go

示例6: GetChannelList

// GetChannelList returns the ChannelList for the given id.
func GetChannelList(db *sqlx.DB, id int64) (models.ChannelList, error) {
	var cl models.ChannelList
	err := db.Get(&cl, "select * from channel_list where id = $1", id)
	if err != nil {
		return cl, fmt.Errorf("get channel-list %d error: %s", id, err)
	}
	return cl, nil
}
开发者ID:yuseunghyuk,项目名称:loraserver,代码行数:9,代码来源:channels.go

示例7: GetNode

// GetNode returns the Node for the given DevEUI.
func GetNode(db *sqlx.DB, devEUI lorawan.EUI64) (models.Node, error) {
	var node models.Node
	err := db.Get(&node, "select * from node where dev_eui = $1", devEUI[:])
	if err != nil {
		return node, fmt.Errorf("get node %s error: %s", devEUI, err)
	}
	return node, nil
}
开发者ID:yuseunghyuk,项目名称:loraserver,代码行数:9,代码来源:node.go

示例8: GetChannel

// GetChannel returns the Channel matching the given id.
func GetChannel(db *sqlx.DB, id int64) (models.Channel, error) {
	var channel models.Channel
	err := db.Get(&channel, "select * from channel where id = $1", id)
	if err != nil {
		return channel, fmt.Errorf("get channel %d error: %s", id, err)
	}
	return channel, nil
}
开发者ID:yuseunghyuk,项目名称:loraserver,代码行数:9,代码来源:channels.go

示例9: getApplication

// getApplication returns the Application for the given AppEUI.
func getApplication(db *sqlx.DB, appEUI lorawan.EUI64) (models.Application, error) {
	var app models.Application
	err := db.Get(&app, "select * from application where app_eui = $1", appEUI[:])
	if err != nil {
		return app, fmt.Errorf("get application %s error: %s", appEUI, err)
	}
	return app, nil
}
开发者ID:RedBeardLab,项目名称:loraserver,代码行数:9,代码来源:application.go

示例10: GetLocationBySlug

// GetLocationBySlug Looks up a location by its Yelp ID
func GetLocationBySlug(db *sqlx.DB, slug string) (*models.Location, error) {
	l := models.Location{}
	err := db.Get(&l, db.Rebind(`SELECT * FROM locations WHERE slug=?`), slug)
	if err != nil {
		return nil, err
	}
	return &l, nil
}
开发者ID:K4orta,项目名称:lunchclub-api,代码行数:9,代码来源:locations.go

示例11: GetUserData

func GetUserData(db *sqlx.DB, userId int64) (*ApiUser, error) {
	var user ApiUser
	var err error

	err = db.Get(&user, "SELECT * FROM tinyplannr_api.user WHERE user_id = $1", userId)
	log.Println(err)

	return &user, err
}
开发者ID:cyarie,项目名称:tinyplannr-api-v2,代码行数:9,代码来源:apiUser.go

示例12: GetUserByID

func GetUserByID(db *sqlx.DB, id int) (*models.User, error) {
	u := models.User{}
	err := db.Get(&u, db.Rebind(`SELECT * FROM users WHERE id=?`), id)
	if err != nil {
		log.Printf("Error while getting user by id: %v", err)
		return nil, err
	}
	return &u, nil
}
开发者ID:K4orta,项目名称:lunchclub-api,代码行数:9,代码来源:users.go

示例13: FetchToken

func FetchToken(db *sqlx.DB, token string, maxAge int) (*Token, error) {
	t := Token{}
	err := db.Get(&t, "select user_name,token,attempts,email from token where token = ? and timestampdiff(SECOND, created_at, now()) <= ?", token, maxAge)
	if err != nil {
		return nil, err
	}

	return &t, nil
}
开发者ID:HeWhoWas,项目名称:mokey,代码行数:9,代码来源:token.go

示例14: FetchAnswer

func FetchAnswer(db *sqlx.DB, uid string) (*SecurityAnswer, error) {
	answer := SecurityAnswer{}
	err := db.Get(&answer, "select a.user_name,a.question_id,q.question,a.answer from security_answer a join security_question q on a.question_id = q.id  where a.user_name = ?", uid)
	if err != nil {
		return nil, err
	}

	return &answer, nil
}
开发者ID:HeWhoWas,项目名称:mokey,代码行数:9,代码来源:security.go

示例15: GetResult

func (q *Query) GetResult(db *sqlx.DB, result interface{}) error {
	sql, vars := q.GetSQL()

	if debugEnabled {
		marshaled, _ := json.Marshal(vars)
		Debug("%s, %s", sql, string(marshaled))
	}

	return db.Get(result, sql, vars...)
}
开发者ID:jraede,项目名称:go-sqlbuilder,代码行数:10,代码来源:sqlbuilder.go


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