github.com/github.com/IBM-Swift/SwiftKueryMySQL

MySQL plugin for Swift-Kuery framework


Keywords
linux, macos, mysql, relational-databases, swift
License
Apache-2.0

Documentation

SwiftKueryMySQL

MySQL plugin for Swift-Kuery framework

Build Status - Master macOS Linux Apache 2

Summary

MySQL plugin for the Swift-Kuery framework. It enables you to use Swift-Kuery to manipulate data in a MySQL database.

Swift version

The latest version of SwiftKueryMySQL requires Swift 4.0. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.

Install MySQL

macOS

brew install mysql
mysql.server start

Other install options: https://dev.mysql.com/doc/refman/5.7/en/osx-installation.html

On macOS, add -Xlinker -L/usr/local/lib to swift commands to point the linker to the MySQL library location. For example,

swift build -Xlinker -L/usr/local/lib
swift test -Xlinker -L/usr/local/lib
swift package -Xlinker -L/usr/local/lib generate-xcodeproj

Linux

Download the release package for your Linux distribution from http://dev.mysql.com/downloads/repo/apt/ For example: wget https://repo.mysql.com//mysql-apt-config_0.8.4-1_all.deb

sudo dpkg -i mysql-apt-config_0.8.4-1_all.deb
sudo apt-get update
sudo apt-get install mysql-server libmysqlclient-dev
sudo service mysql start

More details: https://dev.mysql.com/doc/refman/5.7/en/linux-installation.html

On linux, regular swift commands should work fine:

swift build
swift test
swift package generate-xcodeproj

MySQL Test Setup

To run swift test you must first set up your MySQL with the following commands:

mysql_upgrade -uroot || echo "No need to upgrade"
mysql -uroot -e "CREATE USER 'swift'@'localhost' IDENTIFIED BY 'kuery';"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS test;"
mysql -uroot -e "GRANT ALL ON test.* TO 'swift'@'localhost';"

Using Swift-Kuery-MySQL

First create an instance of MySQLConnection by calling:

let connection = MySQLConnection(host: host, user: user, password: password, database: database, 
                                 port: port, characterSet: characterSet)

Where:

  • host - hostname or IP of the MySQL server, defaults to localhost
  • user - the user name, defaults to current user
  • password - the user password, defaults to no password
  • database - default database to use if specified
  • port - port number for the TCP/IP connection if connecting to server on a non-standard port (not 3306)
  • characterSet - MySQL character set to use for the connection

All the connection parameters are optional, so if you were using a standard local MySQL server as the current user, you could simply use:

let connection = MySQLConnection(password: password)

password is also optional, but recommended.

Alternatively, call:

let connection = MySQLConnection(url: URL(string: "mysql://\(user):\(password)@\(host):\(port)/\(database)")!))

You now have a connection that can be used to execute SQL queries created using Swift-Kuery.

If you want to share a connection instance between multiple threads use MySQLThreadSafeConnection instead of MySQLConnection:

let connection = MySQLThreadSafeConnection(host: host, user: user, password: password, database: database, 
                                 port: port, characterSet: characterSet)

A MySQLThreadSafeConnection instance can be used to safely execute queries on multiple threads sharing the same connection.

To connect to the server and execute a query:

connection.connect() { error in
   // if error is nil, connect() was successful
   let query = Select(from: table)
   connection.execute(query: query) { queryResult in
      if let resultSet = queryResult.asResultSet {
         for row in resultSet.rows {
            // process each row
         }
      }
   }
}

View the Kuery documentation for detailed information on using the Kuery framework.

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.