Skip to content

oleghnidets/OHMySQL

Repository files navigation

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.

⭐️ Every star is appreciated! ⭐️

Features

  • Supports Swift and Objective-C
  • Requires minimal knowledge in SQL
  • Easy to integrate and use
  • Many functionality features
  • Up-to-date MySQL library
  • Documentation and support

Platforms

Platform Supported
iOS 14.0+
macOS 11.0+
Mac Catalyst 14.0+
watchOS 8.0+
tvOS 15.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.