|
42 | 42 | public abstract class AbstractMetaDataDialect implements RevengDialect { |
43 | 43 |
|
44 | 44 | protected final Logger log = Logger.getLogger(this.getClass()); |
45 | | - |
| 45 | + |
46 | 46 | private Connection connection; |
47 | 47 | private DatabaseMetaData metaData; |
48 | | - |
| 48 | + |
49 | 49 | private ConnectionProvider connectionProvider = null; |
50 | 50 |
|
51 | 51 | public void configure( |
52 | 52 | ConnectionProvider connectionProvider) { |
53 | | - this.connectionProvider = connectionProvider; |
| 53 | + this.connectionProvider = connectionProvider; |
54 | 54 | } |
55 | | - |
| 55 | + |
56 | 56 | public void close() { |
57 | 57 | metaData = null; |
58 | 58 | if(connection != null) { |
59 | 59 | try { |
60 | | - connectionProvider.closeConnection(connection); |
| 60 | + connectionProvider.closeConnection(connection); |
61 | 61 | } |
62 | 62 | catch (SQLException e) { |
63 | 63 | throw new RuntimeException("Problem while closing connection", e); |
64 | 64 | } finally { |
65 | 65 | connection = null; |
66 | 66 | } |
67 | 67 | } |
68 | | - connectionProvider = null; |
| 68 | + connectionProvider = null; |
69 | 69 | } |
70 | | - |
| 70 | + |
71 | 71 | protected DatabaseMetaData getMetaData() { |
72 | 72 | if (metaData == null) { |
73 | 73 | try { |
74 | | - metaData = getConnection().getMetaData(); |
75 | | - } |
| 74 | + metaData = getConnection().getMetaData(); |
| 75 | + } |
76 | 76 | catch (SQLException e) { |
77 | 77 | throw new RuntimeException("Getting database metadata", e); |
78 | 78 | } |
79 | 79 | } |
80 | 80 | return metaData; |
81 | 81 | } |
82 | | - |
| 82 | + |
83 | 83 | protected String getDatabaseStructure(String catalog, String schema) { |
84 | | - ResultSet schemaRs = null; |
85 | | - ResultSet catalogRs = null; |
86 | | - String nl = System.getProperty("line.separator"); |
87 | | - StringBuffer sb = new StringBuffer(nl); |
88 | | - // Let's give the user some feedback. The exception |
89 | | - // is probably related to incorrect schema configuration. |
90 | | - sb.append("Configured schema:").append(schema).append(nl); |
91 | | - sb.append("Configured catalog:").append(catalog ).append(nl); |
92 | | - |
93 | | - try { |
94 | | - schemaRs = getMetaData().getSchemas(); |
95 | | - sb.append("Available schemas:").append(nl); |
96 | | - while (schemaRs.next() ) { |
97 | | - sb.append(" ").append(schemaRs.getString("TABLE_SCHEM") ).append(nl); |
98 | | - } |
99 | | - } |
100 | | - catch (SQLException e2) { |
101 | | - log.warn("Could not get schemas", e2); |
102 | | - sb.append(" <SQLException while getting schemas>").append(nl); |
103 | | - } |
104 | | - finally { |
105 | | - try { |
106 | | - schemaRs.close(); |
107 | | - } |
108 | | - catch (Exception ignore) { |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - try { |
113 | | - catalogRs = getMetaData().getCatalogs(); |
114 | | - sb.append("Available catalogs:").append(nl); |
115 | | - while (catalogRs.next() ) { |
116 | | - sb.append(" ").append(catalogRs.getString("TABLE_CAT") ).append(nl); |
117 | | - } |
118 | | - } |
119 | | - catch (SQLException e2) { |
120 | | - log.warn("Could not get catalogs", e2); |
121 | | - sb.append(" <SQLException while getting catalogs>").append(nl); |
122 | | - } |
123 | | - finally { |
124 | | - try { |
125 | | - catalogRs.close(); |
126 | | - } |
127 | | - catch (Exception ignore) { |
128 | | - } |
129 | | - } |
130 | | - return sb.toString(); |
131 | | - } |
| 84 | + ResultSet schemaRs = null; |
| 85 | + ResultSet catalogRs = null; |
| 86 | + String nl = System.lineSeparator(); |
| 87 | + StringBuilder sb = new StringBuilder(nl); |
| 88 | + // Let's give the user some feedback. The exception |
| 89 | + // is probably related to incorrect schema configuration. |
| 90 | + sb.append("Configured schema:").append(schema).append(nl); |
| 91 | + sb.append("Configured catalog:").append(catalog ).append(nl); |
| 92 | + |
| 93 | + try { |
| 94 | + schemaRs = getMetaData().getSchemas(); |
| 95 | + sb.append("Available schemas:").append(nl); |
| 96 | + while (schemaRs.next() ) { |
| 97 | + sb.append(" ").append(schemaRs.getString("TABLE_SCHEM") ).append(nl); |
| 98 | + } |
| 99 | + } |
| 100 | + catch (SQLException e2) { |
| 101 | + log.warn("Could not get schemas", e2); |
| 102 | + sb.append(" <SQLException while getting schemas>").append(nl); |
| 103 | + } |
| 104 | + finally { |
| 105 | + try { |
| 106 | + if (schemaRs != null) { |
| 107 | + schemaRs.close(); |
| 108 | + } |
| 109 | + } |
| 110 | + catch (Exception ignore) { |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + catalogRs = getMetaData().getCatalogs(); |
| 116 | + sb.append("Available catalogs:").append(nl); |
| 117 | + while (catalogRs.next() ) { |
| 118 | + sb.append(" ").append(catalogRs.getString("TABLE_CAT") ).append(nl); |
| 119 | + } |
| 120 | + } |
| 121 | + catch (SQLException e2) { |
| 122 | + log.warn("Could not get catalogs", e2); |
| 123 | + sb.append(" <SQLException while getting catalogs>").append(nl); |
| 124 | + } |
| 125 | + finally { |
| 126 | + try { |
| 127 | + if (catalogRs != null) { |
| 128 | + catalogRs.close(); |
| 129 | + } |
| 130 | + } |
| 131 | + catch (Exception ignore) { |
| 132 | + } |
| 133 | + } |
| 134 | + return sb.toString(); |
| 135 | + } |
132 | 136 |
|
133 | 137 | protected Connection getConnection() throws SQLException { |
134 | 138 | if(connection==null) { |
135 | 139 | connection = connectionProvider.getConnection(); |
136 | 140 | } |
137 | 141 | return connection; |
138 | 142 | } |
139 | | - |
| 143 | + |
140 | 144 | public void close(Iterator<?> iterator) { |
141 | 145 | if(iterator instanceof ResultSetIterator) { |
142 | 146 | ((ResultSetIterator)iterator).close(); |
143 | 147 | } |
144 | 148 | } |
145 | | - |
| 149 | + |
146 | 150 | public boolean needQuote(String name) { |
147 | | - |
| 151 | + |
148 | 152 | if(name==null) return false; |
149 | | - |
150 | | - // TODO: use jdbc metadata to decide on this. but for now we just handle the most typical cases. |
| 153 | + |
| 154 | + // TODO: use jdbc metadata to decide on this. but for now we just handle the most typical cases. |
151 | 155 | if(name.indexOf('-')>0) return true; |
152 | 156 | if(name.indexOf(' ')>0) return true; |
153 | | - if(name.indexOf('.')>0) return true; |
154 | | - return false; |
| 157 | + return name.indexOf( '.' ) > 0; |
155 | 158 | } |
156 | | - |
| 159 | + |
157 | 160 | protected String caseForSearch(String value) throws SQLException { |
158 | 161 | // TODO: handle quoted requests (just strip it ?) |
159 | 162 | if(needQuote(value)) { |
160 | 163 | if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) { |
161 | 164 | return value; |
162 | | - } else if ( getMetaData().storesUpperCaseQuotedIdentifiers() ) { |
163 | | - return toUpperCase( value ); |
| 165 | + } else if ( getMetaData().storesUpperCaseQuotedIdentifiers() ) { |
| 166 | + return toUpperCase( value ); |
164 | 167 | } else if( getMetaData().storesLowerCaseQuotedIdentifiers() ) { |
165 | 168 | return toLowerCase( value ); |
166 | 169 | } else { |
167 | 170 | return value; |
168 | 171 | } |
169 | 172 | } else if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) { |
170 | 173 | return value; |
171 | | - } else if ( getMetaData().storesUpperCaseIdentifiers() ) { |
172 | | - return toUpperCase( value ); |
| 174 | + } else if ( getMetaData().storesUpperCaseIdentifiers() ) { |
| 175 | + return toUpperCase( value ); |
173 | 176 | } else if( getMetaData().storesLowerCaseIdentifiers() ) { |
174 | 177 | return toLowerCase( value ); |
175 | 178 | } else { |
176 | 179 | return value; |
177 | | - } |
| 180 | + } |
178 | 181 | } |
179 | 182 |
|
180 | 183 | private String toUpperCase(String str) { |
181 | 184 | return str==null ? null : str.toUpperCase(); |
182 | 185 | } |
183 | | - |
| 186 | + |
184 | 187 | private String toLowerCase(String str) { |
185 | 188 | return str == null ? null : str.toLowerCase(Locale.ENGLISH); |
186 | 189 | } |
187 | | - |
| 190 | + |
188 | 191 | public Iterator<Map<String, Object>> getSuggestedPrimaryKeyStrategyName(String catalog, String schema, String table) { |
189 | 192 | Map<String, Object> m = new HashMap<String, Object>(); |
190 | 193 | m.put( "TABLE_CAT", catalog ); |
|
0 commit comments