Categorygithub.com/billgraziano/mssqlh
modulepackage
1.0.2
Repository: https://github.com/billgraziano/mssqlh.git
Documentation: pkg.go.dev

# README

SQL Server Helper Library

Package mssqlh provides connection string building and helper routines for working with Microsoft SQL Server.

This package provides support for connecting to SQL Server using either:

Using the Connection type, you should be able to switch seamlessly between the two. The package defaults to the "mssql" driver (mssqlh.DriverMSSQL) usless you specify the "odbc" driver (mssqlh.DriverODBC).

Example using Open:

db, err := mssqlh.Open(fqdn)

This uses a trusted connection to the designated server using the mssql driver. It accepts server.domain.com, server\instance, server,port, or server:port.

Example code using NewConnection:

cxn := mssqlh.NewConnection("localhost", "", "", "myapp")
db, err := sql.Open("mssql", cxn.String())

If you don't pass user and password, it defaults to a trusted connection.

Example using the Connection type:

cxn := mssqlh.Connect{
	FQDN:        "db-txn.corp.loc",
	Application: "myapp",
	DialTimeout: 15,
}
cxn.Database = "TXNDB"
db, err := cxn.Open()

Defaults

The package provides the following defaults

  1. If no server is specified, use localhost
  2. If no user is specified, default to a trusted connection
  3. If no application name is specified, default to the name of the executable

Using the ODBC driver

The subpackage odbch provides additional support for using ODBC driver (https://github.com/alexbrainman/odbc)

Example code using the Connection object:

cxn := mssqlh.Connect{
	Driver:     mssqlh.DriverODBC,
	ODBCDriver: odbch.ODBC18,
	FQDN:       "localhost",
}
db, err := cxn.Open()

This connects using the specified ODBC driver.

SQL Server Version Support

GetServer and GetSession should support SQL Server 2005 and beyond. They have been tested on SQL Server 2014 through SQL Server 2019.

There is limited testing with Azure SQL Databases. The GetSession method requires VIEW DATABASE STATE permission.

Linux

It should support Linux but this has recived very little testing.

Linux looks for the following files to locate installed ODBC drivers:

  • /usr/local/etc/odbcinst.ini
  • /etc/odbcinst.ini

Applications

The system comes with three sample applications

  • mssqlh.exe is a sample application
  • odbcraw.exe can test ODBC connections from a settings.txt file
  • testkerbexe can test connections for Kerberos from a settings.txt file

# Packages

No description provided by the author
No description provided by the author

# Functions

GetServer gets details on the SQL Server TODO Create a queryer interface and accept that (suppport sqlx).
GetSession gets details on the current connection to SQL Server TODO Create a queryer interface and accept that This requires VIEW DATABASE STATE in Azure.
NewConnection returns a connection with sane defaults.
Open connects to a SQL Server.
QuoteName wraps a string in [brackets].
QuoteString wraps a string in single-quotes and tries to handle embedded quotes.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

DriverMSSQL uses the https://github.com/denisenkom/go-mssqldb library.
DriverODBC uses the https://github.com/alexbrainman/odbc library.
https://learn.microsoft.com/en-us/troubleshoot/sql/connect/certificate-chain-not-trusted?tabs=odbc-driver-18x https://techcommunity.microsoft.com/t5/sql-server-blog/odbc-driver-18-0-for-sql-server-released/ba-p/3169228 https://learn.microsoft.com/en-us/sql/connect/odbc/windows/release-notes-odbc-sql-server-windows?view=sql-server-ver16.

# Structs

Connection is the basis for building connection strings.
Server holds information about an instance of SQL Server.
Session stores information about the connection to SQL Server.