diff --git a/src/main/java/org/fusionsoft/database/mapping/dbd/DbdTriggerMapping.java b/src/main/java/org/fusionsoft/database/mapping/dbd/DbdTriggerMapping.java new file mode 100644 index 0000000..749bde4 --- /dev/null +++ b/src/main/java/org/fusionsoft/database/mapping/dbd/DbdTriggerMapping.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2018-2021 FusionSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. + * + * See the License for the specific language governing permissions + * and limitations under the License. + */ +package org.fusionsoft.database.mapping.dbd; + +import com.amihaiemil.eoyaml.YamlMapping; +import org.fusionsoft.database.mapping.fields.DbdTriggerFields; +import org.fusionsoft.lib.yaml.YamlMappingHasKeys; + +/** + * The type of {@link YamlMappingHasKeys} of DBD/schemas/#schema/triggers/#trigger document node + * that are validated by {@link DbdTriggerFields}. + * @since 0.1 + */ +public class DbdTriggerMapping extends YamlMappingHasKeys { + + /** + * Instantiates a new Dbd trigger mapping. + * @param mapping The YamlMapping to be encapsulated. + */ + public DbdTriggerMapping(final YamlMapping mapping) { + super(mapping, DbdTriggerFields.DDL); + } + +} diff --git a/src/main/java/org/fusionsoft/database/mapping/fields/DbdTriggerFields.java b/src/main/java/org/fusionsoft/database/mapping/fields/DbdTriggerFields.java new file mode 100644 index 0000000..e99e334 --- /dev/null +++ b/src/main/java/org/fusionsoft/database/mapping/fields/DbdTriggerFields.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2018-2021 FusionSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. + * + * See the License for the specific language governing permissions + * and limitations under the License. + */ +package org.fusionsoft.database.mapping.fields; + +import org.cactoos.Text; + +/** + * The enum of {@link org.fusionsoft.database.mapping.dbd.DbdTriggerMapping} fields. + */ +public enum DbdTriggerFields implements Text { + /** + *Schema dbd trigger fields. + */ + SCHEMA("schema"), + /** + *Table dbd trigger fields. + */ + TABLE("table"), + /** + *Trigger dbd trigger fields. + */ + TRIGGER("trigger"), + /** + *Ddl dbd trigger fields. + */ + DDL("ddl"); + + /** + * The String encapsulated. + */ + private final String text; + + /** + * Instantiates a new Dbd trigger fields. + * @param text The String to be encapsulated. + */ + DbdTriggerFields(final String text) { + this.text = text; + } + + @Override + public String asString() { + return this.text; + } +} diff --git a/src/main/java/org/fusionsoft/database/snapshot/objects/dbms/TriggerOfResultSet.java b/src/main/java/org/fusionsoft/database/snapshot/objects/dbms/TriggerOfResultSet.java new file mode 100644 index 0000000..219617f --- /dev/null +++ b/src/main/java/org/fusionsoft/database/snapshot/objects/dbms/TriggerOfResultSet.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2018-2021 FusionSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. + * + * See the License for the specific language governing permissions + * and limitations under the License. + */ +package org.fusionsoft.database.snapshot.objects.dbms; + +import java.sql.ResultSet; +import org.fusionsoft.database.mapping.dbd.DbdTriggerMapping; +import org.fusionsoft.database.mapping.entries.MultilineScalarEntry; +import org.fusionsoft.database.mapping.fields.DbdTriggerFields; +import org.fusionsoft.database.snapshot.objects.ObjectType; +import org.fusionsoft.database.snapshot.objects.SimpleDbObject; +import org.fusionsoft.database.snapshot.objectsignature.FullObjectName; +import org.fusionsoft.database.snapshot.objectsignature.SimpleObjectSignature; +import org.fusionsoft.database.snapshot.query.Query; +import org.fusionsoft.lib.text.TextOfResultSet; +import org.fusionsoft.lib.yaml.MappingWithoutNullScalars; +import org.fusionsoft.lib.yaml.YamlMappingOfEntries; + +/** + * The type of {@link DbdTriggerMapping}, + * can be constructed of {@link ResultSet} and {@link Query} of {@link DbdTriggerFields}. + * @since 0.1 + */ +public class TriggerOfResultSet extends SimpleDbObject { + + /** + * Instantiates a new Trigger of result set. + * @param rset The ResultSet to be encapsulated. + * @param query The Query of {@link DbdTriggerFields} to be encapsulated. + */ + public TriggerOfResultSet(final ResultSet rset, final Query query) { + super( + new DbdTriggerMapping( + new MappingWithoutNullScalars( + new YamlMappingOfEntries( + new MultilineScalarEntry( + DbdTriggerFields.DDL, + new TextOfResultSet( + DbdTriggerFields.DDL, + rset + ) + ) + ) + ) + ), + new SimpleObjectSignature( + new FullObjectName( + new TextOfResultSet( + query.outcomeFor(DbdTriggerFields.SCHEMA), + rset + ), + new TextOfResultSet( + query.outcomeFor(DbdTriggerFields.TABLE), + rset + ), + new TextOfResultSet( + query.outcomeFor(DbdTriggerFields.TRIGGER), + rset + ) + ), + ObjectType.TRIGGER + ) + ); + } + +} diff --git a/src/main/java/org/fusionsoft/database/snapshot/objects/dbms/postgres/PgTriggers.java b/src/main/java/org/fusionsoft/database/snapshot/objects/dbms/postgres/PgTriggers.java index a039c3a..f1f47f7 100644 --- a/src/main/java/org/fusionsoft/database/snapshot/objects/dbms/postgres/PgTriggers.java +++ b/src/main/java/org/fusionsoft/database/snapshot/objects/dbms/postgres/PgTriggers.java @@ -16,14 +16,16 @@ package org.fusionsoft.database.snapshot.objects.dbms.postgres; import java.sql.Connection; -import org.cactoos.iterable.IterableOf; +import org.fusionsoft.database.snapshot.DbObject; import org.fusionsoft.database.snapshot.Objects; import org.fusionsoft.database.snapshot.objects.ObjectsOfScalar; +import org.fusionsoft.database.snapshot.objects.dbms.TriggerOfResultSet; +import org.fusionsoft.database.snapshot.query.PgTriggersQuery; +import org.fusionsoft.lib.collection.ListOfResultSet; /** * The type of {@link Objects} that can be constructed of connection to Postgres DBMS. * @since 0.1 - * @todo #101:30min Adapt query for triggers from DbGit. * @checkstyle StringLiteralsConcatenationCheck (100 lines) */ @SuppressWarnings("PMD") @@ -35,7 +37,12 @@ public class PgTriggers extends ObjectsOfScalar { */ public PgTriggers(final Connection connection) { super( - () -> new IterableOf<>() + () -> + new ListOfResultSet>( + rset -> new TriggerOfResultSet(rset, new PgTriggersQuery()), + new PgTriggersQuery(), + connection + ) ); } diff --git a/src/main/java/org/fusionsoft/database/snapshot/query/PgTriggersQuery.java b/src/main/java/org/fusionsoft/database/snapshot/query/PgTriggersQuery.java new file mode 100644 index 0000000..17bf4df --- /dev/null +++ b/src/main/java/org/fusionsoft/database/snapshot/query/PgTriggersQuery.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018-2021 FusionSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. + * + * See the License for the specific language governing permissions + * and limitations under the License. + */ +package org.fusionsoft.database.snapshot.query; + +import org.fusionsoft.database.mapping.fields.DbdTriggerFields; + +/** + * The only type of {@link PgMessageFormatQuery} of {@link DbdTriggerFields}. + * @since 0.1 + */ +public class PgTriggersQuery extends PgMessageFormatQuery { + + /** + * Instantiates a new Pg triggers query. + */ + public PgTriggersQuery() { + super( + "SELECT \n" + + " ns.nspname AS {0},\n" + + " tbl.relname AS {1},\n" + + " trg.tgname AS {2},\n" + + " pg_get_triggerdef(trg.oid) AS {3} \n" + + "FROM pg_trigger trg\n" + + "JOIN pg_class tbl on trg.tgrelid = tbl.oid\n" + + "JOIN pg_namespace ns ON ns.oid = tbl.relnamespace\n" + + "and trg.tgconstraint=0", + DbdTriggerFields.SCHEMA, + DbdTriggerFields.TABLE, + DbdTriggerFields.TRIGGER, + DbdTriggerFields.DDL + ); + } + +}