| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build windows || !cgo |
| 4 | |
| 5 | package dbdriver |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | func init() { |
| 13 | // ODBC driver not enabled in this build (use -tags odbc to enable) |
| 14 | Register("odbc", &Driver{ |
| 15 | Name: "odbc", |
| 16 | Description: "ODBC driver (not enabled in this build - rebuild with -tags odbc)", |
| 17 | Available: false, |
| 18 | RequiresCGO: true, |
| 19 | RequiresLibs: []string{"unixODBC", "IBM i Access ODBC Driver"}, |
| 20 | DSNFormat: "Driver={IBM i Access ODBC Driver};System=host;Uid=user;Pwd=pass", |
| 21 | }) |
| 22 | } |
| 23 | |
| 24 | // BuildODBCDSN creates an ODBC connection string |
| 25 | func BuildODBCDSN(config *ConnectionConfig) string { |
| 26 | // Determine the ODBC driver name |
| 27 | driverName := config.ODBCDriver |
| 28 | if driverName == "" { |
| 29 | if config.SystemType == "AS400" { |
| 30 | // Common AS/400 ODBC driver names |
| 31 | driverName = "IBM i Access ODBC Driver" |
| 32 | } else { |
| 33 | // Common DB2 ODBC driver names |
| 34 | driverName = "IBM DB2 ODBC DRIVER" |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Handle AS/400 specific format |
| 39 | if config.SystemType == "AS400" || |
| 40 | strings.Contains(driverName, "AS400") || |
| 41 | strings.Contains(driverName, "IBM i") { |
| 42 | // AS/400 style ODBC connection |
| 43 | dsn := fmt.Sprintf("Driver={%s};System=%s;Uid=%s;Pwd=%s;", |
| 44 | driverName, config.Hostname, config.Username, config.Password) |
| 45 | |
| 46 | // AS/400 specific options |
| 47 | if config.Database != "" && config.Database != "*SYSBAS" { |
| 48 | dsn += fmt.Sprintf("DefaultLibraries=%s;", config.Database) |
| 49 | } |
| 50 | |
| 51 | if config.Port != 0 && config.Port != 8471 { |
| 52 | dsn += fmt.Sprintf("Port=%d;", config.Port) |
| 53 | } |
| 54 | |
| 55 | if config.UseSSL { |
| 56 | dsn += "SSL=1;" |
| 57 | } |
| 58 | |
| 59 | return dsn |
| 60 | } |
| 61 | |
| 62 | // Standard DB2 ODBC format |
| 63 | dsn := fmt.Sprintf("Driver={%s};", driverName) |
| 64 | |
| 65 | if config.Database != "" { |
| 66 | dsn += fmt.Sprintf("Database=%s;", config.Database) |
| 67 | } |
| 68 | |
| 69 | dsn += fmt.Sprintf("Hostname=%s;Port=%d;Protocol=TCPIP;Uid=%s;Pwd=%s;", |
| 70 | config.Hostname, config.Port, config.Username, config.Password) |
| 71 | |
| 72 | if config.UseSSL { |
| 73 | dsn += "Security=SSL;" |
| 74 | if config.SSLServerCertPath != "" { |
| 75 | dsn += fmt.Sprintf("SSLServerCertificate=%s;", config.SSLServerCertPath) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return dsn |
| 80 | } |