Skip to content

Commit

Permalink
Spring helper: HiBeanUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
magicprinc committed Apr 13, 2024
1 parent 10d689a commit 7401454
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ dependencies {

compileOnly libs.micrometer.core

//*** TEST TEST TEST

//*** TEST TEST TEST ***

testAnnotationProcessor("io.ebean:querybean-generator:latest.release") // vs: annotationProcessor
testImplementation(libs.bundles.annotations)

Expand All @@ -60,6 +62,8 @@ dependencies {
testImplementation("io.ebean:ebean-platform-sqlite:latest.release")
testImplementation("io.ebean:ebean-platform-sqlserver:latest.release")

testImplementation('com.microsoft.sqlserver:mssql-jdbc:12.6.1.jre11')

testImplementation libs.bundles.logback
}
// https://tomgregory.com/how-to-exclude-gradle-dependencies/
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/com/github/magicprinc/hibean/HiBeanUtils.java
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 src/test/java/com/github/magicprinc/hibean/HiBeanUtilsTest.java
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());
}
}
4 changes: 4 additions & 0 deletions src/test/java/com/github/magicprinc/hibean/example/Smmo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class Smmo extends Model implements FinderMixin<Smmo> {

public static final Finder<Long,Smmo> FIND = new Finder<>(Smmo.class);

public Smmo (String dbName) {
super(dbName);
}

@Id
@Column(name = "smmo_id")
private Long smmoId;
Expand Down

0 comments on commit 7401454

Please sign in to comment.