@@ -24,8 +24,7 @@ public class SchemaCapturer implements AutoCloseable {
24
24
static final Logger LOGGER = LoggerFactory .getLogger (SchemaCapturer .class );
25
25
26
26
public static final HashSet <String > IGNORED_DATABASES = new HashSet <>(
27
- Arrays .asList (new String []{"performance_schema" , "information_schema" })
28
- );
27
+ Arrays .asList (new String [] { "performance_schema" , "information_schema" }));
29
28
30
29
private final Set <String > includeDatabases ;
31
30
private final Set <String > includeTables ;
@@ -36,12 +35,12 @@ public class SchemaCapturer implements AutoCloseable {
36
35
private final PreparedStatement columnPreparedStatement ;
37
36
private final PreparedStatement pkPreparedStatement ;
38
37
39
-
40
38
public SchemaCapturer (Connection c , CaseSensitivity sensitivity ) throws SQLException {
41
39
this (c , sensitivity , Collections .emptySet (), Collections .emptySet ());
42
40
}
43
41
44
- SchemaCapturer (Connection c , CaseSensitivity sensitivity , Set <String > includeDatabases , Set <String > includeTables ) throws SQLException {
42
+ SchemaCapturer (Connection c , CaseSensitivity sensitivity , Set <String > includeDatabases , Set <String > includeTables )
43
+ throws SQLException {
45
44
this .includeDatabases = includeDatabases ;
46
45
this .includeTables = includeTables ;
47
46
this .connection = c ;
@@ -99,12 +98,10 @@ public Schema capture() throws SQLException {
99
98
LOGGER .debug ("Capturing schemas..." );
100
99
ArrayList <Database > databases = new ArrayList <>();
101
100
102
- String dbCaptureQuery =
103
- "SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.SCHEMATA" ;
101
+ String dbCaptureQuery = "SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.SCHEMATA" ;
104
102
105
- if ( includeDatabases .size () > 0 ) {
106
- dbCaptureQuery +=
107
- " WHERE SCHEMA_NAME IN " + Sql .inListSQL (includeDatabases .size ());
103
+ if (includeDatabases .size () > 0 ) {
104
+ dbCaptureQuery += " WHERE SCHEMA_NAME IN " + Sql .inListSQL (includeDatabases .size ());
108
105
}
109
106
dbCaptureQuery += " ORDER BY SCHEMA_NAME" ;
110
107
@@ -135,13 +132,14 @@ public Schema capture() throws SQLException {
135
132
}
136
133
LOGGER .debug ("{} database schemas captured!" , size );
137
134
138
-
139
135
Schema s = new Schema (databases , captureDefaultCharset (), this .sensitivity );
140
136
try {
141
- if ( isMariaDB () && mariaSupportsJSON ()) {
142
- detectMariaDBJSON (s );
137
+ if (isMariaDB () && mariaSupportsJSON ()) {
138
+ for (String dbName : s .getDatabaseNames ()) {
139
+ detectMariaDBJSON (s , dbName );
140
+ }
143
141
}
144
- } catch ( InvalidSchemaError e ) {
142
+ } catch (InvalidSchemaError e ) {
145
143
e .printStackTrace ();
146
144
}
147
145
return s ;
@@ -150,13 +148,12 @@ public Schema capture() throws SQLException {
150
148
private String captureDefaultCharset () throws SQLException {
151
149
LOGGER .debug ("Capturing Default Charset" );
152
150
try (Statement stmt = connection .createStatement ();
153
- ResultSet rs = stmt .executeQuery ("select @@character_set_server" )) {
151
+ ResultSet rs = stmt .executeQuery ("select @@character_set_server" )) {
154
152
rs .next ();
155
153
return rs .getString ("@@character_set_server" );
156
154
}
157
155
}
158
156
159
-
160
157
private void captureDatabase (Database db ) throws SQLException {
161
158
tablePreparedStatement .setString (1 , db .getName ());
162
159
Sql .prepareInList (tablePreparedStatement , 2 , includeTables );
@@ -173,9 +170,8 @@ private void captureDatabase(Database db) throws SQLException {
173
170
captureTables (db , tables );
174
171
}
175
172
176
-
177
173
private boolean isMySQLAtLeast56 () throws SQLException {
178
- if ( isMariaDB () )
174
+ if (isMariaDB ())
179
175
return true ;
180
176
181
177
DatabaseMetaData meta = connection .getMetaData ();
@@ -190,13 +186,14 @@ private boolean isMariaDB() throws SQLException {
190
186
}
191
187
192
188
static final String MARIA_VERSION_REGEX = "[\\ d\\ .]+-(\\ d+)\\ .(\\ d+)" ;
189
+
193
190
private boolean mariaSupportsJSON () throws SQLException {
194
191
DatabaseMetaData meta = connection .getMetaData ();
195
192
String versionString = meta .getDatabaseProductVersion ();
196
193
Pattern pattern = Pattern .compile (MARIA_VERSION_REGEX );
197
194
Matcher m = pattern .matcher (versionString );
198
195
199
- if ( m .find () ) {
196
+ if (m .find ()) {
200
197
int major = Integer .parseInt (m .group (1 ));
201
198
int minor = Integer .parseInt (m .group (2 ));
202
199
@@ -267,7 +264,7 @@ private void captureTablesPK(Database db, HashMap<String, Table> tables) throws
267
264
String columnName = rs .getString ("COLUMN_NAME" );
268
265
269
266
ArrayList <String > pkList = tablePKMap .get (tableName );
270
- if ( pkList != null )
267
+ if (pkList != null )
271
268
pkList .add (ordinalPosition - 1 , columnName );
272
269
}
273
270
}
@@ -294,7 +291,7 @@ static String[] extractEnumValues(String expandedType) {
294
291
Matcher enumMatcher = pattern .matcher (enumValues );
295
292
296
293
List <String > result = new ArrayList <>();
297
- while (enumMatcher .find ()) {
294
+ while (enumMatcher .find ()) {
298
295
String value = enumMatcher .group (0 );
299
296
if (value .startsWith ("'" ))
300
297
value = value .substring (1 );
@@ -309,44 +306,48 @@ static String[] extractEnumValues(String expandedType) {
309
306
@ Override
310
307
public void close () throws SQLException {
311
308
try (PreparedStatement p1 = tablePreparedStatement ;
312
- PreparedStatement p2 = columnPreparedStatement ;
313
- PreparedStatement p3 = pkPreparedStatement ) {
309
+ PreparedStatement p2 = columnPreparedStatement ;
310
+ PreparedStatement p3 = pkPreparedStatement ) {
314
311
// auto-close shared prepared statements
315
312
}
316
313
}
317
314
318
- private void detectMariaDBJSON (Schema schema ) throws SQLException , InvalidSchemaError {
315
+ private void detectMariaDBJSON (Schema schema , String dbName ) throws SQLException , InvalidSchemaError {
319
316
String checkConstraintSQL = "SELECT CONSTRAINT_SCHEMA, TABLE_NAME, CONSTRAINT_NAME, CHECK_CLAUSE " +
320
- "from INFORMATION_SCHEMA.CHECK_CONSTRAINTS " +
321
- "where CHECK_CLAUSE LIKE 'json_valid(%)'" ;
317
+ "from INFORMATION_SCHEMA.CHECK_CONSTRAINTS " +
318
+ "where CONSTRAINT_SCHEMA = ? AND CHECK_CLAUSE LIKE 'json_valid(%)'" ;
322
319
323
320
String regex = "json_valid\\ (`(.*)`\\ )" ;
324
321
Pattern pattern = Pattern .compile (regex );
325
322
326
323
try (
327
- PreparedStatement statement = connection .prepareStatement (checkConstraintSQL );
328
- ResultSet rs = statement .executeQuery ()
329
- ) {
330
- while ( rs .next () ) {
331
- String checkClause = rs .getString ("CHECK_CLAUSE" );
332
- Matcher m = pattern .matcher (checkClause );
333
- if ( m .find () ) {
334
- String column = m .group (1 );
335
- Database d = schema .findDatabase (rs .getString ("CONSTRAINT_SCHEMA" ));
336
- if ( d == null ) continue ;
337
- Table t = d .findTable (rs .getString ("TABLE_NAME" ));
338
- if ( t == null ) continue ;
339
- short i = t .findColumnIndex (column );
340
- if ( i < 0 ) continue ;
341
-
342
- ColumnDef cd = t .findColumn (i );
343
- if ( cd instanceof StringColumnDef ) {
344
- t .replaceColumn (i , JsonColumnDef .create (cd .getName (), "json" , i ));
324
+ PreparedStatement statement = connection .prepareStatement (checkConstraintSQL )) {
325
+ statement .setString (1 , dbName );
326
+
327
+ try (ResultSet rs = statement .executeQuery ()) {
328
+ while (rs .next ()) {
329
+ String checkClause = rs .getString ("CHECK_CLAUSE" );
330
+ Matcher m = pattern .matcher (checkClause );
331
+ if (m .find ()) {
332
+ String column = m .group (1 );
333
+ Database d = schema .findDatabase (rs .getString ("CONSTRAINT_SCHEMA" ));
334
+ if (d == null )
335
+ continue ;
336
+ Table t = d .findTable (rs .getString ("TABLE_NAME" ));
337
+ if (t == null )
338
+ continue ;
339
+ short i = t .findColumnIndex (column );
340
+ if (i < 0 )
341
+ continue ;
342
+
343
+ ColumnDef cd = t .findColumn (i );
344
+ if (cd instanceof StringColumnDef ) {
345
+ t .replaceColumn (i , JsonColumnDef .create (cd .getName (), "json" , i ));
346
+ }
345
347
}
346
348
}
347
349
}
348
350
}
349
-
350
351
}
351
352
352
353
}
0 commit comments