|
1 | 1 | package net.sourceforge.squirrel_sql.fw.sql.tablenamefind; |
2 | 2 |
|
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 | | - |
9 | 3 | import net.sourceforge.squirrel_sql.client.session.ISession; |
10 | 4 | import net.sourceforge.squirrel_sql.client.session.action.sqlscript.SQLScriptServices; |
11 | 5 | import net.sourceforge.squirrel_sql.fw.datasetviewer.ColumnDisplayDefinition; |
12 | 6 | import net.sourceforge.squirrel_sql.fw.sql.ITableInfo; |
13 | 7 | import net.sourceforge.squirrel_sql.fw.sql.TableInfo; |
14 | 8 | 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; |
15 | 17 |
|
16 | 18 | public class TableNameFindService |
17 | 19 | { |
| 20 | + private final static ILogger s_log = LoggerController.createLogger(TableNameFindService.class); |
| 21 | + |
18 | 22 | private static final Pattern FILL_COLUMN_NAME_PATTERN = Pattern.compile(".+:([^:]+):[^:]+$"); |
19 | 23 |
|
20 | 24 | public static String findTableNameBySqlOrResultMetaData(String sql, ResultSet srcResult, ISession session) throws SQLException |
@@ -43,65 +47,73 @@ public static String findTableNameInSQL(String sql, ISession session) |
43 | 47 |
|
44 | 48 | private static String _findTableNameInSQL(String sql) |
45 | 49 | { |
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 |
67 | 51 | { |
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; |
69 | 57 |
|
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); |
81 | 59 | if(false == matcher.find()) |
82 | 60 | { |
83 | 61 | return null; |
84 | 62 | } |
85 | 63 |
|
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); |
87 | 67 |
|
88 | | - ret = behindTableAllowsEditing(behindAlias); |
| 68 | + String behindTable = ucSql.substring(matchEnd).trim(); |
89 | 69 |
|
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) |
91 | 106 | { |
92 | 107 | return table; |
93 | 108 | } |
94 | | - else |
| 109 | + else //(ALLOWS_EDITING_FALSE == ret) |
95 | 110 | { |
96 | 111 | return null; |
97 | 112 | } |
98 | 113 | } |
99 | | - else if(SingleTableSqlEnum.SINGLE_TABLE_SQL_TRUE == ret) |
100 | | - { |
101 | | - return table; |
102 | | - } |
103 | | - else //(ALLOWS_EDITING_FALSE == ret) |
| 114 | + catch(Exception e) |
104 | 115 | { |
| 116 | + s_log.error("Error on trying to read the table from an SQL:\n--BEGIN SQL\n" + sql + "\n--END SQL", e); |
105 | 117 | return null; |
106 | 118 | } |
107 | 119 | } |
|
0 commit comments