net.anotheria.asg.generator.model.db.SQLGenerator Maven / Gradle / Ivy
package net.anotheria.asg.generator.model.db;
import net.anotheria.asg.generator.*;
import net.anotheria.asg.generator.meta.*;
import java.util.ArrayList;
import java.util.List;
public class SQLGenerator extends AbstractGenerator implements IGenerator{
public List generate(List modules){
ArrayList ret = new ArrayList();
ArrayList documents = new ArrayList();
for (MetaModule m : modules){
if (m.getStorageType().equals(StorageType.DB)){
ret.addAll(generate(m));
documents.addAll(m.getDocuments());
}
}
if (documents.size()>0)
ret.addAll(generateAllScripts(documents));
return ret;
}
private List generateAllScripts(List documents){
List entries = new ArrayList();
GeneratedSQLFile allCreate = new GeneratedSQLFile("create_all");
GeneratedSQLFile allDelete = new GeneratedSQLFile("delete_all");
MetaProperty daoCreated = new MetaProperty("dao_created", MetaProperty.Type.LONG);
MetaProperty daoUpdated = new MetaProperty("dao_updated", MetaProperty.Type.LONG);
String tableNames = "";
for (MetaDocument doc : documents){
GenerationJobManager.getCurrentJob().setBuilder(allCreate.getBody());
generateSQLCreate(doc, daoCreated, daoUpdated);
emptyline();
if (tableNames.length()>0)
tableNames += ",";
tableNames += getSQLTableName(doc);
GenerationJobManager.getCurrentJob().setBuilder(allDelete.getBody());
generateSQLDelete(doc);
emptyline();
}
GenerationJobManager.getCurrentJob().setBuilder(allCreate.getBody());
appendString("GRANT ALL ON "+tableNames+" TO "+GeneratorDataRegistry.getInstance().getContext().getOwner()+" ; ");
entries.add(new FileEntry(allCreate));
entries.add(new FileEntry(allDelete));
return entries;
}
public List generate(IGenerateable gmodule){
MetaModule mod = (MetaModule)gmodule;
List ret = new ArrayList();
List documents = mod.getDocuments();
for (MetaDocument d: documents){
ret.add(new FileEntry(generateDocumentCreate(d)));
}
return ret;
}
public String getCreateScriptName(MetaDocument doc){
return "create_"+doc.getParentModule().getName().toLowerCase()+"_"+doc.getName().toLowerCase();
}
private GeneratedSQLFile generateDocumentCreate(MetaDocument doc){
GeneratedSQLFile file = new GeneratedSQLFile(getCreateScriptName(doc));
startNewJob(file);
MetaProperty daoCreated = new MetaProperty("dao_created", MetaProperty.Type.LONG);
MetaProperty daoUpdated = new MetaProperty("dao_updated", MetaProperty.Type.LONG);
generateSQLCreate(doc, daoCreated, daoUpdated);
return file;
}
private void generateSQLDelete(MetaDocument doc){
appendString("DROP TABLE "+getSQLTableName(doc)+";");
}
private void generateSQLCreate(MetaDocument doc, MetaProperty... additionalProps){
appendString("CREATE TABLE "+getSQLTableName(doc)+"(");
increaseIdent();
appendString("id int8 PRIMARY KEY,");
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy