| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build disable_ibm_direct_driver |
| 4 | |
| 5 | package dbdriver |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | |
| 10 | _ "github.com/ibmdb/go_ibm_db" |
| 11 | ) |
| 12 | |
| 13 | func init() { |
| 14 | // Try to register DB2 driver |
| 15 | // The import will panic if libraries are missing, so we catch it |
| 16 | defer func() { |
| 17 | if r := recover(); r != nil { |
| 18 | // DB2 client libraries not found |
| 19 | Register("go_ibm_db", &Driver{ |
| 20 | Name: "go_ibm_db", |
| 21 | Description: "IBM DB2 client driver (requires IBM DB2 client libraries)", |
| 22 | Available: false, |
| 23 | RequiresCGO: true, |
| 24 | RequiresLibs: []string{"libdb2.so", "libdb2.dll", "libdb2.dylib"}, |
| 25 | DSNFormat: "DATABASE=db;HOSTNAME=host;PORT=port;PROTOCOL=TCPIP;UID=user;PWD=pass", |
| 26 | }) |
| 27 | } |
| 28 | }() |
| 29 | |
| 30 | // If we get here, driver loaded successfully |
| 31 | Register("go_ibm_db", &Driver{ |
| 32 | Name: "go_ibm_db", |
| 33 | Description: "IBM DB2 client driver", |
| 34 | Available: true, |
| 35 | RequiresCGO: true, |
| 36 | RequiresLibs: []string{"libdb2.so", "libdb2.dll", "libdb2.dylib"}, |
| 37 | DSNFormat: "DATABASE=db;HOSTNAME=host;PORT=port;PROTOCOL=TCPIP;UID=user;PWD=pass", |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | // BuildDB2DSN creates a DB2 connection string |
| 42 | func BuildDB2DSN(config *ConnectionConfig) string { |
| 43 | // Handle AS/400 specific format if system type is AS400 |
| 44 | if config.SystemType == "AS400" { |
| 45 | // AS/400 uses different database naming |
| 46 | database := config.Database |
| 47 | if database == "" { |
| 48 | database = "*SYSBAS" // Default AS/400 database |
| 49 | } |
| 50 | |
| 51 | dsn := fmt.Sprintf("DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s", |
| 52 | database, config.Hostname, config.Port, config.Username, config.Password) |
| 53 | |
| 54 | if config.UseSSL { |
| 55 | dsn += ";SECURITY=SSL" |
| 56 | if config.SSLServerCertPath != "" { |
| 57 | dsn += fmt.Sprintf(";SSLServerCertificate=%s", config.SSLServerCertPath) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return dsn |
| 62 | } |
| 63 | |
| 64 | // Standard DB2 format |
| 65 | dsn := fmt.Sprintf("DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s", |
| 66 | config.Database, config.Hostname, config.Port, config.Username, config.Password) |
| 67 | |
| 68 | if config.UseSSL { |
| 69 | dsn += ";SECURITY=SSL" |
| 70 | if config.SSLServerCertPath != "" { |
| 71 | dsn += fmt.Sprintf(";SSLServerCertificate=%s", config.SSLServerCertPath) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return dsn |
| 76 | } |