Skip to content

Commit 5170383

Browse files
committed
Bug fix: Fixed StringIndexOutOfBoundsException when executed SQL contains the German Umlaut sz
1 parent f14cff5 commit 5170383

File tree

1 file changed

+60
-48
lines changed

1 file changed

+60
-48
lines changed

sql12/core/src/net/sourceforge/squirrel_sql/fw/sql/tablenamefind/TableNameFindService.java

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package net.sourceforge.squirrel_sql.fw.sql.tablenamefind;
22

3-
import java.sql.ResultSet;
4-
import java.sql.ResultSetMetaData;
5-
import java.sql.SQLException;
6-
import java.util.regex.Matcher;
7-
import java.util.regex.Pattern;
8-
93
import net.sourceforge.squirrel_sql.client.session.ISession;
104
import net.sourceforge.squirrel_sql.client.session.action.sqlscript.SQLScriptServices;
115
import net.sourceforge.squirrel_sql.fw.datasetviewer.ColumnDisplayDefinition;
126
import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
137
import net.sourceforge.squirrel_sql.fw.sql.TableInfo;
148
import net.sourceforge.squirrel_sql.fw.util.StringUtilities;
9+
import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
10+
import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
11+
12+
import java.sql.ResultSet;
13+
import java.sql.ResultSetMetaData;
14+
import java.sql.SQLException;
15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
1517

1618
public class TableNameFindService
1719
{
20+
private final static ILogger s_log = LoggerController.createLogger(TableNameFindService.class);
21+
1822
private static final Pattern FILL_COLUMN_NAME_PATTERN = Pattern.compile(".+:([^:]+):[^:]+$");
1923

2024
public static String findTableNameBySqlOrResultMetaData(String sql, ResultSet srcResult, ISession session) throws SQLException
@@ -43,65 +47,73 @@ public static String findTableNameInSQL(String sql, ISession session)
4347

4448
private static String _findTableNameInSQL(String sql)
4549
{
46-
Pattern patternBeforeTable = Pattern.compile("SELECT\\s+[A-Z0-9_\\*\\.',\\s\"]*\\s+FROM\\s+([A-Z0-9_\\.\"]+)");
47-
String ucSql = sql.toUpperCase().trim();
48-
// Bug 1371587 - remove useless accent characters if they exist
49-
ucSql = ucSql.replaceAll("\\`", "");
50-
Matcher matcher;
51-
52-
matcher = patternBeforeTable.matcher(ucSql);
53-
if(false == matcher.find())
54-
{
55-
return null;
56-
}
57-
58-
// Get the table name in its original upper-lower case.
59-
int matchEnd = Math.min(sql.trim().length(), matcher.end(1)); // Prevents StringIndexOutOfBoundsException when sql contains German sz-Umlaut.
60-
String table = sql.trim().substring(matcher.start(1), matchEnd);
61-
62-
String behindTable = ucSql.substring(matchEnd).trim();
63-
64-
SingleTableSqlEnum ret = behindTableAllowsEditing(behindTable);
65-
66-
if(SingleTableSqlEnum.SINGLE_TABLE_SQL_UNKNOWN == ret)
50+
try
6751
{
68-
// This might be because an table alias is used maybe with an AS before it.
52+
Pattern patternBeforeTable = Pattern.compile("SELECT\\s+[A-Z0-9_\\*\\.',\\s\"]*\\s+FROM\\s+([A-Z0-9_\\.\"]+)");
53+
String ucSql = sql.toUpperCase().trim();
54+
// Bug 1371587 - remove useless accent characters if they exist
55+
ucSql = ucSql.replaceAll("\\`", "");
56+
Matcher matcher;
6957

70-
Pattern patternBehindTable;
71-
if(behindTable.startsWith("AS") && 2 < behindTable.length() && Character.isWhitespace(behindTable.charAt(2)))
72-
{
73-
patternBehindTable = Pattern.compile("AS\\s+([A-Z0-9_]+)\\s+");
74-
}
75-
else
76-
{
77-
patternBehindTable = Pattern.compile("([A-Z0-9_]+)\\s+|[A-Z0-9_]+$");
78-
}
79-
80-
matcher = patternBehindTable.matcher(behindTable);
58+
matcher = patternBeforeTable.matcher(ucSql);
8159
if(false == matcher.find())
8260
{
8361
return null;
8462
}
8563

86-
String behindAlias = behindTable.substring(matcher.end(0)).trim();
64+
// Get the table name in its original upper-lower case.
65+
int matchEnd = Math.min(sql.trim().length(), matcher.end(1)); // Prevents StringIndexOutOfBoundsException when sql contains German sz-Umlaut.
66+
String table = sql.trim().substring(matcher.start(1), matchEnd);
8767

88-
ret = behindTableAllowsEditing(behindAlias);
68+
String behindTable = ucSql.substring(matchEnd).trim();
8969

90-
if(SingleTableSqlEnum.SINGLE_TABLE_SQL_TRUE == ret)
70+
SingleTableSqlEnum ret = behindTableAllowsEditing(behindTable);
71+
72+
if(SingleTableSqlEnum.SINGLE_TABLE_SQL_UNKNOWN == ret)
73+
{
74+
// This might be because an table alias is used maybe with an AS before it.
75+
76+
Pattern patternBehindTable;
77+
if(behindTable.startsWith("AS") && 2 < behindTable.length() && Character.isWhitespace(behindTable.charAt(2)))
78+
{
79+
patternBehindTable = Pattern.compile("AS\\s+([A-Z0-9_]+)\\s+");
80+
}
81+
else
82+
{
83+
patternBehindTable = Pattern.compile("([A-Z0-9_]+)\\s+|[A-Z0-9_]+$");
84+
}
85+
86+
matcher = patternBehindTable.matcher(behindTable);
87+
if(false == matcher.find())
88+
{
89+
return null;
90+
}
91+
92+
String behindAlias = behindTable.substring(matcher.end(0)).trim();
93+
94+
ret = behindTableAllowsEditing(behindAlias);
95+
96+
if(SingleTableSqlEnum.SINGLE_TABLE_SQL_TRUE == ret)
97+
{
98+
return table;
99+
}
100+
else
101+
{
102+
return null;
103+
}
104+
}
105+
else if(SingleTableSqlEnum.SINGLE_TABLE_SQL_TRUE == ret)
91106
{
92107
return table;
93108
}
94-
else
109+
else //(ALLOWS_EDITING_FALSE == ret)
95110
{
96111
return null;
97112
}
98113
}
99-
else if(SingleTableSqlEnum.SINGLE_TABLE_SQL_TRUE == ret)
100-
{
101-
return table;
102-
}
103-
else //(ALLOWS_EDITING_FALSE == ret)
114+
catch(Exception e)
104115
{
116+
s_log.error("Error on trying to read the table from an SQL:\n--BEGIN SQL\n" + sql + "\n--END SQL", e);
105117
return null;
106118
}
107119
}

0 commit comments

Comments
 (0)