use of org.jkiss.dbeaver.model.DBPIdentifierCase in project dbeaver by serge-rider.
the class SQLTokenizedFormatterConfigurationPage method loadSettings.
@Override
public void loadSettings(DBPPreferenceStore preferenceStore) {
super.loadSettings(preferenceStore);
final String caseName = preferenceStore.getString(ModelPreferences.SQL_FORMAT_KEYWORD_CASE);
DBPIdentifierCase keywordCase = CommonUtils.isEmpty(caseName) ? null : DBPIdentifierCase.valueOf(caseName);
if (keywordCase == null) {
keywordCaseCombo.select(0);
} else {
UIUtils.setComboSelection(keywordCaseCombo, DBPIdentifierCase.capitalizeCaseName(keywordCase.name()));
}
lineFeedBeforeCommaCheck.setSelection(preferenceStore.getBoolean(ModelPreferences.SQL_FORMAT_LF_BEFORE_COMMA));
breakLineBeforeCloseBracket.setSelection(preferenceStore.getBoolean(ModelPreferences.SQL_FORMAT_BREAK_BEFORE_CLOSE_BRACKET));
insertDelimiterInEmptyLines.setSelection(preferenceStore.getBoolean(ModelPreferences.SQL_FORMAT_INSERT_DELIMITERS_IN_EMPTY_LINES));
{
// Text editor settings
IPreferenceStore textEditorPrefs = getTextEditorsPreferenceStore();
this.indentSizeSpinner.setSelection(textEditorPrefs.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH));
useSpacesCheck.setSelection(textEditorPrefs.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS));
}
}
use of org.jkiss.dbeaver.model.DBPIdentifierCase in project dbeaver by serge-rider.
the class SQLTokenizedFormatter method format.
private List<FormatterToken> format(final List<FormatterToken> argList) {
if (argList.isEmpty()) {
return argList;
}
FormatterToken token = argList.get(0);
if (token.getType() == TokenType.SPACE) {
argList.remove(0);
if (argList.isEmpty()) {
return argList;
}
}
token = argList.get(argList.size() - 1);
if (token.getType() == TokenType.SPACE) {
argList.remove(argList.size() - 1);
if (argList.isEmpty()) {
return argList;
}
}
final DBPIdentifierCase keywordCase = formatterCfg.getKeywordCase();
for (int index = 0; index < argList.size(); index++) {
token = argList.get(index);
if (token.getType() == TokenType.KEYWORD) {
token.setString(keywordCase.transform(token.getString()));
}
}
// Remove extra tokens (spaces, etc)
for (int index = argList.size() - 1; index >= 1; index--) {
token = argList.get(index);
FormatterToken prevToken = argList.get(index - 1);
if (token.getType() == TokenType.SPACE && (prevToken.getType() == TokenType.SYMBOL || prevToken.getType() == TokenType.COMMENT)) {
argList.remove(index);
} else if ((token.getType() == TokenType.SYMBOL || token.getType() == TokenType.COMMENT) && prevToken.getType() == TokenType.SPACE) {
argList.remove(index - 1);
} else if (token.getType() == TokenType.SPACE) {
//$NON-NLS-1$
token.setString(" ");
}
}
for (int index = 0; index < argList.size() - 2; index++) {
FormatterToken t0 = argList.get(index);
FormatterToken t1 = argList.get(index + 1);
FormatterToken t2 = argList.get(index + 2);
if (t0.getType() == TokenType.KEYWORD && t1.getType() == TokenType.SPACE && t2.getType() == TokenType.KEYWORD) {
if (((t0.getString().equalsIgnoreCase("ORDER") || //$NON-NLS-1$
t0.getString().equalsIgnoreCase("GROUP")) && //$NON-NLS-1$
t2.getString().equalsIgnoreCase("BY"))) {
//$NON-NLS-1$
//$NON-NLS-1$
t0.setString(t0.getString() + " " + t2.getString());
argList.remove(index + 1);
argList.remove(index + 1);
}
}
// Oracle style joins
if (t0.getString().equals("(") && t1.getString().equals("+") && t2.getString().equals(")")) {
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
//$NON-NLS-1$
t0.setString("(+)");
argList.remove(index + 1);
argList.remove(index + 1);
}
}
int indent = 0;
final List<Integer> bracketIndent = new ArrayList<>();
//$NON-NLS-1$
FormatterToken prev = new FormatterToken(TokenType.SPACE, " ");
boolean encounterBetween = false;
for (int index = 0; index < argList.size(); index++) {
token = argList.get(index);
String tokenString = token.getString().toUpperCase(Locale.ENGLISH);
if (token.getType() == TokenType.SYMBOL) {
if (tokenString.equals("(")) {
//$NON-NLS-1$
functionBracket.add(formatterCfg.isFunction(prev.getString()) ? Boolean.TRUE : Boolean.FALSE);
bracketIndent.add(indent);
indent++;
index += insertReturnAndIndent(argList, index + 1, indent);
} else if (tokenString.equals(")") && !bracketIndent.isEmpty() && !functionBracket.isEmpty()) {
//$NON-NLS-1$
indent = bracketIndent.remove(bracketIndent.size() - 1);
index += insertReturnAndIndent(argList, index, indent);
functionBracket.remove(functionBracket.size() - 1);
} else if (tokenString.equals(",")) {
//$NON-NLS-1$
index += insertReturnAndIndent(argList, index + 1, indent);
} else if (statementDelimiters.contains(tokenString)) {
//$NON-NLS-1$
indent = 0;
index += insertReturnAndIndent(argList, index, indent);
}
} else if (token.getType() == TokenType.KEYWORD) {
switch(tokenString) {
case "DELETE":
case "SELECT":
//$NON-NLS-1$
case "UPDATE":
case "INSERT":
case "INTO":
case "CREATE":
case "DROP":
case "TRUNCATE":
case "TABLE":
case //$NON-NLS-1$
"CASE":
indent++;
index += insertReturnAndIndent(argList, index + 1, indent);
break;
case "FROM":
case "WHERE":
case "SET":
case "ORDER BY":
case "GROUP BY":
case //$NON-NLS-1$
"HAVING":
index += insertReturnAndIndent(argList, index, indent - 1);
index += insertReturnAndIndent(argList, index + 1, indent);
break;
case "LEFT":
case "RIGHT":
case "INNER":
case "OUTER":
case "JOIN":
if (isJoinStart(argList, index)) {
index += insertReturnAndIndent(argList, index, indent - 1);
}
if (tokenString.equals("JOIN")) {
//index += insertReturnAndIndent(argList, index + 1, indent);
}
break;
case //$NON-NLS-1$
"VALUES":
indent--;
index += insertReturnAndIndent(argList, index, indent);
break;
case //$NON-NLS-1$
"END":
indent--;
index += insertReturnAndIndent(argList, index, indent);
break;
case "OR":
case "WHEN":
case //$NON-NLS-1$
"ELSE":
index += insertReturnAndIndent(argList, index, indent);
break;
case "ON":
//indent++;
index += insertReturnAndIndent(argList, index + 1, indent);
break;
case //$NON-NLS-1$ //$NON-NLS-2$
"USING":
index += insertReturnAndIndent(argList, index, indent + 1);
break;
case "UNION":
case "INTERSECT":
case //$NON-NLS-1$
"EXCEPT":
indent -= 2;
index += insertReturnAndIndent(argList, index, indent);
//index += insertReturnAndIndent(argList, index + 1, indent);
indent++;
break;
case //$NON-NLS-1$
"BETWEEN":
encounterBetween = true;
break;
case //$NON-NLS-1$
"AND":
if (!encounterBetween) {
index += insertReturnAndIndent(argList, index, indent);
}
encounterBetween = false;
break;
}
} else if (token.getType() == TokenType.COMMENT) {
boolean isComment = false;
String[] slComments = formatterCfg.getSyntaxManager().getDialect().getSingleLineComments();
if (slComments != null) {
for (String slc : slComments) {
if (token.getString().startsWith(slc)) {
index += insertReturnAndIndent(argList, index + 1, indent);
isComment = true;
break;
}
}
}
if (!isComment) {
Pair<String, String> mlComments = formatterCfg.getSyntaxManager().getDialect().getMultiLineComments();
if (mlComments != null) {
if (token.getString().startsWith(mlComments.getFirst())) {
index += insertReturnAndIndent(argList, index + 1, indent);
}
}
}
} else if (token.getType() == TokenType.COMMAND) {
indent = 0;
if (index > 0) {
index += insertReturnAndIndent(argList, index, 0);
}
index += insertReturnAndIndent(argList, index + 1, 0);
if (!CommonUtils.isEmpty(delimiterRedefiner) && token.getString().startsWith(delimiterRedefiner)) {
final String command = token.getString().trim().toUpperCase(Locale.ENGLISH);
final int divPos = command.lastIndexOf(' ');
if (divPos > 0) {
String delimiter = command.substring(divPos).trim();
if (!CommonUtils.isEmpty(delimiter)) {
statementDelimiters.clear();
statementDelimiters.add(delimiter);
}
}
}
} else {
if (statementDelimiters.contains(tokenString)) {
indent = 0;
index += insertReturnAndIndent(argList, index + 1, indent);
}
}
prev = token;
}
for (int index = argList.size() - 1; index >= 4; index--) {
if (index >= argList.size()) {
continue;
}
FormatterToken t0 = argList.get(index);
FormatterToken t1 = argList.get(index - 1);
FormatterToken t2 = argList.get(index - 2);
FormatterToken t3 = argList.get(index - 3);
FormatterToken t4 = argList.get(index - 4);
if (//$NON-NLS-1$
t4.getString().equals("(") && t3.getString().trim().isEmpty() && t1.getString().trim().isEmpty() && //$NON-NLS-1$
t0.getString().equalsIgnoreCase(")")) {
t4.setString(t4.getString() + t2.getString() + t0.getString());
argList.remove(index);
argList.remove(index - 1);
argList.remove(index - 2);
argList.remove(index - 3);
}
}
for (int index = 1; index < argList.size(); index++) {
prev = argList.get(index - 1);
token = argList.get(index);
if (prev.getType() != TokenType.SPACE && token.getType() != TokenType.SPACE && !token.getString().startsWith("(")) {
if (token.getString().equals(",") || statementDelimiters.contains(token.getString())) {
//$NON-NLS-1$
continue;
}
if (formatterCfg.isFunction(prev.getString()) && token.getString().equals("(")) {
//$NON-NLS-1$
continue;
}
if (token.getType() == TokenType.VALUE && prev.getType() == TokenType.NAME) {
// Do not add space between name and value [JDBC:MSSQL]
continue;
}
if (token.getType() == TokenType.SYMBOL && isEmbeddedToken(token) || prev.getType() == TokenType.SYMBOL && isEmbeddedToken(prev)) {
// Do not insert spaces around colons
continue;
}
if (token.getType() == TokenType.SYMBOL && prev.getType() == TokenType.SYMBOL) {
// Do not add space between symbols
continue;
}
//$NON-NLS-1$
argList.add(index, new FormatterToken(TokenType.SPACE, " "));
}
}
return argList;
}
use of org.jkiss.dbeaver.model.DBPIdentifierCase in project dbeaver by dbeaver.
the class SQLFormatterTokenized method format.
private List<FormatterToken> format(final List<FormatterToken> argList) {
if (argList.isEmpty()) {
return argList;
}
FormatterToken token = argList.get(0);
if (token.getType() == TokenType.SPACE) {
argList.remove(0);
if (argList.isEmpty()) {
return argList;
}
}
token = argList.get(argList.size() - 1);
if (token.getType() == TokenType.SPACE) {
argList.remove(argList.size() - 1);
if (argList.isEmpty()) {
return argList;
}
}
final DBPIdentifierCase keywordCase = formatterCfg.getKeywordCase();
for (int index = 0; index < argList.size(); index++) {
token = argList.get(index);
if (token.getType() == TokenType.KEYWORD) {
token.setString(keywordCase.transform(token.getString()));
}
}
// Remove extra tokens (spaces, etc)
for (int index = argList.size() - 1; index >= 1; index--) {
token = argList.get(index);
FormatterToken prevToken = argList.get(index - 1);
if (token.getType() == TokenType.SPACE && (prevToken.getType() == TokenType.SYMBOL || prevToken.getType() == TokenType.COMMENT)) {
argList.remove(index);
} else if ((token.getType() == TokenType.SYMBOL || token.getType() == TokenType.COMMENT) && prevToken.getType() == TokenType.SPACE) {
argList.remove(index - 1);
} else if (token.getType() == TokenType.SPACE) {
// $NON-NLS-1$
token.setString(" ");
}
}
for (int index = 0; index < argList.size() - 2; index++) {
FormatterToken t0 = argList.get(index);
FormatterToken t1 = argList.get(index + 1);
FormatterToken t2 = argList.get(index + 2);
String tokenString = t0.getString().toUpperCase(Locale.ENGLISH);
String token2String = t2.getString().toUpperCase(Locale.ENGLISH);
;
// Concatenate tokens
if (t0.getType() == TokenType.KEYWORD && t1.getType() == TokenType.SPACE && t2.getType() == TokenType.KEYWORD) {
if (((tokenString.equals("ORDER") || tokenString.equals("GROUP") || tokenString.equals("CONNECT")) && token2String.equals("BY")) || ((tokenString.equals("START")) && token2String.equals("WITH"))) {
t0.setString(t0.getString() + " " + t2.getString());
argList.remove(index + 1);
argList.remove(index + 1);
}
}
// Oracle style joins
if (tokenString.equals("(") && t1.getString().equals("+") && token2String.equals(")")) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// $NON-NLS-1$
t0.setString("(+)");
argList.remove(index + 1);
argList.remove(index + 1);
}
}
int indent = 0;
final List<Integer> bracketIndent = new ArrayList<>();
// $NON-NLS-1$
FormatterToken prev = new FormatterToken(TokenType.SPACE, " ");
boolean encounterBetween = false;
for (int index = 0; index < argList.size(); index++) {
token = argList.get(index);
String tokenString = token.getString().toUpperCase(Locale.ENGLISH);
if (token.getType() == TokenType.SYMBOL) {
if (tokenString.equals("(")) {
// $NON-NLS-1$
functionBracket.add(formatterCfg.isFunction(prev.getString()) ? Boolean.TRUE : Boolean.FALSE);
bracketIndent.add(indent);
if (!isCompact) {
indent++;
index += insertReturnAndIndent(argList, index + 1, indent);
}
} else if (tokenString.equals(")") && !bracketIndent.isEmpty() && !functionBracket.isEmpty()) {
// $NON-NLS-1$
indent = bracketIndent.remove(bracketIndent.size() - 1);
if (!isCompact) {
index += insertReturnAndIndent(argList, index, indent);
}
functionBracket.remove(functionBracket.size() - 1);
} else if (tokenString.equals(",")) {
// $NON-NLS-1$
if (!isCompact) {
index += insertReturnAndIndent(argList, index + 1, indent);
}
} else if (statementDelimiters.contains(tokenString)) {
// $NON-NLS-1$
indent = 0;
index += insertReturnAndIndent(argList, index, indent);
}
} else if (token.getType() == TokenType.KEYWORD) {
switch(tokenString) {
case "DELETE":
case "SELECT":
// $NON-NLS-1$
case "UPDATE":
case "INSERT":
case "INTO":
case "CREATE":
case "DROP":
case "TRUNCATE":
case "TABLE":
case // $NON-NLS-1$
"CASE":
if (!isCompact) {
indent++;
index += insertReturnAndIndent(argList, index + 1, indent);
}
break;
case "FROM":
case "WHERE":
case "SET":
case "START WITH":
case "CONNECT BY":
case "ORDER BY":
case "GROUP BY":
case // $NON-NLS-1$
"HAVING":
index += insertReturnAndIndent(argList, index, indent - 1);
if (!isCompact) {
index += insertReturnAndIndent(argList, index + 1, indent);
}
break;
case "LEFT":
case "RIGHT":
case "INNER":
case "OUTER":
case "JOIN":
if (isJoinStart(argList, index)) {
index += insertReturnAndIndent(argList, index, indent - 1);
}
if (tokenString.equals("JOIN")) {
// index += insertReturnAndIndent(argList, index + 1, indent);
}
break;
case // $NON-NLS-1$
"VALUES":
indent--;
index += insertReturnAndIndent(argList, index, indent);
break;
case // $NON-NLS-1$
"END":
indent--;
index += insertReturnAndIndent(argList, index, indent);
break;
case "OR":
case "WHEN":
case // $NON-NLS-1$
"ELSE":
index += insertReturnAndIndent(argList, index, indent);
break;
case "ON":
// indent++;
index += insertReturnAndIndent(argList, index + 1, indent);
break;
case // $NON-NLS-1$ //$NON-NLS-2$
"USING":
index += insertReturnAndIndent(argList, index, indent + 1);
break;
case // $NON-NLS-1$ //$NON-NLS-2$
"TOP":
// SQL Server specific
index += insertReturnAndIndent(argList, index, indent);
if (argList.size() < index + 3) {
index += insertReturnAndIndent(argList, index + 3, indent);
}
break;
case "UNION":
case "INTERSECT":
case // $NON-NLS-1$
"EXCEPT":
indent -= 2;
index += insertReturnAndIndent(argList, index, indent);
// index += insertReturnAndIndent(argList, index + 1, indent);
indent++;
break;
case // $NON-NLS-1$
"BETWEEN":
encounterBetween = true;
break;
case // $NON-NLS-1$
"AND":
if (!encounterBetween) {
index += insertReturnAndIndent(argList, index, indent);
}
encounterBetween = false;
break;
}
} else if (token.getType() == TokenType.COMMENT) {
boolean isComment = false;
String[] slComments = formatterCfg.getSyntaxManager().getDialect().getSingleLineComments();
if (slComments != null) {
for (String slc : slComments) {
if (token.getString().startsWith(slc)) {
isComment = true;
break;
}
}
}
if (!isComment) {
Pair<String, String> mlComments = formatterCfg.getSyntaxManager().getDialect().getMultiLineComments();
if (mlComments != null) {
if (token.getString().startsWith(mlComments.getFirst())) {
index += insertReturnAndIndent(argList, index + 1, indent);
}
}
}
} else if (token.getType() == TokenType.COMMAND) {
indent = 0;
if (index > 0) {
index += insertReturnAndIndent(argList, index, 0);
}
index += insertReturnAndIndent(argList, index + 1, 0);
if (!CommonUtils.isEmpty(delimiterRedefiner) && token.getString().startsWith(delimiterRedefiner)) {
final String command = token.getString().trim().toUpperCase(Locale.ENGLISH);
final int divPos = command.lastIndexOf(' ');
if (divPos > 0) {
String delimiter = command.substring(divPos).trim();
if (!CommonUtils.isEmpty(delimiter)) {
statementDelimiters.clear();
statementDelimiters.add(delimiter);
}
}
}
} else {
if (statementDelimiters.contains(tokenString)) {
indent = 0;
index += insertReturnAndIndent(argList, index + 1, indent);
}
}
prev = token;
}
for (int index = argList.size() - 1; index >= 4; index--) {
if (index >= argList.size()) {
continue;
}
FormatterToken t0 = argList.get(index);
FormatterToken t1 = argList.get(index - 1);
FormatterToken t2 = argList.get(index - 2);
FormatterToken t3 = argList.get(index - 3);
FormatterToken t4 = argList.get(index - 4);
if (// $NON-NLS-1$
t4.getString().equals("(") && t3.getString().trim().isEmpty() && t1.getString().trim().isEmpty() && // $NON-NLS-1$
t0.getString().equalsIgnoreCase(")")) {
t4.setString(t4.getString() + t2.getString() + t0.getString());
argList.remove(index);
argList.remove(index - 1);
argList.remove(index - 2);
argList.remove(index - 3);
}
}
for (int index = 1; index < argList.size(); index++) {
prev = argList.get(index - 1);
token = argList.get(index);
if (prev.getType() != TokenType.SPACE && token.getType() != TokenType.SPACE && !token.getString().startsWith("(")) {
if (token.getString().equals(",") || statementDelimiters.contains(token.getString())) {
// $NON-NLS-1$
continue;
}
if (formatterCfg.isFunction(prev.getString()) && token.getString().equals("(")) {
// $NON-NLS-1$
continue;
}
if (token.getType() == TokenType.VALUE && prev.getType() == TokenType.NAME) {
// Do not add space between name and value [JDBC:MSSQL]
continue;
}
if (token.getType() == TokenType.SYMBOL && isEmbeddedToken(token) || prev.getType() == TokenType.SYMBOL && isEmbeddedToken(prev)) {
// Do not insert spaces around colons
continue;
}
if (token.getType() == TokenType.SYMBOL && prev.getType() == TokenType.SYMBOL) {
// Do not add space between symbols
continue;
}
// $NON-NLS-1$
argList.add(index, new FormatterToken(TokenType.SPACE, " "));
}
}
return argList;
}
use of org.jkiss.dbeaver.model.DBPIdentifierCase in project dbeaver by dbeaver.
the class GenericStructureAssistant method findObjectsByMask.
@Override
protected void findObjectsByMask(GenericExecutionContext executionContext, JDBCSession session, DBSObjectType objectType, DBSObject parentObject, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults, List<DBSObjectReference> references) throws DBException, SQLException {
GenericSchema schema = parentObject instanceof GenericSchema ? (GenericSchema) parentObject : (globalSearch ? null : executionContext.getDefaultSchema());
GenericCatalog catalog = parentObject instanceof GenericCatalog ? (GenericCatalog) parentObject : schema == null ? (globalSearch ? null : executionContext.getDefaultCatalog()) : schema.getCatalog();
final GenericDataSource dataSource = getDataSource();
DBPIdentifierCase convertCase = caseSensitive ? dataSource.getSQLDialect().storesQuotedCase() : dataSource.getSQLDialect().storesUnquotedCase();
objectNameMask = convertCase.transform(objectNameMask);
if (objectType == RelationalObjectType.TYPE_TABLE) {
findTablesByMask(session, catalog, schema, objectNameMask, maxResults, references);
} else if (objectType == RelationalObjectType.TYPE_PROCEDURE) {
findProceduresByMask(session, catalog, schema, objectNameMask, maxResults, references);
}
}
use of org.jkiss.dbeaver.model.DBPIdentifierCase in project dbeaver by serge-rider.
the class GenericStructureAssistant method findObjectsByMask.
@Override
protected void findObjectsByMask(GenericExecutionContext executionContext, JDBCSession session, DBSObjectType objectType, DBSObject parentObject, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults, List<DBSObjectReference> references) throws DBException, SQLException {
GenericSchema schema = parentObject instanceof GenericSchema ? (GenericSchema) parentObject : (globalSearch ? null : executionContext.getDefaultSchema());
GenericCatalog catalog = parentObject instanceof GenericCatalog ? (GenericCatalog) parentObject : schema == null ? (globalSearch ? null : executionContext.getDefaultCatalog()) : schema.getCatalog();
final GenericDataSource dataSource = getDataSource();
DBPIdentifierCase convertCase = caseSensitive ? dataSource.getSQLDialect().storesQuotedCase() : dataSource.getSQLDialect().storesUnquotedCase();
objectNameMask = convertCase.transform(objectNameMask);
if (objectType == RelationalObjectType.TYPE_TABLE) {
findTablesByMask(session, catalog, schema, objectNameMask, maxResults, references);
} else if (objectType == RelationalObjectType.TYPE_PROCEDURE) {
findProceduresByMask(session, catalog, schema, objectNameMask, maxResults, references);
}
}
Aggregations