Skip to content

Commit

Permalink
0.1.1
Browse files Browse the repository at this point in the history
output selected tables and views
  • Loading branch information
gazer2kanlin committed Oct 17, 2019
1 parent 206c7a1 commit 5910e7c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.uia.solution</groupId>
<artifactId>uia-dao</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<packaging>jar</packaging>
<name>uia-dao</name>
<url>https://github.com/uia4j/uia-dao</url>
Expand Down Expand Up @@ -41,6 +41,18 @@
<version>3.28.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ngdbc</groupId>
<artifactId>ngdbc</artifactId>
<version>2.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ojdbc6</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
Expand Down
31 changes: 23 additions & 8 deletions src/main/java/uia/dao/DatabaseTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -46,24 +48,30 @@ 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);
}

/**
* Output a script to generate all tables.
*
* @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<String> 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");
}
Expand All @@ -74,24 +82,31 @@ 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);
}

/**
* Output a script to generate all tables.
*
* @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<String> 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");
Expand Down
38 changes: 17 additions & 21 deletions src/test/java/uia/dao/hana/HanaSQLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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));
}

Expand All @@ -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<String> tns = db.selectTableNames("Z_");
for (String tn : tns) {
TableType table = db.selectTable(tn, false);
Expand All @@ -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");
}
}

0 comments on commit 5910e7c

Please sign in to comment.