Hong Phuc
02/09/2025, 7:11 AMoverride suspend fun getId(email: String): String? = withContext(Dispatchers.IO) {
dataSource.connection.use { connection ->
connection.prepareStatement("SELECT id FROM users WHERE email = ?;").use { statement ->
statement.setString(1, email)
statement.executeQuery().use { resultSet ->
if (resultSet.next()) {
resultSet.getString("id")
} else {
null
}
}
}
}
}
override suspend fun getId(email: String): String? {
var userId: String? = null
withContext(Dispatchers.IO) {
val dbconnection = dataSource.connection
val statement: PreparedStatement = dbconnection.prepareStatement("SELECT * FROM users WHERE email = ?;")
statement.setString(1, email)
val resultSet: ResultSet = statement.executeQuery()
while(resultSet.next()) {
userId = resultSet.getString("id")
statement.close()
dbconnection.close()
return@withContext userId
}
}
Joffrey
02/09/2025, 10:30 AMuse
is more correct (at least at a quick glance, I didn't dissect the code).Joffrey
02/09/2025, 10:31 AMuserId
so far from its usage in the second snippet?Hong Phuc
02/09/2025, 12:07 PMuserId
was there actuallyHong Phuc
02/09/2025, 12:10 PMuse
affect the testability of the code, since one statement flow into another, I wouldn’t be able to test them separately?Joffrey
02/09/2025, 12:38 PMuse
will look the same as with finally
Hong Phuc
02/09/2025, 12:59 PM