目前已启动并运行FreeTDS 0.92.4 / unixODBC 2.3.1,与 MSSQL 服务器连接并能够执行查询等。
我找到了 Go 的 ODBC 实现列表,并以粗体尝试了这两个:
- BenoyRNair https://github.com/BenoyRNair/godbc/
- 魏广靖https://github.com/weigj/go-odbc
- 马克·塞弗森 https://bitbucket.org/miquella/mgodbc
- 卢克莫尔丁https://github.com/LukeMauldin/lodbc
- 罗伯特·约翰斯通https://bitbucket.org/rj/odbc3-go/
- Brainman http://code.google.com/p/odbc/
mgodbc
我收到一堆弃用警告(查看它们在 OSX 10.8 及更高版本中弃用的标头):
cc1: warnings being treated as errors
mgodbc.go: In function 'mSQLColAttribute':
mgodbc.go:31: warning: 'SQLColAttributeW' is deprecated (declared at /usr/include/sqlucode.h:128)
mgodbc.go: At top level:
mgodbc.go:44: warning: 'SQLDisconnect' is deprecated (declared at /usr/include/sql.h:896)
mgodbc.go:51: warning: 'SQLGetDiagRecW' is deprecated (declared at /usr/include/sqlucode.h:233)
mgodbc.go:62: warning: 'SQLGetInfoW' is deprecated (declared at /usr/include/sqlucode.h:273)
mgodbc.go:67: warning: 'SQLBindParameter' is deprecated (declared at /usr/include/sqlext.h:2519)
mgodbc.go:70: warning: 'SQLDriverConnectW' is deprecated (declared at /usr/include/sqlucode.h:336)
mgodbc.go:73: warning: 'SQLSetEnvAttr' is deprecated (declared at /usr/include/sql.h:1120)
mgodbc.go:74: warning: 'SQLFreeHandle' is deprecated (declared at /usr/include/sql.h:942)
mgodbc.go:75: warning: 'SQLSetConnectAttrW' is deprecated (declared at /usr/include/sqlucode.h:245)
mgodbc.go:78: warning: 'SQLGetDiagFieldW' is deprecated (declared at /usr/include/sqlucode.h:223)
mgodbc.go:82: warning: 'SQLRowCount' is deprecated (declared at /usr/include/sql.h:1076)
mgodbc.go:98: warning: 'SQLGetData' is deprecated (declared at /usr/include/sql.h:975)
mgodbc.go:99: warning: 'SQLEndTran' is deprecated (declared at /usr/include/sql.h:902)
mgodbc.go:102: warning: 'SQLCloseCursor' is deprecated (declared at /usr/include/sql.h:831)
mgodbc.go:103: warning: 'SQLPrepareW' is deprecated (declared at /usr/include/sqlucode.h:239)
mgodbc.go:107: warning: 'SQLNumResultCols' is deprecated (declared at /usr/include/sql.h:1058)
mgodbc.go:113: warning: 'SQLAllocHandle' is deprecated (declared at /usr/include/sql.h:799)
mgodbc.go:114: warning: 'SQLExecute' is deprecated (declared at /usr/include/sql.h:921)
mgodbc.go:115: warning: 'SQLFetch' is deprecated (declared at /usr/include/sql.h:924)
mgodbc.go:119: warning: 'SQLNumParams' is deprecated (declared at /usr/include/sqlext.h:2448)
更新
根据来自 go-nuts irc 的 mac01021 的建议,我添加了
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
这消除了弃用警告,但仍然没有针对 OS X 包含的 iODBC 构建
将上面的行替换为:
#cgo darwin CFLAGS: -I/opt/local/include
瞧,现在可以构建 mgodbc (使用已安装的 unixODBC),
尽管现在导入时会出现一个不错的小段错误 =(
数据库
我收到构建错误:
# code.google.com/p/odbc/api
api/api.go:13: undefined: SQLSMALLINT
api/api.go:14: undefined: SQLUSMALLINT
api/api.go:15: undefined: SQLUSMALLINT
api/api.go:19: undefined: SQLSMALLINT
api/api.go:20: undefined: SQLUSMALLINT
api/api.go:21: undefined: SQLUSMALLINT
api/api.go:22: undefined: SQLUSMALLINT
api/api.go:23: undefined: SQLUSMALLINT
api/api.go:24: undefined: SQLUSMALLINT
api/api.go:25: undefined: SQLUINTEGER
api/api.go:25: too many errors
更新
感谢@alex 提供的cgo
信息。我已经修改api_unix.go
了以下内容
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux darwin
// +build cgo
package api
// #cgo linux LDFLAGS: -lodbc
// #cgo darwin LDFLAGS: -lodbc
// #cgo darwin CFLAGS: -I /opt/local/include
// #include <sql.h>
// #include <sqlext.h>
import "C"
OS X 附带的 iODBC 有一些被列为已弃用的东西(过去我对 unixODBC 的运气更好)
我已将-I /opt/local/include
CFLAGS 添加到,希望指向 unixODBC 标头,而不是 Apple 包含的标头(有弃用警告等)
跑步# go build -x
给了我:
WORK=/var/folders/z2/k9vxn7gn6395vb3y2qc7_1040000gn/T/go-build784364461
mkdir -p $WORK/code.google.com/p/odbc/api/_obj/
mkdir -p $WORK/code.google.com/p/odbc/
cd /Users/jr/Development/go/src/code.google.com/p/odbc/api
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/code.google.com/p/odbc/api/_obj/ -- -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ api_unix.go
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/code.google.com/p/odbc/api/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.o -c $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o -c $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -c $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -lodbc
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/code.google.com/p/odbc/api/_obj/ -dynimport $WORK/code.google.com/p/odbc/api/_obj/_cgo_.o -dynout $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.c
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/code.google.com/p/odbc/api/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/code.google.com/p/odbc/api/_obj/_all.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -Wl,-r -nostdlib /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/x86_64/libgcc.a
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/code.google.com/p/odbc/api/_obj/_go_.6 -p code.google.com/p/odbc/api -D _/Users/jr/Development/go/src/code.google.com/p/odbc/api -I $WORK ./api.go $WORK/code.google.com/p/odbc/api/_obj/_cgo_gotypes.go $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo1.go
/usr/local/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/code.google.com/p/odbc/api.a $WORK/code.google.com/p/odbc/api/_obj/_go_.6 $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.6 $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.6 $WORK/code.google.com/p/odbc/api/_obj/_all.o
mkdir -p $WORK/code.google.com/p/odbc/_obj/
mkdir -p $WORK/code.google.com/p/
cd /Users/jr/Development/go/src/code.google.com/p/odbc
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/code.google.com/p/odbc/_obj/_go_.6 -p code.google.com/p/odbc -complete -D _/Users/jr/Development/go/src/code.google.com/p/odbc -I $WORK -I /Users/jr/Development/go/pkg/darwin_amd64 ./column.go ./conn.go ./driver.go ./error.go ./handle.go ./odbcstmt.go ./param.go ./result.go ./rows.go ./stats.go ./stmt.go ./tx.go
# code.google.com/p/odbc
./column.go:22: undefined: api.SQLGetData
./column.go:28: undefined: api.SQLBindCol
./column.go:47: undefined: api.SQLDescribeCol
./conn.go:20: undefined: api.SQLAllocHandle
./conn.go:28: undefined: api.SQLDriverConnect
./conn.go:39: undefined: api.SQLDisconnect
./driver.go:26: undefined: api.SQLAllocHandle
./driver.go:34: undefined: api.SQLSetEnvAttr
./driver.go:43: undefined: api.SQLSetEnvAttr
./driver.go:50: undefined: api.SQLSetEnvAttr
./driver.go:50: too many errors
看起来标题路径包含正在正确传递?
但看起来事情仍然没有正确链接?
因为SQLGetData
我确实从内部的//sys SQLGetData...
评论中看到了匹配的定义api.go
/opt/local/include/sql.h
更新
顶部提到的库:
Brainman http://code.google.com/p/odbc/
现在可以在 OSX 上作为 go-gettable 包使用。甚至还有帮助您开始使用 odbc/tds 部分的文档。