Postgres.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright IBM Corporation 2018
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import SwiftKuery
  18. import SwiftKueryPostgreSQL
  19. import Configuration
  20. #if os(Linux)
  21. import Glibc
  22. #else
  23. import Darwin
  24. #endif
  25. // We will load our database configuration from config.json, but this can be
  26. // overridden with the TFB_DB_CONFIG environment variable.
  27. let configurationFilename: String = ProcessInfo.processInfo.environment["TFB_DB_CONFIG"] ?? "config.json"
  28. let manager = ConfigurationManager().load(file: configurationFilename, relativeFrom: .pwd).load(.environmentVariables)
  29. let dbHost = manager["DB_HOST"] as? String ?? manager["db:host"] as? String ?? "localhost"
  30. let dbPort = Int32(manager["DB_PORT"] as? String != nil ? Int(manager["DB_PORT"] as! String) ?? 5432 : manager["db:port"] as? Int ?? 5432)
  31. let dbName = manager["db:name"] as? String ?? "hello_world"
  32. let dbUser = manager["db:user"] as? String ?? "benchmarkdbuser"
  33. let dbPass = manager["db:password"] as? String ?? "benchmarkdbpass"
  34. let dbConnPoolOpts = ConnectionPoolOptions(initialCapacity: 20, maxCapacity: 50, timeout:10000)
  35. public let dbConnPool = PostgreSQLConnection.createPool(host: dbHost, port: dbPort, options: [.databaseName(dbName), .userName(dbUser), .password(dbPass)], poolOptions: dbConnPoolOpts)