Skip to content

Keep one connection open for the lifetime of sqlcmd #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/sqlcmd/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package sqlcmd

import (
"context"
"strings"
"testing"

Expand Down Expand Up @@ -62,7 +63,7 @@ func TestCalcColumnDetails(t *testing.T) {
if assert.NoError(t, err, "ConnectDB failed") {
defer db.Close()
for x, test := range tests {
rows, err := db.Query(test.query)
rows, err := db.QueryContext(context.Background(), test.query)
if assert.NoError(t, err, "Query failed: %s", test.query) {
defer rows.Close()
cols, err := rows.ColumnTypes()
Expand Down
5 changes: 2 additions & 3 deletions pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Console interface {
type Sqlcmd struct {
lineIo Console
workingDirectory string
db *sql.DB
db *sql.Conn
out io.WriteCloser
err io.WriteCloser
batch *Batch
Expand Down Expand Up @@ -232,8 +232,7 @@ func (s *Sqlcmd) ConnectDb(connect *ConnectSettings, nopw bool) error {
if err != nil {
return err
}
db := sql.OpenDB(connector)
err = db.Ping()
db, err := sql.OpenDB(connector).Conn(context.Background())
if err != nil {
fmt.Fprintln(s.GetOutput(), err)
return err
Expand Down
11 changes: 10 additions & 1 deletion pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestSqlCmdConnectDb(t *testing.T) {
}
}

func ConnectDb(t testing.TB) (*sql.DB, error) {
func ConnectDb(t testing.TB) (*sql.Conn, error) {
v := InitializeVariables(true)
s := &Sqlcmd{vars: v}
s.Connect = newConnect(t)
Expand Down Expand Up @@ -408,6 +408,15 @@ func TestSqlCmdDefersToPrintError(t *testing.T) {
}
}

func TestSqlCmdMaintainsConnectionBetweenBatches(t *testing.T) {
s, buf := setupSqlCmdWithMemoryOutput(t)
defer buf.Close()
err := runSqlCmd(t, s, []string{"CREATE TABLE #tmp1 (col1 int)", "insert into #tmp1 values (1)", "GO", "select * from #tmp1", "drop table #tmp1", "GO"})
if assert.NoError(t, err, "runSqlCmd failed") {
assert.Equal(t, oneRowAffected+SqlcmdEol+"1"+SqlcmdEol+SqlcmdEol+oneRowAffected+SqlcmdEol, buf.buf.String(), "Sqlcmd uses the same connection for all queries")
}
}

// runSqlCmd uses lines as input for sqlcmd instead of relying on file or console input
func runSqlCmd(t testing.TB, s *Sqlcmd, lines []string) error {
t.Helper()
Expand Down