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

Golang driver.Conn类代码示例

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

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



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

示例1: driverExec

func driverExec(t *testing.T, dc driver.Conn, query string) {
	st, err := dc.Prepare(query)
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		if err := st.Close(); err != nil && t != nil {
			t.Fatal(err)
		}
	}()

	r, err := st.Exec([]driver.Value{})
	if err != nil {
		if t != nil {
			t.Fatal(err)
		}
		return
	}
	_, err = r.RowsAffected()
	if err != nil {
		if t != nil {
			t.Fatalf("r.RowsAffected(%q ...) failed: %v", query, err)
		}
		return
	}
}
开发者ID:weave-lab,项目名称:odbc,代码行数:26,代码来源:mssql_test.go


示例2: ctxDriverBegin

func ctxDriverBegin(ctx context.Context, ci driver.Conn) (driver.Tx, error) {
	if ciCtx, is := ci.(driver.ConnBeginContext); is {
		return ciCtx.BeginContext(ctx)
	}

	if ctx.Done() == context.Background().Done() {
		return ci.Begin()
	}

	// Check the transaction level in ctx. If set and non-default
	// then return an error here as the BeginContext driver value is not supported.
	if level, ok := driver.IsolationFromContext(ctx); ok && level != driver.IsolationLevel(LevelDefault) {
		return nil, errors.New("sql: driver does not support non-default isolation level")
	}

	// Check for a read-only parameter in ctx. If a read-only transaction is
	// requested return an error as the BeginContext driver value is not supported.
	if ro := driver.ReadOnlyFromContext(ctx); ro {
		return nil, errors.New("sql: driver does not support read-only transactions")
	}

	txi, err := ci.Begin()
	if err == nil {
		select {
		default:
		case <-ctx.Done():
			txi.Rollback()
			return nil, ctx.Err()
		}
	}
	return txi, err
}
开发者ID:oshimaya,项目名称:go,代码行数:32,代码来源:ctxutil.go


示例3: ctxDriverBegin

func ctxDriverBegin(ctx context.Context, ci driver.Conn) (driver.Tx, error) {
	if ciCtx, is := ci.(driver.ConnBeginContext); is {
		return ciCtx.BeginContext(ctx)
	}

	if ctx.Done() == context.Background().Done() {
		return ci.Begin()
	}

	// Check the transaction level in ctx. If set and non-default
	// then return an error here as the BeginContext driver value is not supported.
	if level, ok := driver.IsolationFromContext(ctx); ok && level != driver.IsolationLevel(LevelDefault) {
		return nil, errors.New("sql: driver does not support non-default isolation level")
	}

	// Check for a read-only parameter in ctx. If a read-only transaction is
	// requested return an error as the BeginContext driver value is not supported.
	if ro := driver.ReadOnlyFromContext(ctx); ro {
		return nil, errors.New("sql: driver does not support read-only transactions")
	}

	type R struct {
		err   error
		panic interface{}
		txi   driver.Tx
	}
	rc := make(chan R, 1)
	go func() {
		r := R{}
		defer func() {
			if v := recover(); v != nil {
				r.panic = v
			}
			rc <- r
		}()
		r.txi, r.err = ci.Begin()
	}()
	select {
	case <-ctx.Done():
		go func() {
			<-rc
			close(rc)
		}()
		return nil, ctx.Err()
	case r := <-rc:
		if r.panic != nil {
			panic(r.panic)
		}
		return r.txi, r.err
	}
}
开发者ID:Harvey-OS,项目名称:go,代码行数:51,代码来源:ctxutil.go


示例4: queryConn

// queryConn executes a query on the given connection.
// The connection gets released by the releaseConn function.
func (db *DB) queryConn(ci driver.Conn, releaseConn func(error), query string, args []interface{}) (*Rows, error) {
	if queryer, ok := ci.(driver.Queryer); ok {
		dargs, err := driverArgs(nil, args)
		if err != nil {
			releaseConn(err)
			return nil, err
		}
		rowsi, err := queryer.Query(query, dargs)
		if err != driver.ErrSkip {
			if err != nil {
				releaseConn(err)
				return nil, err
			}
			// Note: ownership of ci passes to the *Rows, to be freed
			// with releaseConn.
			rows := &Rows{
				db:          db,
				ci:          ci,
				releaseConn: releaseConn,
				rowsi:       rowsi,
			}
			return rows, nil
		}
	}

	sti, err := ci.Prepare(query)
	if err != nil {
		releaseConn(err)
		return nil, err
	}

	rowsi, err := rowsiFromStatement(sti, args...)
	if err != nil {
		releaseConn(err)
		sti.Close()
		return nil, err
	}

	// Note: ownership of ci passes to the *Rows, to be freed
	// with releaseConn.
	rows := &Rows{
		db:          db,
		ci:          ci,
		releaseConn: releaseConn,
		rowsi:       rowsi,
		closeStmt:   sti,
	}
	return rows, nil
}
开发者ID:serge-hulne,项目名称:golang,代码行数:51,代码来源:sql.go


示例5: ctxDriverPrepare

func ctxDriverPrepare(ctx context.Context, ci driver.Conn, query string) (driver.Stmt, error) {
	if ciCtx, is := ci.(driver.ConnPrepareContext); is {
		return ciCtx.PrepareContext(ctx, query)
	}
	si, err := ci.Prepare(query)
	if err == nil {
		select {
		default:
		case <-ctx.Done():
			si.Close()
			return nil, ctx.Err()
		}
	}
	return si, err
}
开发者ID:oshimaya,项目名称:go,代码行数:15,代码来源:ctxutil.go


示例6: commitTrans

func commitTrans(dbcon driver.Conn, cmd string) error {
	dbstmt, e := dbcon.Prepare(cmd)
	if e != nil {
		return e
	}
	_, e = dbstmt.Exec(nil)
	if e != nil {
		return e
	}
	e = dbstmt.Close()
	if e != nil {
		return e
	}
	return nil
}
开发者ID:ajjaic,项目名称:scrapmtc,代码行数:15,代码来源:addtodb.go


示例7: putConn

// putConn adds a connection to the db's free pool.
// err is optionally the last error that occurred on this connection.
func (db *DB) putConn(c driver.Conn, err error) {
	if err == driver.ErrBadConn {
		// Don't reuse bad connections.
		return
	}
	db.mu.Lock()
	if putConnHook != nil {
		putConnHook(db, c)
	}
	if n := len(db.freeConn); !db.closed && n < db.maxIdleConns() {
		db.freeConn = append(db.freeConn, c)
		db.mu.Unlock()
		return
	}
	// TODO: check to see if we need this Conn for any prepared
	// statements which are still active?
	db.mu.Unlock()
	c.Close()
}
开发者ID:redcatmiss,项目名称:gcc,代码行数:21,代码来源:sql.go


示例8: ctxDriverBegin

func ctxDriverBegin(ctx context.Context, ci driver.Conn) (driver.Tx, error) {
	if ciCtx, is := ci.(driver.ConnBeginContext); is {
		return ciCtx.BeginContext(ctx)
	}
	if ctx.Done() == context.Background().Done() {
		return ci.Begin()
	}

	// TODO(kardianos): check the transaction level in ctx. If set and non-default
	// then return an error here as the BeginContext driver value is not supported.

	type R struct {
		err   error
		panic interface{}
		txi   driver.Tx
	}
	rc := make(chan R, 1)
	go func() {
		r := R{}
		defer func() {
			if v := recover(); v != nil {
				r.panic = v
			}
			rc <- r
		}()
		r.txi, r.err = ci.Begin()
	}()
	select {
	case <-ctx.Done():
		go func() {
			<-rc
			close(rc)
		}()
		return nil, ctx.Err()
	case r := <-rc:
		if r.panic != nil {
			panic(r.panic)
		}
		return r.txi, r.err
	}
}
开发者ID:achanda,项目名称:go,代码行数:41,代码来源:ctxutil.go


示例9: ctxDriverPrepare

func ctxDriverPrepare(ctx context.Context, ci driver.Conn, query string) (driver.Stmt, error) {
	if ciCtx, is := ci.(driver.ConnPrepareContext); is {
		return ciCtx.PrepareContext(ctx, query)
	}
	if ctx.Done() == context.Background().Done() {
		return ci.Prepare(query)
	}

	type R struct {
		err   error
		panic interface{}
		si    driver.Stmt
	}

	rc := make(chan R, 1)
	go func() {
		r := R{}
		defer func() {
			if v := recover(); v != nil {
				r.panic = v
			}
			rc <- r
		}()
		r.si, r.err = ci.Prepare(query)
	}()
	select {
	case <-ctx.Done():
		go func() {
			<-rc
			close(rc)
		}()
		return nil, ctx.Err()
	case r := <-rc:
		if r.panic != nil {
			panic(r.panic)
		}
		return r.si, r.err
	}
}
开发者ID:achanda,项目名称:go,代码行数:39,代码来源:ctxutil.go


示例10: putConn

// putConn adds a connection to the db's free pool.
// err is optionally the last error that occurred on this connection.
func (db *DB) putConn(c driver.Conn, err error) {
	db.mu.Lock()
	if !db.outConn[c] {
		if debugGetPut {
			fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", c, stack(), db.lastPut[c])
		}
		panic("sql: connection returned that was never out")
	}
	if debugGetPut {
		db.lastPut[c] = stack()
	}
	delete(db.outConn, c)

	if fns, ok := db.onConnPut[c]; ok {
		for _, fn := range fns {
			fn()
		}
		delete(db.onConnPut, c)
	}

	if err == driver.ErrBadConn {
		// Don't reuse bad connections.
		db.mu.Unlock()
		return
	}
	if putConnHook != nil {
		putConnHook(db, c)
	}
	if n := len(db.freeConn); !db.closed && n < db.maxIdleConns() {
		db.freeConn = append(db.freeConn, c)
		db.mu.Unlock()
		return
	}
	// TODO: check to see if we need this Conn for any prepared
	// statements which are still active?
	db.mu.Unlock()
	c.Close()
}
开发者ID:serge-hulne,项目名称:golang,代码行数:40,代码来源:sql.go


示例11: Open

func (p *Proxy) Open(name string) (driver.Conn, error) {
	var err error
	var ctx interface{}

	var conn driver.Conn
	if h := p.Hooks.PostOpen; h != nil {
		// Setup PostOpen. This needs to be a closure like this
		// or otherwise changes to the `ctx` and `conn` parameters
		// within this Open() method does not get applied at the
		// time defer is fired
		defer func() { h(ctx, conn) }()
	}

	if h := p.Hooks.PreOpen; h != nil {
		if ctx, err = h(name); err != nil {
			return nil, err
		}
	}
	conn, err = p.Driver.Open(name)
	if err != nil {
		return nil, err
	}

	conn = &Conn{
		Conn:  conn,
		Proxy: p,
	}

	if hook := p.Hooks.Open; hook != nil {
		if err = hook(ctx, conn); err != nil {
			conn.Close()
			return nil, err
		}
	}
	return conn, nil
}
开发者ID:mosasiru,项目名称:go-sql-proxy,代码行数:36,代码来源:proxy.go


示例12: closeConn

func (db *DB) closeConn(c driver.Conn) {
	// TODO: check to see if we need this Conn for any prepared statements
	// that are active.
	c.Close()
}
开发者ID:krasin,项目名称:go-deflate,代码行数:5,代码来源:sql.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang driver.Stmt类代码示例发布时间:2022-05-24
下一篇:
Golang driver.Value函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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