• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang mysql.Transaction类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: AddFeed

func AddFeed(transaction mysql.Transaction, uri string) (uint64, error) {
	doc := xmlx.New()

	if err := doc.LoadUriClient(uri, http.DefaultClient, CharsetReader); err != nil {
		return 0, errors.New("Unable to load URI: " + uri)
	}

	getTitle := func() string {
		title := doc.Root.S("*", "title")

		if title == "" {
			title = doc.SelectNode("*", "channel").S("*", "title")
		}
		return title
	}

	getDescription := func() string {
		description := doc.Root.S("*", "subtitle")

		if description == "" {
			description = doc.SelectNode("*", "channel").S("*", "description")
		}
		return description
	}

	getLink := func() string {
		link := ""

		for _, node := range doc.SelectNodes("*", "link") {
			if node.As("", "rel") == "alternate" {
				link = node.As("", "href")
			}
		}

		if link == "" {
			link = doc.SelectNode("*", "channel").S("*", "link")
		}

		return link
	}

	_, res, err := transaction.Query("INSERT INTO `feed`(`title`,`description`,`link`,`last_update`,`feedURL`) VALUES ('%s','%s','%s',0,'%s')",
		getTitle(),
		getDescription(),
		getLink(),
		uri)

	if err != nil {
		return 0, err
	}

	feedId := res.InsertId()

	if err := UpdateFeedDocument(transaction, feedId, doc); err != nil {
		return 0, err
	}

	return feedId, nil
}
开发者ID:Nican,项目名称:RabbitReader,代码行数:59,代码来源:updater.go


示例2: Begin

// Begin begins a transaction and calls f to complete it .
// If f returns an error and IsNetErr(error) == true it reconnects and calls
// f up to MaxRetries times. If error is of type *mysql.Error it tries rollback
// the transaction.
func (c *Conn) Begin(f func(mysql.Transaction, ...interface{}) error, args ...interface{}) error {
	err := c.connectIfNotConnected()
	if err != nil {
		return err
	}
	nn := 0
	for {
		var tr mysql.Transaction
		if tr, err = c.Raw.Begin(); err == nil {
			if err = f(tr, args...); err == nil {
				return nil
			}
		}
		if c.reconnectIfNetErr(&nn, &err); err != nil {
			if _, ok := err.(*mysql.Error); ok && tr.IsValid() {
				tr.Rollback()
			}
			return err
		}
	}
	panic(nil)
}
开发者ID:chogaths,项目名称:robin,代码行数:26,代码来源:autorecon.go


示例3: updatePriorities

func updatePriorities(transaction mysql.Transaction, userId int, newPriorities map[string]GroupInfo) error {

	for feedIdStr, priority := range newPriorities {
		feedId, err := strconv.ParseInt(feedIdStr, 10, 64)

		if err != nil {
			return err
		}

		_, _, err = transaction.Query("UPDATE `user_feed` SET `priority`=%d, `group`='%s'  WHERE feed_id=%d AND user_id=%d",
			priority.Priority,
			gConn.Escape(priority.Group),
			feedId,
			userId)

		if err != nil {
			return err
		}
	}

	return nil
}
开发者ID:Nican,项目名称:RabbitReader,代码行数:22,代码来源:web.go


示例4: UpdateFeedDocument

func UpdateFeedDocument(transaction mysql.Transaction, feedId uint64, doc *xmlx.Document) (retErr error) {

	defer func() {
		if e := recover(); e != nil {
			retErr = e.(error)
		}
	}()

	const ns = "*"

	findFeedEntry := func(title string, content string, date string, guid string) bool {
		search := ""

		if len(guid) > 0 {
			search = fmt.Sprintf("`guid`='%s'", gConn.Escape(guid))
		} else {
			where := make([]string, 0)

			if len(date) > 0 {
				where = append(where, fmt.Sprintf("`published`='%s'", gConn.Escape(date)))
			}

			if len(content) > 0 {
				where = append(where, fmt.Sprintf("`content`='%s'", gConn.Escape(content)))
			}

			if len(title) > 0 {
				where = append(where, fmt.Sprintf("`title`='%s'", gConn.Escape(title)))
			}

			if len(where) == 0 {
				panic(fmt.Sprintf("Can not search item! %s", title))
			}
			search = strings.Join(where, " OR ")
		}

		rows, _, err := transaction.Query("SELECT `id` FROM `feed_entry` WHERE `feed_id`=%d AND (%s)",
			feedId,
			search)

		if err != nil {
			panic(err)
		}

		if len(rows) > 1 {
			ids := make([]string, len(rows))

			for i, row := range rows {
				ids[i] = row.Str(0)
			}

			err := fmt.Sprintf("Found more than one answer! (%s)", strings.Join(ids, ","))
			panic(errors.New(err))
		}

		return len(rows) == 1
	}

	findItems(doc.Root, func(node *xmlx.Node) {
		content := getContent(node)
		link := getLink(node)
		author := getAuthor(node)
		guid := getGUID(node)
		title := node.S(ns, "title")
		published := node.S(ns, "pubDate")
		if len(published) == 0 {
			published = node.S(ns, "published")
		}

		parsedTime, err := parseTime(published)

		if err != nil {
			fmt.Printf("Warning: Failed to parse (%s)\n", published)
		}

		if parsedTime.After(time.Now()) {
			parsedTime = time.Now()
		}

		//Thanks VGCats! You have no content! http://www.vgcats.com/vgcats.rdf.xml
		//if content == "" {
		//	panic(errors.New("Could not find content"))
		//}

		if findFeedEntry(title, content, published, guid) {
			return
		}

		_, _, err = transaction.Query("INSERT INTO `feed_entry`(`feed_id`,`title`,`content`,`published`,`updated`,`author`,`link`,`guid`) VALUES (%d,'%s','%s','%s',%d,'%s','%s','%s')",
			feedId,
			gConn.Escape(title),
			gConn.Escape(content),
			gConn.Escape(published),
			parsedTime.Unix(),
			gConn.Escape(author),
			gConn.Escape(link),
			gConn.Escape(guid))

		if err != nil {
			panic(err)
//.........这里部分代码省略.........
开发者ID:Nican,项目名称:RabbitReader,代码行数:101,代码来源:updater.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang telnet.Conn类代码示例发布时间:2022-05-28
下一篇:
Golang mysql.Row类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap