Groovy wrapper for postgres-async-driver
- Result mapping based on Templates (Map or Class)
- Execute queries with Named Parameters
- Use Date types as you want: java.util.Date, Calendar, LocalDate, LocalDateTime
- Basic Jsonb native support
- Pagination support
- Tests
- Anything you think is useful :)
repositories {
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
compile 'com.github.leosilvadev:groovy-rx-postgres-async-driver:1.0.0-SNAPSHOT'
}
<repository>
<id>oss.sonatype.org.snapshot</id>
<name>Oss Sonatype Snapshot</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<dependency>
<groupId>com.github.leosilvadev</groupId>
<artifactId>groovy-rx-postgres-async-driver</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
def db = new PgDb([
hostname: "localhost",
port: 5432,
database: "glogger-test",
username: "dev",
password: "dev",
poolSize: 20
])
All the non-transactional methods are available on PgTransaction object
The rollback happens if there is any error inside the Observables, but you can trigger it anytime you want if needed
def log = new MyObj(name:'whatever', age:30)
db.transaction().flatMap { final PgTransaction tx ->
tx.insert(sqlInsert, log).flatMap({ id ->
tx.update(sqlUpdate, paramsTwo)
}).flatMap({ numberOfUpdated ->
tx.delete(sqlDelete, paramsThree)
}).flatMap({ numberOfDeleted ->
tx.commit()
})
}.onErrorReturn({
println 'Transaction was rolled back'
}).subscribe({
println 'Transaction OK'
})
class User {
Long id
String login
String password
LocalDate date
LocalDateTime timestamp
Map jsonbField
}
def sql = 'INSERT INTO Users (login, password, dateField, timestampField, jsonbField) VALUES (:login, :password, :date, :timestamp, :jsonbField)'
def jsonbObject = [any:'Attrvalue', date:new Date(), sub:[name:'UHA']]
def user = new User(login:'any', password:'any', date:LocalDate.now(), timestamp:LocalDateTime.now(), jsonbField:jsonbObject)
Single<Long> id = db.insert(sql, user)
def sql = 'UPDATE Users SET password = :password WHERE login = :login'
def user = new User(login:'mylogin', password:'newpass')
Single<Long> numberOfUpdated = db.update(sql, user)
def sql = 'DELETE FROM Users WHERE login = :login'
def params = [login:'mylogin']
Single<Long> numberOfDeleted = db.delete(sql, params)
def sql = "SELECT * FROM Users WHERE jsonbField ->> 'any' = 'Attrvalue'"
Observable<User> users = db.find(sql, User)
def sql = 'SELECT * FROM Users WHERE login = :login ORDER BY UserID'
def params = [login:'any']
def page = 1
def itemsPerPage = 10
def paging = new PageRequest(page, itemsPerPage)
Single<Page<User>> usersPage = db.find(sql, User, params, paging)
def sql = 'SELECT * FROM Users WHERE id = :id'
def params = [id:1]
Single<User> user = db.findOne(sql, User, params)
def sql = 'DROP TABLE Users'
Single<ResultSet> result = db.execute(sql)