Go Sql Server database driver.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 1da6daf Sep 3, 2018
Permalink
Failed to load latest commit information.
example Extend basic example with insert and update Jul 22, 2016
.gitignore Ignore binary Mar 12, 2016
MIT-LICENSE.txt Adding licence Dec 19, 2012
README.md Fix readme formatting Sep 3, 2018
callbacks.go Fix build Mar 26, 2018
camelize.go running go fmt Mar 4, 2014
camelize_test.go Added sp_help example Mar 12, 2016
code_generator.rb Result Scan and Next functions. Jan 5, 2014
conn.go Fix leak on opening new connection Jul 30, 2018
conn_pool.go Keep listening for pool cleanup ticks Sep 3, 2018
conn_pool_test.go Fix conn pool cleanup Jul 29, 2018
conn_sp.go adjustments for Sybase ASE 12.5 and addition of tests Apr 24, 2018
conn_sp_test.go adjustments for Sybase ASE 12.5 and addition of tests Apr 24, 2018
conn_test.go Reduce number of places for making decision Apr 25, 2018
convert.go Store time.Zero as null in mssql May 22, 2016
convert_sql_buf.go Fix reading local time during day light saving interval Mar 26, 2018
convert_sql_buf_test.go Changing assert package used in tests Feb 9, 2016
convert_test.go Renaming files to match with database/sql from go source. Adding test… Feb 2, 2014
credentials.go Fixes parsing connect string when for e.g. pass contains "=" character Jul 18, 2017
credentials_test.go adjustments for Sybase ASE 12.5 and addition of tests Apr 24, 2018
executesql.go Reduce number of places for making decision Apr 25, 2018
executesql_test.go Reduce number of places for making decision Apr 25, 2018
fetch.go adjustments for Sybase ASE 12.5 and addition of tests Apr 24, 2018
mssql_conn.go Removing Connect and Connect2 methods from Conn, the only way to esta… Mar 4, 2014
mssql_stmt.go Reduce number of places for making decision Apr 25, 2018
mssql_test.go adjustments for Sybase ASE 12.5 and addition of tests Apr 24, 2018
params_cache.go Added params_cache file Sep 9, 2016
result.go When scanning into a single value, check if that value is a time and Oct 30, 2017
result_column.go Move type ResultColumn to result_column.go Nov 23, 2016
result_test.go When scanning into a single value, check if that value is a time and Oct 30, 2017
sp_result.go running go fmt Mar 4, 2014
sp_result_test.go Changing assert package used in tests Feb 9, 2016
tds_types.txt Converting go to sql datatypes. Jan 5, 2014

README.md

gofreetds

Go FreeTDS wrapper. Native Sql Server database driver.

Features:

  • can be used as database/sql driver
  • handles calling stored procedures
  • handles multiple resultsets
  • supports database mirroring
  • connection pooling
  • scaning resultsets into structs

Get started

Install dependencines

FreeTDS libraries must be installed on the system.

Mac

brew install freetds

Ubuntu, Debian...

sudo apt-get install freetds-dev

Go get

go get github.com/minus5/gofreetds

Docs

http://godoc.org/github.com/minus5/gofreetds

Using as database/sql driver

Name of the driver is mssql.

db, err := sql.Open("mssql", connStr)
...
row := db.QueryRow("SELECT au_fname, au_lname name FROM authors WHERE au_id = ?", "172-32-1176")
..
var firstName, lastName string
err = row.Scan(&firstName, &lastName)

Full example in example/mssql.

Stored Procedures

What I'm missing in database/sql is calling stored procedures, handling return values and output params. And especially handling multiple result sets. Which is all supported by FreeTDS and of course by gofreetds.

Connect:

pool, err := freetds.NewConnPool("user=ianic;pwd=ianic;database=pubs;host=iow")
defer pool.Close()
...
//get connection
conn, err := pool.Get()
defer conn.Close()

Execute stored procedure:

rst, err := conn.ExecSp("sp_help", "authors")  

Read sp return value, and output params:

returnValue := rst.Status()
var param1, param2 int
rst.ParamScan(&param1, &param2)

Read sp resultset (fill the struct):

author := &Author{}
rst.Scan(author)

Read next resultset:

if rst.NextResult() {
    for rst.Next() {
        var v1, v2 string
        rst.Scan(&v1, &v2)
    }
}

Full example in example/stored_procedure

Other usage

Executing arbitrary sql is supported with Exec or ExecuteSql.

Execute query:

rst, err := conn.Exec("select au_id, au_lname, au_fname from authors")

Rst is array of results. Each result has Columns and Rows array. Each row is array of values. Each column is array of ResultColumn objects.

Full example in example/exec.

Execute query with params:

rst, err := conn.ExecuteSql("select au_id, au_lname, au_fname from authors where au_id = ?", "998-72-3567")

Sybase Compatibility Mode

Gofreetds now supports Sybase ASE 16.0 through the driver. In order to support this, this post is very helpful: Connect to MS SQL Server and Sybase ASE from Mac OS X and Linux with unixODBC and FreeTDS (from Internet Archive)

To use a Sybase ASE server with Gofreetds, you simply need to set a compatibility mode on your connection string after you've configured your .odbc.ini file and .freetds.conf file.

This mode uses TDS Version 5.

Connection String Parameter

You can set your connection string up for Sybase by using the 'compatibility_mode' Parameter. The parameter can be named 'compatibility', 'compatibility mode', 'compatibility_mode' or 'Compatibility Mode'. Currently this mode only supports Sybase. To specify you can use 'sybase' or 'Sybase'.

Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;Failover Partner=myMirror;Max Pool Size=200;Compatibility Mode=Sybase

Testing

Tests depend on the pubs database.

Pubs sample database install script could be downloaded. After installing that package you will find instpubs.sql on the disk (C:\SQL Server 2000 Sample Databases). Execute that script to create pubs database.

Tests and examples are using environment variable GOFREETDS_CONN_STR to connect to the pubs database.

export GOFREETDS_CONN_STR="user=ianic;pwd=ianic;database=pubs;host=iow"
export GOFREETDS_MIRROR_HOST="iow-mirror"

If you don't want to setup and test database mirroring than don't define GOFREETDS_MIRROR_HOST. Mirroring tests will be skipped.