示例#1
0
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
	}
}
示例#2
0
文件: sql.go 项目: serge-hulne/golang
// 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
}
示例#3
0
文件: ctxutil.go 项目: oshimaya/go
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
}
示例#4
0
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
}
示例#5
0
文件: ctxutil.go 项目: achanda/go
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
	}
}