Skip to content

Commit

Permalink
添加生成insert sql参数列表字符串的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Aug 23, 2016
1 parent 8260a86 commit 7c0bd39
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
40 changes: 28 additions & 12 deletions src/main/java/cn/binarywang/tools/generator/InsertSQLGenerator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.binarywang.tools.generator;

import static com.google.common.collect.Collections2.transform;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
Expand All @@ -9,6 +11,8 @@
import java.util.Collections;
import java.util.List;

import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

Expand All @@ -32,33 +36,45 @@ public InsertSQLGenerator(String url, String username, String password,
}

public String generateSQL() {
List<String> columns = getColumns();

return String.format("insert into %s(%s) values(%s)", this.tableName,
COMMA_JOINER.join(columns),
COMMA_JOINER.join(Collections.nCopies(columns.size(), "?")));
}

public String generateParams() {
return COMMA_JOINER
.join(transform(getColumns(), new Function<String, String>() {

@Override
public String apply(String input) {
return "abc.get" + CaseFormat.LOWER_UNDERSCORE
.to(CaseFormat.UPPER_CAMEL, input) + "()";
}
}));
}

private List<String> getColumns() {
List<String> columns = Lists.newArrayList();
try (PreparedStatement ps = this.con
.prepareStatement("select * from " + this.tableName);
ResultSet rs = ps.executeQuery();) {

ResultSetMetaData rsm = rs.getMetaData();
List<String> columns = Lists.newArrayList();
for (int i = 1; i <= rsm.getColumnCount(); i++) {
String columnName = rsm.getColumnName(i);
String dbType = rsm.getColumnTypeName(i);

System.out.print("Name: " + columnName);
System.out.println(", DBType : " + dbType);
System.out.println(", Type : " + rsm.getColumnClassName(i));
columns.add(columnName);
}

return String.format("insert into %s(%s) values(%s)",
this.tableName, COMMA_JOINER.join(columns),
COMMA_JOINER.join(Collections.nCopies(columns.size(), "?")));
} catch (SQLException e) {
e.printStackTrace();
return null;
}

return null;
}

public String generateParams() {
return null;
return columns;
}

public void close() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.binarywang.tools.generator;

import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Guice;
Expand Down Expand Up @@ -64,7 +63,7 @@ public void testGenerateSQL() {
* .
*/
public void testGenerateParams() {
Assert.fail("Not yet implemented");
System.err.println(this.generator.generateParams());
}

}

0 comments on commit 7c0bd39

Please sign in to comment.