Skip to content

Commit

Permalink
Compact database schema
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Jul 23, 2023
1 parent 78ea39e commit 3b26011
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,37 @@
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public final class CatalogDescription {

private final List<TableDescription> tables;
private final List<SchemaDescription> schemas;

public CatalogDescription() {
tables = new ArrayList<>();
schemas = new ArrayList<>();
}

public void addTable(final TableDescription table) {
if (table != null) {
tables.add(table);
public void addSchema(final SchemaDescription schema) {
if (schema != null) {
schemas.add(schema);
}
}

public List<TableDescription> getTables() {
return tables;
public List<SchemaDescription> getSchemas() {
return schemas;
}

@Override
public String toString() {
try {
return new ObjectMapper().writeValueAsString(this);
} catch (final JsonProcessingException e) {
return super.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package schemacrawler.tools.command.chatgpt.systemfunctions;

import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public final class SchemaDescription {

private String name;
private final List<TableDescription> tables;

public SchemaDescription() {
tables = new ArrayList<>();
}

public void addTable(final TableDescription table) {
if (table != null) {
tables.add(table);
}
}

public String getName() {
return name;
}

public List<TableDescription> getTables() {
return tables;
}

public void setName(final String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.function.Function;
import schemacrawler.schema.Column;
import schemacrawler.schema.Schema;
import schemacrawler.schema.Table;
import schemacrawler.schemacrawler.exceptions.ExecutionRuntimeException;
import schemacrawler.tools.command.chatgpt.FunctionReturn;
Expand Down Expand Up @@ -59,19 +60,23 @@ public Function<NoFunctionParameters, FunctionReturn> getExecutor() {

protected CatalogDescription createCatalogDescription() {
final CatalogDescription catalogDescription = new CatalogDescription();
for (final Table table : catalog.getTables()) {
final TableDescription tableDescription = new TableDescription();
tableDescription.setSchema(table.getSchema().getFullName());
tableDescription.setName(table.getName());
tableDescription.setRemarks(table.getRemarks());
for (final Column column : table.getColumns()) {
final ColumnDescription columnDescription = new ColumnDescription();
columnDescription.setName(column.getName());
columnDescription.setDataType(column.getColumnDataType().getName());
columnDescription.setRemarks(column.getRemarks());
tableDescription.addColumn(columnDescription);
for (final Schema schema : catalog.getSchemas()) {
final SchemaDescription schemaDescription = new SchemaDescription();
schemaDescription.setName(schema.getFullName());
for (final Table table : catalog.getTables(schema)) {
final TableDescription tableDescription = new TableDescription();
tableDescription.setName(table.getName());
tableDescription.setRemarks(table.getRemarks());
for (final Column column : table.getColumns()) {
final ColumnDescription columnDescription = new ColumnDescription();
columnDescription.setName(column.getName());
columnDescription.setDataType(column.getColumnDataType().getName());
columnDescription.setRemarks(column.getRemarks());
tableDescription.addColumn(columnDescription);
}
schemaDescription.addTable(tableDescription);
}
catalogDescription.addTable(tableDescription);
catalogDescription.addSchema(schemaDescription);
}
return catalogDescription;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public final class TableDescription {

private String schema;
private String name;
private final List<ColumnDescription> columns;
private String remarks;
Expand All @@ -39,20 +36,11 @@ public String getRemarks() {
return remarks;
}

public String getSchema() {
return schema;
}

public void setName(final String name) {
this.name = name;
}

public void setRemarks(final String remarks) {
this.remarks = remarks;
}

@JsonSetter(nulls = Nulls.AS_EMPTY)
public void setSchema(final String schema) {
this.schema = schema;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"tables":[{"schema":"PUBLIC.BOOKS","name":"AUTHORS","columns":[{"name":"ID","type":"INTEGER"},{"name":"FIRSTNAME","type":"VARCHAR"},{"name":"LASTNAME","type":"VARCHAR"},{"name":"ADDRESS1","type":"VARCHAR"},{"name":"ADDRESS2","type":"VARCHAR"},{"name":"CITY","type":"VARCHAR"},{"name":"STATE","type":"CHARACTER"},{"name":"POSTALCODE","type":"VARCHAR"},{"name":"COUNTRY","type":"VARCHAR"}],"remarks":"Contact details for book authors"},{"schema":"PUBLIC.BOOKS","name":"BOOKS","columns":[{"name":"ID","remarks":"Unique (internal) id for book","type":"INTEGER"},{"name":"TITLE","remarks":"Book title","type":"VARCHAR"},{"name":"DESCRIPTION","remarks":"Book description\n(Usually the blurb from the book jacket or promotional materials)","type":"VARCHAR"},{"name":"PUBLISHERID","remarks":"Foreign key to the book publisher","type":"INTEGER"},{"name":"PUBLICATIONDATE","remarks":"Book publication date","type":"DATE"},{"name":"PRICE","remarks":"Current price for the book","type":"DOUBLE"},{"name":"PREVIOUSEDITIONID","type":"INTEGER"}],"remarks":"Details for published books"},{"schema":"PUBLIC.BOOKS","name":"Celebrities","columns":[{"name":"Id","type":"INTEGER"},{"name":"NAME","type":"VARCHAR"}]},{"schema":"PUBLIC.BOOKS","name":"COUPONS","columns":[{"name":"ID","type":"INTEGER"},{"name":"DATA","type":"CLOB"},{"name":"COUPONS","type":"INTEGER ARRAY"},{"name":"BOOKS","type":"VARCHAR(20) ARRAY[10]"}]},{"schema":"PUBLIC.BOOKS","name":"CUSTOMERDATA","columns":[{"name":"ID","type":"INTEGER"},{"name":"DATA","type":"VALID_STRING"}]},{"schema":"PUBLIC.BOOKS","name":"CUSTOMERS","columns":[{"name":"ID","type":"INTEGER"},{"name":"FIRSTNAME","type":"NAME_TYPE"},{"name":"LASTNAME","type":"NAME_TYPE"},{"name":"AGE","type":"AGE_TYPE"}]},{"schema":"PUBLIC.BOOKS","name":"PUBLISHERS","columns":[{"name":"ID","remarks":"Unique (internal) id for book publisher","type":"INTEGER"},{"name":"PUBLISHER","remarks":"Name of book publisher","type":"VARCHAR"}],"remarks":"List of book publishers"},{"schema":"PUBLIC.\"PUBLISHER SALES\"","name":"REGIONS","columns":[{"name":"CITY","type":"VARCHAR"},{"name":"STATE","type":"VARCHAR"},{"name":"POSTALCODE","type":"VARCHAR"},{"name":"COUNTRY","type":"VARCHAR"}]},{"schema":"PUBLIC.\"PUBLISHER SALES\"","name":"SALESDATA","columns":[{"name":"SALESDATAID","type":"INTEGER"},{"name":"YEARLYAMOUNT","type":"DOUBLE"}]},{"schema":"PUBLIC.BOOKS","name":"BOOKAUTHORS","columns":[{"name":"BOOKID","type":"INTEGER"},{"name":"AUTHORID","type":"INTEGER"},{"name":"SOMEDATA","type":"VARCHAR"}],"remarks":"Relationship between books and their authors, \nalong with the latest updated information"},{"schema":"PUBLIC.BOOKS","name":"Celebrity Updates","columns":[{"name":"Celebrity Id","type":"INTEGER"},{"name":"UPDATE","type":"VARCHAR"}]},{"schema":"PUBLIC.BOOKS","name":"ΒΙΒΛΊΑ","columns":[{"name":"ΜΟΝΑΔΙΚΌΣ","type":"SMALLINT"},{"name":"ΤΊΤΛΟΣ","type":"VARCHAR"},{"name":"ΠΕΡΙΓΡΑΦΉ","type":"VARCHAR"},{"name":"ΕΚΔΌΤΗΣ","type":"SMALLINT"}]},{"schema":"PUBLIC.\"PUBLISHER SALES\"","name":"SALES","columns":[{"name":"POSTALCODE","type":"VARCHAR"},{"name":"COUNTRY","type":"VARCHAR"},{"name":"BOOKID","type":"INTEGER"},{"name":"COUPON_ID","type":"INTEGER"},{"name":"PERIODENDDATE","type":"DATE"},{"name":"TOTALAMOUNT","type":"DOUBLE"},{"name":"SALESDATAID","type":"INTEGER"}]},{"schema":"PUBLIC.BOOKS","name":"AUTHORSLIST","columns":[{"name":"ID","type":"INTEGER"},{"name":"FIRSTNAME","type":"VARCHAR"},{"name":"LASTNAME","type":"VARCHAR"}]}]}
{"schemas":[{"name":"PUBLIC.BOOKS","tables":[{"name":"AUTHORS","columns":[{"name":"ID","type":"INTEGER"},{"name":"FIRSTNAME","type":"VARCHAR"},{"name":"LASTNAME","type":"VARCHAR"},{"name":"ADDRESS1","type":"VARCHAR"},{"name":"ADDRESS2","type":"VARCHAR"},{"name":"CITY","type":"VARCHAR"},{"name":"STATE","type":"CHARACTER"},{"name":"POSTALCODE","type":"VARCHAR"},{"name":"COUNTRY","type":"VARCHAR"}],"remarks":"Contact details for book authors"},{"name":"BOOKS","columns":[{"name":"ID","remarks":"Unique (internal) id for book","type":"INTEGER"},{"name":"TITLE","remarks":"Book title","type":"VARCHAR"},{"name":"DESCRIPTION","remarks":"Book description\n(Usually the blurb from the book jacket or promotional materials)","type":"VARCHAR"},{"name":"PUBLISHERID","remarks":"Foreign key to the book publisher","type":"INTEGER"},{"name":"PUBLICATIONDATE","remarks":"Book publication date","type":"DATE"},{"name":"PRICE","remarks":"Current price for the book","type":"DOUBLE"},{"name":"PREVIOUSEDITIONID","type":"INTEGER"}],"remarks":"Details for published books"},{"name":"Celebrities","columns":[{"name":"Id","type":"INTEGER"},{"name":"NAME","type":"VARCHAR"}]},{"name":"COUPONS","columns":[{"name":"ID","type":"INTEGER"},{"name":"DATA","type":"CLOB"},{"name":"COUPONS","type":"INTEGER ARRAY"},{"name":"BOOKS","type":"VARCHAR(20) ARRAY[10]"}]},{"name":"CUSTOMERDATA","columns":[{"name":"ID","type":"INTEGER"},{"name":"DATA","type":"VALID_STRING"}]},{"name":"CUSTOMERS","columns":[{"name":"ID","type":"INTEGER"},{"name":"FIRSTNAME","type":"NAME_TYPE"},{"name":"LASTNAME","type":"NAME_TYPE"},{"name":"AGE","type":"AGE_TYPE"}]},{"name":"PUBLISHERS","columns":[{"name":"ID","remarks":"Unique (internal) id for book publisher","type":"INTEGER"},{"name":"PUBLISHER","remarks":"Name of book publisher","type":"VARCHAR"}],"remarks":"List of book publishers"},{"name":"BOOKAUTHORS","columns":[{"name":"BOOKID","type":"INTEGER"},{"name":"AUTHORID","type":"INTEGER"},{"name":"SOMEDATA","type":"VARCHAR"}],"remarks":"Relationship between books and their authors, \nalong with the latest updated information"},{"name":"Celebrity Updates","columns":[{"name":"Celebrity Id","type":"INTEGER"},{"name":"UPDATE","type":"VARCHAR"}]},{"name":"ΒΙΒΛΊΑ","columns":[{"name":"ΜΟΝΑΔΙΚΌΣ","type":"SMALLINT"},{"name":"ΤΊΤΛΟΣ","type":"VARCHAR"},{"name":"ΠΕΡΙΓΡΑΦΉ","type":"VARCHAR"},{"name":"ΕΚΔΌΤΗΣ","type":"SMALLINT"}]},{"name":"AUTHORSLIST","columns":[{"name":"ID","type":"INTEGER"},{"name":"FIRSTNAME","type":"VARCHAR"},{"name":"LASTNAME","type":"VARCHAR"}]}]},{"name":"PUBLIC.INFORMATION_SCHEMA"},{"name":"PUBLIC.PUBLIC"},{"name":"PUBLIC.\"PUBLISHER SALES\"","tables":[{"name":"REGIONS","columns":[{"name":"CITY","type":"VARCHAR"},{"name":"STATE","type":"VARCHAR"},{"name":"POSTALCODE","type":"VARCHAR"},{"name":"COUNTRY","type":"VARCHAR"}]},{"name":"SALESDATA","columns":[{"name":"SALESDATAID","type":"INTEGER"},{"name":"YEARLYAMOUNT","type":"DOUBLE"}]},{"name":"SALES","columns":[{"name":"POSTALCODE","type":"VARCHAR"},{"name":"COUNTRY","type":"VARCHAR"},{"name":"BOOKID","type":"INTEGER"},{"name":"COUPON_ID","type":"INTEGER"},{"name":"PERIODENDDATE","type":"DATE"},{"name":"TOTALAMOUNT","type":"DOUBLE"},{"name":"SALESDATAID","type":"INTEGER"}]}]},{"name":"PUBLIC.SYSTEM_LOBS"}]}

0 comments on commit 3b26011

Please sign in to comment.