1 Star 0 Fork 0

张政 / mysql-kit-main

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT
MySQL Documentation Team Chat MIT License Continuous Integration Swift 5.2

🐬 Non-blocking, event-driven Swift client for MySQL.

Major Releases

The table below shows a list of MySQLKit major releases alongside their compatible NIO and Swift versions.

Version NIO Swift SPM
4.0 2.0 5.2+ from: "4.0.0"
3.0 1.0 4.0+ from: "3.0.0"
2.0 N/A 3.1+ from: "2.0.0"
1.0 N/A 3.1+ from: "1.0.0"

Use the SPM string to easily include the dependency in your Package.swift file.

.package(url: "https://github.com/vapor/mysql-kit.git", from: ...)

Supported Platforms

MySQLKit supports the following platforms:

  • Ubuntu 16.04+
  • macOS 10.15+

Overview

MySQLKit is a MySQL client library built on SQLKit. It supports building and serializing MySQL-dialect SQL queries. MySQLKit uses MySQLNIO to connect and communicate with the database server asynchronously. AsyncKit is used to provide connection pooling.

Configuration

Database connection options and credentials are specified using a MySQLConfiguration struct.

import MySQLKit

let configuration = MySQLConfiguration(
    hostname: "localhost",
    port: 3306,
    username: "vapor_username",
    password: "vapor_password",
    database: "vapor_database"
)

URL string based configuration is also supported.

guard let configuration = MySQLConfiguration(url: "mysql://...") else {
    ...
}

To connect via unix-domain sockets, use unixDomainSocketPath instead of hostname and port.

let configuration = MySQLConfiguration(
    unixDomainSocketPath: "/path/to/socket",
    username: "vapor_username",
    password: "vapor_password",
    database: "vapor_database"
)

Connection Pool

Once you have a MySQLConfiguration, you can use it to create a connection source and pool.

let eventLoopGroup: EventLoopGroup = ...
defer { try! eventLoopGroup.syncShutdown() }

let pools = EventLoopGroupConnectionPool(
    source: MySQLConnectionSource(configuration: configuration), 
    on: eventLoopGroup
)
defer { pools.shutdown() }

First create a MySQLConnectionSource using the configuration struct. This type is responsible for creating new connections to your database server as needed.

Next, use the connection source to create an EventLoopGroupConnectionPool. You will also need to pass an EventLoopGroup. For more information on creating an EventLoopGroup, visit SwiftNIO's documentation. Make sure to shutdown the connection pool before it deinitializes.

EventLoopGroupConnectionPool is a collection of pools for each event loop. When using EventLoopGroupConnectionPool directly, random event loops will be chosen as needed.

pools.withConnection { conn 
    print(conn) // MySQLConnection on randomly chosen event loop
}

To get a pool for a specific event loop, use pool(for:). This returns an EventLoopConnectionPool.

let eventLoop: EventLoop = ...
let pool = pools.pool(for: eventLoop)

pool.withConnection { conn
    print(conn) // MySQLConnection on eventLoop
}

MySQLDatabase

Both EventLoopGroupConnectionPool and EventLoopConnectionPool can be used to create instances of MySQLDatabase.

let mysql = pool.database(logger: ...) // MySQLDatabase
let rows = try mysql.simpleQuery("SELECT @@version;").wait()

Visit MySQLNIO's docs for more information on using MySQLDatabase.

SQLDatabase

A MySQLDatabase can be used to create an instance of SQLDatabase.

let sql = mysql.sql() // SQLDatabase
let planets = try sql.select().column("*").from("planets").all().wait()

Visit SQLKit's docs for more information on using SQLDatabase.

The MIT License (MIT) Copyright (c) 2018 Qutheory, LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Visit SQLKit's docs for more information on using SQLDatabase. 展开 收起
Swift
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/hfbdqn/mysql-kit-main.git
git@gitee.com:hfbdqn/mysql-kit-main.git
hfbdqn
mysql-kit-main
mysql-kit-main
main

搜索帮助