| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package dbdriver |
| 4 | |
| 5 | import "time" |
| 6 | |
| 7 | // ConnectionConfig holds common connection parameters for database connections |
| 8 | type ConnectionConfig struct { |
| 9 | // Connection type selection |
| 10 | ConnectionType string // "auto", "db2", "odbc" |
| 11 | PreferODBC bool // Prefer ODBC when auto-detecting |
| 12 | SystemType string // "AS400", "DB2", etc. |
| 13 | |
| 14 | // Universal DSN (if provided directly) |
| 15 | DSN string |
| 16 | |
| 17 | // Component-based connection |
| 18 | Database string |
| 19 | Hostname string |
| 20 | Port int |
| 21 | Username string |
| 22 | Password string |
| 23 | |
| 24 | // SSL/TLS |
| 25 | UseSSL bool |
| 26 | SSLCertPath string |
| 27 | SSLServerCertPath string |
| 28 | |
| 29 | // ODBC specific |
| 30 | ODBCDriver string // Driver name for ODBC (e.g., "IBM i Access ODBC Driver") |
| 31 | |
| 32 | // Connection pool settings |
| 33 | MaxOpenConns int |
| 34 | MaxIdleConns int |
| 35 | ConnMaxLifetime time.Duration |
| 36 | |
| 37 | // Context for operations |
| 38 | Timeout time.Duration |
| 39 | } |
| 40 | |
| 41 | // SetDefaults sets default values for connection configuration |
| 42 | func (c *ConnectionConfig) SetDefaults() { |
| 43 | if c.Timeout == 0 { |
| 44 | c.Timeout = 30 * time.Second |
| 45 | } |
| 46 | |
| 47 | if c.MaxOpenConns == 0 { |
| 48 | c.MaxOpenConns = 1 |
| 49 | } |
| 50 | |
| 51 | if c.ConnMaxLifetime == 0 { |
| 52 | c.ConnMaxLifetime = 10 * time.Minute |
| 53 | } |
| 54 | |
| 55 | // Set default ports based on system type |
| 56 | if c.Port == 0 { |
| 57 | switch c.SystemType { |
| 58 | case "AS400": |
| 59 | c.Port = 8471 // Default AS/400 DRDA port |
| 60 | default: |
| 61 | c.Port = 50000 // Default DB2 port |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Set default database based on system type |
| 66 | if c.Database == "" && c.SystemType == "AS400" { |
| 67 | c.Database = "*SYSBAS" |
| 68 | } |
| 69 | } |