BAEL-1560 Code for the article: JDBC with Groovy (#3707)
This commit is contained in:
parent
612159c977
commit
e685ca66db
|
@ -0,0 +1,4 @@
|
||||||
|
# Groovy
|
||||||
|
|
||||||
|
## Relevant articles:
|
||||||
|
|
|
@ -0,0 +1,118 @@
|
||||||
|
<?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>
|
||||||
|
|
||||||
|
<artifactId>core-groovy</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>central</id>
|
||||||
|
<url>http://jcenter.bintray.com</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy</artifactId>
|
||||||
|
<version>2.4.13</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-sql</artifactId>
|
||||||
|
<version>2.4.13</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>${junit.jupiter.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hsqldb</groupId>
|
||||||
|
<artifactId>hsqldb</artifactId>
|
||||||
|
<version>2.4.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.gmavenplus</groupId>
|
||||||
|
<artifactId>gmavenplus-plugin</artifactId>
|
||||||
|
<version>1.6</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>addSources</goal>
|
||||||
|
<goal>addTestSources</goal>
|
||||||
|
<goal>compile</goal>
|
||||||
|
<goal>compileTests</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-failsafe-plugin</artifactId>
|
||||||
|
<version>2.19.1</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>junit5</id>
|
||||||
|
<goals>
|
||||||
|
<goal>integration-test</goal>
|
||||||
|
<goal>verify</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<includes>
|
||||||
|
<include>**/*Test5.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
|
||||||
|
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
|
||||||
|
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
|
||||||
|
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
|
||||||
|
<kotlinx.version>0.15</kotlinx.version>
|
||||||
|
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||||
|
|
||||||
|
<junit.jupiter.version>5.0.0</junit.jupiter.version>
|
||||||
|
<junit.platform.version>1.0.0</junit.platform.version>
|
||||||
|
<junit.vintage.version>4.12.0</junit.vintage.version>
|
||||||
|
<junit4.version>4.12</junit4.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,229 @@
|
||||||
|
package com.baeldung.groovy.sql
|
||||||
|
|
||||||
|
import groovy.sql.GroovyResultSet
|
||||||
|
import groovy.sql.GroovyRowResult
|
||||||
|
import groovy.sql.Sql
|
||||||
|
import groovy.transform.CompileStatic
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class SqlTest {
|
||||||
|
|
||||||
|
final Map dbConnParams = [url: 'jdbc:hsqldb:mem:testDB', user: 'sa', password: '', driver: 'org.hsqldb.jdbc.JDBCDriver']
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenNewSqlInstance_thenDbIsAccessed() {
|
||||||
|
def sql = Sql.newInstance(dbConnParams)
|
||||||
|
sql.close()
|
||||||
|
sql.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenTableDoesNotExist_thenSelectFails() {
|
||||||
|
try {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.eachRow('select * from PROJECT') {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fail("An exception should have been thrown")
|
||||||
|
} catch (ignored) {
|
||||||
|
//Ok
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenTableCreated_thenSelectIsPossible() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
def result = sql.execute 'create table PROJECT_1 (id integer not null, name varchar(50), url varchar(100))'
|
||||||
|
|
||||||
|
assertEquals(0, sql.updateCount)
|
||||||
|
assertFalse(result)
|
||||||
|
|
||||||
|
result = sql.execute('select * from PROJECT_1')
|
||||||
|
|
||||||
|
assertTrue(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenIdentityColumn_thenInsertReturnsNewId() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.execute 'create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
def ids = sql.executeInsert("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')")
|
||||||
|
|
||||||
|
assertEquals(0, ids[0][0])
|
||||||
|
|
||||||
|
ids = sql.executeInsert("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')")
|
||||||
|
|
||||||
|
assertEquals(1, ids[0][0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUpdate_thenNumberOfAffectedRows() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.execute 'create table PROJECT_3 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')")
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')")
|
||||||
|
def count = sql.executeUpdate("UPDATE PROJECT_3 SET URL = 'https://' + URL")
|
||||||
|
|
||||||
|
assertEquals(2, count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenEachRow_thenResultSetHasProperties() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.execute 'create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')")
|
||||||
|
|
||||||
|
sql.eachRow("SELECT * FROM PROJECT_4") { GroovyResultSet rs ->
|
||||||
|
assertNotNull(rs.name)
|
||||||
|
assertNotNull(rs.url)
|
||||||
|
assertNotNull(rs[0])
|
||||||
|
assertNotNull(rs[1])
|
||||||
|
assertNotNull(rs[2])
|
||||||
|
assertEquals(rs.name, rs['name'])
|
||||||
|
assertEquals(rs.url, rs['url'])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenPagination_thenSubsetIsReturned() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.execute 'create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')")
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')")
|
||||||
|
def rows = sql.rows('SELECT * FROM PROJECT_5 ORDER BY NAME', 1, 1)
|
||||||
|
|
||||||
|
assertEquals(1, rows.size())
|
||||||
|
assertEquals('REST with Spring', rows[0].name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenParameters_thenReplacement() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.execute 'create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.execute('INSERT INTO PROJECT_6 (NAME, URL) VALUES (?, ?)', 'tutorials', 'github.com/eugenp/tutorials')
|
||||||
|
sql.execute("INSERT INTO PROJECT_6 (NAME, URL) VALUES (:name, :url)", [name: 'REST with Spring', url: 'github.com/eugenp/REST-With-Spring'])
|
||||||
|
|
||||||
|
def rows = sql.rows("SELECT * FROM PROJECT_6 WHERE NAME = 'tutorials'")
|
||||||
|
|
||||||
|
assertEquals(1, rows.size())
|
||||||
|
|
||||||
|
rows = sql.rows("SELECT * FROM PROJECT_6 WHERE NAME = 'REST with Spring'")
|
||||||
|
|
||||||
|
assertEquals(1, rows.size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenParametersInGString_thenReplacement() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.execute 'create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.execute "INSERT INTO PROJECT_7 (NAME, URL) VALUES (${'tutorials'}, ${'github.com/eugenp/tutorials'})"
|
||||||
|
def name = 'REST with Spring'
|
||||||
|
def url = 'github.com/eugenp/REST-With-Spring'
|
||||||
|
sql.execute "INSERT INTO PROJECT_7 (NAME, URL) VALUES (${name}, ${url})"
|
||||||
|
|
||||||
|
def rows = sql.rows("SELECT * FROM PROJECT_7 WHERE NAME = 'tutorials'")
|
||||||
|
|
||||||
|
assertEquals(1, rows.size())
|
||||||
|
|
||||||
|
rows = sql.rows("SELECT * FROM PROJECT_7 WHERE NAME = 'REST with Spring'")
|
||||||
|
|
||||||
|
assertEquals(1, rows.size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenTransactionRollback_thenNoDataInserted() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.withTransaction {
|
||||||
|
sql.execute 'create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_8 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_8 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')")
|
||||||
|
sql.rollback()
|
||||||
|
|
||||||
|
def rows = sql.rows("SELECT * FROM PROJECT_8")
|
||||||
|
|
||||||
|
assertEquals(0, rows.size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenTransactionRollbackThenCommit_thenOnlyLastInserted() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.withTransaction {
|
||||||
|
sql.execute 'create table PROJECT_9 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
|
||||||
|
sql.rollback()
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')")
|
||||||
|
sql.rollback()
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')")
|
||||||
|
}
|
||||||
|
|
||||||
|
def rows = sql.rows("SELECT * FROM PROJECT_9")
|
||||||
|
|
||||||
|
assertEquals(2, rows.size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenException_thenTransactionIsRolledBack() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
try {
|
||||||
|
sql.withTransaction {
|
||||||
|
sql.execute 'create table PROJECT_10 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_10 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
|
||||||
|
throw new Exception('rollback')
|
||||||
|
}
|
||||||
|
} catch (ignored) {}
|
||||||
|
|
||||||
|
def rows = sql.rows("SELECT * FROM PROJECT_10")
|
||||||
|
|
||||||
|
assertEquals(0, rows.size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCachedConnection_whenException_thenDataIsPersisted() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
try {
|
||||||
|
sql.cacheConnection {
|
||||||
|
sql.execute 'create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_11 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
|
||||||
|
throw new Exception('This does not rollback')
|
||||||
|
}
|
||||||
|
} catch (ignored) {}
|
||||||
|
|
||||||
|
def rows = sql.rows("SELECT * FROM PROJECT_11")
|
||||||
|
|
||||||
|
assertEquals(1, rows.size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*@Test
|
||||||
|
void whenModifyResultSet_thenDataIsChanged() {
|
||||||
|
Sql.withInstance(dbConnParams) { Sql sql ->
|
||||||
|
sql.execute 'create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')")
|
||||||
|
sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')")
|
||||||
|
|
||||||
|
sql.eachRow("SELECT * FROM PROJECT_5 FOR UPDATE ") { GroovyResultSet rs ->
|
||||||
|
rs['name'] = "The great ${rs.name}!" as String
|
||||||
|
rs.updateRow()
|
||||||
|
}
|
||||||
|
|
||||||
|
sql.eachRow("SELECT * FROM PROJECT_5") { GroovyResultSet rs ->
|
||||||
|
assertTrue(rs.name.startsWith('The great '))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
}
|
1
pom.xml
1
pom.xml
|
@ -49,6 +49,7 @@
|
||||||
<module>core-java</module>
|
<module>core-java</module>
|
||||||
<module>core-java-io</module>
|
<module>core-java-io</module>
|
||||||
<module>core-java-8</module>
|
<module>core-java-8</module>
|
||||||
|
<module>core-groovy</module>
|
||||||
<!--
|
<!--
|
||||||
<module>core-java-concurrency</module>
|
<module>core-java-concurrency</module>
|
||||||
-->
|
-->
|
||||||
|
|
Loading…
Reference in New Issue