Skip to content

Commit

Permalink
Merge branch 'release/v0.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
gazer2kanlin committed Sep 29, 2018
2 parents 01dd1cf + 471c141 commit 74f4fc9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 20 deletions.
2 changes: 1 addition & 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.utils</groupId>
<artifactId>uia-utils-ext</artifactId>
<version>0.2.0</version>
<version>0.2.2</version>
<packaging>jar</packaging>
<name>uia-utils-ext</name>
<url>https://github.com/gazer2kanlin/uia.utils4j.ext</url>
Expand Down
51 changes: 37 additions & 14 deletions src/main/java/uia/utils/dao/JavaClassPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ public class JavaClassPrinter {

private final String templateDAOTable;

private final String templateDAOTable2;

private final String templateDAOView;

public JavaClassPrinter(Database db, String tableName) throws IOException, SQLException {
this.templateDTO = readContent(JavaClassPrinter.class.getResourceAsStream("dto.template.txt"));
this.templateDAOTable = readContent(JavaClassPrinter.class.getResourceAsStream("dao_table.template.txt"));
this.templateDAOTable2 = readContent(JavaClassPrinter.class.getResourceAsStream("dao_table.template2.txt"));
this.templateDAOView = readContent(JavaClassPrinter.class.getResourceAsStream("dao_view.template.txt"));
this.table = db.selectTable(tableName, true);
}

public JavaClassPrinter(TableType table) throws IOException {
this.templateDTO = readContent(JavaClassPrinter.class.getResourceAsStream("dto.template.txt"));
this.templateDAOTable = readContent(JavaClassPrinter.class.getResourceAsStream("dao_table.template.txt"));
this.templateDAOTable2 = readContent(JavaClassPrinter.class.getResourceAsStream("dao_table.template2.txt"));
this.templateDAOView = readContent(JavaClassPrinter.class.getResourceAsStream("dao_view.template.txt"));
this.table = table;
}
Expand Down Expand Up @@ -129,20 +133,39 @@ public String generateDAO(String daoPackageName, String dtoPackageName, String d
pkWhere.add(pk.get(i).getColumnName().toLowerCase() + "=?");
}

return this.templateDAOTable
.replace("{TABLE_NAME}", this.table.getTableName().toLowerCase())
.replace("{DAO_PACKAGE}", daoPackageName)
.replace("{DTO_PACKAGE}", dtoPackageName)
.replace("{DTO}", dtoName)
.replace("{SQL_INS}", this.table.generateInsertSQL())
.replace("{SQL_UPD}", this.table.generateUpdateSQL())
.replace("{SQL_SEL}", this.table.generateSelectSQL())
.replace("{CODE_INS}", codeIns.toString())
.replace("{CODE_UPD}", codeUpd.toString())
.replace("{CODE_SEL_PK_ARGS}", String.join(", ", codeSelPkArgs))
.replace("{WHERE_PK}", String.join(" AND ", pkWhere))
.replace("{CODE_SEL_PK}", codeSelPk.toString())
.replace("{CODE_CONVERT}", codeConvert.toString());
String updateSQL = this.table.generateUpdateSQL();

if (updateSQL != null) {
return this.templateDAOTable
.replace("{TABLE_NAME}", this.table.getTableName().toLowerCase())
.replace("{DAO_PACKAGE}", daoPackageName)
.replace("{DTO_PACKAGE}", dtoPackageName)
.replace("{DTO}", dtoName)
.replace("{SQL_INS}", this.table.generateInsertSQL())
.replace("{SQL_UPD}", updateSQL)
.replace("{SQL_SEL}", this.table.generateSelectSQL())
.replace("{CODE_INS}", codeIns.toString())
.replace("{CODE_UPD}", codeUpd.toString())
.replace("{CODE_SEL_PK_ARGS}", String.join(", ", codeSelPkArgs))
.replace("{WHERE_PK}", String.join(" AND ", pkWhere))
.replace("{CODE_SEL_PK}", codeSelPk.toString())
.replace("{CODE_CONVERT}", codeConvert.toString());
}
else {
return this.templateDAOTable2
.replace("{TABLE_NAME}", this.table.getTableName().toLowerCase())
.replace("{DAO_PACKAGE}", daoPackageName)
.replace("{DTO_PACKAGE}", dtoPackageName)
.replace("{DTO}", dtoName)
.replace("{SQL_INS}", this.table.generateInsertSQL())
.replace("{SQL_SEL}", this.table.generateSelectSQL())
.replace("{CODE_INS}", codeIns.toString())
.replace("{CODE_UPD}", codeUpd.toString())
.replace("{CODE_SEL_PK_ARGS}", String.join(", ", codeSelPkArgs))
.replace("{WHERE_PK}", String.join(" AND ", pkWhere))
.replace("{CODE_SEL_PK}", codeSelPk.toString())
.replace("{CODE_CONVERT}", codeConvert.toString());
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/uia/utils/dao/TableType.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public String generateUpdateSQL() {
}
}

if (cs.isEmpty()) {
return null;
}

return String.format("UPDATE %s SET %s WHERE %s",
this.tableName.toLowerCase(),
String.join(",", cs),
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/uia/utils/dao/hana/Hana.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ public class Hana extends AbstractDatabase {
e.printStackTrace();
}
}

public Hana() throws SQLException {
super(null, null, null, null, null);
}

public Hana(String host, String port, String service, String user, String pwd) throws SQLException {
public Hana(String host, String port, String schema, String user, String pwd) throws SQLException {
// jdbc:sap://host:port?reconnect=true
super("com.sap.db.jdbc.Driver", "jdbc:sap://" + host + ":" + port, user, pwd, user);
super("com.sap.db.jdbc.Driver", "jdbc:sap://" + host + ":" + port, user, pwd, schema == null ? user : schema);
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/uia/utils/dao/ora/Oracle.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class Oracle extends AbstractDatabase {
catch (Exception e) {
e.printStackTrace();
}
}

public Oracle() throws SQLException {
super(null, null, null, null, null);
}

public Oracle(String host, String port, String service, String user, String pwd) throws SQLException {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/uia/utils/dao/pg/PostgreSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public class PostgreSQL extends AbstractDatabase {
}
}

public PostgreSQL() throws SQLException {
super(null, null, null, null, null);
}

public PostgreSQL(String host, String port, String service, String user, String pwd) throws SQLException {
// jdbc:postgresql://host:port/database
super("org.postgresql.Driver", "jdbc:postgresql://" + host + ":" + port + "/" + service, user, pwd, "public");
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/uia/utils/dao/HanaSQLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@

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.1.52", "39015", null, "WIP", "Hdb12345");
Database db = new Hana("10.160.2.23", "31015", "WIP_ARCHIVE", "WIP_ARCHIVE", "Sap54321");
db.selectTableNames().forEach(t -> System.out.println(t));
db.close();
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/uia/utils/dao/JavaClassPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class JavaClassPrinterTest {

@Test
public void testGenerateTable() throws Exception {
JavaClassPrinter.Result result = new JavaClassPrinter(createDB(), "chamber_param")
.generate("uia.pie.db.dao", "uia.pie.db", "ChamberParam");
JavaClassPrinter.Result result = new JavaClassPrinter(createDB(), "dcp_tile_prop")
.generate("uia.pie.db.dao", "uia.pie.db", "DcpTileProp");
System.out.println("=========================");
System.out.println(result.dto);
System.out.println("=========================");
Expand Down

0 comments on commit 74f4fc9

Please sign in to comment.