Home

Awesome

OHMySQL

License License CocoaPods Compatible Carthage Compatible Documentation

OHMySQL can connect to remote or local MySQL database and execute CRUD operations. The framework is built upon MySQL C API, but you don’t need to dive into low-level. The following diagram represents a general architecture. Logic (saving, editing, removing etc.) is aggregated in the app. The database is just a shared storage.

<p align="center" >⭐️ <b>Every star is appreciated!</b> ⭐️</p> <!-- @import "[TOC]" {cmd="toc" depthFrom=2 depthTo=6 orderedList=false} --> <!-- code_chunk_output --> <!-- /code_chunk_output -->

Features

Platforms

PlatformSupported
iOS14.0+
macOS11.0+
Mac Catalyst14.0+
watchOS8.0+
tvOS15.0+

Project Support

I wish to support the library and extend API and functionality. If you donate me some money 💵, it keeps me motivated and happy 🙂 You may support me via PayPal or let me know any other method convenient for you.

Installation

Read documentation how to install the library as a dependency in your project.

Usage

Connect to the database.

let user = MySQLConfiguration(userName: "root", 
                              password: "root", 
                            serverName: "localhost", 
                                dbName: "dbname", 
                                  port: 3306, 
                                socket: "/mysql/mysql.sock")

let coordinator = MySQLStoreCoordinator(user: user!)
coordinator.encoding = .UTF8MB4
coordinator.connect()

To end a connection:

coordinator.disconnect()

Query Context

To execute a query you have to create the context:

let context = MySQLQueryContext()
context.storeCoordinator = coordinator

Execute Query

let dropQueryString = "DROP TABLE `MyTable`"
let dropQueryRequest = MySQLQueryRequest(queryString: dropQueryString)
try? context.execute(dropQueryRequest)

SELECT

The response contains the array of the dictionaries.

let query = MySQLQueryRequestFactory.select("tasks", condition: nil)
let response = try? context.executeQueryRequestAndFetchResult(query)

INSERT

let query = MySQLQueryRequestFactory.insert("tasks", set: ["name": "Something", "desctiption": "new task"])
try? context.execute(query)

UPDATE

let query = MySQLQueryRequestFactory.update("tasks", set: ["name": "Something"], condition: "id=7")
try? context.execute(query)

DELETE

let query = MySQLQueryRequestFactory.delete("tasks", condition: "id=10")
try? context.execute(query)

JOINs

You can execute 4 types of joins - INNER, RIGHT, LEFT, FULL.

let query = MySQLQueryRequestFactory.joinType(OHJoinInner, fromTable: "tasks", columnNames: ["id", "name", "description"], joinOn: ["subtasks": "tasks.id=subtasks.parentId"])
let result = try? context.executeQueryRequestAndFetchResult(query)

Object Mapping

You have to implement the protocol OHMappingProtocol for your models. The library has only a primary logic for mapping, so I would recommend you writing a mapping logic by yourself. If you are using Swift you cannot use fundamental number types (Int, Double), only NSNumber (due to run-time).

context.insertObject(task)
try? context.save()

You can update/delete the objects easily.

let task = Task()
task.name = "sample"
context.updateObject(task)
context.deleteObject(task)

try? context.save()

Communication

License

See LICENSE.