github.com/qutheory/mysql

🐬 Pure Swift MySQL client built on non-blocking, event-driven sockets.


Keywords
mysql, server-side-swift, swift, swift-linux, vapor, vapor-service
License
MIT

Documentation

MySQL for Swift

Swift Build Status

A Swift wrapper for MySQL.

  • Thread-Safe
  • Prepared Statements
  • Tested

This wrapper uses the latest MySQL fetch API to enable performant prepared statements and output bindings. Data is sent to and received from the MySQL server in its native data type without converting to and from strings.

The Swift wrappers around the MySQL's C structs and pointers automatically manage closing connections and deallocating memeory. Additionally, the MySQL library API is used to perform thread safe, performant queries to the database.

~40 assertions tested on Ubuntu 14.04 and macOS 10.11 on every push.

Examples

Connecting to the Database

import MySQL

let mysql = try MySQL.Database(
    host: "127.0.0.1",
    user: "root",
    password: "",
    database: "test"
)

Select

let version = try mysql.execute("SELECT @@version")

Prepared Statement

The second parameter to execute() is an array of MySQL.Values.

let results = try mysql.execute("SELECT * FROM users WHERE age >= ?", [.int(21)])
public enum Value {
    case string(String)
    case int(Int)
    case uint(UInt)
    case double(Double)
    case null
}

Connection

Each call to execute() creates a new connection to the MySQL database. This ensures thread safety since a single connection cannot be used on more than one thread.

If you would like to re-use a connection between calls to execute, create a reusable connection and pass it as the third parameter to execute().

let connection = msyql.makeConnection()
let result = try mysql.execute("SELECT LAST_INSERTED_ID() as id", [], connection)

No need to worry about closing the connection.

Building

macOS

Install MySQL

brew install mysql
brew link mysql
mysql.server start

Link MySQL during swift build

swift build -Xswiftc -I/usr/local/include/mysql -Xlinker -L/usr/local/lib

-I tells the compiler where to find the MySQL header files, and -L tells the linker where to find the library. This is required to compile and run on macOS.

Linux

Install MySQL

sudo apt-get update
sudo apt-get install -y mysql-server libmysqlclient-dev
sudo mysql_install_db
sudo service mysql start

swift build should work normally.

Travis

Travis builds Swift MySQL on both Ubuntu 14.04 and macOS 10.11. Check out the .travis.yml file to see how this package is built and compiled during testing.

Fluent

This wrapper was created to power Fluent, an ORM for Swift.

Author

Created by Tanner Nelson.