org.dbtools.schema.schemafile.SchemaDatabase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbtools-gen Show documentation
Show all versions of dbtools-gen Show documentation
DBTools ORM Class Generator.
package org.dbtools.schema.schemafile;
import org.dbtools.schema.ClassInfo;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Root
public class SchemaDatabase {
@Attribute
private String name;
@Attribute(required = false)
private Boolean fieldsDefaultNotNull = null;
@Attribute(required = false)
private Boolean readOnly = null;
@ElementList(entry = "table", inline = true, required = false)
private List tables = new ArrayList<>();
@ElementList(entry = "view", inline = true, required = false)
private List views = new ArrayList<>();
@ElementList(entry = "query", inline = true, required = false)
private List queries = new ArrayList<>();
@ElementList(entry = "postSQLScriptFile", inline = true, required = false)
private List postSQLScriptFiles;
public SchemaDatabase() {
}
public SchemaDatabase(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getTables() {
if (fieldsDefaultNotNull != null) {
for (SchemaTable entity : tables) {
entity.setFieldsDefaultNotNull(fieldsDefaultNotNull);
}
}
// readOnly
if (readOnly != null) {
for (SchemaTable entity : tables) {
entity.setReadOnly(readOnly);
}
}
return tables;
}
public void setTables(List tables) {
this.tables = tables;
}
public List getViews() {
if (fieldsDefaultNotNull != null) {
for (SchemaView entity : views) {
entity.setFieldsDefaultNotNull(fieldsDefaultNotNull);
}
}
return views;
}
public void setViews(List views) {
this.views = views;
}
public List getQueries() {
if (fieldsDefaultNotNull != null) {
for (SchemaQuery entity : queries) {
entity.setFieldsDefaultNotNull(fieldsDefaultNotNull);
}
}
return queries;
}
public void setQueries(List queries) {
this.queries = queries;
}
public List getPostSQLScriptFiles() {
return postSQLScriptFiles;
}
public void setPostSQLScriptFiles(List postSQLScriptFiles) {
this.postSQLScriptFiles = postSQLScriptFiles;
}
public List getTableNames() {
List names = new ArrayList<>();
for (SchemaTable table : tables) {
names.add(table.getName());
}
return names;
}
public List getViewNames() {
List names = new ArrayList<>();
for (SchemaView view : views) {
names.add(view.getName());
}
return names;
}
/**
* Case insensitive search for table
*/
public SchemaTable getTable(String tableName) {
for (SchemaTable table : tables) {
if (table.getName().equalsIgnoreCase(tableName)) {
return table;
}
}
return null;
}
/**
* Case insensitive search for views
*/
public SchemaView getView(String viewName) {
for (SchemaView view : views) {
if (view.getName().equalsIgnoreCase(viewName)) {
return view;
}
}
return null;
}
/**
* Case insensitive search for queries
*/
public SchemaQuery getQuery(String queryName) {
for (SchemaQuery query : queries) {
if (query.getName().equalsIgnoreCase(queryName)) {
return query;
}
}
return null;
}
public ClassInfo getTableClassInfo(String tableName) {
String className = ClassInfo.createJavaStyleName(tableName);
return new ClassInfo(className, null);
}
public boolean validate() {
// Check for duplicate table names
Set existingTableViewNames = new HashSet<>();
Set existingSequences = new HashSet<>();
for (SchemaTable table : tables) {
// table self validation
table.validate();
// Check for duplicate table names
String tableName = table.getName();
if (existingTableViewNames.contains(tableName)) {
throw new IllegalStateException("Table named [" + tableName + "] already exists in database [" + getName() + "]");
}
existingTableViewNames.add(tableName);
// Check for duplicate sequence name
for (String seqName : table.getSequenceNames()) {
if (existingSequences.contains(seqName)) {
throw new IllegalStateException("Sequencer named [" + seqName + "] already exists in database [" + getName() + "]");
}
existingSequences.add(seqName);
}
}
// Check for duplicate view names
for (String viewName : getViewNames()) {
if (existingTableViewNames.contains(viewName)) {
throw new IllegalStateException("View named [" + viewName + "] already exists in database [" + getName() + "]");
}
existingTableViewNames.add(viewName);
}
return true;
}
}