Skip to content

Alipsa/data-utils

Repository files navigation

data-utils

Groovy data utils. The main issue I needed to address was the reliance on System classloader in @Grab to handle JDBC drivers. This is very inconvenient when using an IDE such as Gade and Ride as it requires you to copy jars to the lib folder and restart the IDE in order for the System classpath to be updated.

However, with some Reflection magic it is possible to do without this. This is what se.alipsa.groovy.datautil.SqlUtil does.

Here is an example:

@Grab('se.alipsa.groovy:data-utils:1.0.6')
@Grab('org.postgresql:postgresql:42.4.0')

import se.alipsa.groovy.datautil.SqlUtil

def idList = new ArrayList()

SqlUtil.withInstance("jdbc:postgresql://localhost:5432/mydb", "dbUser", "dbPasswd", "org.postgresql.Driver") { sql ->
    sql.query('SELECT id FROM project') { rs ->
        while (rs.next()) {
            idList.add(rs.getLong(1))
        }
    }
}

See test.alipsa.groovy.datautil.SqlUtilTest for more examples!

Using the dependency

data-utils is available from maven central

Gradle:

implementation "se.alipsa.groovy:data-utils:1.0.6"

Maven:

<dependency>
    <groupId>se.alipsa.groovy</groupId>
    <artifactId>data-utils</artifactId>
    <version>1.0.6</version>
</dependency>