Skip to content

Commit

Permalink
Merge pull request #8 from alberlau/fix/minor-fixes
Browse files Browse the repository at this point in the history
Fix RawColumn types, README, TableExtractor not fails if DB not provi…
  • Loading branch information
alberlau authored Dec 27, 2023
2 parents 5bd1807 + 092d0f0 commit e60a2ee
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Modify your pom.xml as bellow and adjust necessary parameters:
</extractionParameters>
<baseDir>${project.basedir}</baseDir>
<targetFolder>target/generated-sources</targetFolder>
<testPackage>testpkg</testPackage>
<targetPackage>testpkg</targetPackage>
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -75,14 +75,27 @@ Optionally if you need to attach generated code to your source code add this:
</plugin>
```

Configuration params:
- __jdbcUrl__
- __jdbcClassName__
- __jdbcUser__
- __jdbcPassword__
- __extractionParameters__ exactly as in DatabaseMetadata.getTables
- __schemaPattern__ supports %_, can be blank
- __catalog__ can be blank, mostly it's database
- __tableNamePattern__ supports %_, can be blank, selects all tables
- __types__ one of TABLE, VIEW, SYSTEM_TABLE, GLOBAL_TEMPORARY, LOCAL_TEMPORARY, ALIAS, SYNONYM, can be blank
- __baseDir__ where to output generated source, can be ${project.baseDir}
- __targetFolder__ where to put sources under baseDir, can be target/generated-sources
- __targetPackage__ what package should be used for generated classes
- __ext__ extension for generated files, defaults to .java
- __dateImpl__ what java date implementation should be used: UTIL_DATE or LOCAL_DATE
- __includeGenerationInfo__ should info about generation be included? Defaults to false

You can customize generation template, by providing __templates__ list.
To filter out objects included into metadata, you can specify:
- __catalog__
- __schemaPattern__
- __tableNamePattern__
- __types__ - one of TABLE, VIEW, SYSTEM_TABLE, GLOBAL_TEMPORARY, LOCAL_TEMPORARY, ALIAS, SYNONYM
see DatabaseMetadata.getTables and database in use documentation for more info
You can provide multiple executions with different id's to select from different schemas, providing different templates or some other config options.

Check https://github.com/alberlau/DB2Code/tree/master/java-pojo-generator-mojo-example and see example usage.

To see what is exposed into model, check: https://github.com/alberlau/DB2Code/tree/master/core/src/main/java/org/db2code/rawmodel
To see what is exposed into model, check: https://github.com/alberlau/DB2Code/tree/master/core/src/main/java/org/db2code/rawmodel classes
along with adapter class: https://github.com/alberlau/DB2Code/blob/master/java-pojo-generator/src/main/java/org/db2code/generator/java/pojo/adapter/JavaClassAdapter.java
13 changes: 12 additions & 1 deletion core/src/main/java/org/db2code/extractors/TableExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.db2code.convert.JavaPropertyConverter;
import org.db2code.md.ExportedKeyMetadata;
import org.db2code.md.PrimaryKeyMetadata;
import org.db2code.md.TableMetadata;
import org.db2code.rawmodel.RawForeignKey;
import org.db2code.rawmodel.RawTable;

@Slf4j
public class TableExtractor extends AbstractExtractor {
public List<RawTable> extract(DatabaseMetaData databaseMetaData, ExtractionParameters params) {
try {
Expand All @@ -33,7 +35,7 @@ private List<RawTable> _extract(DatabaseMetaData databaseMetaData, ExtractionPar
while (tables.next()) {
RawTable rawTable = new RawTable();
for (TableMetadata mdItem : TableMetadata.values()) {
String mdValue = tables.getString(mdItem.name());
String mdValue = tryGetFromMetadata(mdItem, tables);
String propName =
JavaPropertyConverter.camelCaseFromSnakeCaseInitLow(mdItem.name());
setProperty(rawTable, mdValue, propName);
Expand All @@ -48,6 +50,15 @@ private List<RawTable> _extract(DatabaseMetaData databaseMetaData, ExtractionPar
}
}

private static String tryGetFromMetadata(TableMetadata mdItem, ResultSet tables) {
try {
return tables.getString(mdItem.name());
} catch (SQLException e) {
log.error("Error while getting metadata: " + e.getMessage());
}
return null;
}

private List<RawForeignKey> extractForeignKeys(
DatabaseMetaData databaseMetaData, RawTable rawTable) throws SQLException {
List<RawForeignKey> fkResults = new ArrayList<>();
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/java/org/db2code/rawmodel/RawColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
@Data
public class RawColumn extends AbstractRawItem {
private String columnName;
private int dataType;
private Integer dataType;
private String typeName;
private int columnSize;
private int decimalDigits;
private Integer columnSize;
private Integer decimalDigits;
private Integer numPrecRadix;
private int nullable;
private Integer nullable;
private String remarks;
private String columnDef;
private String sqlDataType;
private String sqlDatetimeSub;
private int charOctetLength;
private int ordinalPosition;
private Integer sqlDataType;
private Integer sqlDatetimeSub;
private Integer charOctetLength;
private Integer ordinalPosition;
private String isNullable;
}

0 comments on commit e60a2ee

Please sign in to comment.