Documentation ¶
Overview ¶
Package freetds provides interface to Microsoft Sql Server database by using freetds C lib: http://www.freetds.org.
Index ¶
- Constants
- func NewCredentials(connStr string) *credentials
- type Conn
- func (conn *Conn) Begin() error
- func (conn *Conn) Close()
- func (conn *Conn) Commit() error
- func (conn *Conn) DbUse() error
- func (conn *Conn) Exec(sql string) ([]*Result, error)
- func (conn *Conn) ExecSp(spName string, params ...interface{}) (*SpResult, error)
- func (conn *Conn) ExecuteSql(query string, params ...driver.Value) ([]*Result, error)
- func (conn *Conn) HasMessageNumber(msgno int) int
- func (conn *Conn) MirrorStatus() (bool, bool, bool, error)
- func (conn *Conn) Rollback() error
- func (conn *Conn) SelectValue(sql string) (interface{}, error)
- type ConnPool
- type MssqlConn
- type MssqlConnTx
- type MssqlDriver
- type MssqlResult
- type MssqlRows
- type MssqlStmt
- type ParamsCache
- type RawBytes
- type Result
- func (r *Result) CurrentRow() int
- func (r *Result) FindColumn(name string) (int, error)
- func (r *Result) HasNext() bool
- func (r *Result) MustScan(cnt int, dest ...interface{}) error
- func (r *Result) Next() bool
- func (r *Result) Scan(dest ...interface{}) error
- func (r *Result) ScanColumn(name string, dest interface{}) error
- type ResultColumn
- type SpOutputParam
- type SpResult
- func (r *SpResult) HasOutputParams() bool
- func (r *SpResult) HasResults() bool
- func (r *SpResult) MustScan(cnt int, dest ...interface{}) error
- func (r *SpResult) Next() bool
- func (r *SpResult) NextResult() bool
- func (r *SpResult) ParamScan(values ...interface{}) error
- func (r *SpResult) Result() *Result
- func (r *SpResult) ResultsCount() int
- func (r *SpResult) Scan(dest ...interface{}) error
- func (r SpResult) Status() int
Constants ¶
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 XSYBXML = 241 //XML 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 SYBNUMERIC = 108 SYBDECIMAL = 106 SYBUNIQUE = 36 //uniqueidentifier string )
const ( DBVERSION_UNKNOWN byte = iota DBVERSION_46 DBVERSION_100 // DBVERSION_42 TDS 4.2 Sybase and Microsoft //The version in use at the time of the Sybase/Microsoft split. DBVERSION_42 // DBVERSION_70 TDS 7.0 Microsoft //Introduced for SQL Server 7.0. Includes support for the extended datatypes in SQL Server 7.0 (such as char/varchar fields of more than 255 characters). It also includes support for Unicode. DBVERSION_70 // DBVERSION_71 TDS 7.1 Microsoft, was 8.0 [2] //Introduced for SQL Server 2000. Includes support for big integer (64-bit int) and “variant” datatypes. DBVERSION_71 // DBVERSION_72 TDS 7.2 Microsoft, was 9.0 //Introduced for SQL Server 2005. Includes support for varchar(max), varbinary(max), xml datatypes and MARS. DBVERSION_72 // DBVERSION_73 TDS 7.3 Microsoft //Introduced for SQL Server 2008. Includes support for extended date/time, table as parameters. DBVERSION_73 // DBVERSION_74 TDS 7.4 Microsoft //Introduced for SQL Server 2012. Includes support for session recovery. DBVERSION_74 )
const SYBASE string = "sybase"
const SYBASE_12_5 string = "sybase_12_5"
Variables ¶
This section is empty.
Functions ¶
func NewCredentials ¶
func NewCredentials(connStr string) *credentials
NewCredentials fills credentials stusct from connection string
Types ¶
type Conn ¶
Connection to the database.
func NewConn ¶
NewConn Connect to the database with connection string, returns new connection or error. Example:
conn, err := NewConn("host=myServerA;database=myDataBase;user=myUsername;pwd=myPassword;mirror=myMirror")
Mirror is optional, other params are mandatory.
func (*Conn) Close ¶
func (conn *Conn) Close()
If conn belongs to pool release connection to the pool. If not close connection.
func (*Conn) ExecSp ¶
Execute stored procedure by name and list of params.
Example:
conn.ExecSp("sp_help", "authors")
func (*Conn) ExecuteSql ¶
Execute sql query with arguments. ? in query are arguments placeholders.
ExecuteSql("select * from authors where au_fname = ?", "John")
func (*Conn) HasMessageNumber ¶
Returns the number of occurances of a supplied FreeTDS message number.
func (*Conn) MirrorStatus ¶
Checking database mirroring status:
isDefined - is mirror defined (mirror parametar passed in connection string) isActive - is mirroring active for this database isMaster - is the current host master for this database
Returns error if could not execute query to get current mirroring status.
func (*Conn) SelectValue ¶
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") ... conn, err := pool.Get() //use conn conn.Close() ... pool.Close()
func NewConnPool ¶
NewCoonPool creates new connection pool. Connection will be created using provided connection string. Max number of connections in the pool is controlled by max_pool_size connection string parameter, default is 100.
New connections will be created when needed. There is always one connection in the pool.
Returns err if fails to create initial connection. Valid connection string examples:
"host=myServerA;database=myDataBase;user=myUsername;pwd=myPassword;" "host=myServerA;database=myDataBase;user=myUsername;pwd=myPassword;max_pool_size=500" "host=myServerA;database=myDataBase;user=myUsername;pwd=myPassword;mirror=myMirror"
func (*ConnPool) Close ¶
func (p *ConnPool) Close()
Close connection pool. Closes all existing connections in the pool.
func (*ConnPool) Do ¶
Get connection from pool and execute handler. Release connection after handler is called.
func (*ConnPool) DoInTransaction ¶
Get new connection from pool, and execute handler in transaction. If handler returns error transaction will be rolled back. Release connection after handerl is called.
type MssqlConn ¶
type MssqlConn struct {
// contains filtered or unexported fields
}
MssqlConn implements Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (*MssqlConn) Begin ¶
Begin implements Begin for Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (*MssqlConn) Close ¶
Close implements Close for Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go
type MssqlConnTx ¶
type MssqlConnTx struct {
// contains filtered or unexported fields
}
MssqlConnTx implements Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (*MssqlConnTx) Commit ¶
func (t *MssqlConnTx) Commit() error
Commit implements Commit for Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (*MssqlConnTx) Rollback ¶
func (t *MssqlConnTx) Rollback() error
Rollback implements Rollback for Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go
type MssqlDriver ¶
type MssqlDriver struct{}
MssqlDriver 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)
Open 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
type MssqlStmt ¶
type MssqlStmt struct {
// contains filtered or unexported fields
}
implements Stmt interface from http://golang.org/src/pkg/database/sql/driver/driver.go
type ParamsCache ¶
func NewParamsCache ¶
func NewParamsCache() *ParamsCache
func (*ParamsCache) Get ¶
func (pc *ParamsCache) Get(spName string) ([]*spParam, bool)
func (*ParamsCache) Set ¶
func (pc *ParamsCache) Set(spName string, params []*spParam)
type RawBytes ¶
type RawBytes []byte
RawBytes is a byte slice that holds a reference to memory owned by the database itself. After a Scan into a RawBytes, the slice is only valid until the next call to Next, Scan, or Close.
type Result ¶
type Result struct { Columns []*ResultColumn Rows [][]interface{} ReturnValue int RowsAffected int Message string // contains filtered or unexported fields }
func (*Result) CurrentRow ¶
CurrentRow() returns current row (set by Next()). Returns -1 as an error if Next() wasn't called.
func (*Result) FindColumn ¶
FindColumn returns an index of a column, found by name. Returns error if the column isn't found.
func (*Result) MustScan ¶
Must Scan exactly cnt number of values from result. Useful when scanning into structure, to know whether are all expected fields filled with values. cnt - number of values assigned to fields
func (*Result) Next ¶
Advances to the next row. Returns false if there is no more rows (i.e. we are on the last row).
func (*Result) Scan ¶
Scan copies the columns in the current row into the values pointed at by dest.
func (*Result) ScanColumn ¶
Find column with given name and scan it's value to the result. Returns error if the column isn't found, otherwise returns error if the scan fails.
type SpOutputParam ¶
type SpOutputParam struct { Name string Value interface{} }
Stored procedure output parameter name and value.
type SpResult ¶
type SpResult struct {
// contains filtered or unexported fields
}
Stored procedure execution result.
func NewSpResult ¶
func NewSpResult() *SpResult
func (*SpResult) HasOutputParams ¶
Does the stored procedure has any output params.
func (*SpResult) HasResults ¶
Does the stored procedure returned any resultsets.
func (*SpResult) Next ¶
Call Next on current result. True if next row in current result exists, otherwise false.
func (*SpResult) NextResult ¶
Navigate to next result. True if sucessfull, false if no more resutls.