Skip to content

Commit

Permalink
Merge pull request #52 from vapor/1.0
Browse files Browse the repository at this point in the history
1.0
  • Loading branch information
loganwright authored Sep 15, 2016
2 parents cffa4b2 + 30922db commit e942086
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 151 deletions.
1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ install:
sudo mysql_install_db;
fi
- mysql -u root --password="" -e 'create database test;'
- eval "$(curl -sL swift.vapor.sh/travis)"
- eval "$(curl -sL https://swift.vapor.sh/swift-install)"
script:
# Build MySQL
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
Expand Down
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ let package = Package(
name: "MySQL",
dependencies: [
// Module map for `libmysql`
.Package(url: "https://github.com/vapor/cmysql.git", majorVersion: 0, minor: 3),
.Package(url: "https://github.com/vapor/cmysql.git", majorVersion: 1),

// Data structure for converting between multiple representations
.Package(url: "https://github.com/vapor/node.git", majorVersion: 0, minor: 6),
.Package(url: "https://github.com/vapor/node.git", majorVersion: 1),

// Core extensions, type-aliases, and functions that facilitate common tasks
.Package(url: "https://github.com/vapor/core.git", majorVersion: 0, minor: 5),
.Package(url: "https://github.com/vapor/core.git", majorVersion: 1),

// JSON parsing and serialization for storing arrays and objects in MySQL
.Package(url: "https://github.com/vapor/json.git", majorVersion: 0, minor: 7)
.Package(url: "https://github.com/vapor/json.git", majorVersion: 1)
]
)
150 changes: 5 additions & 145 deletions README.md
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.

0 comments on commit e942086

Please sign in to comment.