Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yockow/swiftpq
A simple wrapper of libpq in Swift.
https://github.com/yockow/swiftpq
postgresql swift
Last synced: 24 days ago
JSON representation
A simple wrapper of libpq in Swift.
- Host: GitHub
- URL: https://github.com/yockow/swiftpq
- Owner: YOCKOW
- License: mit
- Created: 2024-01-30T02:10:46.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-19T09:02:21.000Z (6 months ago)
- Last Synced: 2024-05-19T14:05:56.057Z (6 months ago)
- Topics: postgresql, swift
- Language: Swift
- Homepage:
- Size: 507 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# What is `SwiftPQ`?
`SwiftPQ` is a simple wrapper of [libpq](https://www.postgresql.org/docs/current/libpq.html) that is a C-API to PostgreSQL.
You can send query to PostgreSQL server with raw SQL, or in the *Swifty* way[^1].[^1]: See also [SQLGrammarCorrespondingTypes.md](./SQLGrammarCorrespondingTypes.md)
> [!WARNING]
>
> UNDER DEVELOPMENT. ANY APIs MAY CHANGE IN ANY TIME.# Requirements
* Swift >= 5.10
* libpq
* [libecgp](https://www.postgresql.org/docs/current/app-ecpg.html)
* [libpgtypes](https://www.postgresql.org/docs/current/ecpg-pgtypes.html)# Usage
## First of all: Establish the connection.
### By UNIX Socket
```Swift
import PQlet connection = try Connection(
unixSocketDirectoryPath: "/var/run/postgresql",
database: databaseName,
user: databaseUserName,
password: databasePassword
)
```### Specifying domain
```Swift
import PQlet connection = try Connection(
host: .localhost,
database: databaseName,
user: databaseUserName,
password: databasePassword
)
```## Let's send queries!
You can see the implementations of commands in "Sources/PQ/Commands" directory.
Some macros are useful to embed SQL tokens into queries. See [Macros.swift](Sources/SQLGrammar/Macros.swift).### CREATE TABLE
#### Raw SQL
```Swift
let result = try await connection.execute(.rawSQL("""
CREATE TABLE products (
product_no integer,
name text,
price numeric
);
"""))
```#### SQL with String Interpolation
```Swift
let result = try await connection.execute(.rawSQL("""
CREATE TABLE \(identifier: "my_products#1") (
product_no integer,
name text,
price numeric
);
"""))
```### Swifty way
```Swift
let result = try await connection.execute(
.createTable(
"myFavouriteProducts",
definitions: [
.column(name: "product_no", dataType: .integer),
.column(name: "name", dataType: .text),
.column(name: "price", dataType: .numeric),
],
ifNotExists: true
)
)
```### DROP TABLE
#### Raw SQL
```Swift
let result = try await connection.execute(.rawSQL("DROP TABLE my_table;"))
```#### SQL with String Interpolation
```Swift
let result = try await connection.execute(.rawSQL("DROP TABLE \(identifier: "my_table#1");"))
```### Swifty way
```Swift
let result = try await connection.execute(.dropTable("my_old_table", ifExists: true))
```# License
MIT License.
See "LICENSE.txt" for more information.