{"id":13465588,"url":"https://github.com/novi/mysql-swift","last_synced_at":"2025-04-10T00:18:27.000Z","repository":{"id":54910196,"uuid":"48160274","full_name":"novi/mysql-swift","owner":"novi","description":"A type safe MySQL client for Swift","archived":false,"fork":false,"pushed_at":"2023-11-29T14:59:46.000Z","size":698,"stargazers_count":164,"open_issues_count":1,"forks_count":40,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-10T00:18:22.139Z","etag":null,"topics":["linux","mysql","mysql-client","server-side-swift","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/novi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-12-17T07:50:09.000Z","updated_at":"2025-04-07T11:10:21.000Z","dependencies_parsed_at":"2024-01-02T23:55:22.757Z","dependency_job_id":null,"html_url":"https://github.com/novi/mysql-swift","commit_stats":{"total_commits":316,"total_committers":7,"mean_commits":"45.142857142857146","dds":0.04113924050632911,"last_synced_commit":"07574370c94c534857af2bac0695a722b76fd184"},"previous_names":[],"tags_count":53,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/novi%2Fmysql-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/novi%2Fmysql-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/novi%2Fmysql-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/novi%2Fmysql-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/novi","download_url":"https://codeload.github.com/novi/mysql-swift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131318,"owners_count":21052820,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["linux","mysql","mysql-client","server-side-swift","swift"],"created_at":"2024-07-31T15:00:32.236Z","updated_at":"2025-04-10T00:18:26.952Z","avatar_url":"https://github.com/novi.png","language":"Swift","funding_links":[],"categories":["Libs","Data Management [🔝](#readme)","Data and Storage"],"sub_categories":["Data Management"],"readme":"mysql-swift\n===========\n\n**This library is obsolete and not maintained. Use [MySQLNIO](https://github.com/vapor/mysql-nio) instead.**\n\n![Platform Linux, macOS](https://img.shields.io/badge/Platforms-Linux%2C%20macOS-lightgray.svg)\n[![CircleCI](https://circleci.com/gh/novi/mysql-swift.svg?style=svg)](https://circleci.com/gh/novi/mysql-swift)\n\n\n\nMySQL client library for Swift.\nThis is inspired by Node.js' [mysql](https://github.com/mysqljs/mysql).\n\n* Based on libmysqlclient\n* Raw SQL query\n* Simple query formatting and escaping (same as Node's)\n* Mapping queried results to `Codable` structs or classes\n\n_Note:_ No asynchronous I/O support currently. It depends libmysqlclient.\n\n```swift\n// Declare a model\n\nstruct User: Codable, QueryParameter {\n    let id: Int\n    let userName: String\n    let age: Int?\n    let status: Status\n    let createdAt: Date\n    \n    enum Status: String, Codable {\n        case created = \"created\"\n        case verified = \"verified\"\n    }\n    \n    private enum CodingKeys: String, CodingKey {\n        case id\n        case userName = \"user_name\"\n        case age\n        case status = \"status\"\n        case createdAt = \"created_at\"\n    }\n}\n    \n// Selecting\nlet nameParam = \"some one\"\nlet ids: [QueryParameter] = [1, 2, 3, 4, 5, 6]\nlet optionalInt: Int? = nil\nlet rows: [User] = try conn.query(\"SELECT id,user_name,status,status,created_at FROM `user` WHERE (age \u003e ? OR age is ?) OR name = ? OR id IN (?)\", [50, optionalInt, nameParam, QueryArray(ids)] ])\n\n// Inserting\nlet age: Int? = 26\nlet user = User(id: 0, userName: \"novi\", age: age, status: .created, createdAt: Date())\nlet status = try conn.query(\"INSERT INTO `user` SET ?\", [user]) as QueryStatus\nlet newId = status.insertedId\n\n// Updating\nlet tableName = \"user\"\nlet defaultAge = 30\ntry conn.query(\"UPDATE ?? SET age = ? WHERE age is NULL;\", [tableName, defaultAge])\n\n``` \n\n# Requirements\n\n* Swift 5.0 or later\n* MariaDB or MySQL Connector/C (libmysqlclient) 2.2.3 or later\n\n## macOS\n\nInstall pkg-config `.pc` file in [cmysql](https://github.com/vapor-community/cmysql) or [cmysql-mariadb](https://github.com/novi/cmysql-mariadb/tree/mariadb).\n\n```sh\n# cmysql\n$ brew tap novi/tap\n$ brew install novi/tap/cmysql\n\n# cmysql-mariadb\n$ brew tap novi/tap\n$ brew install novi/tap/cmysqlmariadb\n```\n\n## Ubuntu\n\n* Install `libmariadbclient`\n* Follow [Setting up MariaDB Repositories](https://downloads.mariadb.org/mariadb/repositories/#mirror=yamagata-university) and set up your repository.\n\n```sh\n$ sudo apt-get install libmariadbclient-dev\n```\n\n# Installation\n\n## Swift Package Manager\n\n* Add `mysql-swift` to `Package.swift` of your project.\n\n```swift\n// swift-tools-version:5.2\nimport PackageDescription\n\nlet package = Package(\n    ...,\n    dependencies: [\n        .package(url: \"https://github.com/novi/mysql-swift.git\", .upToNextMajor(from: \"0.9.0\"))\n    ],\n    targets: [\n        .target(\n            name: \"YourAppOrLibrary\",\n            dependencies: [\n                // add a dependency\n                .product(name: \"MySQL\", package: \"mysql-swift\")\n            ]\n        )\n    ]\n)\n```\n\n# Usage\n\n## Connection \u0026 Querying\n\n1. Create a pool with options (hostname, port, password,...).\n2. Use `ConnectionPool.execute()`. It automatically get and release a connection. \n\n```swift\nlet option = Option(host: \"your.mysql.host\"...) // Define and create your option type\nlet pool = ConnectionPool(option: option) // Create a pool with the option\nlet rows: [User] = try pool.execute { conn in\n\t// The connection `conn` is held in this block\n\ttry conn.query(\"SELECT * FROM users;\") // And it returns result to outside execute block\n}\n```\n\n## Transaction\n\n```swift\t\nlet wholeStaus: QueryStatus = try pool.transaction { conn in\n\tlet status = try conn.query(\"INSERT INTO users SET ?;\", [user]) as QueryStatus // Create a user\n\tlet userId = status.insertedId // the user's id\n\ttry conn.query(\"UPDATE info SET some_value = ? WHERE some_key = 'latest_user_id' \", [userId]) // Store user's id that we have created the above\n}\nwholeStaus.affectedRows == 1 // true\n```\n\n\n\n# License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnovi%2Fmysql-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnovi%2Fmysql-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnovi%2Fmysql-swift/lists"}