novi/mysql-swift
mysql-swift
macOS
Install pkg-config .pc file in cmysql or cmysql-mariadb.
# cmysql
$ brew tap novi/tap
$ brew install novi/tap/cmysql
# cmysql-mariadb
$ brew tap novi/tap
$ brew install novi/tap/cmysqlmariadbUbuntu
- Install
libmariadbclient - Follow Setting up MariaDB Repositories and set up your repository.
$ sudo apt-get install libmariadbclient-devInstallation
Swift Package Manager
- Add
mysql-swifttoPackage.swiftof your project.
// swift-tools-version:5.2
import PackageDescription
let package = Package(
...,
dependencies: [
.package(url: "https://github.com/novi/mysql-swift.git", .upToNextMajor(from: "0.9.0"))
],
targets: [
.target(
name: "YourAppOrLibrary",
dependencies: [
// add a dependency
.product(name: "MySQL", package: "mysql-swift")
]
)
]
)Usage
Connection & Querying
- Create a pool with options (hostname, port, password,...).
- Use
ConnectionPool.execute(). It automatically get and release a connection.
let option = Option(host: "your.mysql.host"...) // Define and create your option type
let pool = ConnectionPool(option: option) // Create a pool with the option
let rows: [User] = try pool.execute { conn in
// The connection `conn` is held in this block
try conn.query("SELECT * FROM users;") // And it returns result to outside execute block
}Transaction
let wholeStaus: QueryStatus = try pool.transaction { conn in
let status = try conn.query("INSERT INTO users SET ?;", [user]) as QueryStatus // Create a user
let userId = status.insertedId // the user's id
try conn.query("UPDATE info SET some_value = ? WHERE some_key = 'latest_user_id' ", [userId]) // Store user's id that we have created the above
}
wholeStaus.affectedRows == 1 // trueLicense
MIT
Package Metadata
Repository: novi/mysql-swift
Default branch: master
README: README.md