freetds

package module
v0.0.0-...-b32fe05 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2014 License: MIT Imports: 13 Imported by: 0

README

##About

Go FreeTDS wrapper. Native Sql Server database driver.

##Dependencies FreeTDS libraries must be installed on the system.

Mac

  brew install freetds

Ubuntu, Debian...

  sudo apt-get install freetds

##Usage Connect:

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

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.

Execute stored procedure:

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

Execute query with params:

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

Tests

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.

Documentation

Overview

Package freetds provides interface to Microsoft Sql Server database by using freetds C lib: http://www.freetds.org.

Index

Constants

View Source
const (
	//name               database type   go type
	SYBINT1 = 48  //tinyint       uint8
	SYBINT2 = 52  //smallint      int16
	SYBINT4 = 56  //int           int32
	SYBINT8 = 127 //bigint        int64

	SYBCHAR      = 47
	SYBVARCHAR   = 39  //varchar       string
	SYBNVARCHAR  = 103 //nvarchar      string
	XSYBNVARCHAR = 231 //nvarchar      string
	XSYBNCHAR    = 239 //nchar         string

	SYBREAL = 59 //real          float32
	SYBFLT8 = 62 //float(53)     float64

	SYBBIT  = 50  //bit           bool
	SYBBITN = 104 //bit           bool

	SYBMONEY4 = 122 //smallmoney    float64
	SYBMONEY  = 60  //money         float64

	SYBDATETIME  = 61 //datetime      time.Time
	SYBDATETIME4 = 58 //smalldatetime time.Time

	SYBIMAGE      = 34  //image         []byte
	SYBBINARY     = 45  //binary        []byte
	SYBVARBINARY  = 37  //varbinary     []byte
	XSYBVARBINARY = 165 //varbinary     []byte
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	Error   string
	Message string
	// contains filtered or unexported fields
}

Connection to the database.

func Connect

func Connect(user, pwd, host, database string) (*Conn, error)

Connect to the database, returns new connection or error.

func Connect2

func Connect2(user, pwd, host, mirrorHost, database string) (*Conn, error)

Connect to the database with mirroring support, returns new connection or error.

func ConnectWithConnectionString

func ConnectWithConnectionString(connStr string) (*Conn, error)

Connect to the database with connection string, returns new connection or error. Example:

conn, err := ConnectWithConnectionString("host=myServerA;database=myDataBase;user=myUsername;pwd=myPassword;mirror=myMirror")

Mirror is optional, other params are mandatory.

func (*Conn) Begin

func (conn *Conn) Begin() error

Begin database transaction.

func (*Conn) Close

func (conn *Conn) Close()

If conn belongs to pool release connection to the pool. If not close connection.

func (*Conn) Commit

func (conn *Conn) Commit() error

Commit database transaction.

func (*Conn) DbUse

func (conn *Conn) DbUse() error

Change database.

func (*Conn) Exec

func (conn *Conn) Exec(sql string) ([]*Result, error)

Execute sql query.

func (*Conn) ExecSp

func (conn *Conn) ExecSp(spName string, params ...interface{}) (*SpResult, error)

Execute stored procedure by name and list of params.

Example:

conn.ExecSp("sp_help", "authors")

func (*Conn) ExecuteSql

func (conn *Conn) ExecuteSql(query string, params ...driver.Value) ([]*Result, error)

Execute sql query with arguments. ? in query are arguments placeholders.

ExecuteSql("select * from authors where au_fname = ?", "John")

func (*Conn) Rollback

func (conn *Conn) Rollback() error

Rollback database transaction.

func (*Conn) SelectValue

func (conn *Conn) SelectValue(sql string) (interface{}, error)

Query database and return first column in the first row as result.

type ConnPool

type ConnPool struct {
	// contains filtered or unexported fields
}

ConnPool - connection pool for the maxCount connections.

Connection can be acquired from the pool by pool.Get().

Release conn to the pool by caling conn.Close() or pool.Release(conn).

Destroy pool and all connections by calling pool.Close().

Connections will be removed from the pool if not active for poolExpiresInterval. But there is always one connection in the pool.

Example:

pool, err := NewConnPool("host=myServerA;database=myDataBase;user=myUsername;pwd=myPassword", 100)
...
conn, err := pool.Get()
//use conn
conn.Close()
...
pool.Close()

func NewConnPool

func NewConnPool(connStr string, maxConn int) (*ConnPool, error)

NewCoonPool creates new connection pool. Connection will be created using provided connection string. MaxConn is max number of connections in the pool.

New connections will be created when needed. There is always one connection in the pool.

Returns err if fails to create initial connection.

func (*ConnPool) Close

func (p *ConnPool) Close()

Close connection pool. Closes all existing connections in the pool.

func (*ConnPool) Get

func (p *ConnPool) Get() (*Conn, error)

Get returns connection from the pool. Blocks if there are no free connections, and maxConn is reached.

func (*ConnPool) Release

func (p *ConnPool) Release(conn *Conn)

Release connection to the pool.

func (*ConnPool) Stat

func (p *ConnPool) Stat() (max, count, active int)

Statistic about connections in the pool.

type MssqlConn

type MssqlConn struct {
	// contains filtered or unexported fields
}

implements Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlConn) Begin

func (c *MssqlConn) Begin() (driver.Tx, error)

implements Begin for Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlConn) Close

func (c *MssqlConn) Close() error

implements Close for Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlConn) Prepare

func (c *MssqlConn) Prepare(query string) (driver.Stmt, error)

implements Prepare for Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go

type MssqlConnTx

type MssqlConnTx struct {
	// contains filtered or unexported fields
}

implements Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlConnTx) Commit

func (t *MssqlConnTx) Commit() error

implements Commit for Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlConnTx) Rollback

func (t *MssqlConnTx) Rollback() error

implements Rollback for Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go

type MssqlDriver

type MssqlDriver struct{}

implements Driver interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlDriver) Open

func (d *MssqlDriver) Open(dsn string) (driver.Conn, error)

implements Open for Driver interface from http://golang.org/src/pkg/database/sql/driver/driver.go

type MssqlResult

type MssqlResult struct {
	// contains filtered or unexported fields
}

implements Result interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlResult) LastInsertId

func (r *MssqlResult) LastInsertId() (int64, error)

func (*MssqlResult) RowsAffected

func (r *MssqlResult) RowsAffected() (int64, error)

type MssqlRows

type MssqlRows struct {
	// contains filtered or unexported fields
}

implements Rows interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlRows) Close

func (r *MssqlRows) Close() error

func (*MssqlRows) Columns

func (r *MssqlRows) Columns() []string

func (*MssqlRows) Next

func (r *MssqlRows) Next(dest []driver.Value) error

type MssqlStmt

type MssqlStmt struct {
	// contains filtered or unexported fields
}

implements Stmt interface from http://golang.org/src/pkg/database/sql/driver/driver.go

func (*MssqlStmt) Close

func (s *MssqlStmt) Close() error

func (*MssqlStmt) Exec

func (s *MssqlStmt) Exec(args []driver.Value) (driver.Result, error)

func (*MssqlStmt) NumInput

func (s *MssqlStmt) NumInput() int

func (*MssqlStmt) Query

func (s *MssqlStmt) Query(args []driver.Value) (driver.Rows, error)

type Result

type Result struct {
	Columns      []*ResultColumn
	Rows         [][]interface{}
	ReturnValue  int
	RowsAffected int
	Message      string
	// contains filtered or unexported fields
}

func NewResult

func NewResult() *Result

func (*Result) Next

func (r *Result) Next() bool

func (*Result) Scan

func (r *Result) Scan(dest ...interface{}) error

Scan copies the columns in the current row into the values pointed at by dest.

type ResultColumn

type ResultColumn struct {
	Name   string
	DbSize int
	DbType int
	Type   string
}

type SpOutputParam

type SpOutputParam struct {
	Name  string
	Value interface{}
}

Stored procedure output parameter name and value.

type SpResult

type SpResult struct {
	Results      []*Result
	Status       int
	OutputParams []*SpOutputParam
}

Stored procedure execution result.

func (*SpResult) HasOutputParams

func (r *SpResult) HasOutputParams() bool

Does the stored procedure has any output params.

func (*SpResult) HasResults

func (r *SpResult) HasResults() bool

Does the stored procedure returned any resultsets.

func (*SpResult) Scan

func (r *SpResult) Scan(values ...interface{}) error

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL