Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A PostgreSQL client library for Swift. Does not require libpq.

License

NotificationsYou must be signed in to change notification settings

codewinsdotcom/PostgresClientKit

Repository files navigation

PostgresClientKit provides a friendly Swift API for operating against a PostgreSQL database.

Features

  • Doesn't require libpq. PostgresClientKit implements the Postgres network protocol in Swift, so it does not requirelibpq.

  • Developer-friendly API using modern Swift. For example, errors are represented by instances ofenum PostgresError: Error and are raised by athrow or by returning aResult<Success, Error>.

  • Safe conversion between Postgres and Swift types. Type conversion is explicit and robust. Conversion errors are signaled, not masked. PostgresClientKit provides additional Swift types for dates and times to address the impedance mismatch between Postgres types and FoundationDate.

  • Memory efficient. The rows in a result are exposed through an iterator, not an array. Rows are lazily retrieved from the Postgres server.

  • SSL/TLS support. Encrypts the connection between PostgresClientKit and the Postgres server.

  • Well-engineered. Complete API documentation, an extensive test suite, actively supported.

Sounds good? Let's look at an example.

Example

This is a basic, but complete, example of how to connect to Postgres, perform a SQLSELECT command, and process the resulting rows. It uses theweather table in thePostgres tutorial.

import PostgresClientKitdo{varconfiguration=PostgresClientKit.ConnectionConfiguration()    configuration.host="127.0.0.1"    configuration.database="example"    configuration.user="bob"    configuration.credential=.scramSHA256(password:"welcome1")letconnection=tryPostgresClientKit.Connection(configuration: configuration)defer{ connection.close()}lettext="SELECT city, temp_lo, temp_hi, prcp, date FROM weather WHERE city = $1;"letstatement=try connection.prepareStatement(text: text)defer{ statement.close()}letcursor=try statement.execute(parameterValues:["San Francisco"])defer{ cursor.close()}forrowin cursor{letcolumns=try row.get().columnsletcity=trycolumns[0].string()lettempLo=trycolumns[1].int()lettempHi=trycolumns[2].int()letprcp=trycolumns[3].optionalDouble()letdate=trycolumns[4].date()print("""\(city) on\(date): low:\(tempLo), high:\(tempHi),\            precipitation:\(String(describing: prcp))""")}}catch{print(error) // better error handling goes here}

Output:

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)

Prerequisites

  • Swift 5 or later (PostgresClientKit uses Swift 5 language features)
  • libssl-dev (only required on Linux)

PostgresClientKit is compatible with Linux, macOS, and iOS. It has been tested on:

  • Ubuntu 18.04 LTS, 20.04 LTS
  • macOS 10.14, 10.15, 11, 12
  • iOS 12, 13, 14, 15
  • Postgres 10, 11, 12, 13, 14

Building

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

Testing

Set up a Postgres database for testing. This is a one-time process.

Then:

cd <path-to-clone>swift package cleanswift buildswift test

Using

From an Xcode project (as a package dependency)

In Xcode:

  • Select File > Add Packages...

  • Enter the package URL:https://github.com/codewinsdotcom/PostgresClientKit

  • Set the package version requirements (seeDecide on Package Requirements). For example, chooseUp To Next Major Version and1.0.0 to select the latest 1.x.x release of PostgresClientKit.

  • Click Add Package.

Import to your source code file:

import PostgresClientKit

From a standalone Swift package (Package.swift)

In yourPackage.swift file:

  • Add PostgresClientKit to thedependencies. For example:
dependencies:[.package(url:"https://github.com/codewinsdotcom/PostgresClientKit", from:"1.0.0"),],
  • Reference thePostgresClientKit product in thetargets. For example:
targets:[.target(        name:"MyProject",        dependencies:["PostgresClientKit"]),]

Import to your source code file:

import PostgresClientKit

Using CocoaPods

AddPostgresClientKit to yourPodfile. For example:

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

Then runpod install.

Import to your source code file:

import PostgresClientKit

Documentation

Additional examples

Contributing

Thank you for your interest in contributing to PostgresClientKit.

This project has a code of conduct. SeeCODE_OF_CONDUCT.md for details.

Please useissues to:

  • ask questions
  • report problems (bugs)
  • request enhancements

Pull requests against thedevelop branch are welcomed. For a non-trivial contribution (for example, more than correcting spelling, typos, or whitespace) please first discuss the proposed change by opening an issue.

License

PostgresClientKit is licensed under the Apache 2.0 license. SeeLICENSE for details.

Versioning

PostgresClientKit usesSemantic Versioning 2.0.0. For the versions available, see thetags on this repository.

Built with

Authors

About

A PostgreSQL client library for Swift. Does not require libpq.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp