Created
February 18, 2021 00:00
-
-
Save maxandersen/7a0654df5df0721d864693db6f5f5ef4 to your computer and use it in GitHub Desktop.
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
///usr/bin/env jbang "$0" "$@" ; exit $? | |
// Update the Quarkus version to what you want here or run jbang with | |
// `-Dquarkus.version=<version>` to override it. | |
//DEPS io.quarkus:quarkus-bom:${quarkus.version:1.11.3.Final}@pom | |
//DEPS io.quarkus:quarkus-picocli | |
//DEPS io.quarkus:quarkus-hibernate-orm-panache | |
//DEPS io.quarkus:quarkus-jdbc-postgresql | |
//DEPS org.postgresql:postgresql:42.2.14 | |
//DEPS org.testcontainers:postgresql:1.15.2 | |
//DEPS io.quarkus:quarkus-hibernate-orm | |
//Q:CONFIG quarkus.banner.enabled=false | |
//Q:CONFIG quarkus.log.level=INFO | |
//Q:CONFIG quarkus.hibernate-orm.log.sql=true | |
//Q:CONFIG quarkus.hibernate-orm.log.format-sql=true | |
//Q:CONFIG quarkus.datasource.db-kind=postgresql | |
//Q:CONFIG quarkus.datasource.username=postgres | |
//Q:CONFIG quarkus.datasource.password=sakila | |
//Q:CONFIG quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/sakila | |
import java.sql.Date; | |
import java.util.Arrays; | |
import java.util.function.Consumer; | |
import javax.enterprise.context.Dependent; | |
import javax.enterprise.context.control.ActivateRequestContext; | |
import javax.inject.Inject; | |
import javax.persistence.Entity; | |
import javax.persistence.EntityManager; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
import javax.transaction.Transactional; | |
import com.github.dockerjava.api.command.CreateContainerCmd; | |
import com.github.dockerjava.api.model.ExposedPort; | |
import com.github.dockerjava.api.model.PortBinding; | |
import com.github.dockerjava.api.model.Ports; | |
import org.testcontainers.containers.GenericContainer; | |
import org.testcontainers.containers.PostgreSQLContainer; | |
import org.testcontainers.utility.DockerImageName; | |
import io.quarkus.hibernate.orm.panache.PanacheEntity; | |
import io.quarkus.hibernate.orm.panache.PanacheEntityBase; | |
import io.quarkus.runtime.Quarkus; | |
import io.quarkus.runtime.QuarkusApplication; | |
import io.quarkus.runtime.annotations.QuarkusMain; | |
import picocli.CommandLine; | |
@CommandLine.Command | |
@QuarkusMain | |
public class db implements Runnable, QuarkusApplication { | |
@CommandLine.Parameters(index = "0", description = "The greeting to print", defaultValue = "World!") | |
String name; | |
@Inject | |
CommandLine.IFactory factory; | |
private final GreetingService greetingService; | |
public db(GreetingService greetingService) { | |
this.greetingService = greetingService; | |
} | |
@Override | |
public int run(String... args) { | |
return new CommandLine(this, factory).execute(args); | |
} | |
@Override | |
@ActivateRequestContext | |
@Transactional | |
public void run() { | |
greetingService.sayHello(name); | |
} | |
public static void main(String... args) { | |
GenericContainer gc = startdb(); | |
try { | |
gc.start(); | |
Quarkus.run(db.class, args); | |
} finally { | |
System.out.println("Press enter to stop database"); | |
System.console().readLine(); | |
gc.stop(); | |
} | |
} | |
private static GenericContainer startdb() { | |
int hostPort = 5432; | |
int containerExposedPort = 5432; | |
Consumer<CreateContainerCmd> cmd = e -> e.withPortBindings( | |
new PortBinding((Ports.Binding.bindPort(hostPort)), new ExposedPort(containerExposedPort))); | |
DockerImageName myImage = DockerImageName.parse("frantiseks/postgres-sakila") | |
.asCompatibleSubstituteFor("postgres"); | |
var dbcontainer = new PostgreSQLContainer(myImage).withDatabaseName("sakila").withUsername("postgres") | |
.withPassword("sakila").withCreateContainerCmdModifier(cmd); | |
return dbcontainer; | |
} | |
@Entity | |
@Table(name = "actor") | |
static public class Actor extends PanacheEntityBase { | |
@Id | |
public long actor_id; | |
public String first_name; | |
public String last_name; | |
public Date last_update; | |
@Override | |
public String toString() { | |
return actor_id + ": " + first_name + " " + last_name; | |
} | |
} | |
} | |
@Dependent | |
class GreetingService { | |
@Inject EntityManager em; | |
void sayHello(String name) { | |
System.out.println("Hello " + name + "!"); | |
db.Actor.listAll().forEach(System.out::println); | |
em.createNativeQuery("select * from Actor").getResultStream().forEach(x -> System.out.println(Arrays.toString((Object[])x))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment