| 1 | |
package liquibase.sqlgenerator.core; |
| 2 | |
|
| 3 | |
import liquibase.database.Database; |
| 4 | |
import liquibase.database.core.PostgresDatabase; |
| 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.SelectSequencesStatement; |
| 13 | |
|
| 14 | 10 | public class SelectSequencesGeneratorPostgres extends AbstractSqlGenerator<SelectSequencesStatement> { |
| 15 | |
@Override |
| 16 | |
public int getPriority() { |
| 17 | 1 | return PRIORITY_DATABASE; |
| 18 | |
} |
| 19 | |
|
| 20 | |
@Override |
| 21 | |
public boolean supports(SelectSequencesStatement statement, Database database) { |
| 22 | 0 | return database instanceof PostgresDatabase; |
| 23 | |
} |
| 24 | |
|
| 25 | |
public ValidationErrors validate(SelectSequencesStatement statement, Database database, |
| 26 | |
SqlGeneratorChain sqlGeneratorChain) { |
| 27 | 0 | return new ValidationErrors(); |
| 28 | |
} |
| 29 | |
|
| 30 | |
public Sql[] generateSql(SelectSequencesStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { |
| 31 | |
try { |
| 32 | 0 | String schema = statement.getSchemaName(); |
| 33 | |
|
| 34 | 0 | return new Sql[] { new UnparsedSql( |
| 35 | |
"SELECT relname AS SEQUENCE_NAME FROM pg_class, pg_namespace " |
| 36 | |
+ "WHERE relkind='S' " |
| 37 | |
+ "AND pg_class.relnamespace = pg_namespace.oid " |
| 38 | |
+ "AND nspname = '" |
| 39 | |
+ database.convertRequestedSchemaToSchema(schema) |
| 40 | |
+ "' " |
| 41 | |
+ "AND 'nextval(''" |
| 42 | |
+ (schema == null ? "" : schema + ".") |
| 43 | |
+ "'||relname||'''::regclass)' not in (select adsrc from pg_attrdef where adsrc is not null) " |
| 44 | |
+ "AND 'nextval(''" |
| 45 | |
+ (schema == null ? "" : schema + ".") |
| 46 | |
+ "\"'||relname||'\"''::regclass)' not in (select adsrc from pg_attrdef where adsrc is not null) " |
| 47 | |
+ "AND 'nextval('''||relname||'''::regclass)' not in (select adsrc from pg_attrdef where adsrc is not null)") }; |
| 48 | 0 | } catch (DatabaseException e) { |
| 49 | 0 | throw new UnexpectedLiquibaseException(e); |
| 50 | |
} |
| 51 | |
} |
| 52 | |
} |