├── .gitignore
├── pom.xml
└── src
├── main
└── kotlin
│ └── cn
│ └── codetector
│ └── vertx
│ └── jdbc
│ └── coroutine
│ ├── JDBCClientExt.kt
│ └── JDBCConnectionExt.kt
└── test
└── kotlin
└── cn
└── codetector
└── vertx
└── HelloTest.kt
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 | target/
3 | pom.xml.tag
4 | pom.xml.releaseBackup
5 | pom.xml.versionsBackup
6 | pom.xml.next
7 | release.properties
8 | dependency-reduced-pom.xml
9 | buildNumber.properties
10 | .mvn/timing.properties
11 | !/.mvn/wrapper/maven-wrapper.jar
12 | !.gitkeep
13 | .idea
14 | *.iml
15 | *.ipr
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 | 4.0.0
6 |
7 | cn.codetector.vertx
8 | jdbc-kotlin-coroutine-extension
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 | cn.codetector.vertx jdbc-kotlin-coroutine-extension
13 |
14 |
15 | UTF-8
16 | 1.3.11
17 | 4.12
18 | 3.6.2
19 | 1.1.0
20 |
21 |
22 |
23 |
24 | jcenter
25 | jcenter
26 | https://jcenter.bintray.com
27 |
28 |
29 |
30 | false
31 |
32 | dl
33 | bintray
34 | http://dl.bintray.com/kotlin/kotlin-eap-1.1
35 |
36 |
37 | codetectorRepoRelease
38 | codetectorRelease
39 | http://nexus.codetector.cn/repository/codetector/
40 |
41 | true
42 |
43 |
44 | false
45 |
46 |
47 |
48 | codetectorRepoStage
49 | codetectorStaging
50 | http://nexus.codetector.cn/repository/codetector-staging/
51 |
52 | false
53 |
54 |
55 | true
56 |
57 |
58 |
59 |
60 |
61 |
62 | org.jetbrains.kotlinx
63 | kotlinx-coroutines-core
64 | ${kotlinx.coroutines.version}
65 | compile
66 |
67 |
68 | io.vertx
69 | vertx-core
70 | ${vertx.version}
71 |
72 |
73 | io.vertx
74 | vertx-jdbc-client
75 | ${vertx.version}
76 |
77 |
78 | org.jetbrains.kotlin
79 | kotlin-stdlib
80 | ${kotlin.version}
81 |
82 |
83 | org.jetbrains.kotlin
84 | kotlin-test-junit
85 | ${kotlin.version}
86 | test
87 |
88 |
89 | junit
90 | junit
91 | ${junit.version}
92 | test
93 |
94 |
95 |
96 |
97 | src/main/kotlin
98 | src/test/kotlin
99 |
100 |
101 | org.jetbrains.kotlin
102 | kotlin-maven-plugin
103 | ${kotlin.version}
104 |
105 |
106 | compile
107 | compile
108 |
109 | compile
110 |
111 |
112 |
113 | test-compile
114 | test-compile
115 |
116 | test-compile
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 | codetectorRepo
127 | http://nexus.codetector.cn/repository/codetector/
128 |
129 |
130 | codetectorRepo
131 | http://nexus.codetector.cn/repository/codetector-staging/
132 |
133 |
134 |
135 |
136 |
--------------------------------------------------------------------------------
/src/main/kotlin/cn/codetector/vertx/jdbc/coroutine/JDBCClientExt.kt:
--------------------------------------------------------------------------------
1 | package cn.codetector.vertx.jdbc.coroutine
2 |
3 | import io.vertx.ext.jdbc.JDBCClient
4 | import io.vertx.ext.sql.SQLConnection
5 | import kotlin.coroutines.resume
6 | import kotlin.coroutines.resumeWithException
7 | import kotlin.coroutines.suspendCoroutine
8 |
9 | suspend fun JDBCClient.getConnection(): SQLConnection = suspendCoroutine { cont ->
10 | this.getConnection { conn ->
11 | if (conn.succeeded()) {
12 | cont.resume(conn.result())
13 | } else {
14 | cont.resumeWithException(conn.cause())
15 | }
16 | }
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/src/main/kotlin/cn/codetector/vertx/jdbc/coroutine/JDBCConnectionExt.kt:
--------------------------------------------------------------------------------
1 | package cn.codetector.vertx.jdbc.coroutine
2 |
3 | import io.vertx.core.json.JsonArray
4 | import io.vertx.ext.sql.ResultSet
5 | import io.vertx.ext.sql.SQLConnection
6 | import io.vertx.ext.sql.UpdateResult
7 | import kotlin.coroutines.resume
8 | import kotlin.coroutines.resumeWithException
9 | import kotlin.coroutines.suspendCoroutine
10 |
11 | /**
12 | * Created by codetector on 2017/3/15.
13 | * Kotlin Vert.x JDBC (SQLConnection) Coroutine Extension functions
14 | */
15 | suspend fun SQLConnection.setAutoCommit(autoCommit: Boolean): Boolean = suspendCoroutine { cont ->
16 | this.setAutoCommit(autoCommit) {
17 | cont.resume(it.succeeded())
18 | }
19 | }
20 |
21 | suspend fun SQLConnection.execute(query: String): Boolean = suspendCoroutine { cont ->
22 | this.execute(query) {
23 | cont.resume(it.succeeded())
24 | }
25 | }
26 |
27 | suspend fun SQLConnection.query(query: String): ResultSet = suspendCoroutine { cont ->
28 | this.query(query) {
29 | if (it.succeeded()) {
30 | cont.resume(it.result())
31 | } else {
32 | cont.resumeWithException(it.cause())
33 | }
34 | }
35 | }
36 |
37 | suspend fun SQLConnection.queryWithParams(query: String, args: JsonArray): ResultSet = suspendCoroutine { cont ->
38 | this.queryWithParams(query, args) {
39 | if (it.succeeded()) {
40 | cont.resume(it.result())
41 | } else {
42 | cont.resumeWithException(it.cause())
43 | }
44 | }
45 | }
46 |
47 | suspend fun SQLConnection.update(query: String): UpdateResult = suspendCoroutine { cont ->
48 | this.update(query) {
49 | if (it.succeeded()) {
50 | cont.resume(it.result())
51 | } else {
52 | cont.resumeWithException(it.cause())
53 | }
54 | }
55 | }
56 |
57 | suspend fun SQLConnection.updateWithParams(query: String, args: JsonArray): UpdateResult = suspendCoroutine { cont ->
58 | this.updateWithParams(query, args) {
59 | if (it.succeeded()) {
60 | cont.resume(it.result())
61 | } else {
62 | cont.resumeWithException(it.cause())
63 | }
64 | }
65 | }
66 |
67 | suspend fun SQLConnection.batch(sqlStatements: List): List = suspendCoroutine { cont ->
68 | this.batch(sqlStatements) {
69 | if (it.succeeded()) {
70 | cont.resume(it.result())
71 | } else {
72 | cont.resumeWithException(it.cause())
73 | }
74 | }
75 | }
76 |
77 | suspend fun SQLConnection.batchWithParams(sqlStatements: String, args: List): List = suspendCoroutine { cont ->
78 | this.batchWithParams(sqlStatements, args) {
79 | if (it.succeeded()) {
80 | cont.resume(it.result())
81 | } else {
82 | cont.resumeWithException(it.cause())
83 | }
84 | }
85 | }
86 |
87 | suspend fun SQLConnection.close(): Boolean = suspendCoroutine { cont ->
88 | this.close {
89 | cont.resume(it.succeeded())
90 | }
91 | }
--------------------------------------------------------------------------------
/src/test/kotlin/cn/codetector/vertx/HelloTest.kt:
--------------------------------------------------------------------------------
1 | package cn.codetector.vertx
2 |
3 | import org.junit.Test
4 | import kotlin.test.assertEquals
5 |
6 | class HelloTest {
7 |
8 | }
9 |
--------------------------------------------------------------------------------