Skip to content

Commit

Permalink
(#115) Implemented functions fetching from Postgres, rendering functi…
Browse files Browse the repository at this point in the history
…ons from objects in DBD
  • Loading branch information
rocket-3 committed Nov 7, 2021
1 parent b9e6c11 commit 2fa4b8f
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.DbdFunctionFields;
import org.fusionsoft.lib.yaml.YamlMappingHasKeys;

public class DbdFunctionMapping extends YamlMappingHasKeys {

public DbdFunctionMapping(final YamlMapping mapping) {
super(
mapping,
DbdFunctionFields.OWNER,
DbdFunctionFields.DDL,
DbdFunctionFields.AGGREGATE
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.ofobjects;

import org.fusionsoft.database.mapping.MappingOfRepresentative;
import org.fusionsoft.database.mapping.dbd.DbdFunctionMapping;
import org.fusionsoft.database.mapping.entries.UnwrapEntriesOfObjects;
import org.fusionsoft.database.snapshot.DbObject;
import org.fusionsoft.database.snapshot.Objects;
import org.fusionsoft.database.snapshot.objects.ObjectType;
import org.fusionsoft.database.snapshot.objects.filtered.ObjectsWithParentAndType;
import org.fusionsoft.lib.yaml.YamlMappingOfEntries;

public class DbdFunctionsMappingOfObjects extends YamlMappingOfEntries {

public DbdFunctionsMappingOfObjects(
final Objects objects,
final DbObject<?> schema
) {
super(
new UnwrapEntriesOfObjects<DbdFunctionMapping>(
objects,
new ObjectsWithParentAndType(
schema,
ObjectType.FUNCTION,
objects
),
(objs, object) -> new DbdFunctionMapping(
new MappingOfRepresentative(object)
)
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public DbdSchemaMappingOfObjects(
)
)
),
new IterableOf<>(
new MapEntry<>(
DbdSchemaFields.FUNCTIONS,
new DbdFunctionsMappingOfObjects(
objects,
schema
)
)
),
new IterableOf<>(
new MapEntry<>(
DbdSchemaFields.DOMAINS,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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;

public enum DbdFunctionFields implements Text {
SCHEMA("schema"),
OWNER("owner"),
FUNCTION("function"),
ARGUMENTS("arguments"),
DDL("ddl"),
AGGREGATE("aggregate");

private final String text;

DbdFunctionFields(final String text) {
this.text = text;
}

@Override
public String asString() {
return this.text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public enum DbdSchemaFields implements Text {
*The dbd/schema/views field.
*/
VIEWS("views"),
/**
*The dbd/schema/functions field.
*/
FUNCTIONS("functions"),
/**
*The dbd/schema/domains field.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.DbdFunctionMapping;
import org.fusionsoft.database.mapping.entries.MultilineScalarEntry;
import org.fusionsoft.database.mapping.entries.ScalarEntry;
import org.fusionsoft.database.mapping.fields.DbdFunctionFields;
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 that can be constructed of.
* @since 0.1
*/
public class FunctionOfResultSet extends SimpleDbObject<DbdFunctionMapping> {

public FunctionOfResultSet(final ResultSet rset, final Query<DbdFunctionFields> query) {
super(
new DbdFunctionMapping(
new MappingWithoutNullScalars(
new YamlMappingOfEntries(
new ScalarEntry(
DbdFunctionFields.OWNER,
new TextOfResultSet(
query.outcomeFor(DbdFunctionFields.OWNER),
rset
)
),
new ScalarEntry(
DbdFunctionFields.ARGUMENTS,
new TextOfResultSet(
query.outcomeFor(DbdFunctionFields.ARGUMENTS),
rset
)
),
new ScalarEntry(
DbdFunctionFields.AGGREGATE,
new TextOfResultSet(
query.outcomeFor(DbdFunctionFields.AGGREGATE),
rset
)
),
new MultilineScalarEntry(
DbdFunctionFields.DDL,
new TextOfResultSet(
query.outcomeFor(DbdFunctionFields.DDL),
rset
)
)
)
)
),
new SimpleObjectSignature(
new FullObjectName(
new TextOfResultSet(
query.outcomeFor(DbdFunctionFields.SCHEMA),
rset
),
new TextOfResultSet(
query.outcomeFor(DbdFunctionFields.FUNCTION),
rset
)
),
ObjectType.FUNCTION
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.sql.Connection;
import org.fusionsoft.database.snapshot.objects.dbms.postgres.PgConstraints;
import org.fusionsoft.database.snapshot.objects.dbms.postgres.PgDomains;
import org.fusionsoft.database.snapshot.objects.dbms.postgres.PgFunctions;
import org.fusionsoft.database.snapshot.objects.dbms.postgres.PgIndexes;
import org.fusionsoft.database.snapshot.objects.dbms.postgres.PgSchemas;
import org.fusionsoft.database.snapshot.objects.dbms.postgres.PgSequences;
Expand Down Expand Up @@ -45,6 +46,7 @@ public ObjectsFromPostgres(final Connection connection) {
PgConstraints::new,
PgSequences::new,
PgViews::new,
PgFunctions::new,
PgTriggers::new,
PgDomains::new
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,50 @@
package org.fusionsoft.database.snapshot.objects.dbms.postgres;

import java.sql.Connection;
import org.fusionsoft.database.mapping.fields.DbdFunctionFields;
import org.fusionsoft.database.snapshot.DbObject;
import org.fusionsoft.database.snapshot.Objects;
import org.fusionsoft.database.snapshot.dbms.DbmsVersionOfConnection;
import org.fusionsoft.database.snapshot.objects.ObjectsOfScalar;
import org.fusionsoft.database.snapshot.objects.dbms.FunctionOfResultSet;
import org.fusionsoft.database.snapshot.query.Query;
import org.fusionsoft.database.snapshot.query.pg.PgFunctionsQuery;
import org.fusionsoft.lib.collection.ListOfResultSet;

/**
* The type of {@link Objects} that can be constructed of connection to Postgres DBMS.
* The type of functions {@link Objects} that can be constructed of connection to Postgres DBMS.
* @since 0.1
* @todo #101:30min Adapt query for functions from DbGit.
* @checkstyle StringLiteralsConcatenationCheck (100 lines)
*/
public class PgFunctions extends ObjectsOfScalar {

/**
* Instantiates a new Postgres ... .
* Instantiates a new Postgres functions.
* @param connection The Connection to be encapsulated.
*/
public PgFunctions(final Connection connection) {
this(
connection,
new PgFunctionsQuery(
new DbmsVersionOfConnection(connection)
)
);
}

/**
* Instantiates a new Postgres functions.
* @param connection The Connection to be encapsulated.
*/
public PgFunctions(final Connection connection, final Query<DbdFunctionFields> query) {
super(
() ->
new ListOfResultSet<DbObject<?>>(
PostgresSchemaOfResultSet::new,
() -> connection.createStatement().executeQuery(
""
)
rset -> new FunctionOfResultSet(
rset,
query
),
query,
connection
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public PgDomainConstraintsQuery(final FullObjectName domain) {
super(
"SELECT \n"
+ " con.conname AS {0},\n"
+ " con.convalidated AS {1},\n"
+ " CASE WHEN con.convalidated = 't' THEN 'true' ELSE 'false' END AS {1},\n"
+ " con.consrc AS {2}\n"
+ "FROM information_schema.domains dom\n"
+ "INNER JOIN information_schema.domain_constraints dcon \n"
Expand Down
Loading

0 comments on commit 2fa4b8f

Please sign in to comment.