Code Monkey home page Code Monkey logo

Comments (4)

qustavo avatar qustavo commented on August 26, 2024

can you provide the code you are using?

from sqlhooks.

tochka avatar tochka commented on August 26, 2024
package mssql_test

import (
	"context"
	"database/sql"
	"database/sql/driver"
	"fmt"
	"net"
	"testing"

	"github.com/docker/docker/api/types/container"
	"github.com/docker/go-connections/nat"
	gomssql "github.com/microsoft/go-mssqldb"
	"github.com/qustavo/sqlhooks/v2"
	"github.com/stretchr/testify/require"
	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/wait"
)

const ddlTestFunc = `create procedure test_proc  AS
begin
	DECLARE @ErrorMsg varchar(7000)

	SET NOCOUNT ON;
	select @ErrorMsg = 'Test error'
	raiserror(@ErrorMsg, 16, 1)
	return
end;`

func TestMsSQL(t *testing.T) {
	ctx := context.Background()
	req := testcontainers.ContainerRequest{
		Image:        "mcr.microsoft.com/mssql/server:2019-latest",
		ExposedPorts: []string{"1433/tcp"},
		SkipReaper:   true,
		Env: map[string]string{
			"ACCEPT_EULA":       "Y",
			"MSSQL_PID":         "Evaluation",
			"MSSQL_SA_PASSWORD": "Test87654321",
		},
		WaitingFor: wait.ForSQL("1433/tcp", "sqlserver", func(host string, port nat.Port) string {
			return fmt.Sprintf("sqlserver://sa:Test87654321@%s/master", net.JoinHostPort(host, port.Port()))
		}),
		HostConfigModifier: func(hc *container.HostConfig) {
			hc.AutoRemove = true
		},
	}
	container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: req,
		Started:          true,
		Logger:           testcontainers.TestLogger(t),
	})
	require.NoError(t, err)

	t.Cleanup(func() {
		container.Stop(ctx, nil)
	})

	hostPort, err := container.PortEndpoint(ctx, "1433/tcp", "")
	require.NoError(t, err)

	connStr := fmt.Sprintf("sqlserver://sa:Test87654321@%s/master", hostPort)
	t.Log(connStr)

	db := openDB(connStr, []sqlhooks.Hooks{simpleHook{}})

	_, err = db.Exec(ddlTestFunc)
	require.NoError(t, err)

	_, err = db.Exec(`EXEC test_proc`)
	require.Error(t, err, "error is: %v", err)
}

type simpleHook struct{}

func (h simpleHook) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	return ctx, nil
}
func (h simpleHook) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	return ctx, nil
}
func (h simpleHook) OnError(ctx context.Context, err error, query string, args ...interface{}) error {
	return err
}

// openDB opens new sql DB pool with an optional hooks and optional tracing.
func openDB(dsn string, hooks []sqlhooks.Hooks) *sql.DB {
	var drv driver.Driver = &gomssql.Driver{}

	if len(hooks) > 0 {
		drv = sqlhooks.Wrap(drv, sqlhooks.Compose(hooks...))
	}

	// Open DB connections pool.
	return sql.OpenDB(&connector{
		driver: drv, dsn: dsn,
	})
}

// connector is a connector with dsn and driver.
type connector struct {
	driver driver.Driver
	dsn    string
}

// Connect returns a connection to the database.
func (c *connector) Connect(_ context.Context) (driver.Conn, error) {
	return c.driver.Open(c.dsn)
}

// Driver returns the underlying Driver of the Connector.
func (c *connector) Driver() driver.Driver {
	return c.driver
}

from sqlhooks.

tochka avatar tochka commented on August 26, 2024

@qustavo
Do you have any news about the panic?

from sqlhooks.

qustavo avatar qustavo commented on August 26, 2024

sorry @tochka for the late reply. Unfortunately I haven't had the capacity nor the possibility to experiment with mssql. Have you tried to debug it?

from sqlhooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.