The pnuts.jdo module defines high level functions on top of Java Data Object API.
Open a JDO connection.
jdo = openJDO(loadProperties("lido_mysql.properties"))
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)
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()
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"`)
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)
Delete a persistent object, or a collection of persistent objects, from the JDO storage.
john = jdo.find(Account, `name=="John"`) jdo.delete(john)
Update the properties of a persistent object, or a collection of persistent objects.
new_properties must be one of the followings.
jdo.update(john, map([["email", "john@new"]]))
jdo.update(john, `email="john@new"`)
jdo.update(john, function(record) record.setEmail("john@new"))
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)
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)
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()
transaction() executes the specified function during a transaction.
jdo.transaction(function (jdo){
for (i : 0..num - 1){
jdo.add(account[i])
}
})
Returns a PersistenceManager object which represents the current JDO connection.
Close the JDO connection.
jdo.close()