Swiftpack.co - Package - aroblast/mysql-swift
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

MySQL Swift

This repo is only a first try at a reboot from this great original repository made by @mcorega.

Installation

To install this package, simply use the Swift Packages > Add Package Dependency on your Xcode project.

Usage

Connection

A connection is the object allowing you to communicate to the server via MySQL queries.

// Create the connection object
let connection = MySQL.Connection(
  address: "address",
  user: "user",
  password: "password",
  database: "database"
)

do{
  // Open a new connection
  try connection.open()

  // Closing the connection
  try connection.close()
}
catch {
  print("Error: \(error)")
}

Queries

Create a new query from a connection.

Execute

try connection.exec("USE db")

Prepared

// Prepare the query
let stmt : MySQL.Statement = try connection.prepare("SELECT * FROM ?")

// Execute the query with arguments
stmt.exec(["tableName"])

// Execute the query with arguments and get results
let results : MySQL.Result = try stmt.query(["tableName"])

for row in results.readAllRows() ?? [] {
  print(row)
}

Advanced

Connection Pool

// Create a connection pool with 10 connections from connection
let pool = try MySQL.ConnectionPool(number: 10, connection: connection)

// Use a connection from the connection pool
if let poolConnection = pool.getConnection() {
  
  // Free the connection when done
  connPool.free(poolConnection)
}

License

Copyright (c) 2015, Marius Corega All rights reserved.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the {organization} nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

  • If you use this software in a product, an acknowledgment in the product documentation is required. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. This notice may not be removed or altered from any source or binary distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Github

link
Stars: 1

Related Packages

Dependencies

Releases

Hello world! - 2021-01-08T19:06:18

v0.9.1

Its source code is a vastly updated version of the original code from @mcorega on Github. It includes all of the necessary to create and manage MySQL databases from Swift native. It is not supposed to be a very high level library but more of a medium level interface between high level application and a low level socket communicating to the server.

Despite thorough testing the code might still contains some odd bugs hidden in some places, and a lot of work still remains to be done.