PostgresClientKit

PostgresClientKit 提供了一个友好的 Swift API,用于操作 PostgreSQL 数据库。

特性

听起来不错?让我们看一个例子。

示例

这是一个基本但完整的示例,展示了如何连接到 Postgres,执行 SQL SELECT 命令,并处理结果行。它使用了 Postgres 教程 中的 weather 表。

import PostgresClientKit

do {
    var configuration = PostgresClientKit.ConnectionConfiguration()
    configuration.host = "127.0.0.1"
    configuration.database = "example"
    configuration.user = "bob"
    configuration.credential = .scramSHA256(password: "welcome1")

    let connection = try PostgresClientKit.Connection(configuration: configuration)
    defer { connection.close() }

    let text = "SELECT city, temp_lo, temp_hi, prcp, date FROM weather WHERE city = $1;"
    let statement = try connection.prepareStatement(text: text)
    defer { statement.close() }

    let cursor = try statement.execute(parameterValues: [ "San Francisco" ])
    defer { cursor.close() }

    for row in cursor {
        let columns = try row.get().columns
        let city = try columns[0].string()
        let tempLo = try columns[1].int()
        let tempHi = try columns[2].int()
        let prcp = try columns[3].optionalDouble()
        let date = try columns[4].date()
    
        print("""
            \(city) on \(date): low: \(tempLo), high: \(tempHi), \
            precipitation: \(String(describing: prcp))
            """)
    }
} catch {
    print(error) // better error handling goes here
}

输出

San Francisco on 1994-11-27: low: 46, high: 50, precipitation: Optional(0.25)
San Francisco on 1994-11-29: low: 43, high: 57, precipitation: Optional(0.0)

先决条件

PostgresClientKit 兼容 Linux、macOS 和 iOS。 它已经在以下平台上进行了测试:

构建

cd <path-to-clone>
swift package clean
swift build

测试

设置用于测试的 Postgres 数据库。这是一个一次性过程。

然后

cd <path-to-clone>
swift package clean
swift build
swift test

使用

从 Xcode 项目(作为包依赖项)

在 Xcode 中

导入到您的源代码文件

import PostgresClientKit

从独立的 Swift 包 (Package.swift)

在您的 Package.swift 文件中

dependencies: [
    .package(url: "https://github.com/codewinsdotcom/PostgresClientKit", from: "1.0.0"),
],
targets: [
    .target(
        name: "MyProject",
        dependencies: ["PostgresClientKit"]),
]

导入到您的源代码文件

import PostgresClientKit

使用 CocoaPods

PostgresClientKit 添加到您的 Podfile。例如

target 'MyApp' do
  pod 'PostgresClientKit', '~> 1.0'
end

然后运行 pod install

导入到您的源代码文件

import PostgresClientKit

文档

更多示例

贡献

感谢您对 PostgresClientKit 做出贡献的兴趣。

该项目有一个行为准则。请参阅 CODE_OF_CONDUCT.md 获取详细信息。

请使用 issues

欢迎对 develop 分支进行 Pull Request。 对于非微不足道的贡献(例如,不仅仅是更正拼写、错别字或空格),请首先通过提出 issue 来讨论提议的更改。

许可证

PostgresClientKit 在 Apache 2.0 许可下获得许可。 有关详细信息,请参阅 LICENSE

版本控制

PostgresClientKit 使用 语义化版本 2.0.0。 有关可用版本,请参阅 此存储库上的标签

构建于

作者