项目作者: programingjd

项目描述 :
Async Mysql/Mariadb client with kotlin suspend functions.
高级语言: TSQL
项目地址: git://github.com/programingjd/asynk_mysql.git
创建时间: 2018-02-24T10:16:35Z
项目社区:https://github.com/programingjd/asynk_mysql

开源协议:Apache License 2.0

下载


jcenterjcenter

Asynk MYSQL

A Mysql/Mariadb async client with suspend functions for kotlin coroutines.

Download

The maven artifacts are on Bintray
and jcenter.

Download the latest jar.

Maven

Include those settings
to be able to resolve jcenter artifacts.

  1. <dependency>
  2. <groupId>info.jdavid.asynk</groupId>
  3. <artifactId>mysql</artifactId>
  4. <version>0.0.0.34</version>
  5. </dependency>

Gradle

Add jcenter to the list of maven repositories.

  1. repositories {
  2. jcenter()
  3. }
  1. dependencies {
  2. compile 'info.jdavid.asynk:mysql:0.0.0.34'
  3. }

Limitations

Usage

Connecting

  1. val credentials =
  2. MysqlAuthentication.Credentials.PasswordCredentials("user", "password")
  3. val serverAddress = InetAddress.getByName("hostname")
  4. println(
  5. runBlocking {
  6. credentials.connectTo("database", InetSocketAddress(serverAddress, db.port)).use { connection ->
  7. // returns a list of one row as a map of key (column name or alias) to value.
  8. connection.rows("SELECT VERSION();").toList().first().values.first()
  9. }
  10. }
  11. )

Fetching rows

  1. val total: Int =
  2. connection.values("SELECT COUNT(*) as count FROM table;", "count").iterate {
  3. it.next() // only one row
  4. }
  5. val max = 123
  6. val count: Int =
  7. connection.values(
  8. "SELECT COUNT(*) as count FROM table WHERE id > ?;",
  9. listOf(max),
  10. "count"
  11. ).iterate {
  12. it.next() // only one value
  13. }
  14. val ids = ArrayList<Int>(count)
  15. connection.values("SELECT * FROM table WHERE id > ?", listOf(max), "id").toList(ids)
  16. val names = Map<Int,String?>(count)
  17. connection.entries("SELECT id, name FROM table", "id", "name").toMap(map)
  18. val json = com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(
  19. connection.rows("SELECT * FROM table").toList(ids)
  20. )

Updating rows

  1. println(
  2. connection.affectedRows("UPDATE table SET key = ? WHERE id = ?", listOf("a", 1))
  3. )
  4. println(
  5. connection.prepare("DELETE FROM table WHERE id = ?").use { statement ->
  6. listOf(1, 3, 4, 6, 9).count { id ->
  7. statement.affectedRows(listOf(id)) == 1
  8. }
  9. }
  10. )

Tests

The unit tests assume that docker is running and that the docker daemon is exposed on tcp port 2375.
This option is not enabled by default.
On windows, you will also get prompts to give permission to share your drive that you have to accept.

Docker settings