Activity Not Available
0
I Use This!

Project Summary

Analyzed 3 months ago. based on code collected 7 months ago.

This project provides utilities for groovy to access db4o databases. Features provided in V0.1

Db4oBuilder: a groovy based DSL for accessing db4o database Db4oGroovyShell and Db4oQueryGroovyShell: db4o aware extensions of the groovy shell Db4oBuilder is supposed to be used by groovy based scripts or programs to access db4o database. It builds on the builder concept of groovy to provide a DSL. Although the db4o provides a very simple API, the Db4oBuilder provides an alternative way of access in Groovy. The builder takes over opening and closing of the database, executing queries. Custom code is injected using closures.

Examples:

Create a builder object:

Db4oBuilder builder3 = new Db4oBuilder();Storing objects is easy by embedding set calls:

builder.file('test.yap') {
set( new Person("Doe","John"))
}Within the closure you can use any code: This object stores ten objects

Db4oBuilder builder2 = new Db4oBuilder();
builder2.file('test.yap') {
(1..10).each {
def pers = new Person("Doe"+it,"John");
set(pers)
}
}Open the db4o database and build a SODA query using a tree like form:

builder3.file('d:/Db4oSampleDatabase.yap') {
query(Person.class) {
descend("lastName") {
constrain("aa") {
startsWith() {
or() {
descend("age") {
constrain(34) { greater()
}
}
}
}
}
}
}

if( objectSet ) {
println ("${objectSet.size()}")
objectSet.each() { p ->
println "Person: ${p.lastName},${p.firstName},${p.age}"
}
}
}Using a SQL based query is possible as well (requires db4o-sql.jar)

builder3.file('d:/Db4oSampleDatabase.yap') {
sqlquery("from com.sample.domain.Person where age > 42") {

if( resultSet ) {
println ("${resultSet.size()}")
resultSet.each() { p ->
println "Person: ${p.lastName},${p.firstName},${p.age}"
}
}
}

}Db4o allows you to setup configuration parameters. Most of them have to be set before opening the database.

That is supported by the db4oBuilder by using a special configuration node. The configuration node sets up a reference the db4o configuration object. Using this reference named config can be use to set any db4o configuration parameter.

Db4oBuilder builder = new Db4oBuilder();
builder.configuration() {
config.activationDepth(5)
config.updateDepth(5)
config.freespace().discardSmallerThan(5)
}Alternatively, you can pass a map of configuration parameters as argument to the configuration node. These will then be set by the db4o builder automatically.

def config = ["activationDepth" : 5,
"updateDepth": 5]
builder.configuration( config ) {
}After setting the configuration the db4o database can be opened as seen before.

builder.configuration() {
config.activationDepth(5)
config.updateDepth(5)
config.freespace().discardSmallerThan(5)

file('d:/Db4oSampleDatabase.yap') {
// store or query objects
}
}Or

def config = ["activationDepth" : 5,
"updateDepth": 5]

builder.configuration( config ) {
file('d:/Db4oSampleDatabase.yap') {
// store or query objects
}
}Db4oGroovyShell and Db4oQueryGroovyShell are supposed to be used by java programs to integrate groovy based scripts that access db4o databases.

Db4oGroovyShell is the more general class to execute any scripts. Example:

Db4oGroovyShell shell = new Db4oGroovyShell(objCtr);
ObjectSet os = (ObjectSet) shell.run("" +
"class APredicate" +
" extends com.db4o.query.Predicate {\n" +
"" +
" public boolean match(gk.samples.db4o.domain.Person person) { " +
" return (person.lastName ==~ /Mu\\w+/);\n" +
" }\n" +
" public boolean match(Object person) { return super.match(person); };\n"+
"};\n" +
"objectContainer.query( new APredicate() );\n");

for (int it = 0; it < os.size(); it++) {
Person pers = (Person) os.get(it);
System.out.println(pers.getLastName());
}Db4oQueryGroovyShell is more specific and focused on queries:

final String qualifier = "pers";
final String constraint = "pers.age < 42 && pers.lastName != null";

// Also regular expressions may be used:
// final String constraints = "pers.lastName ==~ /Mu\\w+/";
Db4oGroovyQueryShell shell = new Db4oGroovyQueryShell(objCtr, Person.class, constraint);
shell.setQualifier(qualifier);
ObjectSet os = (ObjectSet) shell.run();

for (int it = 0; it < os.size(); it++) {
Person pers = (Person) os.get(it);
System.out.println(pers.getLastName());
}

Share

In a Nutshell, db4o-groovy...

Activity

30 Day Summary

Jan 10 2016 — Feb 9 2016

12 Month Summary

Feb 9 2015 — Feb 9 2016

Community

Ratings

Be the first to rate this project
 
Click to add your rating
   Spinner f6ecff617ec2ba7f559e6f535cad9b70a3f91120737535dab4d4548a6c83576c
 
Review this Project!