Golang 如何操作DB2的?

yyt030發表於2019-02-16

go-db2-example

ibm db2是款商用的db,預設支援的程式語言中沒有Go,也沒有介紹過,今天就演示下Go通過odbc方式連線ibm db2的例子。

DB2 ODBC driver

安裝DB2 ODBC driver

DB2 ODBC driver的來源有一下幾種:

  • db2安裝包自帶odbc驅動,和常用的jdbc驅動一樣,odbc驅動一般都是在安裝包中自帶。
  • 此外,一些單獨的db2套件也含有odbc驅動,如:DB2 Application Development Client, Db2 Run-time Client, Db2 Data Server Runtime client等等
  • 單獨的免安裝odbc驅動包:db2 driver for ODBC Cli

其中,db2 cli(db2 Call Level Interface)是一個和db2互動的SQL介面,基於ODBC規範和SQL / CLI國際標準。

下載macos64_odbc_cli.tar.gz,解壓到任意目錄下;

建立Data Source配置

db2 有兩種data source配置檔案,具體路徑在<install-odbc-dir>/cfg下面

  • db2cli.ini # text格式,該檔案可用於所有的odbc驅動
  • db2dsdriver.cfg # xml格式, 9.5 版本後引入的,可以公DB2 Data Server Driver for ODBC and CLI使用

db2cli.ini範例

; db2cli.ini data source
[DB2_SAMPLE]
Database=SAMPLE
Protocol=TCPIP
Port=50000
Hostname=my-db2-machine
UID=my-OS-user
PWD=my-OS-password

db2dsdriver.cfg範例

<parameter name="name" value="value"/>
<!--  db2dsdriver.cfg data source -->
<configuration>
   <dsncollection>
      <dsn alias="DB2_SAMPLE" name="SAMPLE" host="my-db2-machine" port="50000">
         <parameter name="UserID" value="my-db2-user"/>
         <parameter name="Password" value="my-db2-password"/>
      </dsn>
   </dsncollection>
</configuration>

比較簡單,不做過多介紹,cfg下也有example。注意,配置檔案中存在相同的dsn時,優先載入db2cli.ini檔案;

配置完成後,驗證下檔案是否正確

$ cd /home/myuser/db2/odbc_cli/clidriver/bin/
$ ./db2cli validate -dsn DB2_SAMPLE
 db2cli validate -dsn sample

===============================================================================
Client information for the current copy:
===============================================================================

Client Package Type       : IBM Data Server Driver For ODBC and CLI
Client Version (level/bit): DB2 v10.5.0.5 (special_35187/64-bit)
Client Platform           : Darwin
Install/Instance Path     : .../clidriver
DB2DSDRIVER_CFG_PATH value: <not-set>
db2dsdriver.cfg Path      : .../clidriver/cfg/db2dsdriver.cfg
DB2CLIINIPATH value       : <not-set>
db2cli.ini Path           : .../clidriver/cfg/db2cli.ini
db2diag.log Path          : .../clidriver/db2dump/db2diag.log

===============================================================================
db2dsdriver.cfg schema validation for the entire file:
===============================================================================

Note: The validation utility could not find the configuration file
db2dsdriver.cfg. The file is searched at
".../clidriver/cfg/db2dsdriver.cfg".

===============================================================================
db2cli.ini validation for data source name "sample":
===============================================================================

[ Keywords used for the connection ]

Keyword                   Value
---------------------------------------------------------------------------
DATABASE                  sample
PROTOCOL                  TCPIP
HOSTNAME                  127.0.0.1
SERVICENAME               50000
UID                       db2inst1
PWD                       ********

Test下連線

$ echo "select count(1) from syscat.tables" |db2cli execsql -dsn sample [ -user *** -passwd *** ]
IBM DATABASE 2 Interactive CLI Sample Program
(C) COPYRIGHT International Business Machines Corp. 1993,1996
All Rights Reserved
Licensed Materials - Property of IBM
US Government Users Restricted Rights - Use, duplication or
disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
> select count(1) from syscat.tables
FetchAll:  Columns: 1
  1
  643
FetchAll: 1 rows fetched.

其中,使用者名稱和密碼可省略,配置ini檔案中;

unixODBC

首先要安裝unixODBC驅動管理器,該odbc管理器是開源專案,能夠幫助非win平臺下的使用者輕鬆使用odbc訪問目標資料庫.

與其它連線方式不同的是,ODBC應用程式app通常載入連結到ODBC驅動程式管理器而不是特定的ODBC驅動程式。
ODBC驅動程式管理器是ODBC應用程式和ODBC驅動程式之間的介面和橋樑。

在執行時,應用程式提供了一個連線字串,即DSN,該連線字串定義了要連線的ODBC資料來源,並依次定義將處理連線的ODBC驅動程式。
unixODBC載入所請求的ODBC驅動程式並將所有ODBC API呼叫傳遞給目標驅動程式,也就是db2 odbc driver;
流程如下:
app —> unixODBC —> db2 ODBC driver

對於DB2 ODBC驅動程式來說,ODBC應用程式需要提供一個與DB2 ODBC驅動程式資料來源同名的ODBC資料來源。
unixODBC載入相應的資料來源驅動程式(DB2 ODBC driver),並將資料來源配置資訊傳遞給載入的DB2 ODBC driver,DB2 ODBC驅動程式檢查它的資料來源的配置檔案,判斷它的名稱與它傳遞的名稱相同;

安裝unixODBC

brew install unixODBC

其它平臺常見unixODBC官方網站即可;

註冊DB2 ODBC driver和資料來源

檢視odbc配置檔案

$ odbcinst -j
unixODBC 2.3.6
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: .../.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

其中,odbcinst.ini配置範例如下:

# Example odbcinst.ini driver definition for DB2 Data Server Driver for ODBC and
# CLI
[DB2]
Description=DB2 ODBC Driver
Driver=/usr/lib/libdb2.so # Replace /usr/lib with the directory where your
                          # driver shared object is located.
Fileusage=1               #
Dontdlclose=1             # IBM recommend setting Dontdlclose to 1, which stops
                          # unixODBC unloading the ODBC Driver on disconnect.
                          # Note that in unixODBC 2.2.11 and later, the Driver
                          # Manager default for Dontdlclose is 1.

上面已經提到過,為了讓應用程式通過odbc方式連線db,unixODBC管理器需要知道要載入的驅動在哪裡,同時需要指定odbc驅動的連線引數,如:ip,user等;
要應用這些引數,就要配置相關的DSN 引數連線;

~/.odbc.ini配置如下:

[DB2_SAMPLE]
Driver=DB2

故此,當使用者程式需要連線DB2_SAMPLE時,

- 首先,unixODBC會載入DB2 ODBC driver驅動,
- 之後,DB2 ODBC driver會在db2cli.ini/db2dsdriver.cfg找相同名字的dns的相關配置;
- 若使用者程式提供的使用者名稱和密碼,配置檔案中的使用者名稱和密碼會忽略。

如下isql是unixODBC自帶的ODBC程式,通過它可以驗證dsn的配置是否正確,連線是否ok;

$ isql -v DB2_SAMPLE username password
錯誤1:說明~/.odbc.ini配置檔案中的Data Source Name(DSN)沒有找到;
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect

$ isql -v DB2_SAMPLE username password
錯誤2:說明DB2 ODBC driver配置檔案沒有找到匹配的DSN名字的配置資訊
[     ][unixODBC][IBM][CLI Driver] SQL1531N  The connection failed because the name specified
with the DSN connection string keyword could not be found in either the db2dsdriver.cfg configuration file or the db2cli.ini configuration file.
Data source name specified in the connection string: "DB2_SAMPLE".

$ isql -v DB2_SAMPLE
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

使用go連線db2

見原始碼

PS:對於win下的go,抽空再嘗試。

相關文章