{"id":13505017,"url":"https://github.com/Kitura/SwiftKueryMySQL","last_synced_at":"2025-03-29T22:31:20.995Z","repository":{"id":47641693,"uuid":"83483039","full_name":"Kitura/SwiftKueryMySQL","owner":"Kitura","description":"MySQL plugin for Swift-Kuery framework","archived":false,"fork":false,"pushed_at":"2021-08-20T02:23:26.000Z","size":471,"stargazers_count":34,"open_issues_count":8,"forks_count":13,"subscribers_count":25,"default_branch":"master","last_synced_at":"2024-05-21T06:06:50.662Z","etag":null,"topics":["linux","macos","mysql","relational-databases","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kitura.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-28T21:53:20.000Z","updated_at":"2023-05-15T04:16:30.000Z","dependencies_parsed_at":"2022-09-23T14:58:13.931Z","dependency_job_id":null,"html_url":"https://github.com/Kitura/SwiftKueryMySQL","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKueryMySQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKueryMySQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKueryMySQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FSwiftKueryMySQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kitura","download_url":"https://codeload.github.com/Kitura/SwiftKueryMySQL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246254077,"owners_count":20747946,"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","macos","mysql","relational-databases","swift"],"created_at":"2024-08-01T00:00:56.937Z","updated_at":"2025-03-29T22:31:19.228Z","avatar_url":"https://github.com/Kitura.png","language":"Swift","funding_links":[],"categories":["Packages","Swift"],"sub_categories":["Database"],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"http://kitura.io/\"\u003e\n        \u003cimg src=\"https://raw.githubusercontent.com/IBM-Swift/Kitura/master/Sources/Kitura/resources/kitura-bird.svg?sanitize=true\" height=\"100\" alt=\"Kitura\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://ibm-swift.github.io/SwiftKueryMySQL/index.html\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/apidoc-SwiftKueryMySQL-1FBCE4.svg?style=flat\" alt=\"APIDoc\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://travis-ci.org/IBM-Swift/SwiftKueryMySQL\"\u003e\n    \u003cimg src=\"https://travis-ci.org/IBM-Swift/SwiftKueryMySQL.svg?branch=master\" alt=\"Build Status - Master\"\u003e\n    \u003c/a\u003e\n    \u003cimg src=\"https://img.shields.io/badge/os-macOS-green.svg?style=flat\" alt=\"macOS\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/os-linux-green.svg?style=flat\" alt=\"Linux\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/license-Apache2-blue.svg?style=flat\" alt=\"Apache 2\"\u003e\n    \u003ca href=\"http://swift-at-ibm-slack.mybluemix.net/\"\u003e\n    \u003cimg src=\"http://swift-at-ibm-slack.mybluemix.net/badge.svg\" alt=\"Slack Status\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n# SwiftKueryMySQL\n\n[MySQL](https://dev.mysql.com/) plugin for the [Swift-Kuery](https://github.com/IBM-Swift/Swift-Kuery) framework. It enables you to use Swift-Kuery to manipulate data in a MySQL database.\n\n## Swift version\nThe latest version of SwiftKueryMySQL requires **Swift 4.0 or newer**. You can download this version of the Swift binaries by following this [link](https://swift.org/download/). Compatibility with other Swift versions is not guaranteed.\n\n## Install MySQL\n\n#### macOS\n```\nbrew install mysql\nmysql.server start\n```\n\n#### Linux\n```\nsudo apt-get update\nsudo apt-get install mysql-server libmysqlclient-dev pkg-config\nsudo service mysql start\n```\n\n## Usage\n\nOn macOS, regular swift commands can be used for build and test. Use the example command below for generating an Xcode project.\n\nFor example,\n```\nswift build\nswift test\nswift package generate-xcodeproj --xcconfig-overrides Config.xcconfig\n```\nOn linux standard swift commands will also work provided your mysql installation is version 5.7 or greater. If using an earlier version of mysql add ` -Xcc -I/usr/include/mysql` to swift commands to point the compiler at the mysql header files:\n\nFor example,\n```\nswift build -Xcc -I/usr/include/mysql/\nswift test -Xcc -I/usr/include/mysql/\n```\n\n#### Add dependencies\n\nAdd the `SwiftKueryMySQL` package to the dependencies within your application’s `Package.swift` file. Substitute `\"x.x.x\"` with the latest `SwiftKueryMySQL` [release](https://github.com/IBM-Swift/SwiftKueryMySQL/releases).\n\n```swift\n.package(url: \"https://github.com/IBM-Swift/SwiftKueryMySQL.git\", from: \"x.x.x\")\n```\n\nAdd `SwiftKueryMySQL` to your target's dependencies:\n\n```swift\n.target(name: \"example\", dependencies: [\"SwiftKueryMySQL\"]),\n```\n\n#### Import package\n\n  ```swift\n  import SwiftKueryMySQL\n  ```\n\n## Using SwiftKueryMySQL\n\nCreate an instance of `MySQLConnection` by calling:\n\n```swift\nlet connection = MySQLConnection(host: host, user: user, password: password, database: database,\n                                 port: port, characterSet: characterSet)\n```\n**Where:**\n- *host* - hostname or IP of the MySQL server, defaults to localhost\n- *user* - the user name, defaults to current user\n- *password* - the user password, defaults to no password\n- *database* - default database to use, if specified\n- *port* - port number for the TCP/IP connection if connecting to server on a non-standard port (i.e. not 3306)\n- *characterSet* - MySQL character set to use for the connection\n\nAll the connection parameters are optional, so if you were using a standard local MySQL server as the current user, you could simply use:\n```swift\nlet connection = MySQLConnection(password: password)\n```\n*password* is also optional, but recommended.\n\nAlternatively, call:\n```swift\nlet connection = MySQLConnection(url: URL(string: \"mysql://\\(user):\\(password)@\\(host):\\(port)/\\(database)\")!))\n```\nYou now have a connection that can be used to execute SQL queries created using Swift-Kuery.\n\nTo connect to the server and execute a query:\n```swift\nconnection.connect() { result in\n    guard result.success else {\n        // Connection unsuccessful\n        return\n    }\n    // Connection succesful\n    // Use connection\n    connection.execute(query: query) { queryResult in\n      guard queryResult.success else {\n          // Check for Error and handle\n          return\n      }\n      // Process queryResult\n   }\n}\n```\n\nMySQLConnections should not be used to execute concurrent operations and therefore should not be shared across threads without proper synchronisation in place. It is recommended to use a connection pool if you wish to share connections between multiple threads as the connection pool will ensure your connection is not used concurrently.  \n\nThe example below creates a `ConnectionPool` containing a single connection and uses it to perform an insert on multiple threads:\n\n```swift\nvar connectionPoolOptions = ConnectionPoolOptions.init(initialCapacity: 1, maxCapacity: 1)\nlet connectionPool = MySQLConnection.createPool(host: host, user: user, password: password, database: database, port: port, characterSet: nil, connectionTimeout: 10000, poolOptions: connectionPoolOptions)\n.......\nlet insertQuery = Insert(into: infos, values: \"firstname\", \"surname\", Parameter())\nlet insertGroup = DispatchGroup()\nfor age in 0 ... 5 {\n    insertGroup.enter()\n    connectionPool.getConnection() { connection, error in\n        guard let connection = connection else {\n            // Error Handling and return\n        }\n        connection.execute(query: insertQuery, parameters: [age]) { result in\n            guard result.success else {\n                // Error handling and return\n            }\n            print(\"Successfully inserted age: \\(age)\")\n            return insertGroup.leave()\n        }\n    }\n}\ninsertGroup.wait()\n```\nWhen executing this example code you see output similar to:\n```\nSuccessfully inserted age: 0\nSuccessfully inserted age: 1\nSuccessfully inserted age: 2\nSuccessfully inserted age: 3\nSuccessfully inserted age: 4\nSuccessfully inserted age: 5\n```\nThis is because the connection pool only allows that connection to be obtained by a single task. Because the connection pool is now empty, additional tasks are queued for later execution. As each task completes, the single connection is returned to the pool, and the next task is then invoked.\n\nIn the example above, a DispatchGroup is used to pause the main thread until all of the tasks complete. This is necessary because the call to connectionPool.getConnection() returns immediately - the task is invoked later, once a connection is available.\n\nIf you increase the capacity of the thread pool, then the order of inserts will be unpredictable, as they are able to execute concurrently on different connections:\n```\nSuccessfully inserted age: 0\nSuccessfully inserted age: 1\nSuccessfully inserted age: 3\nSuccessfully inserted age: 2\nSuccessfully inserted age: 5\nSuccessfully inserted age: 4\n```\n\nView the [Swift-Kuery](https://github.com/IBM-Swift/Swift-Kuery) documentation for detailed information on using the Swift-Kuery framework.\n\n\n## For testing purposes - MySQL test setup\n\nTo run `swift test` to validate your MySQL installation, you must first run the following commands to set up your MySQL:\n```\nmysql_upgrade -uroot || echo \"No need to upgrade\"\nmysql -uroot -e \"CREATE USER 'swift'@'localhost' IDENTIFIED BY 'kuery';\"\nmysql -uroot -e \"CREATE DATABASE IF NOT EXISTS test;\"\nmysql -uroot -e \"GRANT ALL ON test.* TO 'swift'@'localhost';\"\n```\n\n## API Documentation\nFor more information visit our [API reference](https://ibm-swift.github.io/SwiftKueryMySQL/index.html).\n\n## Community\n\nWe love to talk server-side Swift, and Kitura. Join our [Slack](http://swift-at-ibm-slack.mybluemix.net/) to meet the team!\n\n## Deployment via Cloud Foundry\n\nIf you include SwiftKueryMySQL as a dependancy in an application you are deploying using the Cloud Foundry Swift buildpack then you will need to specify some additional flags when compiling your application. This is best achieved by adding a file to the root of your application named .swift-build-linux-options with the content:\n\n```\n$ cat .swift-build-options-linux \n-Xcc -I/usr/include/mysql/\n```\nThese flags tell the compiler where to find the MySQL header files required to build the CMySQL library.\n\n## License\nThis library is licensed under Apache 2.0. Full license text is available in [LICENSE](https://github.com/IBM-Swift/SwiftKueryMySQL/blob/master/LICENSE.txt).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKitura%2FSwiftKueryMySQL","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKitura%2FSwiftKueryMySQL","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKitura%2FSwiftKueryMySQL/lists"}