Skip to content

Commit

Permalink
(#116) Implemented PgTriggers, created DbdTriggerMapping, DbdTriggerF…
Browse files Browse the repository at this point in the history
…ields, PgTriggersQuery, TriggerOfResultSet
  • Loading branch information
rocket-3 committed Nov 4, 2021
1 parent 117d1db commit 6cf2852
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<DbdTriggerMapping> {

/**
* 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<DbdTriggerFields> 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
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -35,7 +37,12 @@ public class PgTriggers extends ObjectsOfScalar {
*/
public PgTriggers(final Connection connection) {
super(
() -> new IterableOf<>()
() ->
new ListOfResultSet<DbObject<?>>(
rset -> new TriggerOfResultSet(rset, new PgTriggersQuery()),
new PgTriggersQuery(),
connection
)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<DbdTriggerFields> {

/**
* 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
);
}

}

0 comments on commit 6cf2852

Please sign in to comment.