diff --git a/exposed-core/api/exposed-core.api b/exposed-core/api/exposed-core.api index fa6f0174e8..2375ee2ba3 100644 --- a/exposed-core/api/exposed-core.api +++ b/exposed-core/api/exposed-core.api @@ -3574,6 +3574,7 @@ public abstract class org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMe public abstract fun sequences ()Ljava/util/List; public abstract fun tableConstraints (Ljava/util/List;)Ljava/util/Map; public abstract fun tableNamesByCurrentSchema (Ljava/util/Map;)Lorg/jetbrains/exposed/sql/vendors/SchemaMetadata; + public abstract fun tableNamesForAllSchemas ()Ljava/util/List; } public abstract class org/jetbrains/exposed/sql/statements/api/ExposedSavepoint { diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMetadata.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMetadata.kt index 78271a0e70..817eacb25a 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMetadata.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMetadata.kt @@ -55,6 +55,8 @@ abstract class ExposedDatabaseMetadata(val database: String) { */ abstract fun tableNamesByCurrentSchema(tableNamesCache: Map>?): SchemaMetadata + abstract fun tableNamesForAllSchemas(): List + /** Returns a map with the [ColumnMetadata] of all the defined columns in each of the specified [tables]. */ abstract fun columns(vararg tables: Table): Map> diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/VendorDialect.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/VendorDialect.kt index 4d3c979a82..0fe4c3cbad 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/VendorDialect.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/VendorDialect.kt @@ -32,17 +32,15 @@ abstract class VendorDialect( } protected fun getAllTableNamesCache(): Map> { - val connection = TransactionManager.current().connection if (_allTableNames == null) { - _allTableNames = connection.metadata { tableNames } + _allTableNames = TransactionManager.current().connection.metadata { tableNames } } return _allTableNames!! } private fun getAllSchemaNamesCache(): List { - val connection = TransactionManager.current().connection if (_allSchemaNames == null) { - _allSchemaNames = connection.metadata { schemaNames } + _allSchemaNames = TransactionManager.current().connection.metadata { schemaNames } } return _allSchemaNames!! } @@ -53,7 +51,7 @@ abstract class VendorDialect( /** Returns a list with the names of all the defined tables with schema prefixes if the database supports it. */ override fun allTablesNames(): List = TransactionManager.current().connection.metadata { - getAllTableNamesCache().flatMap { it.value } + tableNamesForAllSchemas() } override fun tableExists(table: Table): Boolean { diff --git a/exposed-jdbc/api/exposed-jdbc.api b/exposed-jdbc/api/exposed-jdbc.api index 5ee212d776..6724deac1c 100644 --- a/exposed-jdbc/api/exposed-jdbc.api +++ b/exposed-jdbc/api/exposed-jdbc.api @@ -55,6 +55,7 @@ public final class org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadat public fun sequences ()Ljava/util/List; public fun tableConstraints (Ljava/util/List;)Ljava/util/Map; public fun tableNamesByCurrentSchema (Ljava/util/Map;)Lorg/jetbrains/exposed/sql/vendors/SchemaMetadata; + public fun tableNamesForAllSchemas ()Ljava/util/List; } public final class org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl$Companion { diff --git a/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt b/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt index e8d8a7d34b..6390dc6db9 100644 --- a/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt +++ b/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt @@ -131,6 +131,9 @@ class JdbcDatabaseMetadataImpl(database: String, val metadata: DatabaseMetaData) return SchemaMetadata(currentSchema!!, tablesInSchema) } + override fun tableNamesForAllSchemas(): List = + schemaNames.flatMap { tableNamesFor(it) } + private fun ResultSet.extractColumns(): List { val result = mutableListOf() while (next()) { diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt index 80f1d5c9b0..12d27a13e0 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt @@ -13,6 +13,7 @@ import org.jetbrains.exposed.sql.tests.shared.Category import org.jetbrains.exposed.sql.tests.shared.Item import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections import org.jetbrains.exposed.sql.tests.shared.assertEquals +import org.jetbrains.exposed.sql.tests.shared.assertFalse import org.jetbrains.exposed.sql.tests.shared.assertTrue import org.jetbrains.exposed.sql.transactions.TransactionManager import org.jetbrains.exposed.sql.vendors.MysqlDialect @@ -666,4 +667,23 @@ class CreateTableTests : DatabaseTestsBase() { } } } + + @Test + fun testListTablesDoesNotCloseDatabaseConnection() { + val tester = object : IntIdTable("tester") { + val int = integer("intColumn") + } + withDb { testDb -> + val defaultSchemaName = when (currentDialectTest) { + is SQLServerDialect -> "dbo" + is OracleDialect -> testDb.user + is MysqlDialect -> testDb.db!!.name + else -> "public" + } + assertTrue(SchemaUtils.listTables().none { it.equals("$defaultSchemaName.${OneTable.tableName}", ignoreCase = true) }) + } + withDb { + assertFalse(tester.exists()) + } + } }