pnuts.jdo module

The pnuts.jdo module defines high level functions on top of Java Data Object API.

jdo = openJDO ( Properties properties )

Open a JDO connection.

jdo = openJDO(loadProperties("lido_mysql.properties"))
jdo . add ( javax.jdo.PersistenceCapable object { , boolean transaction } )

Add a persisntent object to the JDO storage.

If the second parameter is true, this operation is executed during a new transaction. The default value is false.

account = Account()
account.name = "Foo"
account.email = "foo@bar"
jdo.add(account, true)
e.g.
tran = jdo.currentTransaction()
tran.begin()
catch(JDOException, function (e) tran.rollback())
account = Account()
account.name = "Foo"
account.email = "foo@bar"
jdo.add(account, false)
...
tran.commit()
jdo . find ( Class class , String filter_expression ) or
( Query query )

Search a persistent object of class that satisfies the filter_expression and return the first one if any. Returns null if no objects are found.

user = jdo.find(Account, `name=="John"`)
== != > < >= <= & && | || ~ + - * / !
String.startsWith()
String.endsWith()
Collection.isEmpty()
Collection.contains()
Operators in filter_expression

The parameter can be a Query object which can be obtained from jdo.query() function described below.

q = jdo.query(Account)
q.filter = `name=="John"`
user = jdo.find(q)
jdo . delete ( javax.jdo.PersistenceCapable object { , boolean transaction } ) or
( Collection collection { , boolean transaction } )

Delete a persistent object, or a collection of persistent objects, from the JDO storage.

john = jdo.find(Account, `name=="John"`)
jdo.delete(john)
jdo . update ( Class class , String filter_expression , update_properties { , boolean transaction } )

Update the properties of a persistent object, or a collection of persistent objects.

new_properties must be one of the followings.

Map
Defines the properties to be modified.
String
Defines new properties as a Pnuts expression.
Pnuts function
Specifies a function that takes a persistent object.
jdo.update(john, map([["email", "john@new"]]))
jdo.update(john, `email="john@new"`)
jdo.update(john, function(record) record.setEmail("john@new"))
jdo . query ( Class class {, String query_properties } )

Returns a Query object that can be used with other functions, such as find() and list().

The Query object has the following properties that characterize the query.

ordering
Set the ordering specification for the result Collection.
filter
Set the filter for the query.
ignoreCache
Specify whether the query should execute entirely in the back end, instead of in the cache.
q = jdo.query(Account)
q.filter = `name.startsWith("john")`
q.ordering = `ascending age`
john = jdo.find(q)

If the 2nd parameter query_properties is specified, it must be a Map or a Pnuts expression that defines the query properties.

q = jdo.query(Account, map([["filter", `name.startsWith("john")`]])
john = jdo.find(q)

q = jdo.query(Account, `filter = name.startsWith("john")`)
john = jdo.find(q)
jdo . list ( Class class , String query_properties ) or
( Query query )

Execute a query and returns the result as a collection of persistent objects.

q = jdo.query(Account, "age < 20")
kids = jdo.list(q)
printAll(kids)
jdo . currentTransaction ( )

currentTransaction() returns the current transaction of the persistence manager.

tran = jdo.currentTransaction()
tran.begin()
catch(Exception, function (e) tran.rollback())
for (i : 0..num - 1){
  jdo.add(account[i], false)
}
tran.commit()
jdo . transaction ( func(PersistenceManager) )

transaction() executes the specified function during a transaction.

jdo.transaction(function (jdo){
  for (i : 0..num - 1){
    jdo.add(account[i])
}
})
jdo . getPersistenceManager ( )

Returns a PersistenceManager object which represents the current JDO connection.

jdo . close ( )

Close the JDO connection.

jdo.close()