提交 35b2460d 编写于 作者: S Serge Rider

#1635 Parse SQL parameters for non-DDL queries

上级 70451c75
......@@ -104,6 +104,17 @@ public class ArrayUtils {
return false;
}
public static boolean containsIgnoreCase(String[] array, String value)
{
if (isEmpty(array) || value == null)
return false;
for (int i = 0; i < array.length; i++) {
if (value.equalsIgnoreCase(array[i]))
return true;
}
return false;
}
public static <OBJECT_TYPE> boolean containsRef(@NotNull OBJECT_TYPE[] array, @Nullable OBJECT_TYPE value)
{
final int length = array.length;
......
......@@ -61,6 +61,7 @@ import org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplatesPage;
import org.jkiss.dbeaver.ui.editors.sql.util.SQLSymbolInserter;
import org.jkiss.dbeaver.ui.editors.text.BaseTextEditor;
import org.jkiss.dbeaver.ui.preferences.*;
import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.Pair;
......@@ -826,10 +827,12 @@ public abstract class SQLEditorBase extends BaseTextEditor implements IErrorVisu
}
protected List<SQLQueryParameter> parseParameters(IDocument document, SQLQuery query) {
boolean execQuery = SQLUtils.isExecQuery(getSQLDialect(), query.getText());
final SQLDialect sqlDialect = getSQLDialect();
boolean execQuery = false;
List<SQLQueryParameter> parameters = null;
ruleManager.setRange(document, query.getOffset(), query.getLength());
int blockDepth = 0;
boolean firstKeyword = true;
for (;;) {
IToken token = ruleManager.nextToken();
int tokenOffset = ruleManager.getTokenOffset();
......@@ -842,12 +845,24 @@ public abstract class SQLEditorBase extends BaseTextEditor implements IErrorVisu
if (token instanceof SQLToken) {
tokenType = ((SQLToken) token).getType();
}
if (tokenType == SQLToken.T_BLOCK_BEGIN) {
blockDepth++;
}else if (tokenType == SQLToken.T_BLOCK_END) {
blockDepth--;
if (token.isWhitespace() || tokenType == SQLToken.T_COMMENT) {
continue;
}
if (firstKeyword) {
// Detect query type
try {
String tokenText = document.get(tokenOffset, tokenLength);
if (ArrayUtils.containsIgnoreCase(sqlDialect.getDDLKeywords(), tokenText)) {
// DDL doesn't support parameters
return null;
}
execQuery = ArrayUtils.containsIgnoreCase(sqlDialect.getExecuteKeywords(), tokenText);
} catch (BadLocationException e) {
log.warn(e);
}
firstKeyword = false;
}
if (tokenType == SQLToken.T_PARAMETER && tokenLength > 0 && blockDepth <= 0) {
if (tokenType == SQLToken.T_PARAMETER && tokenLength > 0) {
try {
String paramName = document.get(tokenOffset, tokenLength);
if (execQuery && paramName.equals("?")) {
......
......@@ -42,6 +42,9 @@ public class BasicSQLDialect implements SQLDialect {
private static final String[] DEFAULT_LINE_COMMENTS = {SQLConstants.SL_COMMENT};
private static final String[] EXEC_KEYWORDS = new String[0];
private static final String[] DDL_KEYWORDS = new String[] {
"CREATE", "ALTER", "DROP"
};
private static final String[][] DEFAULT_BEGIN_END_BLOCK = new String[][]{
{SQLConstants.BLOCK_BEGIN, SQLConstants.BLOCK_END}
......@@ -90,6 +93,12 @@ public class BasicSQLDialect implements SQLDialect {
return EXEC_KEYWORDS;
}
@NotNull
@Override
public String[] getDDLKeywords() {
return DDL_KEYWORDS;
}
protected void addSQLKeyword(String keyword)
{
reservedWords.add(keyword);
......
......@@ -71,6 +71,13 @@ public interface SQLDialect {
@NotNull
String[] getExecuteKeywords();
/**
* Retrieves a list of execute keywords. If database doesn't support implicit execute returns empty list or null.
* @return the list of execute keywords.
*/
@NotNull
String[] getDDLKeywords();
/**
* Retrieves a list of all of this database's SQL keywords
* that are NOT also SQL92 keywords.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册