Search in sources :

Example 1 with TableMeta

use of org.sagacity.sqltoy.model.TableMeta in project sagacity-sqltoy by chenrenfei.

the class SqlServerDialectUtils method getTables.

@SuppressWarnings("unchecked")
public static List<TableMeta> getTables(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    String sql = "select d.name TABLE_NAME, cast(isnull(f.value,'') as varchar(1000)) COMMENTS,d.xtype TABLE_TYPE" + " from syscolumns a " + "		 inner join sysobjects d on a.id=d.id and d.xtype in ('U','V') and d.name<>'dtproperties' " + "		 left join sys.extended_properties f on d.id=f.major_id and f.minor_id=0 " + "		 where a.colorder=1 ";
    if (StringUtil.isNotBlank(tableName)) {
        sql = sql.concat(" and d.name like ?");
    }
    PreparedStatement pst = conn.prepareStatement(sql);
    ResultSet rs = null;
    // 通过preparedStatementProcess反调,第二个参数是pst
    return (List<TableMeta>) SqlUtil.preparedStatementProcess(null, pst, rs, new PreparedStatementResultHandler() {

        @Override
        public void execute(Object rowData, PreparedStatement pst, ResultSet rs) throws Exception {
            if (StringUtil.isNotBlank(tableName)) {
                if (tableName.contains("%")) {
                    pst.setString(1, tableName);
                } else {
                    pst.setString(1, "%" + tableName + "%");
                }
            }
            rs = pst.executeQuery();
            List<TableMeta> tables = new ArrayList<TableMeta>();
            while (rs.next()) {
                TableMeta tableMeta = new TableMeta();
                tableMeta.setTableName(rs.getString("TABLE_NAME"));
                tableMeta.setType(rs.getString("TABLE_TYPE"));
                if (tableMeta.getType().equals("V")) {
                    tableMeta.setType("VIEW");
                } else {
                    tableMeta.setType("TABLE");
                }
                tableMeta.setRemarks(rs.getString("COMMENTS"));
                tables.add(tableMeta);
            }
            this.setResult(tables);
        }
    });
}
Also used : PreparedStatementResultHandler(org.sagacity.sqltoy.callback.PreparedStatementResultHandler) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) TableMeta(org.sagacity.sqltoy.model.TableMeta)

Example 2 with TableMeta

use of org.sagacity.sqltoy.model.TableMeta in project sagacity-sqltoy by chenrenfei.

the class DefaultDialectUtils method getTables.

@SuppressWarnings("unchecked")
public static List<TableMeta> getTables(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    // 可自定义 PreparedStatement pst=conn.xxx;
    ResultSet rs = conn.getMetaData().getTables(catalog, schema, tableName, new String[] { "TABLE", "VIEW" });
    // 通过preparedStatementProcess反调,第二个参数是pst
    return (List<TableMeta>) SqlUtil.preparedStatementProcess(null, null, rs, new PreparedStatementResultHandler() {

        @Override
        public void execute(Object rowData, PreparedStatement pst, ResultSet rs) throws Exception {
            List<TableMeta> tables = new ArrayList<TableMeta>();
            while (rs.next()) {
                TableMeta tableMeta = new TableMeta();
                tableMeta.setTableName(rs.getString("TABLE_NAME"));
                tableMeta.setSchema(rs.getString("TABLE_SCHEM"));
                tableMeta.setType(rs.getString("TABLE_TYPE"));
                tableMeta.setRemarks(rs.getString("REMARKS"));
                tables.add(tableMeta);
            }
            this.setResult(tables);
        }
    });
}
Also used : PreparedStatementResultHandler(org.sagacity.sqltoy.callback.PreparedStatementResultHandler) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement) TableMeta(org.sagacity.sqltoy.model.TableMeta)

Example 3 with TableMeta

use of org.sagacity.sqltoy.model.TableMeta in project sagacity-sqltoy by chenrenfei.

the class OracleDialectUtils method getTables.

@SuppressWarnings("unchecked")
public static List<TableMeta> getTables(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    String sql = "select * from user_tab_comments";
    if (StringUtil.isNotBlank(tableName)) {
        sql = sql.concat(" where TABLE_NAME like ?");
    }
    PreparedStatement pst = conn.prepareStatement(sql);
    ResultSet rs = null;
    // 通过preparedStatementProcess反调,第二个参数是pst
    return (List<TableMeta>) SqlUtil.preparedStatementProcess(null, pst, rs, new PreparedStatementResultHandler() {

        @Override
        public void execute(Object rowData, PreparedStatement pst, ResultSet rs) throws Exception {
            if (StringUtil.isNotBlank(tableName)) {
                if (tableName.contains("%")) {
                    pst.setString(1, tableName);
                } else {
                    pst.setString(1, "%" + tableName + "%");
                }
            }
            rs = pst.executeQuery();
            List<TableMeta> tables = new ArrayList<TableMeta>();
            while (rs.next()) {
                TableMeta tableMeta = new TableMeta();
                tableMeta.setTableName(rs.getString("TABLE_NAME"));
                tableMeta.setType(rs.getString("TABLE_TYPE"));
                tableMeta.setRemarks(rs.getString("COMMENTS"));
                tables.add(tableMeta);
            }
            this.setResult(tables);
        }
    });
}
Also used : PreparedStatementResultHandler(org.sagacity.sqltoy.callback.PreparedStatementResultHandler) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) TableMeta(org.sagacity.sqltoy.model.TableMeta)

Aggregations

PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 PreparedStatementResultHandler (org.sagacity.sqltoy.callback.PreparedStatementResultHandler)3 TableMeta (org.sagacity.sqltoy.model.TableMeta)3