From 5910e7cb5a64a44c6efc16780844af60f35420ed Mon Sep 17 00:00:00 2001 From: "Kyle K. Lin" Date: Thu, 17 Oct 2019 11:01:46 +0800 Subject: [PATCH] 0.1.1 output selected tables and views --- pom.xml | 14 +++++++- src/main/java/uia/dao/DatabaseTool.java | 31 ++++++++++++----- src/test/java/uia/dao/hana/HanaSQLTest.java | 38 +++++++++------------ 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index 8fc1fca..b87d54c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.uia.solution uia-dao - 0.1.0 + 0.1.1 jar uia-dao https://github.com/uia4j/uia-dao @@ -41,6 +41,18 @@ 3.28.0 provided + + ngdbc + ngdbc + 2.1.2 + provided + + + ojdbc6 + ojdbc6 + 11.2.0.4 + provided + ${project.artifactId}-${project.version} diff --git a/src/main/java/uia/dao/DatabaseTool.java b/src/main/java/uia/dao/DatabaseTool.java index d670180..17a9005 100644 --- a/src/main/java/uia/dao/DatabaseTool.java +++ b/src/main/java/uia/dao/DatabaseTool.java @@ -22,6 +22,8 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; /** * Database tool. @@ -46,11 +48,12 @@ public DatabaseTool(Database source) { * Output a script to generate all tables. * * @param file The file name. + * @param tableNames Names of tables. * @throws SQLException Failed to execute SQL statements. * @throws IOException Failed to save files. */ - public void toTableScript(String file) throws IOException, SQLException { - toTableScript(file, this.source); + public void toTableScript(String file, String... tableNames) throws IOException, SQLException { + toTableScript(file, this.source, tableNames); } /** @@ -58,12 +61,17 @@ public void toTableScript(String file) throws IOException, SQLException { * * @param file The file name. * @param target The database the script is executed on. + * @param tableNames Names of tables. * @throws SQLException Failed to execute SQL statements. * @throws IOException Failed to save files. */ - public void toTableScript(String file, Database target) throws IOException, SQLException { + public void toTableScript(String file, Database target, String... tableNames) throws IOException, SQLException { StringBuilder scripts = new StringBuilder(); - for (String t : this.source.selectTableNames()) { + List ts = Arrays.asList(tableNames); + if (ts.isEmpty()) { + ts = this.source.selectTableNames(); + } + for (String t : ts) { String sql1 = target.generateCreateTableSQL(this.source.selectTable(t, false)); scripts.append(sql1).append(";\n\n"); } @@ -74,11 +82,12 @@ public void toTableScript(String file, Database target) throws IOException, SQLE * Output a script to generate all view. * * @param file The file name. + * @param viewNames Names of views. * @throws SQLException Failed to execute SQL statements. * @throws IOException Failed to save files. */ - public void toViewScript(String file) throws IOException, SQLException { - toViewScript(file, this.source); + public void toViewScript(String file, String... viewNames) throws IOException, SQLException { + toViewScript(file, this.source, viewNames); } /** @@ -86,12 +95,18 @@ public void toViewScript(String file) throws IOException, SQLException { * * @param file The file name. * @param target The database the script is executed on. + * @param viewNames Names of views. * @throws SQLException Failed to execute SQL statements. * @throws IOException Failed to save files. */ - public void toViewScript(String file, Database target) throws IOException, SQLException { + public void toViewScript(String file, Database target, String... viewNames) throws IOException, SQLException { + List vs = Arrays.asList(viewNames); + if (vs.isEmpty()) { + vs = this.source.selectViewNames(); + } + StringBuilder scripts = new StringBuilder(); - for (String v : this.source.selectViewNames()) { + for (String v : vs) { String sql = this.source.selectViewScript(v); String script = target.generateCreateViewSQL(v, sql); scripts.append(script).append(";\n\n"); diff --git a/src/test/java/uia/dao/hana/HanaSQLTest.java b/src/test/java/uia/dao/hana/HanaSQLTest.java index d8ef139..4b964fd 100644 --- a/src/test/java/uia/dao/hana/HanaSQLTest.java +++ b/src/test/java/uia/dao/hana/HanaSQLTest.java @@ -18,13 +18,13 @@ *******************************************************************************/ package uia.dao.hana; +import java.sql.SQLException; import java.util.List; import org.junit.Test; import uia.dao.Database; import uia.dao.TableType; -import uia.dao.hana.Hana; import uia.dao.ora.Oracle; import uia.dao.pg.PostgreSQL; @@ -35,30 +35,23 @@ */ public class HanaSQLTest { - @Test - public void testConn() throws Exception { - Database db = new Hana("10.160.2.23", "31015", null, "WIP_ARCHIVE", "Sap54321"); - db.close(); - } - @Test public void testSelectTableNames() throws Exception { - Database db = new Hana("10.160.2.23", "31015", "WIP_ARCHIVE", "WIP_ARCHIVE", "Sap54321"); - db.selectTableNames().forEach(t -> System.out.println(t)); + Database db = db(); + db.selectTableNames().forEach(System.out::println); db.close(); } @Test public void testSelectViewNames() throws Exception { - Database db = new Hana("10.160.1.52", "39015", null, "WIP", "Hdb12345"); - db.selectViewNames("VIEW_").forEach(t -> System.out.println(t)); + Database db = db(); + db.selectViewNames("VIEW_").forEach(System.out::println); db.close(); } @Test public void testSelectTable() throws Exception { - Database db = new Hana("10.160.1.52", "39015", null, "WIP", "Hdb12345"); - + Database db = db(); TableType table = db.selectTable("ZD_TEST", true); System.out.println(table.getTableName()); table.getColumns().forEach(System.out::println); @@ -71,8 +64,7 @@ public void testSelectTable() throws Exception { @Test public void testSelectView() throws Exception { - Database db = new Hana("10.160.1.52", "39015", null, "WIP", "Hdb12345"); - + Database db = db(); TableType table = db.selectTable("VIEW_DISPATCH_SFC", false); System.out.println(table.getTableName()); table.getColumns().forEach(System.out::println); @@ -83,26 +75,26 @@ public void testSelectView() throws Exception { @Test public void testSelectViewScript() throws Exception { - Database db = new Hana("10.160.1.52", "39015", null, "WIP", "Hdb12345"); + Database db = db(); System.out.println(db.selectViewScript("VIEW_DISPATCH_SFC")); db.close(); } @Test public void testGenerateCreateTableSQL() throws Exception { - Database db = new Hana("10.160.1.52", "39015", null, "WIP", "Hdb12345"); - TableType table = db.selectTable("ZR_CARRIER_CLEAN", false); + Database db = db(); + TableType table = db.selectTable("SHOP_ORDER", false); System.out.println("=== Hana ==="); System.out.println(db.generateCreateTableSQL(table)); System.out.println("=== Oracle ==="); - try (Database ora = new Oracle("WIP", null, null, null, null)) { + try (Database ora = new Oracle()) { System.out.println(ora.generateCreateTableSQL(table)); } System.out.println("=== PogtgreSQL ==="); - try (PostgreSQL pg = new PostgreSQL("public", null, null, null, null)) { + try (PostgreSQL pg = new PostgreSQL()) { System.out.println(pg.generateCreateTableSQL(table)); } @@ -111,7 +103,7 @@ public void testGenerateCreateTableSQL() throws Exception { @Test public void testCase1() throws Exception { - Database db = new Hana("10.160.2.23", "31015", null, "WIP", "Sap12345"); + Database db = db(); List tns = db.selectTableNames("Z_"); for (String tn : tns) { TableType table = db.selectTable(tn, false); @@ -120,4 +112,8 @@ public void testCase1() throws Exception { db.close(); } + + private Hana db() throws SQLException { + return new Hana("192.168.137.245", "39015", null, "WIP", "Sap12345"); + } }