initiate repo
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"antrian-operasi/internal/config"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *service) openSQLServerConnection(config config.DatabaseConfig) (*sql.DB, error) {
|
||||
// Build connection string with security parameters
|
||||
// Convert timeout to seconds for SQL Server
|
||||
connectTimeoutSec := int(config.ConnectTimeout.Seconds())
|
||||
|
||||
connStr := fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s&connection timeout=%d",
|
||||
config.Username,
|
||||
config.Password,
|
||||
config.Host,
|
||||
config.Port,
|
||||
config.Database,
|
||||
connectTimeoutSec,
|
||||
)
|
||||
|
||||
// Add SSL configuration if required
|
||||
if config.RequireSSL {
|
||||
connStr += "&encrypt=true"
|
||||
if config.SSLRootCert != "" {
|
||||
connStr += "&trustServerCertificate=false"
|
||||
} else {
|
||||
connStr += "&trustServerCertificate=true"
|
||||
}
|
||||
}
|
||||
|
||||
// Open connection
|
||||
db, err := sql.Open("sqlserver", connStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open SQL Server connection: %w", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
Reference in New Issue
Block a user