PostgresClientKit
PostgresClientKit 为操作 PostgreSQL 数据库提供了一组友好的 Swift API。
特性
-
无需 libpq。 PostgresClientKit 在 Swift 中实现了 PostgreSQL 网络协议,因此不需要
libpq
。 -
使用现代 Swift 开发者友好的 API。 例如,错误通过
enum PostgresError: Error
的实例表示,并且通过throw
或返回Result
引发。 -
Postgres 与 Swift 类型之间安全的转换。 类型转换是显式和健壮的。转换错误被发出,而不是被抑制。PostgresClientKit 为日期和时间提供了额外的 Swift 类型,以解决 PostgreSQL 类型与 Foundation
Date
类型之间的阻抗不匹配问题。 -
内存高效。 结果集中的行通过迭代器暴露,而不是数组。行是懒地从 PostgreSQL 服务器检索的。
-
SSL/TLS 支持。 加密 PostgresClientKit 与 PostgreSQL 服务器之间的连接。
-
精心设计。 完整的 API 文档、详尽的测试套件,积极支持。
听起来不错?让我们看看一个例子。
例子
这是一个连接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)
先决条件
- Swift 5或更高版本(PostgresClientKit使用Swift 5语言功能)
libssl-dev
(仅在Linux上需要)
PostgresClientKit与Linux、macOS和iOS兼容。已在以下系统上进行了测试:
- Ubuntu 18.04 LTS, 20.04 LTS
- macOS 10.14, 10.15, 11
- iOS 12, 13, 14, 15
- Postgres 10, 11, 12, 13, 14
构建
cd <path-to-clone>
swift package clean
swift build
测试
为测试设置Postgres数据库。这是一个一次性过程。
然后
cd <path-to-clone>
swift package clean
swift build
swift test
使用
Swift包管理器
在您的Package.swift
文件中
- 将PostgresClientKit添加到
依赖项
中。例如:
dependencies: [
.package(url: "https://github.com/codewinsdotcom/PostgresClientKit", from: "1.0.0"),
],
- 在
targets
中引用PostgresClientKit
产品。例如:
targets: [
.target(
name: "MyProject",
dependencies: ["PostgresClientKit"]),
]
导入到源代码文件中
import PostgresClientKit
CocoaPods
将 PostgresClientKit
添加到您的 Podfile
文件中。例如
target 'MyApp' do
pod 'PostgresClientKit', '~> 1.0'
end
然后运行 pod install
。
导入到源代码文件中
import PostgresClientKit
文档
附加示例
-
PostgresClientKit-CommandLine-Example:一个命令行应用程序示例
-
PostgresClientKit-iOS-Example:一个iOS应用程序示例
贡献
感谢您对为PostgresClientKit做出贡献的兴趣。
本项目有一项行为准则。有关详细信息,请参阅CODE_OF_CONDUCT.md。
请使用问题来
- 提问
- 报告问题(错误)
- 请求增强
欢迎对 develop
分支的Pull请求。对于非平凡的贡献(例如,纠正拼写、错别字或空格以外的内容),请首先通过创建问题来讨论建议的更改。
许可证
PostgresClientKit在Apache 2.0许可证下授权。有关详细信息,请参阅LICENSE。
版本控制
PostgresClientKit 使用 语义版本控制 2.0.0。有关可用版本,请参阅本存储库的 标签。
构建工具
- Kitura BlueSocket - 套接字库
- Kitura BlueSSLService - SSL/TLS 支持
- Jazzy - API 文档页面生成
作者
- David Pitfield (@pitfield)