1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.MySQLDatabase; |
5 | |
import liquibase.exception.DatabaseException; |
6 | |
import liquibase.exception.UnexpectedLiquibaseException; |
7 | |
import liquibase.exception.ValidationErrors; |
8 | |
import liquibase.sql.Sql; |
9 | |
import liquibase.sql.UnparsedSql; |
10 | |
import liquibase.sqlgenerator.SqlGenerator; |
11 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
12 | |
import liquibase.statement.core.FindForeignKeyConstraintsStatement; |
13 | |
|
14 | 11 | public class FindForeignKeyConstraintsGeneratorMySQL extends AbstractSqlGenerator<FindForeignKeyConstraintsStatement> { |
15 | |
@Override |
16 | |
public int getPriority() { |
17 | 1 | return PRIORITY_DATABASE; |
18 | |
} |
19 | |
|
20 | |
@Override |
21 | |
public boolean supports(FindForeignKeyConstraintsStatement statement, Database database) { |
22 | 1 | return database instanceof MySQLDatabase; |
23 | |
} |
24 | |
|
25 | |
public ValidationErrors validate(FindForeignKeyConstraintsStatement findForeignKeyConstraintsStatement, |
26 | |
Database database, SqlGeneratorChain sqlGeneratorChain) { |
27 | 0 | ValidationErrors validationErrors = new ValidationErrors(); |
28 | 0 | validationErrors.checkRequiredField("baseTableName", findForeignKeyConstraintsStatement.getBaseTableName()); |
29 | 0 | return validationErrors; |
30 | |
} |
31 | |
|
32 | |
public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database, |
33 | |
SqlGeneratorChain sqlGeneratorChain) { |
34 | 0 | StringBuilder sb = new StringBuilder(); |
35 | |
|
36 | 0 | sb.append("SELECT "); |
37 | 0 | sb.append("RC.TABLE_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME) |
38 | |
.append(", "); |
39 | 0 | sb.append("KCU.COLUMN_NAME as ") |
40 | |
.append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(", "); |
41 | 0 | sb.append("RC.REFERENCED_TABLE_NAME ") |
42 | |
.append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(", "); |
43 | 0 | sb.append("KCU.REFERENCED_COLUMN_NAME as ") |
44 | |
.append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(", "); |
45 | 0 | sb.append("RC.CONSTRAINT_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME) |
46 | |
.append(" "); |
47 | 0 | sb.append("FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC,"); |
48 | 0 | sb.append(" INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU "); |
49 | 0 | sb.append("WHERE RC.TABLE_NAME = KCU.TABLE_NAME "); |
50 | 0 | sb.append("AND RC.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA "); |
51 | 0 | sb.append("AND RC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME "); |
52 | 0 | sb.append("AND RC.TABLE_NAME = '").append(statement.getBaseTableName()).append("' "); |
53 | |
try { |
54 | 0 | sb.append("AND RC.CONSTRAINT_SCHEMA = '").append(database.convertRequestedSchemaToSchema(null)).append("'"); |
55 | 0 | } catch (DatabaseException e) { |
56 | 0 | throw new UnexpectedLiquibaseException(e); |
57 | 0 | } |
58 | 0 | sb.append("LIMIT 1"); |
59 | 0 | return new Sql[] { new UnparsedSql(sb.toString()) }; |
60 | |
} |
61 | |
} |