use of com.cubrid.common.ui.query.format.SqlFormattingStrategy 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;
}
}
}
use of com.cubrid.common.ui.query.format.SqlFormattingStrategy in project cubrid-manager by CUBRID.
the class EditQueryPlanDialog method createStatementGroup.
/**
* Creates statement group
*
* @param composite Composite
*/
private void createStatementGroup(Composite composite) {
final Group statementGroup = new Group(composite, SWT.NONE);
statementGroup.setText(Messages.msgQryStateLbl);
final GridData gdStatementGroup = new GridData(SWT.FILL, SWT.FILL, true, true);
statementGroup.setLayoutData(gdStatementGroup);
statementGroup.setLayout(new GridLayout());
statementText = new Text(statementGroup, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
final GridData gdText = new GridData(SWT.FILL, SWT.FILL, true, true);
gdText.widthHint = 470;
gdText.heightHint = 100;
statementText.setLayoutData(gdText);
// sets the initial value
if (operation == AddEditType.EDIT) {
String sql = queryPlanInfo.getQuery_string();
SqlFormattingStrategy formator = new SqlFormattingStrategy();
sql = formator.format(sql);
statementText.setText(sql);
}
statementText.addModifyListener(new StatementModifyListener());
}
use of com.cubrid.common.ui.query.format.SqlFormattingStrategy in project cubrid-manager by CUBRID.
the class MakeInsertQueryAction method getStmtSQL.
/**
* Create insert prepared statement SQL
*
* @param schemaNode DefaultSchemaNode
* @return String
*/
protected String getStmtSQL(DefaultSchemaNode schemaNode, IEditorPart editorPart) {
// FIXME move this logic to core module
String sql = SQLGenerateUtils.getInsertSQL(schemaNode);
try {
sql = wrapShardSQL(schemaNode, editorPart, sql);
sql = new SqlFormattingStrategy().format(sql).trim();
} catch (Exception ignored) {
}
return sql;
}
use of com.cubrid.common.ui.query.format.SqlFormattingStrategy 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;
}
}
use of com.cubrid.common.ui.query.format.SqlFormattingStrategy in project cubrid-manager by CUBRID.
the class MakeUpdateQueryAction method getStmtSQL.
/**
* Create update statement SQL
*
* @param schemaNode DefaultSchemaNode
* @return String
*/
protected String getStmtSQL(DefaultSchemaNode schemaNode, IEditorPart editorPart) {
// FIXME move this logic to core module
String sql = SQLGenerateUtils.getUpdateSQL(schemaNode);
try {
sql = wrapShardSQL(schemaNode, editorPart, sql);
sql = new SqlFormattingStrategy().format(sql).trim();
} catch (Exception ignored) {
}
return sql;
}
Aggregations