Skip to content

Commit 163a314

Browse files
committed
added tableTypes parameter to readAllSqlTables instead, with a helpful error when no tables are found, hinting at the tableTypes argument and what the database supports
1 parent 7be2575 commit 163a314

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

dataframe-jdbc/api/dataframe-jdbc.api

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public final class org/jetbrains/kotlinx/dataframe/io/ReadJdbcKt {
5050
public static final fun getSchemaForSqlTable (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Lorg/jetbrains/kotlinx/dataframe/io/DbConnectionConfig;Ljava/lang/String;Lorg/jetbrains/kotlinx/dataframe/io/db/DbType;)Lorg/jetbrains/kotlinx/dataframe/schema/DataFrameSchema;
5151
public static synthetic fun getSchemaForSqlTable$default (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Ljava/sql/Connection;Ljava/lang/String;Lorg/jetbrains/kotlinx/dataframe/io/db/DbType;ILjava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/schema/DataFrameSchema;
5252
public static synthetic fun getSchemaForSqlTable$default (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Lorg/jetbrains/kotlinx/dataframe/io/DbConnectionConfig;Ljava/lang/String;Lorg/jetbrains/kotlinx/dataframe/io/db/DbType;ILjava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/schema/DataFrameSchema;
53-
public static final fun readAllSqlTables (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Ljava/sql/Connection;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;)Ljava/util/Map;
54-
public static final fun readAllSqlTables (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Lorg/jetbrains/kotlinx/dataframe/io/DbConnectionConfig;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;)Ljava/util/Map;
55-
public static synthetic fun readAllSqlTables$default (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Ljava/sql/Connection;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;ILjava/lang/Object;)Ljava/util/Map;
56-
public static synthetic fun readAllSqlTables$default (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Lorg/jetbrains/kotlinx/dataframe/io/DbConnectionConfig;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;ILjava/lang/Object;)Ljava/util/Map;
53+
public static final fun readAllSqlTables (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Ljava/sql/Connection;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;Ljava/util/List;)Ljava/util/Map;
54+
public static final fun readAllSqlTables (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Lorg/jetbrains/kotlinx/dataframe/io/DbConnectionConfig;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;Ljava/util/List;)Ljava/util/Map;
55+
public static synthetic fun readAllSqlTables$default (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Ljava/sql/Connection;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;Ljava/util/List;ILjava/lang/Object;)Ljava/util/Map;
56+
public static synthetic fun readAllSqlTables$default (Lorg/jetbrains/kotlinx/dataframe/DataFrame$Companion;Lorg/jetbrains/kotlinx/dataframe/io/DbConnectionConfig;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;Ljava/util/List;ILjava/lang/Object;)Ljava/util/Map;
5757
public static final fun readDataFrame (Ljava/sql/Connection;Ljava/lang/String;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;Z)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
5858
public static final fun readDataFrame (Ljava/sql/ResultSet;Ljava/sql/Connection;IZLorg/jetbrains/kotlinx/dataframe/io/db/DbType;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
5959
public static final fun readDataFrame (Ljava/sql/ResultSet;Lorg/jetbrains/kotlinx/dataframe/io/db/DbType;IZ)Lorg/jetbrains/kotlinx/dataframe/DataFrame;

dataframe-jdbc/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/readJdbc.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ public fun ResultSet.readDataFrame(
604604
* @param [inferNullability] indicates how the column nullability should be inferred.
605605
* @param [dbType] the type of database, could be a custom object, provided by user, optional, default is `null`,
606606
* in that case the [dbType] will be recognized from the [dbConfig].
607+
* @param [tableTypes] an optional list of table types, which must be from the list of table types
608+
* returned from [DatabaseMetaData.getTableTypes]; `null` returns all types. By default, it's `["TABLE"]`.
607609
* @return a map of [String] to [AnyFrame] objects representing the non-system tables from the database.
608610
*/
609611
public fun DataFrame.Companion.readAllSqlTables(
@@ -612,9 +614,10 @@ public fun DataFrame.Companion.readAllSqlTables(
612614
limit: Int = DEFAULT_LIMIT,
613615
inferNullability: Boolean = true,
614616
dbType: DbType? = null,
617+
tableTypes: List<String>? = listOf("TABLE"),
615618
): Map<String, AnyFrame> {
616619
DriverManager.getConnection(dbConfig.url, dbConfig.user, dbConfig.password).use { connection ->
617-
return readAllSqlTables(connection, catalogue, limit, inferNullability, dbType)
620+
return readAllSqlTables(connection, catalogue, limit, inferNullability, dbType, tableTypes)
618621
}
619622
}
620623

@@ -628,6 +631,8 @@ public fun DataFrame.Companion.readAllSqlTables(
628631
* @param [inferNullability] indicates how the column nullability should be inferred.
629632
* @param [dbType] the type of database, could be a custom object, provided by user, optional, default is `null`,
630633
* in that case the [dbType] will be recognized from the [connection].
634+
* @param [tableTypes] an optional list of table types, which must be from the list of table types
635+
* returned from [DatabaseMetaData.getTableTypes]; `null` returns all types. By default, it's `["TABLE"]`.
631636
* @return a map of [String] to [AnyFrame] objects representing the non-system tables from the database.
632637
*
633638
* @see DriverManager.getConnection
@@ -638,12 +643,13 @@ public fun DataFrame.Companion.readAllSqlTables(
638643
limit: Int = DEFAULT_LIMIT,
639644
inferNullability: Boolean = true,
640645
dbType: DbType? = null,
646+
tableTypes: List<String>? = listOf("TABLE"),
641647
): Map<String, AnyFrame> {
642648
val metaData = connection.metaData
643649
val determinedDbType = dbType ?: extractDBTypeFromConnection(connection)
644650

645-
// exclude a system and other tables without data, but it looks like it is supported badly for many databases
646-
val tables = metaData.getTables(catalogue, null, null, null)
651+
// exclude system- and other tables without data, but it looks like it is supported badly for many databases
652+
val tables = metaData.getTables(catalogue, null, null, tableTypes?.toTypedArray())
647653

648654
val dataFrames = mutableMapOf<String, AnyFrame>()
649655

@@ -668,6 +674,27 @@ public fun DataFrame.Companion.readAllSqlTables(
668674
}
669675
}
670676

677+
// We may have no tables or filtered for the wrong table type, let's give a helpful error message
678+
if (dataFrames.isEmpty()) {
679+
val supportedTableTypes = metaData.tableTypes.let {
680+
buildList { while (it.next()) add(it.getString("TABLE_TYPE")) }
681+
}
682+
if (tableTypes?.any { it !in supportedTableTypes } == true) {
683+
val unsupportedTypes = tableTypes.filter { it !in supportedTableTypes }
684+
throw IllegalArgumentException(
685+
buildString {
686+
appendLine("Found no tables with type(s) $tableTypes.")
687+
if (unsupportedTypes.isNotEmpty()) {
688+
appendLine(
689+
"Table type(s) $unsupportedTypes are unsupported for ${determinedDbType::class.simpleName}.",
690+
)
691+
}
692+
appendLine("If this is unexpected, try adjusting `tableTypes=` to any of $supportedTableTypes.")
693+
},
694+
)
695+
}
696+
}
697+
671698
return dataFrames
672699
}
673700

0 commit comments

Comments
 (0)