## gofreetds
Go [FreeTDS](http://www.freetds.org/) wrapper. Native Sql Server database driver.
Features:
* can be used as [database/sql](http://golang.org/pkg/database/sql/) driver
* handles calling [stored procedures](#stored-procedures)
* handles multiple resultsets
* supports database mirroring
* connection pooling
* scaning resultsets into structs
## Get started
### Install dependencines
[FreeTDS](http://www.freetds.org/) libraries must be installed on the system.
Mac
```shell
brew install freetds
```
Ubuntu, Debian...
```shell
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.
```go
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:
```go
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:
```go
rst, err := conn.ExecSp("sp_help", "authors")
```
Read sp return value, and output params:
```go
returnValue := rst.Status()
var param1, param2 int
rst.ParamScan(¶m1, ¶m2)
```
Read sp resultset (fill the struct):
```go
author := &Author{}
rst.Scan(author)
```
Read next resultset:
```go
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:
```go
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:
```go
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)](http://web.archive.org/web/20160325095720/http://2tbsp.com/articles/2012/06/08/connect-ms-sql-server-and-sybase-ase-mac-os-x-and-linux-unixodbc-and-freetds)
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](http://www.microsoft.com/en-us/download/details.aspx?id=23654).
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.
```shell
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.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
gofreetds转到FreeTDS包装器。 本机Sql Server数据库驱动程序。 功能:可用作数据库/ sql驱动程序句柄,调用存储过程,处理多个结果集,支持数据库镜像连接池,i gofreetds Go FreeTDS包装器。 本机Sql Server数据库驱动程序。 特点:可用作数据库/ sql驱动程序句柄,调用存储过程,处理多个结果集,支持数据库镜像连接池,将结果集扫描到结构中。开始安装dependencines必须在系统上安装FreeTDS库。 Mac brew安装freetds Ubuntu,Debian ... sudo apt-get安装freetds-dev去get去获取github.com/minus5/gofreetds文档http://godoc.org/githu
资源详情
资源评论
资源推荐
收起资源包目录
gofreetds-master.zip (38个子文件)
gofreetds-master
.gitignore 481B
convert_sql_buf_test.go 3KB
conn_test.go 19KB
camelize.go 1KB
CODEOWNERS 29B
params_cache.go 483B
mssql_conn.go 2KB
convert.go 6KB
conn_pool_test.go 4KB
result_column.go 100B
sp_result.go 2KB
convert_sql_buf.go 9KB
credentials_test.go 5KB
tds_types.txt 1KB
conn.go 12KB
credentials.go 1KB
conn_sp.go 6KB
fetch.go 6KB
code_generator.rb 386B
conn_sp_test.go 16KB
README.md 4KB
conn_pool.go 5KB
mssql_test.go 6KB
example
sp_help
main.go 1KB
exec
main.go 1KB
scan_struct
main.go 921B
stored_procedure
main.go 3KB
mssql
main.go 934B
MIT-LICENSE.txt 1KB
convert_test.go 8KB
result.go 4KB
executesql.go 4KB
executesql_test.go 3KB
mssql_stmt.go 3KB
callbacks.go 2KB
sp_result_test.go 1KB
camelize_test.go 643B
result_test.go 4KB
共 38 条
- 1
zhuyurrr
- 粉丝: 29
- 资源: 4714
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0