Search in sources :

Example 1 with GetAllAttrTask

use of com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask in project cubrid-manager by CUBRID.

the class GetSchemaDDLTask method execute.

/**
	 * Execute the tasks
	 */
public void execute() {
    try {
        if (CompatibleUtil.isAfter900(databaseInfo)) {
            String sql = QueryUtil.getShowCreateSQL(schemaName, isTable);
            sql = DatabaseInfo.wrapShardQuery(databaseInfo, sql);
            StringBuilder sb = new StringBuilder();
            if (errorMsg != null && errorMsg.trim().length() > 0) {
                return;
            }
            if (connection == null || connection.isClosed()) {
                errorMsg = Messages.error_getConnection;
                return;
            }
            String viewName = null;
            stmt = connection.createStatement();
            rs = (CUBRIDResultSetProxy) stmt.executeQuery(sql);
            while (rs.next()) {
                if (isTable) {
                    sb.append(rs.getString(2));
                } else {
                    viewName = QuerySyntax.escapeKeyword(rs.getString(1));
                    if (sb.length() > 0) {
                        sb.append(" UNION ALL ");
                    }
                    sb.append(" ").append(rs.getString(2)).append(" ");
                }
            }
            if (isTable) {
                ddl = sb.toString();
            } else {
                ddl = "CREATE OR REPLACE VIEW " + viewName + " AS " + sb.toString() + ";";
            }
        } else {
            StringBuilder sqlScript = new StringBuilder();
            if (!isTable) {
                // Get class info
                GetAllClassListTask getAllClassListTask = new GetAllClassListTask(databaseInfo, connection);
                getAllClassListTask.setTableName(schemaName);
                getAllClassListTask.getClassInfoTaskExcute();
                // If failed
                if (getAllClassListTask.getErrorMsg() != null || getAllClassListTask.isCancel()) {
                    errorMsg = getAllClassListTask.getErrorMsg();
                    LOGGER.error(errorMsg);
                    return;
                }
                /*Check user cancel*/
                if (monitor != null && monitor.isCanceled()) {
                    errorMsg = "The user canceled.";
                    return;
                }
                ClassInfo classInfo = getAllClassListTask.getClassInfo();
                // Get view column
                GetViewAllColumnsTask getAllDBVclassTask = new GetViewAllColumnsTask(databaseInfo, connection);
                getAllDBVclassTask.setClassName(schemaName);
                getAllDBVclassTask.getAllVclassListTaskExcute();
                // If failed
                if (getAllDBVclassTask.getErrorMsg() != null || getAllDBVclassTask.isCancel()) {
                    errorMsg = getAllDBVclassTask.getErrorMsg();
                    LOGGER.error(errorMsg);
                    return;
                }
                /*Check user cancel*/
                if (monitor != null && monitor.isCanceled()) {
                    errorMsg = "The user canceled.";
                    return;
                }
                // Get query list
                List<String> vclassList = getAllDBVclassTask.getAllVclassList();
                List<Map<String, String>> queryListData = new ArrayList<Map<String, String>>();
                for (String sql : vclassList) {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("0", sql);
                    queryListData.add(map);
                }
                /*Check user cancel*/
                if (monitor != null && monitor.isCanceled()) {
                    errorMsg = "The user canceled.";
                    return;
                }
                // Get all attribute
                GetAllAttrTask getAllAttrTask = new GetAllAttrTask(databaseInfo, connection);
                getAllAttrTask.setClassName(schemaName);
                getAllAttrTask.getAttrList();
                // If failed
                if (getAllAttrTask.getErrorMsg() != null) {
                    errorMsg = getAllAttrTask.getErrorMsg();
                    LOGGER.error(errorMsg);
                    return;
                }
                List<DBAttribute> attrList = getAllAttrTask.getAllAttrList();
                List<Map<String, String>> viewColListData = GetInfoDataUtil.getViewColMapList(attrList);
                sqlScript.append(GetInfoDataUtil.getViewCreateSQLScript(false, databaseInfo, classInfo, schemaName, viewColListData, queryListData));
            } else {
                String ddl = SQLGenerateUtils.getCreateSQL(databaseInfo, schemaName);
                sqlScript.append(ddl == null ? "" : ddl);
            }
            ddl = sqlScript.toString();
        }
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
        errorMsg = e.getMessage();
    } finally {
        finish();
        if (StringUtil.isNotEmpty(ddl)) {
            SqlFormattingStrategy formator = new SqlFormattingStrategy();
            String formated = formator.format(ddl);
            ddl = formated;
        }
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SqlFormattingStrategy(com.cubrid.common.ui.query.format.SqlFormattingStrategy) GetAllClassListTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllClassListTask) GetViewAllColumnsTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetViewAllColumnsTask) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) HashMap(java.util.HashMap) Map(java.util.Map) ClassInfo(com.cubrid.cubridmanager.core.cubrid.table.model.ClassInfo) GetAllAttrTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)

Example 2 with GetAllAttrTask

use of com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask in project cubrid-manager by CUBRID.

the class TableSchemaCompareComposite method getTableSchema.

private String getTableSchema(CubridDatabase db, Map<String, SchemaInfo> schemas, String tableName) {
    // FIXME logic code move to core module
    String tableSchemaInfo = "";
    try {
        SchemaInfo schemaInfo = schemas.get(tableName);
        if (schemaInfo == null) {
            return "";
        }
        if (schemaInfo.getVirtual().equals(ClassType.VIEW.getText())) {
            GetAllClassListTask getAllClassListTask = new GetAllClassListTask(db.getDatabaseInfo());
            getAllClassListTask.setTableName(tableName);
            getAllClassListTask.getClassInfoTaskExcute();
            ClassInfo classInfo = getAllClassListTask.getClassInfo();
            GetAllAttrTask getAllAttrTask = new GetAllAttrTask(db.getDatabaseInfo());
            getAllAttrTask.setClassName(tableName);
            getAllAttrTask.getAttrList();
            List<DBAttribute> attrList = getAllAttrTask.getAllAttrList();
            List<Map<String, String>> viewColListData = GetInfoDataUtil.getViewColMapList(attrList);
            /*Get view column*/
            GetViewAllColumnsTask getAllDBVclassTask = new GetViewAllColumnsTask(db.getDatabaseInfo());
            getAllDBVclassTask.setClassName(tableName);
            getAllDBVclassTask.getAllVclassListTaskExcute();
            /*Get query list*/
            List<String> vclassList = getAllDBVclassTask.getAllVclassList();
            List<Map<String, String>> queryListData = new ArrayList<Map<String, String>>();
            for (String sql : vclassList) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("0", sql);
                queryListData.add(map);
            }
            tableSchemaInfo = GetInfoDataUtil.getViewCreateSQLScript(true, db, classInfo, tableName, viewColListData, queryListData);
        } else {
            SchemaDDL schemaDDL = null;
            schemaDDL = new SchemaDDL(null, db.getDatabaseInfo());
            tableSchemaInfo = schemaDDL.getSchemaDDL(schemaInfo);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        return "";
    }
    return tableSchemaInfo;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GetAllClassListTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllClassListTask) GetViewAllColumnsTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetViewAllColumnsTask) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) HashMap(java.util.HashMap) Map(java.util.Map) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo) ClassInfo(com.cubrid.cubridmanager.core.cubrid.table.model.ClassInfo) GetAllAttrTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)

Example 3 with GetAllAttrTask

use of com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask in project cubrid-manager by CUBRID.

the class CreateTriggerDialog method addColumns.

/**
	 *
	 * Add columns of this table to combo
	 *
	 * @param tableName the tablename
	 */
private void addColumns(String tableName) {
    triggerTargetColumnCombo.removeAll();
    if (tableName == null || tableName.trim().length() == 0) {
        return;
    }
    CubridDatabase db = database;
    DatabaseInfo dbInfo = db.getDatabaseInfo();
    GetAllAttrTask task = new GetAllAttrTask(dbInfo);
    List<String> attrNameList = task.getAttrNameList(tableName);
    if (task.getErrorMsg() != null) {
        CommonUITool.openErrorBox(task.getErrorMsg());
        return;
    }
    triggerTargetColumnCombo.add("");
    for (String name : attrNameList) {
        triggerTargetColumnCombo.add(name);
    }
}
Also used : DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase) GetAllAttrTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)

Example 4 with GetAllAttrTask

use of com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask in project cubrid-manager by CUBRID.

the class QueryEditorDNDController method getScript.

/**
	 * Get the script
	 *
	 * @param selectedNode ISchemaNode
	 * @return script
	 */
private String getScript(ISchemaNode selectedNode) {
    // FIXME move this logic to core module
    if (selectedNode == null) {
        return null;
    }
    CubridDatabase db = selectedNode.getDatabase();
    DatabaseInfo dbInfo = db.getDatabaseInfo();
    GetAllAttrTask task = new GetAllAttrTask(dbInfo);
    task.setClassName(selectedNode.getName());
    task.getAttrList();
    if (task.getErrorMsg() != null) {
        CommonUITool.openErrorBox(task.getErrorMsg());
        return null;
    }
    List<DBAttribute> allAttrList = task.getAllAttrList();
    String sql = SQLGenerateUtils.getSelectSQLWithLimit(selectedNode.getName(), allAttrList);
    if (sql == null) {
        return "";
    }
    try {
        return new SqlFormattingStrategy().format(sql).trim() + StringUtil.NEWLINE + StringUtil.NEWLINE;
    } catch (Exception ignored) {
        return sql.trim() + StringUtil.NEWLINE + StringUtil.NEWLINE;
    }
}
Also used : DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase) SqlFormattingStrategy(com.cubrid.common.ui.query.format.SqlFormattingStrategy) GetAllAttrTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)

Example 5 with GetAllAttrTask

use of com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask in project cubrid-manager by CUBRID.

the class MakeSelectQueryAction method getStmtSQL.

/**
	 * Create select statement SQL
	 *
	 * @param schemaNode DefaultSchemaNode
	 * @return String
	 */
protected String getStmtSQL(DefaultSchemaNode schemaNode, IEditorPart editorPart) {
    // FIXME move this logic to core module
    String sql = "";
    if (schemaNode != null) {
        CubridDatabase db = schemaNode.getDatabase();
        if (db == null) {
            return sql;
        }
        DatabaseInfo dbInfo = db.getDatabaseInfo();
        if (dbInfo == null) {
            return sql;
        }
        GetAllAttrTask task = new GetAllAttrTask(dbInfo);
        task.setClassName(schemaNode.getName());
        task.getAttrList();
        if (task.getErrorMsg() != null) {
            return sql;
        }
        List<DBAttribute> allAttrList = task.getAllAttrList();
        sql = SQLGenerateUtils.getSelectSQLNoWhere(schemaNode.getName(), allAttrList, true);
        sql = wrapShardSQL(schemaNode, editorPart, sql);
    }
    try {
        return new SqlFormattingStrategy().format(sql).trim();
    } catch (Exception ignored) {
        return sql;
    }
}
Also used : DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase) SqlFormattingStrategy(com.cubrid.common.ui.query.format.SqlFormattingStrategy) GetAllAttrTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)

Aggregations

GetAllAttrTask (com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)12 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)10 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)7 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)6 GetAllClassListTask (com.cubrid.cubridmanager.core.cubrid.table.task.GetAllClassListTask)6 GetViewAllColumnsTask (com.cubrid.cubridmanager.core.cubrid.table.task.GetViewAllColumnsTask)6 ClassInfo (com.cubrid.cubridmanager.core.cubrid.table.model.ClassInfo)5 SqlFormattingStrategy (com.cubrid.common.ui.query.format.SqlFormattingStrategy)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CreateViewDialog (com.cubrid.common.ui.cubrid.table.dialog.CreateViewDialog)2 JDBCGetAllDbUserTask (com.cubrid.cubridmanager.core.cubrid.user.task.JDBCGetAllDbUserTask)2 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)1 ITask (com.cubrid.common.core.task.ITask)1 CubridNodeChangedEvent (com.cubrid.common.ui.spi.event.CubridNodeChangedEvent)1 ISchemaNode (com.cubrid.common.ui.spi.model.ISchemaNode)1 ExecTaskWithProgress (com.cubrid.common.ui.spi.progress.ExecTaskWithProgress)1 TaskExecutor (com.cubrid.common.ui.spi.progress.TaskExecutor)1 SchemaDDL (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL)1