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

加视图

parent 45502daa
package com.egolm.common.jdbc.dialect; package com.egolm.common.jdbc.dialect;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* 数据库工具类 * 数据库工具类
* *
* @author onedear * @author onedear
* @data:2010-10-21 下午06:12:39 * @data:2010-10-21 下午06:12:39
*/ */
public class SqlServerTo { public class SqlServerTo {
private String root; private String root;
private String pkg_name; private String pkg_name;
private String author; private String author;
private Connection conn; private Connection conn;
static { static {
try { try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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 { 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.root = root;
this.pkg_name = pkg_name; this.pkg_name = pkg_name;
this.author = author; this.author = author;
this.conn = DriverManager.getConnection("jdbc:sqlserver://" + db_host + ";instanceName=SQLSERVER;DatabaseName=" + db_name, db_user, db_pass); this.conn = DriverManager.getConnection("jdbc:sqlserver://" + db_host + ";instanceName=SQLSERVER;DatabaseName=" + db_name, db_user, db_pass);
} }
public void execute() { public void execute() {
try { 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'"; 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); PreparedStatement pStemt = conn.prepareStatement(sql);
ResultSet set = pStemt.executeQuery(); ResultSet set = pStemt.executeQuery();
while(set.next()) { while(set.next()) {
String name = set.getString("TABLE_NAME").trim(); String name = set.getString("TABLE_NAME").trim();
String comment = set.getString("TABLE_COMMENT"); String comment = set.getString("TABLE_COMMENT");
execute(name, comment); execute(name, comment);
} }
conn.close(); conn.close();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void executeFilter(String sign) { public void executeView() {
try { 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'"; String sql = "SELECT TABLE_NAME , 'REMARK' AS TABLE_COMMENT FROM INFORMATION_SCHEMA.VIEWS";
PreparedStatement pStemt = conn.prepareStatement(sql); PreparedStatement pStemt = conn.prepareStatement(sql);
ResultSet set = pStemt.executeQuery(); ResultSet set = pStemt.executeQuery();
while(set.next()) { while(set.next()) {
String name = set.getString("TABLE_NAME").trim(); String name = set.getString("TABLE_NAME").trim();
if(name.contains(sign)) { String comment = set.getString("TABLE_COMMENT");
String comment = set.getString("TABLE_COMMENT"); execute(name, comment);
execute(name, comment); }
} conn.close();
} } catch (Exception e) {
conn.close(); throw new RuntimeException(e);
} catch (Exception e) { }
throw new RuntimeException(e); }
}
} public void executeFilter(String sign) {
try {
public void execute(String name, String comment) { String sql = "SELECT D.name AS TABLE_NAME, 'REMARK' AS TABLE_COMMENT FROM sysobjects AS D WHERE D.XTYPE = 'U' AND D.NAME <> 'dtproperties'";
try { PreparedStatement pStemt = conn.prepareStatement(sql);
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(); ResultSet set = pStemt.executeQuery();
List<String> names = new ArrayList<String>(); while(set.next()) {
List<String> types = new ArrayList<String>(); String name = set.getString("TABLE_NAME").trim();
List<String> comments = new ArrayList<String>(); if(name.contains(sign)) {
List<String> paramyKeys = new ArrayList<String>(); String comment = set.getString("TABLE_COMMENT");
while(set.next()) { execute(name, comment);
String columnName = set.getString("COLUMN_NAME").trim(); }
String columnType = set.getString("COLUMN_TYPE").trim(); }
String columnComment = set.getString("COLUMN_COMMENT"); conn.close();
Integer isParamyKey = set.getInt("IS_PARAMY_KEY"); } catch (Exception e) {
names.add(columnName); throw new RuntimeException(e);
types.add(columnType); }
comments.add(columnComment); }
if(isParamyKey == 1) {
paramyKeys.add(columnName); 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();
if(!name.startsWith("sys") && !name.startsWith("SYS")) { List<String> names = new ArrayList<String>();
ReverseUtil.parseEntity(pkg_name, author, root, name, comment, paramyKeys, names, types, comments); List<String> types = new ArrayList<String>();
} List<String> comments = new ArrayList<String>();
} catch (Exception e) { List<String> paramyKeys = new ArrayList<String>();
throw new RuntimeException(e); 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