Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oracle example #489

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions examples/database-oracle/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.quarkus.qe</groupId>
<artifactId>quarkus-test-parent</artifactId>
<version>1.2.1.Beta1-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<artifactId>examples-database-oracle</artifactId>
<packaging>jar</packaging>
<name>Quarkus - Test Framework - Examples - Database - Oracle</name>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-oracle</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus.qe</groupId>
<artifactId>quarkus-test-containers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus.qe</groupId>
<artifactId>quarkus-test-service-database</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus.qe</groupId>
<artifactId>quarkus-test-openshift</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<!-- Skipped on Windows as does not support Linux Containers / Testcontainers -->
<profile>
<id>skip-tests-on-windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.qe.database.oracle;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.ObjectMapper;

@Provider
public class ApplicationExceptionMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
int code = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
if (exception instanceof WebApplicationException) {
code = ((WebApplicationException) exception).getResponse().getStatus();
}

return Response.status(code)
.type(MediaType.APPLICATION_JSON)
.entity(new ObjectMapper().createObjectNode()
.put("code", code)
.put("error", exception.getMessage())
.toString())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.qe.database.oracle;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;

import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
@Table(name = "book")
public class Book extends PanacheEntity {
@NotBlank(message = "book title must be set")
public String title;

@NotBlank(message = "book author must be set")
public String author;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.quarkus.qe.database.oracle;

import java.util.List;

import javax.transaction.Transactional;
import javax.validation.Valid;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import io.quarkus.panache.common.Sort;

@Path("/book")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BookResource {

@GET
public List<Book> getAll() {
return Book.listAll(Sort.by("title"));
}

@GET
@Path("/{id}")
public Book get(@PathParam("id") Long id) {
Book book = Book.findById(id);
if (book == null) {
throw new NotFoundException("book '" + id + "' not found");
}
return book;
}

@POST
@Transactional
public Response create(@Valid Book book) {
if (book.id != null) {
throw new ClientErrorException("unexpected ID in request", ValidationExceptionMapper.UNPROCESSABLE_ENTITY);
}

book.persist();
return Response.ok(book).status(Response.Status.CREATED).build();
}

@PUT
@Path("/{id}")
@Transactional
public Book update(@PathParam("id") Long id, @Valid Book newBook) {
Book book = Book.findById(id);
if (book == null) {
throw new NotFoundException("book '" + id + "' not found");
}

book.title = newBook.title;
book.author = newBook.author;
return book;
}

@DELETE
@Path("/{id}")
@Transactional
public Response delete(@PathParam("id") Long id) {
Book book = Book.findById(id);
if (book == null) {
throw new NotFoundException("book '" + id + "' not found");
}
book.delete();
return Response.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.qe.database.oracle;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException exception) {
return new ApplicationExceptionMapper().toResponse(exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.qe.database.oracle;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

@Provider
public class ValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

public static final int UNPROCESSABLE_ENTITY = 422;

@Override
public Response toResponse(ConstraintViolationException exception) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode errors = mapper.createArrayNode();

for (ConstraintViolation<?> constraintViolation : exception.getConstraintViolations()) {
errors.addObject()
.put("path", constraintViolation.getPropertyPath().toString())
.put("message", constraintViolation.getMessage());
}

return Response.status(UNPROCESSABLE_ENTITY)
.type(MediaType.APPLICATION_JSON)
.entity(mapper.createObjectNode()
.put("code", UNPROCESSABLE_ENTITY)
.set("error", errors))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkus.datasource.db-kind=oracle
quarkus.hibernate-orm.sql-load-script=import.sql
quarkus.hibernate-orm.database.generation=drop-and-create
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log contains a lot of error messages because of drop-and-create strategy, tests are passing, application boots etc.
It's just noise and distraction, so I was wondering if that could be suppressed. I would keep drop-and-create in place, just if the verbosity could be a bit reduced.

12:49:16,923 INFO [app] Caused by: Error : 942, Position : 11, Sql = drop table book cascade constraints, OriginalSql = drop table book cascade constraints, Error Msg = ORA-00942: table or view does not exist

12:49:16,928 INFO [app] Caused by: Error : 2289, Position : 14, Sql = drop sequence hibernate_sequence, OriginalSql = drop sequence hibernate_sequence, Error Msg = ORA-02289: sequence does not exist

7 changes: 7 additions & 0 deletions examples/database-oracle/src/main/resources/import.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Foundation', 'Isaac Asimov');
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, '2001: A Space Odyssey', 'Arthur C. Clarke');
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Stranger in a Strange Land', 'Robert A. Heinlein');
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Ender''s Game', 'Orson Scott Card');
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Hyperion', 'Dan Simmons');
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Anathem', 'Neal Stephenson');
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Perdido Street Station', 'China Miéville');
Loading