-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10d689a
commit 7401454
Showing
4 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/com/github/magicprinc/hibean/HiBeanUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.github.magicprinc.hibean; | ||
|
||
import io.ebean.Database; | ||
import io.ebean.DatabaseBuilder; | ||
import io.ebean.DatabaseFactory; | ||
import io.ebean.annotation.Platform; | ||
import io.ebean.config.CurrentUserProvider; | ||
import io.ebean.config.DatabaseConfig; | ||
import jakarta.persistence.PersistenceException; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.Setter; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.sql.DataSource; | ||
|
||
/** | ||
https://github.com/ebean-orm-examples | ||
<pre>{@code | ||
PostgresContainer.builder("15") | ||
.port(5532) // Note: defaults to 6432 | ||
.dbName("my_app") | ||
.user("my_app") | ||
.password("silly") | ||
.containerName("pg15") | ||
.extensions("hstore,pgcrypto") | ||
.build() | ||
.start(); | ||
}</pre> | ||
<pre>{@code | ||
// GenerateDbMigration | ||
DbMigration dbMigration = DbMigration.create(); | ||
dbMigration.setPlatform(Platform.POSTGRES); | ||
dbMigration.setVersion("20221101.0"); | ||
dbMigration.setName("initial"); | ||
dbMigration.generateMigration(); | ||
}</pre> | ||
<pre>{@code | ||
logging.level.io.ebean.SQL=DEBUG | ||
logging.level.io.ebean.TXN=DEBUG | ||
}</pre> | ||
@see io.ebean.BeanRepository | ||
@see io.ebean.annotation.Transactional | ||
@see jakarta.persistence.Entity | ||
@see jakarta.persistence.Table | ||
@see io.ebean.Model | ||
@see FinderMixin | ||
@see io.ebean.Database | ||
*/ | ||
public final class HiBeanUtils { | ||
|
||
/** | ||
oracle, h2, postgres, mysql, sqlserver16, sqlserver17 | ||
@see DatabaseBuilder#databasePlatformName(String) | ||
@see io.ebean.annotation.Platform | ||
@see io.ebean.config.dbplatform.DatabasePlatform | ||
@see io.ebean.platform.sqlserver.SqlServerPlatformProvider#create(Platform) | ||
*/ | ||
@Getter @Setter | ||
private static String databasePlatformName = "sqlserver16"; | ||
|
||
/** | ||
{@code -Debean.migration.run=true} | ||
@see DatabaseConfig#ddlGenerate(boolean) | ||
@see DatabaseConfig#ddlRun(boolean) | ||
@see DatabaseConfig#ddlCreateOnly(boolean) | ||
@see DatabaseConfig#runMigration(boolean) | ||
*/ | ||
@Getter @Setter | ||
private static boolean ddl = false; | ||
|
||
|
||
public static Database database ( | ||
String ebeanDatabaseName, | ||
@NonNull DataSource dataSource, | ||
@Nullable CurrentUserProvider currentUserProvider | ||
){ | ||
DatabaseConfig config = new DatabaseConfig();// config.loadFromProperties(); | ||
config.dataSource(dataSource); | ||
config.setRegister(true);// register in DB singleton too (in addition to Spring bean) | ||
|
||
if (ebeanDatabaseName == null || ebeanDatabaseName.isEmpty() || "db".equals(ebeanDatabaseName)){ | ||
config.name("db");// db is the default name | ||
config.defaultDatabase(true); | ||
} else { | ||
config.name(ebeanDatabaseName); | ||
config.defaultDatabase(false); | ||
} | ||
|
||
if (currentUserProvider != null){ | ||
config.currentUserProvider(currentUserProvider); | ||
} | ||
|
||
if (ddl){ | ||
config.ddlGenerate(true); | ||
config.ddlRun(true); | ||
config.ddlCreateOnly(true); | ||
} | ||
|
||
try { | ||
return DatabaseFactory.create(config); | ||
} catch (PersistenceException | IllegalArgumentException e){ | ||
// For SqlServer please explicitly choose either sqlserver16 or sqlserver17 as the platform via DatabaseConfig.setDatabasePlatformName. Refer to issue #1340 for more details | ||
config.databasePlatformName(databasePlatformName); | ||
return DatabaseFactory.create(config); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/github/magicprinc/hibean/HiBeanUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.github.magicprinc.hibean; | ||
|
||
import com.github.magicprinc.hibean.example.Smmo; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import io.ebean.DB; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
@see HiBeanUtils */ | ||
class HiBeanUtilsTest { | ||
@Test | ||
void basic () { | ||
var springDataSource = new HikariDataSource(); | ||
//springDataSource.setJdbcUrl("jdbc:sqlserver://127.0.0.1;databaseName=front;trustServerCertificate=true"); | ||
//springDataSource.setUsername("and password"); | ||
springDataSource.setJdbcUrl("jdbc:h2:mem:fooBarZooDb"); | ||
|
||
HiBeanUtils.setDdl(true); | ||
var db = HiBeanUtils.database("fooBarZoo", springDataSource, null); | ||
|
||
assertSame(springDataSource, db.dataSource()); | ||
assertEquals("fooBarZoo", db.name()); | ||
assertSame(db, DB.byName("fooBarZoo")); | ||
|
||
var mo = new Smmo(db.name()); | ||
mo.setSrcAddr("from"); | ||
mo.setDstAddr("1917393791"); | ||
mo.save(); | ||
|
||
var mo2 = mo.finder().query().where().eq("SrcAddr", "from").and().eq("dstAddr", "1917393791").findOne(); | ||
assertNotSame(mo, mo2); | ||
assertEquals(mo, mo2); | ||
|
||
assertSame(db, mo.db()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters