Logger.info("Siena DB Type: %s", dbType);
final String db = Play.configuration.getProperty("db");
final String dbUrl = Play.configuration.getProperty("db.url");
if((db==null || db=="" ) && (dbUrl == null || dbUrl == "")){
throw new UnexpectedException("SienaPlugin : not using GAE requires at least a db config");
}
if(dbType.contains("postgresql")){
persistenceManager = new PostgresqlPersistenceManager(new PlayConnectionManager(), null);
ddlType = "postgresql";
generator = new DdlGenerator("postgresql");
}else if(dbType.contains("h2")){
// the H2 dbMode in Play is "mysql"
persistenceManager = new H2PersistenceManager(new PlayConnectionManager(), null, "mysql");
// the DDL type is mysql because in play the DB is H2 in Mysql mode.
// But the DDLGenerator is wired to h2
// because longvarchar and CLOB is not managed the same way in H2/MYSQL and real MYSQL
ddlType = "mysql";
generator = new DdlGenerator("h2");
}
else if(dbType.contains("google")){
persistenceManager = new GoogleSqlPersistenceManager(new PlayConnectionManager(), null);
generator = new DdlGenerator("mysql");
}
else {
persistenceManager = new JdbcPersistenceManager(new PlayConnectionManager(), null);
generator = new DdlGenerator("mysql");
}
Logger.debug("Siena DDL Type: %s", ddlType);
// Alter tables before installing
for(Class<?> c : classes) {
// adds classes to the DDL generator
generator.addTable(c);
}
// get the Database model
Database database = generator.getDatabase();
Platform platform = PlatformFactory.createNewPlatformInstance(ddlType);
platform.setDataSource(DB.datasource);
// siena.ddl can have create/update/ddl
// if siena.ddl is defined, uses it
// if not:
// in dev mode, will be update by default
// in prod mode, will be none by default
String ddl = "none";
if(Play.mode.isDev()){
ddl = Play.configuration.getProperty("siena.ddl", "update");
Logger.debug("Siena DDL dev mode: %s", ddl);
}else if(Play.mode.isProd()){
ddl = Play.configuration.getProperty("siena.ddl", "none");
Logger.debug("Siena DDL prod mode: %s", ddl);
}
if("create".equals(ddl)){
if(Logger.isDebugEnabled()) {
Logger.debug("Siena DDL Generator SQL: %s", platform.getCreateModelSql(database, false, false));
}
// creates tables and do not drop tables and do not continues on error
try {
platform.createModel(connection, database, false, false);
}catch(DatabaseOperationException ex){
Logger.warn("Siena DDL createTables generated exception:%s", ex.getCause()!=null?ex.getCause():ex.getMessage());
}
}else if("update".equals(ddl)){
Database currentDatabase = platform.readModelFromDatabase(connection, ddlType);
if(!disableJPA){
// Remove from the current schema those tables that are not known by Siena,
// since they're likely to be JPA classes.
// Iterate in reverse order since removeTable() changes the list size.
for (int i = currentDatabase.getTableCount() - 1; i >= 0; i--) {
Table table = currentDatabase.getTable(i);
if(database.findTable(table.getName(), false) == null){
Logger.debug("Keeping existing table %s", table.getName());
currentDatabase.removeTable(i);
}
}
}
if(Logger.isDebugEnabled()){
Logger.debug("Siena DDL Generator SQL: %s", platform.getAlterModelSql(currentDatabase, database));
}
// alters tables and continues on error
platform.alterModel(currentDatabase, database, true);
}
// activate lifecycle or not
if(useLifecycle()){
Logger.debug("Siena activating lifecycle management");
persistenceManager = new PersistenceManagerLifeCycleWrapper(persistenceManager);
}
// is it required ?
// connection.close();
// for googlesql, forces Google driver
//if(dbType.contains("google")){
// Properties p = new Properties();
// p.setProperty("driver", "com.google.appengine.api.rdbms.AppEngineDriver");
// p.setProperty("url", Play.configuration.getProperty("db.url"));
// p.setProperty("user", Play.configuration.getProperty("db.user"));
// p.setProperty("password", Play.configuration.getProperty("db.pass"));
// persistenceManager.init(p);
//}else {
persistenceManager.init(null);
//}
if(!disableJPA){
JPAPlugin.closeTx(false);
}
} else if(dbType.equals("nosql:gae")) {
Logger.debug("Siena DB Type: GAE");
persistenceManager = new GaePersistenceManager();
// activate lifecycle or not
if(useLifecycle()){
Logger.debug("Siena activating lifecycle management");
persistenceManager = new PersistenceManagerLifeCycleWrapper(persistenceManager);
}
persistenceManager.init(null);
}
else if(dbType.equals("nosql:sdb")) {
Logger.debug("Siena DB Type: SDB");
persistenceManager = new SdbPersistenceManager();
String awsAccessKeyId = Play.configuration.getProperty("siena.aws.accesskeyid");
String awsSecretAccessKey = Play.configuration.getProperty("siena.aws.secretaccesskey");
String prefix = Play.configuration.getProperty("siena.aws.prefix", "siena_devel_");
String consistentread = Play.configuration.getProperty("siena.aws.consistentread", "true");
if(awsAccessKeyId == null || awsSecretAccessKey == null){
throw new UnexpectedException("siena.aws.accesskeyid & siena.aws.secretaccesskey required in conf");
}
Properties p = new Properties();
p.setProperty("implementation", "siena.sdb.SdbPersistenceManager");
p.setProperty("awsAccessKeyId", awsAccessKeyId);