Commit 793adda4 authored by 张永's avatar 张永

加视图

parent 45502daa
package com.egolm.common.jdbc.dialect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 数据库工具类
*
* @author onedear
* @data:2010-10-21 下午06:12:39
*/
public class SqlServerTo {
private String root;
private String pkg_name;
private String author;
private Connection conn;
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
public SqlServerTo(String root, String pkg_name, String author, String db_host, String db_name, String db_user, String db_pass) throws SQLException {
this.root = root;
this.pkg_name = pkg_name;
this.author = author;
this.conn = DriverManager.getConnection("jdbc:sqlserver://" + db_host + ";instanceName=SQLSERVER;DatabaseName=" + db_name, db_user, db_pass);
}
public void execute() {
try {
String sql = "SELECT D.name AS TABLE_NAME, 'REMARK' AS TABLE_COMMENT FROM sysobjects AS D WHERE D.XTYPE = 'U' AND D.NAME <> 'dtproperties'";
PreparedStatement pStemt = conn.prepareStatement(sql);
ResultSet set = pStemt.executeQuery();
while(set.next()) {
String name = set.getString("TABLE_NAME").trim();
String comment = set.getString("TABLE_COMMENT");
execute(name, comment);
}
conn.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void executeFilter(String sign) {
try {
String sql = "SELECT D.name AS TABLE_NAME, 'REMARK' AS TABLE_COMMENT FROM sysobjects AS D WHERE D.XTYPE = 'U' AND D.NAME <> 'dtproperties'";
PreparedStatement pStemt = conn.prepareStatement(sql);
ResultSet set = pStemt.executeQuery();
while(set.next()) {
String name = set.getString("TABLE_NAME").trim();
if(name.contains(sign)) {
String comment = set.getString("TABLE_COMMENT");
execute(name, comment);
}
}
conn.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void execute(String name, String comment) {
try {
ResultSet set = conn.prepareStatement("SELECT COL.NAME AS COLUMN_NAME, T.NAME AS COLUMN_TYPE, CAST(P.VALUE AS VARCHAR(999)) AS COLUMN_COMMENT, CASE WHEN EXISTS (SELECT 1 FROM dbo.sysindexes SI INNER JOIN dbo.sysindexkeys SIK ON SI.ID = SIK.ID AND SI.INDID = SIK.INDID INNER JOIN dbo.syscolumns SC ON SC.ID = SIK.ID AND SC.COLID = SIK.COLID INNER JOIN dbo.sysobjects SO ON SO.NAME = SI.NAME AND SO.XTYPE = 'PK' WHERE SC.ID = COL.ID AND SC.COLID = COL.COLID ) THEN 1 ELSE 0 END AS IS_PARAMY_KEY FROM syscolumns AS COL LEFT JOIN systypes T ON T.XUSERTYPE = COL.XTYPE LEFT JOIN sys.extended_properties P ON COL.ID = P.MAJOR_ID AND COL.COLID = P.MAJOR_ID WHERE COL.ID=OBJECT_ID('" + name + "')").executeQuery();
List<String> names = new ArrayList<String>();
List<String> types = new ArrayList<String>();
List<String> comments = new ArrayList<String>();
List<String> paramyKeys = new ArrayList<String>();
while(set.next()) {
String columnName = set.getString("COLUMN_NAME").trim();
String columnType = set.getString("COLUMN_TYPE").trim();
String columnComment = set.getString("COLUMN_COMMENT");
Integer isParamyKey = set.getInt("IS_PARAMY_KEY");
names.add(columnName);
types.add(columnType);
comments.add(columnComment);
if(isParamyKey == 1) {
paramyKeys.add(columnName);
}
}
if(!name.startsWith("sys") && !name.startsWith("SYS")) {
ReverseUtil.parseEntity(pkg_name, author, root, name, comment, paramyKeys, names, types, comments);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
package com.egolm.common.jdbc.dialect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 数据库工具类
*
* @author onedear
* @data:2010-10-21 下午06:12:39
*/
public class SqlServerTo {
private String root;
private String pkg_name;
private String author;
private Connection conn;
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
public SqlServerTo(String root, String pkg_name, String author, String db_host, String db_name, String db_user, String db_pass) throws SQLException {
this.root = root;
this.pkg_name = pkg_name;
this.author = author;
this.conn = DriverManager.getConnection("jdbc:sqlserver://" + db_host + ";instanceName=SQLSERVER;DatabaseName=" + db_name, db_user, db_pass);
}
public void execute() {
try {
String sql = "SELECT D.name AS TABLE_NAME, 'REMARK' AS TABLE_COMMENT FROM sysobjects AS D WHERE D.XTYPE = 'U' AND D.NAME <> 'dtproperties'";
PreparedStatement pStemt = conn.prepareStatement(sql);
ResultSet set = pStemt.executeQuery();
while(set.next()) {
String name = set.getString("TABLE_NAME").trim();
String comment = set.getString("TABLE_COMMENT");
execute(name, comment);
}
conn.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void executeView() {
try {
String sql = "SELECT TABLE_NAME , 'REMARK' AS TABLE_COMMENT FROM INFORMATION_SCHEMA.VIEWS";
PreparedStatement pStemt = conn.prepareStatement(sql);
ResultSet set = pStemt.executeQuery();
while(set.next()) {
String name = set.getString("TABLE_NAME").trim();
String comment = set.getString("TABLE_COMMENT");
execute(name, comment);
}
conn.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void executeFilter(String sign) {
try {
String sql = "SELECT D.name AS TABLE_NAME, 'REMARK' AS TABLE_COMMENT FROM sysobjects AS D WHERE D.XTYPE = 'U' AND D.NAME <> 'dtproperties'";
PreparedStatement pStemt = conn.prepareStatement(sql);
ResultSet set = pStemt.executeQuery();
while(set.next()) {
String name = set.getString("TABLE_NAME").trim();
if(name.contains(sign)) {
String comment = set.getString("TABLE_COMMENT");
execute(name, comment);
}
}
conn.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void execute(String name, String comment) {
try {
ResultSet set = conn.prepareStatement("SELECT COL.NAME AS COLUMN_NAME, T.NAME AS COLUMN_TYPE, CAST(P.VALUE AS VARCHAR(999)) AS COLUMN_COMMENT, CASE WHEN EXISTS (SELECT 1 FROM dbo.sysindexes SI INNER JOIN dbo.sysindexkeys SIK ON SI.ID = SIK.ID AND SI.INDID = SIK.INDID INNER JOIN dbo.syscolumns SC ON SC.ID = SIK.ID AND SC.COLID = SIK.COLID INNER JOIN dbo.sysobjects SO ON SO.NAME = SI.NAME AND SO.XTYPE = 'PK' WHERE SC.ID = COL.ID AND SC.COLID = COL.COLID ) THEN 1 ELSE 0 END AS IS_PARAMY_KEY FROM syscolumns AS COL LEFT JOIN systypes T ON T.XUSERTYPE = COL.XTYPE LEFT JOIN sys.extended_properties P ON COL.ID = P.MAJOR_ID AND COL.COLID = P.MAJOR_ID WHERE COL.ID=OBJECT_ID('" + name + "')").executeQuery();
List<String> names = new ArrayList<String>();
List<String> types = new ArrayList<String>();
List<String> comments = new ArrayList<String>();
List<String> paramyKeys = new ArrayList<String>();
while(set.next()) {
String columnName = set.getString("COLUMN_NAME").trim();
String columnType = set.getString("COLUMN_TYPE").trim();
String columnComment = set.getString("COLUMN_COMMENT");
Integer isParamyKey = set.getInt("IS_PARAMY_KEY");
names.add(columnName);
types.add(columnType);
comments.add(columnComment);
if(isParamyKey == 1) {
paramyKeys.add(columnName);
}
}
if(!name.startsWith("sys") && !name.startsWith("SYS")) {
ReverseUtil.parseEntity(pkg_name, author, root, name, comment, paramyKeys, names, types, comments);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment