Skip to content

Commit 4b2406c

Browse files
committed
Rename Connection.release() to Connection.close()
1 parent d686842 commit 4b2406c

File tree

22 files changed

+175
-178
lines changed

22 files changed

+175
-178
lines changed

examples/mysql/src/commonMain/kotlin/io/github/smyrgeorge/sqlx4k/examples/mysql/Examples.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ object Examples {
107107
val rows = cn.fetchAll("select * from sqlx4k;", Sqlx4kRowMapper).getOrThrow()
108108
println("Rows via connection: $rows")
109109
} finally {
110-
cn.release().getOrThrow()
110+
cn.close().getOrThrow()
111111
delay(1000) // Ensure that the connection has been released.
112112
println("Released connection. Pool size: ${db.poolSize()} (idle: ${db.poolIdleSize()})")
113113
}

examples/postgres/src/commonMain/kotlin/io/github/smyrgeorge/sqlx4k/examples/postgres/Examples.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ object Examples {
201201
val rows = cn.fetchAll("select * from sqlx4k;", Sqlx4kRowMapper).getOrThrow()
202202
println("Rows via connection: $rows")
203203
} finally {
204-
cn.release().getOrThrow()
204+
cn.close().getOrThrow()
205205
delay(1000) // Ensure that the connection has been released.
206206
println("Released connection. Pool size: ${db.poolSize()} (idle: ${db.poolIdleSize()})")
207207
}

examples/sqlite/src/commonMain/kotlin/io/github/smyrgeorge/sqlx4k/examples/sqlite/Examples.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ object Examples {
103103
val rows = cn.fetchAll("select * from sqlx4k;", Sqlx4kRowMapper).getOrThrow()
104104
println("Rows via connection: $rows")
105105
} finally {
106-
cn.release().getOrThrow()
106+
cn.close().getOrThrow()
107107
delay(200) // Ensure that the connection has been released.
108108
println("Released connection. Pool size: ${db.poolSize()} (idle: ${db.poolIdleSize()})")
109109
}

sqlx4k-mysql/src/commonTest/kotlin/io/github/smyrgeorge/sqlx4k/mysql/CommonMySQLConnectionTests.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ class CommonMySQLConnectionTests(
2929
// Perform operation while acquired
3030
assertThat(cn.execute("insert into $table(v) values (1);")).isSuccess()
3131
// release
32-
assertThat(cn.release()).isSuccess()
32+
assertThat(cn.close()).isSuccess()
3333
// further ops should fail
3434
val res = cn.execute("insert into $table(v) values (2);")
3535
assertThat(res).isFailure()
3636
val ex = res.exceptionOrNull() as SQLError
37-
assertThat(ex.code).isEqualTo(SQLError.Code.ConnectionIsReleased)
37+
assertThat(ex.code).isEqualTo(SQLError.Code.ConnectionIsOpen)
3838

3939
assertThat(countRows(table)).isEqualTo(1L)
4040
runCatching { db.execute("drop table if exists $table;").getOrThrow() }
4141
}
4242

4343
fun `double release should fail with ConnectionIsReleased`() = runBlocking {
4444
val cn: Connection = db.acquire().getOrThrow()
45-
assertThat(cn.release()).isSuccess()
46-
val res = cn.release()
45+
assertThat(cn.close()).isSuccess()
46+
val res = cn.close()
4747
assertThat(res).isFailure()
4848
val ex = res.exceptionOrNull() as SQLError
49-
assertThat(ex.code).isEqualTo(SQLError.Code.ConnectionIsReleased)
49+
assertThat(ex.code).isEqualTo(SQLError.Code.ConnectionIsOpen)
5050
}
5151

5252
fun `connection begin-commit and rollback should work`() = runBlocking {
@@ -59,15 +59,15 @@ class CommonMySQLConnectionTests(
5959
val tx1 = cn1.begin().getOrThrow()
6060
assertThat(tx1.execute("insert into $table(v) values (1);")).isSuccess()
6161
tx1.commit().getOrThrow()
62-
cn1.release().getOrThrow()
62+
cn1.close().getOrThrow()
6363
assertThat(countRows(table)).isEqualTo(1L)
6464

6565
// rollback path
6666
val cn2: Connection = db.acquire().getOrThrow()
6767
val tx2 = cn2.begin().getOrThrow()
6868
assertThat(tx2.execute("insert into $table(v) values (2);")).isSuccess()
6969
tx2.rollback().getOrThrow()
70-
cn2.release().getOrThrow()
70+
cn2.close().getOrThrow()
7171
assertThat(countRows(table)).isEqualTo(1L)
7272

7373
runCatching { db.execute("drop table if exists $table;").getOrThrow() }
@@ -82,15 +82,15 @@ class CommonMySQLConnectionTests(
8282
assertThat(cn.execute("insert into $table(v) values (10);")).isSuccess()
8383
val rs = cn.fetchAll("select count(*) from $table;").getOrThrow()
8484
assertThat(rs.first().get(0).asLong()).isEqualTo(1L)
85-
cn.release().getOrThrow()
85+
cn.close().getOrThrow()
8686

8787
runCatching { db.execute("drop table if exists $table;").getOrThrow() }
8888
}
8989

9090
fun `status should be Acquired then Released`() = runBlocking {
9191
val cn: Connection = db.acquire().getOrThrow()
92-
assertThat(cn.status).isEqualTo(Connection.Status.Acquired)
93-
cn.release().getOrThrow()
94-
assertThat(cn.status).isEqualTo(Connection.Status.Released)
92+
assertThat(cn.status).isEqualTo(Connection.Status.Open)
93+
cn.close().getOrThrow()
94+
assertThat(cn.status).isEqualTo(Connection.Status.Closed)
9595
}
9696
}

sqlx4k-mysql/src/jvmMain/kotlin/io/github/smyrgeorge/sqlx4k/mysql/MySQL.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,20 @@ class MySQL(
171171
private val connection: R2dbcConnection
172172
) : Connection {
173173
private val mutex = Mutex()
174-
private var _status: Connection.Status = Connection.Status.Acquired
174+
private var _status: Connection.Status = Connection.Status.Open
175175
override val status: Connection.Status get() = _status
176176

177-
override suspend fun release(): Result<Unit> = runCatching {
177+
override suspend fun close(): Result<Unit> = runCatching {
178178
mutex.withLock {
179-
assertIsAcquired()
180-
_status = Connection.Status.Released
179+
assertIsOpen()
180+
_status = Connection.Status.Closed
181181
connection.close().awaitFirstOrNull()
182182
}
183183
}
184184

185185
override suspend fun execute(sql: String): Result<Long> = runCatching {
186186
mutex.withLock {
187-
assertIsAcquired()
187+
assertIsOpen()
188188
@Suppress("SqlSourceToSinkFlow")
189189
connection.createStatement(sql).execute().awaitSingle().rowsUpdated.awaitFirstOrNull() ?: 0
190190
}
@@ -195,7 +195,7 @@ class MySQL(
195195

196196
override suspend fun fetchAll(sql: String): Result<ResultSet> = runCatching {
197197
return mutex.withLock {
198-
assertIsAcquired()
198+
assertIsOpen()
199199
@Suppress("SqlSourceToSinkFlow")
200200
connection.createStatement(sql).execute().awaitSingle().toResultSet().toResult()
201201
}
@@ -209,7 +209,7 @@ class MySQL(
209209

210210
override suspend fun begin(): Result<Transaction> = runCatching {
211211
mutex.withLock {
212-
assertIsAcquired()
212+
assertIsOpen()
213213
try {
214214
connection.beginTransaction().awaitFirstOrNull()
215215
} catch (e: Exception) {

sqlx4k-mysql/src/nativeMain/kotlin/io/github/smyrgeorge/sqlx4k/mysql/MySQL.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,20 @@ class MySQL(
119119
private val cn: CPointer<out CPointed>
120120
) : Connection {
121121
private val mutex = Mutex()
122-
private var _status: Connection.Status = Connection.Status.Acquired
122+
private var _status: Connection.Status = Connection.Status.Open
123123
override val status: Connection.Status get() = _status
124124

125-
override suspend fun release(): Result<Unit> = runCatching {
125+
override suspend fun close(): Result<Unit> = runCatching {
126126
mutex.withLock {
127-
assertIsAcquired()
128-
_status = Connection.Status.Released
127+
assertIsOpen()
128+
_status = Connection.Status.Closed
129129
sqlx { c -> sqlx4k_cn_release(rt, cn, c, DriverNativeUtils.fn) }.throwIfError()
130130
}
131131
}
132132

133133
override suspend fun execute(sql: String): Result<Long> = runCatching {
134134
mutex.withLock {
135-
assertIsAcquired()
135+
assertIsOpen()
136136
sqlx { c -> sqlx4k_cn_query(rt, cn, sql, c, DriverNativeUtils.fn) }.use {
137137
it.throwIfError()
138138
it.rows_affected.toLong()
@@ -145,7 +145,7 @@ class MySQL(
145145

146146
override suspend fun fetchAll(sql: String): Result<ResultSet> = runCatching {
147147
return mutex.withLock {
148-
assertIsAcquired()
148+
assertIsOpen()
149149
sqlx { c -> sqlx4k_cn_fetch_all(rt, cn, sql, c, DriverNativeUtils.fn) }
150150
.use { it.toResultSet() }
151151
.toResult()
@@ -160,7 +160,7 @@ class MySQL(
160160

161161
override suspend fun begin(): Result<Transaction> = runCatching {
162162
mutex.withLock {
163-
assertIsAcquired()
163+
assertIsOpen()
164164
sqlx { c -> sqlx4k_cn_tx_begin(rt, cn, c, DriverNativeUtils.fn) }.use {
165165
it.throwIfError()
166166
Tx(rt, it.tx!!)

sqlx4k-postgres/src/commonTest/kotlin/io/github/smyrgeorge/sqlx4k/postgres/CommonPostgreSQLConnectionTests.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ class CommonPostgreSQLConnectionTests(
2929
// Perform operation while acquired
3030
assertThat(cn.execute("insert into $table(v) values (1);")).isSuccess()
3131
// release
32-
assertThat(cn.release()).isSuccess()
32+
assertThat(cn.close()).isSuccess()
3333
// further ops should fail
3434
val res = cn.execute("insert into $table(v) values (2);")
3535
assertThat(res).isFailure()
3636
val ex = res.exceptionOrNull() as SQLError
37-
assertThat(ex.code).isEqualTo(SQLError.Code.ConnectionIsReleased)
37+
assertThat(ex.code).isEqualTo(SQLError.Code.ConnectionIsOpen)
3838

3939
assertThat(countRows(table)).isEqualTo(1L)
4040
runCatching { db.execute("drop table if exists $table;").getOrThrow() }
4141
}
4242

4343
fun `double release should fail with ConnectionIsReleased`() = runBlocking {
4444
val cn: Connection = db.acquire().getOrThrow()
45-
assertThat(cn.release()).isSuccess()
46-
val res = cn.release()
45+
assertThat(cn.close()).isSuccess()
46+
val res = cn.close()
4747
assertThat(res).isFailure()
4848
val ex = res.exceptionOrNull() as SQLError
49-
assertThat(ex.code).isEqualTo(SQLError.Code.ConnectionIsReleased)
49+
assertThat(ex.code).isEqualTo(SQLError.Code.ConnectionIsOpen)
5050
}
5151

5252
fun `connection begin-commit and rollback should work`() = runBlocking {
@@ -59,15 +59,15 @@ class CommonPostgreSQLConnectionTests(
5959
val tx1 = cn1.begin().getOrThrow()
6060
assertThat(tx1.execute("insert into $table(v) values (1);")).isSuccess()
6161
tx1.commit().getOrThrow()
62-
cn1.release().getOrThrow()
62+
cn1.close().getOrThrow()
6363
assertThat(countRows(table)).isEqualTo(1L)
6464

6565
// rollback path
6666
val cn2: Connection = db.acquire().getOrThrow()
6767
val tx2 = cn2.begin().getOrThrow()
6868
assertThat(tx2.execute("insert into $table(v) values (2);")).isSuccess()
6969
tx2.rollback().getOrThrow()
70-
cn2.release().getOrThrow()
70+
cn2.close().getOrThrow()
7171
assertThat(countRows(table)).isEqualTo(1L)
7272

7373
runCatching { db.execute("drop table if exists $table;").getOrThrow() }
@@ -82,15 +82,15 @@ class CommonPostgreSQLConnectionTests(
8282
assertThat(cn.execute("insert into $table(v) values (10);")).isSuccess()
8383
val rs = cn.fetchAll("select count(*) from $table;").getOrThrow()
8484
assertThat(rs.first().get(0).asLong()).isEqualTo(1L)
85-
cn.release().getOrThrow()
85+
cn.close().getOrThrow()
8686

8787
runCatching { db.execute("drop table if exists $table;").getOrThrow() }
8888
}
8989

9090
fun `status should be Acquired then Released`() = runBlocking {
9191
val cn: Connection = db.acquire().getOrThrow()
92-
assertThat(cn.status).isEqualTo(Connection.Status.Acquired)
93-
cn.release().getOrThrow()
94-
assertThat(cn.status).isEqualTo(Connection.Status.Released)
92+
assertThat(cn.status).isEqualTo(Connection.Status.Open)
93+
cn.close().getOrThrow()
94+
assertThat(cn.status).isEqualTo(Connection.Status.Closed)
9595
}
9696
}

sqlx4k-postgres/src/jvmMain/kotlin/io/github/smyrgeorge/sqlx4k/postgres/PostgreSQLImpl.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,20 @@ class PostgreSQLImpl(
227227
private val connection: R2dbcConnection
228228
) : Connection {
229229
private val mutex = Mutex()
230-
private var _status: Connection.Status = Connection.Status.Acquired
230+
private var _status: Connection.Status = Connection.Status.Open
231231
override val status: Connection.Status get() = _status
232232

233-
override suspend fun release(): Result<Unit> = runCatching {
233+
override suspend fun close(): Result<Unit> = runCatching {
234234
mutex.withLock {
235-
assertIsAcquired()
236-
_status = Connection.Status.Released
235+
assertIsOpen()
236+
_status = Connection.Status.Closed
237237
connection.close().awaitFirstOrNull()
238238
}
239239
}
240240

241241
override suspend fun execute(sql: String): Result<Long> = runCatching {
242242
mutex.withLock {
243-
assertIsAcquired()
243+
assertIsOpen()
244244
@Suppress("SqlSourceToSinkFlow")
245245
connection.createStatement(sql).execute().awaitSingle().rowsUpdated.awaitFirstOrNull() ?: 0
246246
}
@@ -251,7 +251,7 @@ class PostgreSQLImpl(
251251

252252
override suspend fun fetchAll(sql: String): Result<ResultSet> = runCatching {
253253
return mutex.withLock {
254-
assertIsAcquired()
254+
assertIsOpen()
255255
@Suppress("SqlSourceToSinkFlow")
256256
connection.createStatement(sql).execute().awaitSingle().toResultSet().toResult()
257257
}
@@ -265,7 +265,7 @@ class PostgreSQLImpl(
265265

266266
override suspend fun begin(): Result<Transaction> = runCatching {
267267
mutex.withLock {
268-
assertIsAcquired()
268+
assertIsOpen()
269269
try {
270270
connection.beginTransaction().awaitFirstOrNull()
271271
} catch (e: Exception) {

sqlx4k-postgres/src/nativeMain/kotlin/io/github/smyrgeorge/sqlx4k/postgres/PostgreSQL.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,20 @@ class PostgreSQL(
205205
private val cn: CPointer<out CPointed>
206206
) : Connection {
207207
private val mutex = Mutex()
208-
private var _status: Connection.Status = Connection.Status.Acquired
208+
private var _status: Connection.Status = Connection.Status.Open
209209
override val status: Connection.Status get() = _status
210210

211-
override suspend fun release(): Result<Unit> = runCatching {
211+
override suspend fun close(): Result<Unit> = runCatching {
212212
mutex.withLock {
213-
assertIsAcquired()
214-
_status = Connection.Status.Released
213+
assertIsOpen()
214+
_status = Connection.Status.Closed
215215
sqlx { c -> sqlx4k_cn_release(rt, cn, c, DriverNativeUtils.fn) }.throwIfError()
216216
}
217217
}
218218

219219
override suspend fun execute(sql: String): Result<Long> = runCatching {
220220
mutex.withLock {
221-
assertIsAcquired()
221+
assertIsOpen()
222222
sqlx { c -> sqlx4k_cn_query(rt, cn, sql, c, DriverNativeUtils.fn) }.use {
223223
it.throwIfError()
224224
it.rows_affected.toLong()
@@ -231,7 +231,7 @@ class PostgreSQL(
231231

232232
override suspend fun fetchAll(sql: String): Result<ResultSet> = runCatching {
233233
return mutex.withLock {
234-
assertIsAcquired()
234+
assertIsOpen()
235235
sqlx { c -> sqlx4k_cn_fetch_all(rt, cn, sql, c, DriverNativeUtils.fn) }
236236
.use { it.toResultSet() }
237237
.toResult()
@@ -246,7 +246,7 @@ class PostgreSQL(
246246

247247
override suspend fun begin(): Result<Transaction> = runCatching {
248248
mutex.withLock {
249-
assertIsAcquired()
249+
assertIsOpen()
250250
sqlx { c -> sqlx4k_cn_tx_begin(rt, cn, c, DriverNativeUtils.fn) }.use {
251251
it.throwIfError()
252252
Tx(rt, it.tx!!)

0 commit comments

Comments
 (0)