001: package net.sourceforge.squirrel_sql.client.session;
002:
003: import java.util.regex.Pattern;
004: import java.util.regex.Matcher;
005:
006: public class EditableSqlCheck {
007: private static final int ALLOWS_EDITING_FALSE = 0;
008: private static final int ALLOWS_EDITING_TRUE = 1;
009: private static final int ALLOWS_EDITING_UNKNOWN = 2;
010:
011: private String _tableNameFromSQL = null;
012:
013: public EditableSqlCheck(SQLExecutionInfo exInfo) {
014: if (null == exInfo || null == exInfo.getSQL()) {
015: return;
016: }
017:
018: _tableNameFromSQL = getTableFromSQLIntern(exInfo.getSQL());
019: }
020:
021: private String getTableFromSQLIntern(String sql) {
022:
023: Pattern patternBeforeTable = Pattern
024: .compile("SELECT\\s+[A-Z0-9_\\*\\.',\\s]*\\s+FROM\\s+([A-Z0-9_\\.]+)");
025: sql = sql.toUpperCase().trim();
026: // Bug 1371587 - remove useless accent characters if they exist
027: sql = sql.replaceAll("\\`", "");
028: Matcher matcher;
029:
030: matcher = patternBeforeTable.matcher(sql);
031: if (false == matcher.find()) {
032: return null;
033: }
034: String table = matcher.group(1);
035: String behindTable = sql.substring(matcher.end(1)).trim();
036:
037: int ret = behindTableAllowsEditing(behindTable);
038:
039: if (ALLOWS_EDITING_UNKNOWN == ret) {
040: // This might be because an table alias is used maybe with an AS before it.
041:
042: Pattern patternBehindTable;
043: if (behindTable.startsWith("AS")
044: && 2 < behindTable.length()
045: && Character.isWhitespace(behindTable.charAt(2))) {
046: patternBehindTable = Pattern
047: .compile("AS\\s+([A-Z0-9_]+)\\s+");
048: } else {
049: patternBehindTable = Pattern
050: .compile("([A-Z0-9_]+)\\s+");
051: }
052:
053: matcher = patternBehindTable.matcher(behindTable);
054: if (false == matcher.find()) {
055: return null;
056: }
057:
058: String alias = matcher.group(0);
059: String behindAlias = behindTable.substring(matcher.end(0))
060: .trim();
061:
062: ret = behindTableAllowsEditing(behindAlias);
063:
064: if (ALLOWS_EDITING_TRUE == ret) {
065: return table;
066: } else {
067: return null;
068: }
069: } else if (ALLOWS_EDITING_TRUE == ret) {
070: return table;
071: } else //(ALLOWS_EDITING_FALSE == ret)
072: {
073: return null;
074: }
075: }
076:
077: private int behindTableAllowsEditing(String behindTable) {
078: if (0 == behindTable.length()) {
079: return ALLOWS_EDITING_TRUE;
080: } else if (behindTable.startsWith("WHERE")
081: || behindTable.startsWith("ORDER")
082: || behindTable.startsWith("GROUP")) {
083: return ALLOWS_EDITING_TRUE;
084: } else if (behindTable.startsWith(",")
085: || behindTable.startsWith("INNER")
086: || behindTable.startsWith("LEFT")
087: || behindTable.startsWith("RIGHT")
088: || behindTable.startsWith("OUTER")
089: || behindTable.startsWith(",")) {
090: return ALLOWS_EDITING_FALSE;
091: } else {
092: return ALLOWS_EDITING_UNKNOWN;
093: }
094: }
095:
096: public boolean allowsEditing() {
097: return null != _tableNameFromSQL;
098: }
099:
100: public String getTableNameFromSQL() {
101: return _tableNameFromSQL;
102: }
103: }
|