-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1190 from schemacrawler/chatgpt
New function got table relationships
- Loading branch information
Showing
14 changed files
with
419 additions
and
2 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...java/schemacrawler/tools/command/chatgpt/functions/TableReferencesFunctionDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
======================================================================== | ||
SchemaCrawler | ||
http://www.schemacrawler.com | ||
Copyright (c) 2000-2023, Sualeh Fatehi <sualeh@hotmail.com>. | ||
All rights reserved. | ||
------------------------------------------------------------------------ | ||
SchemaCrawler is distributed in the hope that it will be useful, but | ||
WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
SchemaCrawler and the accompanying materials are made available under | ||
the terms of the Eclipse Public License v1.0, GNU General Public License | ||
v3 or GNU Lesser General Public License v3. | ||
You may elect to redistribute this code under any of these licenses. | ||
The Eclipse Public License is available at: | ||
http://www.eclipse.org/legal/epl-v10.html | ||
The GNU General Public License v3 and the GNU Lesser General Public | ||
License v3 are available at: | ||
http://www.gnu.org/licenses/ | ||
======================================================================== | ||
*/ | ||
|
||
package schemacrawler.tools.command.chatgpt.functions; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import schemacrawler.schema.Table; | ||
|
||
public final class TableReferencesFunctionDefinition | ||
extends AbstractFunctionDefinition<TableReferencesFunctionParameters> { | ||
|
||
public TableReferencesFunctionDefinition() { | ||
super( | ||
"get-table-references", | ||
"Gets the relationships of a database table, either child tables or parent tables. " | ||
+ "Child tables are also known as referencing tables or foreign key tables. " | ||
+ "Parent tables are also known as referenced tables, or primary key tables.", | ||
TableReferencesFunctionParameters.class); | ||
} | ||
|
||
@Override | ||
public Function<TableReferencesFunctionParameters, FunctionReturn> getExecutor() { | ||
return args -> { | ||
final String regex = String.format("(?i).*%s.*", args.getTableNameContains()); | ||
final Optional<Table> firstMatchedTable = | ||
catalog.getTables().stream().filter(table -> table.getName().matches(regex)).findFirst(); | ||
|
||
if (firstMatchedTable.isPresent()) { | ||
final Table table = firstMatchedTable.get(); | ||
return new TableReferencesFunctionReturn(table, args.getTableReferenceType()); | ||
} else { | ||
return new NoResultsReturn(); | ||
} | ||
}; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...java/schemacrawler/tools/command/chatgpt/functions/TableReferencesFunctionParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package schemacrawler.tools.command.chatgpt.functions; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonPropertyDescription; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
||
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) | ||
public class TableReferencesFunctionParameters implements FunctionParameters { | ||
|
||
public enum TableReferenceType { | ||
all, | ||
parent, | ||
child; | ||
} | ||
|
||
@JsonPropertyDescription( | ||
"Part of the name of database table to find. For example, 'ABC' is a part of 'QWEABCXYZ'.") | ||
@JsonProperty(required = true) | ||
private String tableNameContains; | ||
|
||
@JsonPropertyDescription( | ||
"The type of related tables requested - either child tables or parent tables, or both types (all relationships).") | ||
private TableReferenceType tableReferenceType; | ||
|
||
public String getTableNameContains() { | ||
return tableNameContains; | ||
} | ||
|
||
public TableReferenceType getTableReferenceType() { | ||
if (tableReferenceType == null) { | ||
return TableReferenceType.all; | ||
} | ||
return tableReferenceType; | ||
} | ||
|
||
public void setTableNameContains(final String tableNameContains) { | ||
this.tableNameContains = tableNameContains; | ||
} | ||
|
||
public void setTableReferenceType(final TableReferenceType tableReferenceType) { | ||
this.tableReferenceType = tableReferenceType; | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
...ain/java/schemacrawler/tools/command/chatgpt/functions/TableReferencesFunctionReturn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
======================================================================== | ||
SchemaCrawler | ||
http://www.schemacrawler.com | ||
Copyright (c) 2000-2023, Sualeh Fatehi <sualeh@hotmail.com>. | ||
All rights reserved. | ||
------------------------------------------------------------------------ | ||
SchemaCrawler is distributed in the hope that it will be useful, but | ||
WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
SchemaCrawler and the accompanying materials are made available under | ||
the terms of the Eclipse Public License v1.0, GNU General Public License | ||
v3 or GNU Lesser General Public License v3. | ||
You may elect to redistribute this code under any of these licenses. | ||
The Eclipse Public License is available at: | ||
http://www.eclipse.org/legal/epl-v10.html | ||
The GNU General Public License v3 and the GNU Lesser General Public | ||
License v3 are available at: | ||
http://www.gnu.org/licenses/ | ||
======================================================================== | ||
*/ | ||
|
||
package schemacrawler.tools.command.chatgpt.functions; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import schemacrawler.schema.Table; | ||
import schemacrawler.schema.TableRelationshipType; | ||
import schemacrawler.schema.View; | ||
import schemacrawler.schemacrawler.IdentifierQuotingStrategy; | ||
import schemacrawler.schemacrawler.Identifiers; | ||
import schemacrawler.schemacrawler.IdentifiersBuilder; | ||
import schemacrawler.tools.command.chatgpt.functions.TableReferencesFunctionParameters.TableReferenceType; | ||
|
||
public class TableReferencesFunctionReturn implements FunctionReturn { | ||
|
||
private static final String NEW_LINE = String.format("%n"); | ||
private static final Identifiers identifiers = | ||
IdentifiersBuilder.builder() | ||
.withIdentifierQuotingStrategy(IdentifierQuotingStrategy.quote_all) | ||
.toOptions(); | ||
private final Table table; | ||
private final TableReferenceType tableReferenceType; | ||
|
||
protected TableReferencesFunctionReturn(final Table table, final TableReferenceType scope) { | ||
this.table = requireNonNull(table, "Table not provided"); | ||
this.tableReferenceType = requireNonNull(scope, "Table description scope not provided"); | ||
} | ||
|
||
@Override | ||
public String render() { | ||
switch (tableReferenceType) { | ||
case parent: | ||
return renderTableRelationships(TableReferenceType.parent); | ||
case child: | ||
return renderTableRelationships(TableReferenceType.child); | ||
case all: | ||
// Fall-through | ||
default: | ||
return renderTableRelationships(TableReferenceType.parent) | ||
+ NEW_LINE | ||
+ renderTableRelationships(TableReferenceType.child); | ||
} | ||
} | ||
|
||
private String noData(final TableReferenceType tableReferenceType) { | ||
final StringBuilder buffer = new StringBuilder(); | ||
if (table instanceof View) { | ||
buffer.append("View "); | ||
} else { | ||
buffer.append("Table "); | ||
} | ||
buffer | ||
.append(identifiers.quoteFullName(table)) | ||
.append(" has no ") | ||
.append(tableReferenceType.name()) | ||
.append(" relationships.") | ||
.append(NEW_LINE); | ||
return buffer.toString(); | ||
} | ||
|
||
private String renderTableRelationships(final TableReferenceType tableReferenceType) { | ||
requireNonNull(tableReferenceType, "No table relationship type specified"); | ||
|
||
final Collection<Table> referencedTables; | ||
switch (tableReferenceType) { | ||
case parent: | ||
referencedTables = table.getRelatedTables(TableRelationshipType.parent); | ||
break; | ||
case child: | ||
referencedTables = table.getRelatedTables(TableRelationshipType.child); | ||
break; | ||
default: | ||
referencedTables = Collections.emptyList(); | ||
} | ||
|
||
if (referencedTables.isEmpty()) { | ||
return noData(tableReferenceType); | ||
} | ||
|
||
final StringBuilder buffer = new StringBuilder(); | ||
tableName(buffer, tableReferenceType); | ||
|
||
for (final Table referencedTable : referencedTables) { | ||
buffer | ||
.append(String.format("- %s", identifiers.quoteFullName(referencedTable))) | ||
.append(NEW_LINE); | ||
} | ||
return buffer.toString(); | ||
} | ||
|
||
private void tableName(final StringBuilder buffer, final TableReferenceType tableReferenceType) { | ||
requireNonNull(buffer); | ||
if (table instanceof View) { | ||
buffer.append("View "); | ||
} else { | ||
buffer.append("Table "); | ||
} | ||
buffer | ||
.append(identifiers.quoteFullName(table)) | ||
.append(" has the following ") | ||
.append(tableReferenceType.name()) | ||
.append(" tables:") | ||
.append(NEW_LINE); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ources/META-INF/services/schemacrawler.tools.command.chatgpt.functions.FunctionDefinition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
schemacrawler.tools.command.chatgpt.functions.DatabaseObjectListFunctionDefinition | ||
schemacrawler.tools.command.chatgpt.functions.TableDecriptionFunctionDefinition | ||
schemacrawler.tools.command.chatgpt.functions.TableReferencesFunctionDefinition | ||
schemacrawler.tools.command.chatgpt.functions.ExitFunctionDefinition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.