-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from vapor/1.0
1.0
- Loading branch information
Showing
4 changed files
with
10 additions
and
151 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,23 @@ | ||
# MySQL for Swift | ||
|
||
![Swift](http://img.shields.io/badge/swift-3.0-brightgreen.svg) | ||
![Swift](https://camo.githubusercontent.com/0727f3687a1e263cac101c5387df41048641339c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53776966742d332e302d6f72616e67652e7376673f7374796c653d666c6174) | ||
[![Build Status](https://travis-ci.org/vapor/mysql.svg?branch=master)](https://travis-ci.org/vapor/mysql) | ||
|
||
A Swift wrapper for MySQL. | ||
|
||
- [x] Thread-Safe | ||
- [x] Prepared Statements | ||
- [x] 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. | ||
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 | ||
|
||
```swift | ||
import MySQL | ||
|
||
let mysql = try MySQL.Database( | ||
host: "127.0.0.1", | ||
user: "root", | ||
password: "", | ||
database: "test" | ||
) | ||
``` | ||
|
||
### Select | ||
|
||
```swift | ||
let version = try mysql.execute("SELECT @@version") | ||
``` | ||
|
||
### Prepared Statement | ||
|
||
The second parameter to `execute()` is an array of `MySQL.Value`s. | ||
|
||
```swift | ||
let results = try mysql.execute("SELECT * FROM users WHERE age >= ?", [.int(21)]) | ||
``` | ||
|
||
```swift | ||
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()`. | ||
|
||
```swift | ||
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 | ||
|
||
#### Using brew | ||
|
||
Install MySQL | ||
|
||
```shell | ||
brew install mysql | ||
brew link mysql | ||
mysql.server start | ||
``` | ||
|
||
Link MySQL during `swift build` | ||
|
||
```shell | ||
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. | ||
|
||
#### Using macports | ||
|
||
Install MySQL | ||
|
||
```shell | ||
sudo port install mysql57 | ||
sudo port select mysql mysql57 | ||
``` | ||
Link MySQL during `swift build` | ||
```shell | ||
swift build -Xswiftc -I/opt/local/include/mysql57/mysql/mysql -Xlinker -L/opt/local/lib/mysql57/mysql | ||
``` | ||
`-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 | ||
```shell | ||
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. | ||
### Heroku | ||
If you're deploying a Vapor app to heroku with MySQL, you need to set the buildpack to `https://github.com/loganwright/heroku-buildpack-swift`. | ||
You can do this by going to `https://dashboard.heroku.com/apps/<#your-app-here#>/settings` | ||
and adding the buildpack or from command line in your app directory by running | ||
`heroku buildpacks:set https://github.com/loganwright/heroku-buildpack-swift`. | ||
## * Fluent | ||
This wrapper was created to power [Fluent](https://github.com/qutheory/fluent), an ORM for Swift. | ||
## 🌏 Environment | ||
|MySQL|Xcode|Swift| | ||
|:-:|:-:|:-:| | ||
|0.6.x|8.0 GM|DEVELOPMENT-SNAPSHOT-2016-09-06-a| | ||
|0.5.x|8.0 Beta **6**|DEVELOPMENT-SNAPSHOT-2016-08-18-a| | ||
|0.4.x|8.0 Beta **3**|DEVELOPMENT-SNAPSHOT-2016-07-25-a| | ||
|0.3.x|8.0 Beta **3**|DEVELOPMENT-SNAPSHOT-2016-07-25-a| | ||
|0.2.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-06-20-a| | ||
|0.1.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-06-06-a| | ||
## 📖 Documentation | ||
|
||
Visit the Vapor web framework's [documentation](http://docs.qutheory.io) for instructions on how to install Swift 3. | ||
Visit the Vapor web framework's [documentation](http://docs.vapor.codes) for instructions on how to use this package. | ||
|
||
## 💧 Community | ||
|
||
We pride ourselves on providing a diverse and welcoming community. Join your fellow Vapor developers in [our slack](slack.qutheory.io) and take part in the conversation. | ||
Join the welcoming community of fellow Vapor developers in [slack](http://vapor.team). | ||
|
||
## 🔧 Compatibility | ||
|
||
Node has been tested on OS X 10.11, Ubuntu 14.04, and Ubuntu 15.10. | ||
## 👥 Author | ||
Created by [Tanner Nelson](https://github.com/tannernelson). | ||
This package has been tested on macOS and Ubuntu. |