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


Golang DB.Rebind方法代码示例

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


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

示例1: ExtractCities

// ExtractCities efficiently from a user's message.
func ExtractCities(db *sqlx.DB, in *dt.Msg) ([]dt.City, error) {
	// Interface type is used to expand the args in db.Select below.
	// Although we're only storing strings, []string{} doesn't work.
	var args []interface{}

	// Look for "at", "in", "on" prepositions to signal that locations
	// follow, skipping everything before
	var start int
	for i := range in.Stems {
		switch in.Stems[i] {
		case "at", "in", "on":
			start = i
			break
		}
	}

	// Prepare sentence for iteration
	tmp := regexNonWords.ReplaceAllString(in.Sentence, "")
	words := strings.Fields(strings.Title(tmp))

	// Iterate through words and bigrams to assemble a DB query
	for i := start; i < len(words); i++ {
		args = append(args, words[i])
	}
	bgs := bigrams(words, start)
	for i := 0; i < len(bgs); i++ {
		args = append(args, bgs[i])
	}

	cities := []dt.City{}
	q := `SELECT name, countrycode FROM cities
	      WHERE countrycode='US' AND name IN (?)
	      ORDER BY LENGTH(name) DESC`
	query, arguments, err := sqlx.In(q, args)
	query = db.Rebind(query)
	rows, err := db.Query(query, arguments...)
	if err != nil {
		return nil, err
	}
	defer func() {
		if err = rows.Close(); err != nil {
			log.Info("failed to close db rows.", err)
		}
	}()
	for rows.Next() {
		city := dt.City{}
		if err = rows.Scan(&city.Name, &city.CountryCode); err != nil {
			return nil, err
		}
		cities = append(cities, city)
	}
	if err = rows.Err(); err != nil {
		return nil, err
	}
	if len(cities) == 0 {
		return nil, ErrNotFound
	}
	return cities, nil
}
开发者ID:itsabot,项目名称:abot,代码行数:60,代码来源:extract.go

示例2: InsertUser

func InsertUser(db *sqlx.DB, user *models.User) error {
	_, err := db.NamedExec(db.Rebind(`INSERT INTO users (fbid, first_name, last_name, roles) VALUES (:fbid, :first_name, :last_name, :roles)`), user)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:K4orta,项目名称:lunchclub-api,代码行数:8,代码来源:users.go

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

示例4: RebindInsert

func RebindInsert(db *sqlx.DB, q string) string {
	q = db.Rebind(q)
	q = strings.TrimRight(q, " \t\n;")
	if db.DriverName() == "postgres" {
		q = q + " RETURNING id"
	}

	return q
}
开发者ID:jmptrader,项目名称:go-webapp-skeleton,代码行数:9,代码来源:util.go

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

示例6: insertNotices

func insertNotices(conn *sqlx.DB, results []NoticeResult) {
	sql := conn.Rebind(`
		INSERT INTO notice(id, published)
		SELECT ?, ? 
		WHERE NOT EXISTS (SELECT 1 FROM notice WHERE id = ?)`)
	for _, result := range results {
		conn.MustExec(
			sql, result.DocumentNumber, result.Published,
			result.DocumentNumber)
	}
	insertAgencies(conn, results)
}
开发者ID:cmc333333,项目名称:noticestats,代码行数:12,代码来源:insert.go

示例7: insertAgencies

func insertAgencies(conn *sqlx.DB, results []NoticeResult) {
	delete := conn.Rebind("DELETE FROM notice_agency WHERE notice_id = ?")
	insert := conn.Rebind(`
		INSERT INTO notice_agency (notice_id, agency)
		VALUES (?, ?)`)
	for _, result := range results {
		conn.MustExec(delete, result.DocumentNumber)
		for _, agency := range result.Agencies {
			conn.MustExec(insert, result.DocumentNumber, agency)
		}
	}
}
开发者ID:cmc333333,项目名称:noticestats,代码行数:12,代码来源:insert.go

示例8: ParseQueryStruct

func ParseQueryStruct(q string, d interface{}, db *sqlx.DB) (string, []interface{}, error) {
	errors := make([]error, 2)
	query, args, err := sqlx.Named(q, d)
	errors = append(errors, err)
	query, args, err = sqlx.In(query, args...)
	errors = append(errors, err)
	query = db.Rebind(query)
	for _, e := range errors {
		if e != nil {
			return "", nil, e
		}
	}
	return query, args, nil
}
开发者ID:q1t,项目名称:movielist,代码行数:14,代码来源:user.go

示例9: createLinkSQL

func createLinkSQL(db *sqlx.DB, tx *sql.Tx, table string, p Policy, templates []string) error {
	for _, template := range templates {
		reg, err := compiler.CompileRegex(template, p.GetStartDelimiter(), p.GetEndDelimiter())

		// Execute SQL statement
		query := db.Rebind(fmt.Sprintf("INSERT INTO %s (policy, template, compiled) VALUES (?, ?, ?)", table))
		if _, err = tx.Exec(query, p.GetID(), template, reg.String()); err != nil {
			if rb := tx.Rollback(); rb != nil {
				return errors.Wrap(rb, "")
			}
			return errors.Wrap(err, "")
		}
	}
	return nil
}
开发者ID:ory-am,项目名称:ladon,代码行数:15,代码来源:manager_sql.go

示例10: InsertEvent

// InsertEvent takes a DB connect and inserts a new EventEntry
func InsertEvent(db *sqlx.DB, event *models.Event) (*models.Event, error) {
	rows, err := db.NamedQuery(db.Rebind(`
		INSERT INTO events (title, slug, start_time, end_time, organizer_id, location_id, rsvps)
		VALUES (:title, :slug, :start_time, :end_time, :organizer_id, :location_id, :rsvps)
		RETURNING id`), event)
	if err != nil {
		return nil, err
	}

	if rows.Next() {
		rows.Scan(&event.ID)
	}

	return event, nil
}
开发者ID:K4orta,项目名称:lunchclub-api,代码行数:16,代码来源:events.go

示例11: InsertLocation

// InsertLocation inserts a new Location object into the DB
func InsertLocation(db *sqlx.DB, location *models.Location) (*models.Location, error) {
	rows, err := db.NamedQuery(db.Rebind(`
		INSERT INTO locations (name, slug, address, lat_lng)
		VALUES (:name, :slug, :address, :lat_lng)
		RETURNING id
	`), location)
	if err != nil {
		return nil, err
	}

	if rows.Next() {
		rows.Scan(&location.ID)
	}

	return location, nil
}
开发者ID:K4orta,项目名称:lunchclub-api,代码行数:17,代码来源:locations.go

示例12: getLinkedSQL

func getLinkedSQL(db *sqlx.DB, table, policy string) ([]string, error) {
	urns := []string{}
	rows, err := db.Query(db.Rebind(fmt.Sprintf("SELECT template FROM %s WHERE policy=?", table)), policy)
	if err == sql.ErrNoRows {
		return nil, errors.Wrap(pkg.ErrNotFound, "")
	} else if err != nil {
		return nil, errors.Wrap(err, "")
	}

	defer rows.Close()
	for rows.Next() {
		var urn string
		if err = rows.Scan(&urn); err != nil {
			return []string{}, errors.Wrap(err, "")
		}
		urns = append(urns, urn)
	}
	return urns, nil
}
开发者ID:ory-am,项目名称:ladon,代码行数:19,代码来源:manager_sql.go


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