net.anotheria.asg.generator.model.db.SQLGenerator Maven / Gradle / Ivy
package net.anotheria.asg.generator.model.db;
import net.anotheria.asg.generator.AbstractGenerator;
import net.anotheria.asg.generator.FileEntry;
import net.anotheria.asg.generator.GeneratedSQLFile;
import net.anotheria.asg.generator.GenerationJobManager;
import net.anotheria.asg.generator.GeneratorDataRegistry;
import net.anotheria.asg.generator.IGenerateable;
import net.anotheria.asg.generator.IGenerator;
import net.anotheria.asg.generator.meta.MetaDocument;
import net.anotheria.asg.generator.meta.MetaListProperty;
import net.anotheria.asg.generator.meta.MetaModule;
import net.anotheria.asg.generator.meta.MetaProperty;
import net.anotheria.asg.generator.meta.StorageType;
import java.util.ArrayList;
import java.util.List;
/**
* SQLGenerator class.
*
* @author another
* @version $Id: $Id
*/
public class SQLGenerator extends AbstractGenerator implements IGenerator{
/**
* generate.
*
* @param modules a {@link java.util.List} object.
* @return a {@link java.util.List} object.
*/
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;
}
/** {@inheritDoc} */
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;
}
/**
* getCreateScriptName.
*
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
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