Search in sources :

Example 1 with TableInfo

use of com.varsql.core.db.valueobject.TableInfo in project varsql by varsqlinfo.

the class TableInfoHandler method handleResult.

@Override
public void handleResult(ResultContext<? extends ParamMap> paramResultContext) {
    ParamMap rowData = paramResultContext.getResultObject();
    String tblName = rowData.getString(MetaColumnConstants.TABLE_NAME);
    if (!tblName.equals(beforeTableName)) {
        if (useTableIndex) {
            currentTableInfo = this.tableInfoList.get(this.tableIndexInfo.get(tblName));
            currentTableInfo.setColList(new ArrayList<ColumnInfo>());
            currentTableInfo.setRemarks(StringUtils.nullToString(currentTableInfo.getRemarks(), ""));
        } else {
            currentTableInfo = new TableInfo();
            currentTableInfo.setName(tblName);
            currentTableInfo.setColList(new ArrayList<ColumnInfo>());
            currentTableInfo.setRemarks(rowData.getString(MetaColumnConstants.REMARKS, ""));
            tableInfoList.add(currentTableInfo);
        }
    }
    ColumnInfo column = new ColumnInfo();
    String cName = rowData.getString(MetaColumnConstants.COLUMN_NAME);
    String columnSize = rowData.getString(MetaColumnConstants.COLUMN_SIZE);
    String degitsLen = rowData.getString(MetaColumnConstants.DECIMAL_DIGITS);
    String dataType = rowData.getString(MetaColumnConstants.DATA_TYPE);
    DataTypeInfo dataTypeInfo = dataTypeImpl.getDataType(dataType);
    column.setName(cName);
    column.setDataType(dataType);
    Object lenInfoObj = rowData.get(MetaColumnConstants.COLUMN_SIZE);
    if (lenInfoObj != null) {
        if (lenInfoObj instanceof Integer) {
            column.setLength(Integer.parseInt(lenInfoObj + ""));
        } else {
            column.setLength(new BigDecimal(lenInfoObj + ""));
        }
    }
    column.setDefaultVal(rowData.getString(MetaColumnConstants.COLUMN_DEF));
    column.setNullable(rowData.getString(MetaColumnConstants.IS_NULLABLE));
    column.setTypeName(dataTypeInfo.getDataTypeName());
    String typeAndLength = rowData.getString(MetaColumnConstants.TYPE_NAME_SIZE, "");
    if ("".equals(typeAndLength)) {
        column.setTypeAndLength(DbMetaUtils.getTypeName(dataTypeInfo, column, dataTypeInfo.getDataTypeName(), columnSize, degitsLen));
    } else {
        column.setTypeAndLength(typeAndLength);
    }
    column.setComment(rowData.getString(MetaColumnConstants.COMMENT, ""));
    column.setConstraints(rowData.getString(MetaColumnConstants.CONSTRAINTS, ""));
    currentTableInfo.addColInfo(column);
    beforeTableName = tblName;
}
Also used : DataTypeInfo(com.varsql.core.db.valueobject.DataTypeInfo) ParamMap(com.vartech.common.app.beans.ParamMap) ColumnInfo(com.varsql.core.db.valueobject.ColumnInfo) TableInfo(com.varsql.core.db.valueobject.TableInfo) BigDecimal(java.math.BigDecimal)

Example 2 with TableInfo

use of com.varsql.core.db.valueobject.TableInfo in project varsql by varsqlinfo.

the class TableReportImpl method columnsInfo.

@Override
public ExcelReport columnsInfo(DatabaseParamInfo dataParamInfo, List<Map> columnInfoList, boolean addTableDefinitionFlag, boolean multiSheetFlag, String... tableNmArr) throws Exception {
    ExcelReport report;
    List<TableInfo> tableList = MetaControlFactory.getDbInstanceFactory(dataParamInfo.getDbType()).getDBObjectMeta(ObjectType.TABLE.getObjectTypeId(), dataParamInfo, tableNmArr);
    List<String> tableNames = Arrays.asList(tableNmArr);
    Collections.sort(tableList, new Comparator<TableInfo>() {

        @Override
        public int compare(TableInfo o1, TableInfo o2) {
            return tableNames.indexOf(o1.getName()) > tableNames.indexOf(o2.getName()) ? 1 : -1;
        }
    });
    // 테이블 요약 정보 추가 여부.
    if (addTableDefinitionFlag) {
        List<ExcelReportVO> tableLabelInfo = new ArrayList<ExcelReportVO>();
        for (VarsqlReportConfig.TABLE tableInfo : VarsqlReportConfig.TABLE.values()) {
            tableLabelInfo.add(ExcelReportVO.builder().key(tableInfo.getKey()).name(tableInfo.getLabel()).width(tableInfo.getWidth()).cellStyle(ExcelCellStyle.builder().align(ExcelConstants.ALIGN.CENTER).build()).headerCellStyle(ExcelCellStyle.builder().align(ExcelConstants.ALIGN.CENTER).bgColor(ExcelConstants.DEFAULT_LABEL_BG_COLOR).bold(true).build()).build());
        }
        ExcelReportVO[] labelInfoArr = tableLabelInfo.toArray(new ExcelReportVO[tableLabelInfo.size()]);
        report = new ExcelReport("Table Info", labelInfoArr);
        Map<String, String> tableInfoMap = new HashMap<String, String>();
        report.createSheet();
        report.addLabelRow();
        tableInfoMap.put(VarsqlReportConfig.TABLE.DATABASE.getKey(), dataParamInfo.getSchema());
        for (TableInfo tableInfo : tableList) {
            tableInfoMap.put(VarsqlReportConfig.TABLE.NAME.getKey(), tableInfo.getName());
            tableInfoMap.put(VarsqlReportConfig.TABLE.COMMENT.getKey(), tableInfo.getRemarks());
            report.addRow(tableInfoMap);
        }
    } else {
        report = new ExcelReport();
    }
    Map<String, String> headerInfo = new HashMap<String, String>();
    headerInfo.put("{databaseName}", dataParamInfo.getSchema());
    List<ExcelReportVO> columnList = new ArrayList<ExcelReportVO>();
    for (Map colInfo : columnInfoList) {
        String colName = String.valueOf(colInfo.get(VarsqlReportConfig.MapperKey.code.getKey()));
        VarsqlReportConfig.TABLE_COLUMN tableInfo = VarsqlReportConfig.TABLE_COLUMN.valueOf(colName.toUpperCase());
        String title = tableInfo.colName();
        if (colInfo.containsKey(VarsqlReportConfig.MapperKey.title.getKey())) {
            title = String.valueOf(colInfo.get(VarsqlReportConfig.MapperKey.title.getKey()));
        }
        int width = tableInfo.colWidth();
        if (colInfo.containsKey(VarsqlReportConfig.MapperKey.width.getKey())) {
            try {
                width = Integer.parseInt(String.valueOf(colInfo.get(VarsqlReportConfig.MapperKey.width.getKey())));
            } catch (NumberFormatException e) {
                e.getMessage();
            }
        }
        // columnList.add(new ExcelReportVO(tableInfo.getKey(), title, width, tableInfo.getAlign()));
        columnList.add(ExcelReportVO.builder().key(tableInfo.getKey()).name(title).width(width).cellStyle(ExcelCellStyle.builder().align(tableInfo.getAlign()).build()).headerCellStyle(ExcelCellStyle.builder().align(ExcelConstants.ALIGN.CENTER).bgColor(ExcelConstants.DEFAULT_LABEL_BG_COLOR).bold(true).build()).build());
    }
    report.setColumnArr(columnList.toArray(new ExcelReportVO[columnList.size()]));
    report.setHaderInfoList(getHeaderInfoList());
    if (multiSheetFlag) {
        report.createSheet(tableList.get(0).getName());
    } else {
        report.createSheet("Table Column Info");
    }
    boolean firstFlag = true;
    List<ColumnInfo> colList;
    ColumnInfo columnInfo = null;
    String tableNm = "";
    for (TableInfo tableInfo : tableList) {
        tableNm = tableInfo.getName();
        if (multiSheetFlag && !firstFlag) {
            report.createSheet(tableNm);
        }
        headerInfo.put("{tableName}", tableNm);
        headerInfo.put("{tableSpace}", "");
        headerInfo.put("{entity}", "");
        headerInfo.put("{comment}", tableInfo.getRemarks());
        report.setHeaderInfo(headerInfo);
        report.addHeaderInfo(true);
        report.addLabelRow();
        colList = tableInfo.getColList();
        for (int j = 0; j < colList.size(); j++) {
            columnInfo = colList.get(j);
            columnInfo.setNo(j + 1);
            report.addRow(columnInfo);
        }
        report.addSpaceRow();
        firstFlag = false;
    }
    return report;
}
Also used : VarsqlReportConfig(com.varsql.core.db.report.VarsqlReportConfig) ExcelReportVO(com.vartech.common.excel.ExcelReportVO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ColumnInfo(com.varsql.core.db.valueobject.ColumnInfo) ExcelReport(com.vartech.common.excel.ExcelReport) TableInfo(com.varsql.core.db.valueobject.TableInfo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TableInfo

use of com.varsql.core.db.valueobject.TableInfo in project varsql by varsqlinfo.

the class DBMetaImpl method getTableMetadata.

public List<TableInfo> getTableMetadata(DatabaseParamInfo dataParamInfo, String... tableNames) throws Exception {
    logger.debug("DBMetaImpl getTableMetadata {}  tableArr :: {}", VartechReflectionUtils.reflectionToString(dataParamInfo), tableNames);
    SqlSession session = SQLManager.getInstance().openSession(dataParamInfo.getVconnid());
    try {
        Connection conn = session.getConnection();
        List<TableInfo> tableList = null;
        if (tableNames != null && tableNames.length > 0) {
            tableList = new ArrayList<TableInfo>();
            TableInfo tableInfo = null;
            for (String nm : tableNames) {
                tableInfo = new TableInfo();
                tableInfo.setName(nm);
                tableInfo.setRemarks("");
                tableList.add(tableInfo);
            }
        } else {
            tableList = dBMetaDataUtil.tableInfo(dataParamInfo, conn, "TABLE");
        }
        return dBMetaDataUtil.tableAndColumnsInfo(dataParamInfo, conn, dbInstanceFactory, "TABLE", tableList);
    } finally {
        sessionClose(dataParamInfo.getVconnid(), session);
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) Connection(java.sql.Connection) TableInfo(com.varsql.core.db.valueobject.TableInfo)

Example 4 with TableInfo

use of com.varsql.core.db.valueobject.TableInfo in project varsql by varsqlinfo.

the class DDLScriptImpl method getTables.

/**
 * @Method Name  : getTable
 * @Method 설명 : table ddl
 * @Method override : @see com.varsql.core.db.ddl.script.DDLScript#getTable(com.varsql.core.db.valueobject.DatabaseParamInfo, java.lang.String[])
 * @작성자   : ytkim
 * @작성일   : 2015. 6. 18.
 * @변경이력  :
 * @param dataParamInfo
 * @param objNm
 * @return
 * @throws Exception
 */
public List<DDLInfo> getTables(DatabaseParamInfo dataParamInfo, DDLCreateOption ddlOption, String... objNmArr) throws Exception {
    List<TableInfo> tableInfoList = dbInstanceFactory.getDBObjectMeta(ObjectType.TABLE.getObjectTypeId(), dataParamInfo, objNmArr);
    StringBuilder ddlStrBuf;
    List<DDLInfo> reval = new ArrayList<DDLInfo>();
    DDLInfo ddlInfo;
    for (TableInfo tableInfo : tableInfoList) {
        String name = tableInfo.getName();
        dataParamInfo.setObjectName(name);
        ddlInfo = new DDLInfo();
        ddlInfo.setName(name);
        ddlStrBuf = new StringBuilder();
        if (ddlOption.isAddDropClause()) {
            ddlStrBuf.append("/* DROP TABLE " + name + " CASCADE CONSTRAINT; */ ").append(BlankConstants.NEW_LINE_TWO);
        }
        List<ColumnInfo> columnList = tableInfo.getColList();
        ddlStrBuf.append("CREATE TABLE " + name + "(").append(BlankConstants.NEW_LINE);
        DataTypeInfo dataTypeInfo;
        ColumnInfo columnInfo;
        for (int i = 0; i < columnList.size(); i++) {
            columnInfo = columnList.get(i);
            dataTypeInfo = dbInstanceFactory.getDataTypeImpl().getDataType(columnInfo.getDataType());
            ddlStrBuf.append(BlankConstants.TAB);
            if (i > 0) {
                ddlStrBuf.append(",");
            }
            ddlStrBuf.append(columnInfo.getName()).append(" ");
            ddlStrBuf.append(columnInfo.getTypeAndLength());
            ddlStrBuf.append(getDefaultValue(columnInfo.getDefaultVal(), dataTypeInfo));
            ddlStrBuf.append(getNotNullValue(columnInfo.getNullable()));
            ddlStrBuf.append(BlankConstants.NEW_LINE);
        }
        List<Map> keyInfo = getTablePrimaryKeyInfo(dataParamInfo, name);
        if (keyInfo.size() > 0) {
            Map source;
            ddlStrBuf.append(BlankConstants.TAB).append(",CONSTRAINT ").append(keyInfo.get(0).get(MetaColumnConstants.PK_NAME)).append(" PRIMARY KEY  ( ");
            for (int i = 0; i < keyInfo.size(); i++) {
                source = keyInfo.get(i);
                ddlStrBuf.append(i == 0 ? "" : ", ").append(source.get(MetaColumnConstants.COLUMN_NAME));
            }
            ddlStrBuf.append(")").append(BlankConstants.NEW_LINE);
        }
        ddlStrBuf.append(")").append(ddlOption.isAddLastSemicolon() ? ";" : "").append(BlankConstants.NEW_LINE_TWO);
        ddlInfo.setCreateScript(ddlStrBuf.toString());
        reval.add(ddlInfo);
    }
    return reval;
}
Also used : DataTypeInfo(com.varsql.core.db.valueobject.DataTypeInfo) ArrayList(java.util.ArrayList) ColumnInfo(com.varsql.core.db.valueobject.ColumnInfo) DDLInfo(com.varsql.core.db.valueobject.ddl.DDLInfo) TableInfo(com.varsql.core.db.valueobject.TableInfo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with TableInfo

use of com.varsql.core.db.valueobject.TableInfo in project varsql by varsqlinfo.

the class DBMetaDataUtil method tableInfo.

protected List<TableInfo> tableInfo(DatabaseParamInfo dataParamInfo, Connection conn, String type) throws SQLException {
    List<TableInfo> reLst = new ArrayList<TableInfo>();
    ResultSet rs = null;
    try {
        DatabaseMetaData dbmd = conn.getMetaData();
        String[] types = { type };
        rs = dbmd.getTables(null, dataParamInfo.getSchema(), "%", types);
        TableInfo tableInfo = null;
        while (rs.next()) {
            tableInfo = new TableInfo();
            tableInfo.setName(rs.getString(MetaColumnConstants.TABLE_NAME));
            tableInfo.setRemarks(StringUtils.nullToString(rs.getString(MetaColumnConstants.REMARKS)));
            reLst.add(tableInfo);
        }
    } finally {
        JdbcUtils.close(rs);
    }
    return reLst;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) TableInfo(com.varsql.core.db.valueobject.TableInfo) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

TableInfo (com.varsql.core.db.valueobject.TableInfo)9 ArrayList (java.util.ArrayList)5 ColumnInfo (com.varsql.core.db.valueobject.ColumnInfo)4 Connection (java.sql.Connection)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 SqlSession (org.apache.ibatis.session.SqlSession)3 DataTypeInfo (com.varsql.core.db.valueobject.DataTypeInfo)2 ObjectColumnInfo (com.varsql.core.db.valueobject.ObjectColumnInfo)2 Map (java.util.Map)2 ConnectionInfo (com.varsql.core.connection.beans.ConnectionInfo)1 DataTypeImpl (com.varsql.core.db.meta.datatype.DataTypeImpl)1 DBMetaHandlerImpl (com.varsql.core.db.meta.handler.DBMetaHandlerImpl)1 VarsqlReportConfig (com.varsql.core.db.report.VarsqlReportConfig)1 DatabaseInfo (com.varsql.core.db.valueobject.DatabaseInfo)1 DatabaseParamInfo (com.varsql.core.db.valueobject.DatabaseParamInfo)1 IndexInfo (com.varsql.core.db.valueobject.IndexInfo)1 DDLInfo (com.varsql.core.db.valueobject.ddl.DDLInfo)1 BaseTest (com.varsql.core.test.BaseTest)1