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