|
| 1 | +package org.embulk.output.databricks; |
| 2 | + |
| 3 | +import static java.lang.String.format; |
| 4 | +import static org.embulk.output.databricks.util.ConnectionUtil.*; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.DatabaseMetaData; |
| 9 | +import java.sql.ResultSet; |
| 10 | +import java.sql.SQLException; |
| 11 | +import org.embulk.output.databricks.util.ConfigUtil; |
| 12 | +import org.embulk.output.jdbc.JdbcUtils; |
| 13 | +import org.junit.After; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +// The purpose of this class is to understand the behavior of DatabaseMetadata, |
| 18 | +// so if this test fails due to a library update, please change the test result. |
| 19 | +public class TestDatabaseMetadata { |
| 20 | + private DatabaseMetaData dbm; |
| 21 | + private Connection conn; |
| 22 | + |
| 23 | + ConfigUtil.TestTask t = ConfigUtil.createTestTask(); |
| 24 | + String catalog = t.getCatalogName(); |
| 25 | + String schema = t.getSchemaName(); |
| 26 | + String table = t.getTablePrefix() + "_test"; |
| 27 | + String nonAsciiCatalog = t.getNonAsciiCatalogName(); |
| 28 | + String nonAsciiSchema = t.getNonAsciiSchemaName(); |
| 29 | + String nonAsciiTable = t.getTablePrefix() + "_テスト"; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setup() throws SQLException, ClassNotFoundException { |
| 33 | + conn = connectByTestTask(); |
| 34 | + dbm = conn.getMetaData(); |
| 35 | + run(conn, "USE CATALOG " + catalog); |
| 36 | + run(conn, "USE SCHEMA " + schema); |
| 37 | + createTables(); |
| 38 | + } |
| 39 | + |
| 40 | + @After |
| 41 | + public void cleanup() { |
| 42 | + try { |
| 43 | + conn.close(); |
| 44 | + } catch (SQLException ignored) { |
| 45 | + |
| 46 | + } |
| 47 | + dropAllTemporaryTables(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testGetPrimaryKeys() throws SQLException { |
| 52 | + assertEquals(1, countPrimaryKeys(catalog, schema, table, "a0")); |
| 53 | + assertEquals(1, countPrimaryKeys(null, schema, table, "a0")); |
| 54 | + assertEquals(1, countPrimaryKeys(nonAsciiCatalog, nonAsciiSchema, nonAsciiTable, "h0")); |
| 55 | + assertEquals(1, countPrimaryKeys(null, nonAsciiSchema, nonAsciiTable, "d0")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testGetTables() throws SQLException { |
| 60 | + assertEquals(1, countTablesResult(catalog, schema, table)); |
| 61 | + assertEquals(2, countTablesResult(null, schema, table)); |
| 62 | + assertEquals(1, countTablesResult(nonAsciiCatalog, nonAsciiSchema, nonAsciiTable)); |
| 63 | + assertEquals(0, countTablesResult(null, nonAsciiSchema, nonAsciiTable)); // expected 2 |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testGetColumns() throws SQLException { |
| 68 | + assertEquals(2, countColumnsResult(catalog, schema, table)); |
| 69 | + assertEquals(4, countColumnsResult(null, schema, table)); |
| 70 | + assertEquals(2, countColumnsResult(nonAsciiCatalog, nonAsciiSchema, nonAsciiTable)); |
| 71 | + assertEquals(0, countColumnsResult(null, nonAsciiSchema, nonAsciiTable)); // expected 2 |
| 72 | + } |
| 73 | + |
| 74 | + private void createTables() { |
| 75 | + String queryFormat = |
| 76 | + "CREATE TABLE IF NOT EXISTS `%s`.`%s`.`%s` (%s String PRIMARY KEY, %s INTEGER)"; |
| 77 | + run(conn, format(queryFormat, catalog, schema, table, "a0", "a1")); |
| 78 | + run(conn, format(queryFormat, catalog, schema, nonAsciiTable, "b0", "b1")); |
| 79 | + run(conn, format(queryFormat, catalog, nonAsciiSchema, table, "c0", "c1")); |
| 80 | + run(conn, format(queryFormat, catalog, nonAsciiSchema, nonAsciiTable, "d0", "d1")); |
| 81 | + run(conn, format(queryFormat, nonAsciiCatalog, schema, table, "e0", "e1")); |
| 82 | + run(conn, format(queryFormat, nonAsciiCatalog, schema, nonAsciiTable, "f0", "f1")); |
| 83 | + run(conn, format(queryFormat, nonAsciiCatalog, nonAsciiSchema, table, "g0", "g1")); |
| 84 | + run(conn, format(queryFormat, nonAsciiCatalog, nonAsciiSchema, nonAsciiTable, "h0", "h1")); |
| 85 | + } |
| 86 | + |
| 87 | + private int countPrimaryKeys( |
| 88 | + String catalogName, String schemaName, String tableName, String primaryKey) |
| 89 | + throws SQLException { |
| 90 | + try (ResultSet rs = dbm.getPrimaryKeys(catalogName, schemaName, tableName)) { |
| 91 | + int count = 0; |
| 92 | + while (rs.next()) { |
| 93 | + String columnName = rs.getString("COLUMN_NAME"); |
| 94 | + assertEquals(primaryKey, columnName); |
| 95 | + count += 1; |
| 96 | + } |
| 97 | + return count; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private int countTablesResult(String catalogName, String schemaName, String tableName) |
| 102 | + throws SQLException { |
| 103 | + String e = dbm.getSearchStringEscape(); |
| 104 | + String c = JdbcUtils.escapeSearchString(catalogName, e); |
| 105 | + String s = JdbcUtils.escapeSearchString(schemaName, e); |
| 106 | + String t = JdbcUtils.escapeSearchString(tableName, e); |
| 107 | + try (ResultSet rs = dbm.getTables(c, s, t, null)) { |
| 108 | + return countResultSet(rs); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private int countColumnsResult(String catalogName, String schemaName, String tableName) |
| 113 | + throws SQLException { |
| 114 | + String e = dbm.getSearchStringEscape(); |
| 115 | + String c = JdbcUtils.escapeSearchString(catalogName, e); |
| 116 | + String s = JdbcUtils.escapeSearchString(schemaName, e); |
| 117 | + String t = JdbcUtils.escapeSearchString(tableName, e); |
| 118 | + try (ResultSet rs = dbm.getColumns(c, s, t, null)) { |
| 119 | + return countResultSet(rs); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private int countResultSet(ResultSet rs) throws SQLException { |
| 124 | + int count = 0; |
| 125 | + while (rs.next()) { |
| 126 | + count += 1; |
| 127 | + } |
| 128 | + return count; |
| 129 | + } |
| 130 | +} |
0 commit comments