SwiftPQ is a simple wrapper of libpq that is a C-API to PostgreSQL.
You can send query to PostgreSQL server with raw SQL, or in the Swifty way1.
Warning
UNDER DEVELOPMENT. ANY APIs MAY CHANGE IN ANY TIME.
- Swift >= 5.10
- libpq
- libecgp
- libpgtypes
import PQ
let connection = try Connection(
unixSocketDirectoryPath: "/var/run/postgresql",
database: databaseName,
user: databaseUserName,
password: databasePassword
)import PQ
let connection = try Connection(
host: .localhost,
database: databaseName,
user: databaseUserName,
password: databasePassword
)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.
let result = try await connection.execute(.rawSQL("""
CREATE TABLE products (
product_no integer,
name text,
price numeric
);
"""))let result = try await connection.execute(.rawSQL("""
CREATE TABLE \(identifier: "my_products#1") (
product_no integer,
name text,
price numeric
);
"""))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
)
)let result = try await connection.execute(.rawSQL("DROP TABLE my_table;"))let result = try await connection.execute(.rawSQL("DROP TABLE \(identifier: "my_table#1");"))let result = try await connection.execute(.dropTable("my_old_table", ifExists: true))MIT License.
See "LICENSE.txt" for more information.
Footnotes
-
See also SQLGrammarCorrespondingTypes.md ↩