JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer
JPOX 1.1 Runtime
Runtime Tools
Queries
RDBMS Datastores
JPOX Shell
Introduction

JPOX Shell extends Jython to incorporates JPOX tools to facilitate development, administration and automation of tasks for management of JDO aware applications using scripts.

Starting JPOX Shell

JPOX Shell requires the JPOX Core, JPOX Shell and its dependencies to run. Optionally, the JPOX Enhancer is required if enhancement is invoked within JPOX Shell. All libraries must be added to the classpath. The classpath can be set in the environment variable CLASSPATH.

Start JPOX Shell by lauching jpoxshell.bat under Windows or jpoxshell.sh under Linux.

D:\jpox>jpoxshell
Starting JPOX Shell Interpreter... 

Type 'help()' for command list.
>>> 
JPOX Shell Modes

JPOX Shell interpreter evaluates instructions either interactively or in batch mode. In interactive mode, JPOX Shell will evaluate every command at the time it is provided. In batch mode, JPOX Shell will load a script file (.py) and interprete all commands.

To start JPOX Shell in batch mode, you run the jpoxshell command followed by the script file name (sample.py), and to start in interactive mode, you simply run the jpoxshell command.

Sample Script

The below script exemplifies the enhancement and persistence of two objects that are queried and printed to the console.

#the JDO, JPOX Core, JPOX Enhancer, JPOX Shell and Jython libraries are in the classpath
#we set an additional classpath to load persistent classes and database drivers
classPath(['/home/user/classes','/home/lib/derby.jar'])

#enhance 
enhance(['/home/user/classes/org/test/package.jdo'])

#imports                
import java
from java.io import File
from javax.jdo import JDOHelper

#create a PMF and obtain a PersistenceManager
props = File("/home/PMFProperties.properties")
pmf = JDOHelper.getPersistenceManagerFactory(props)
pm = pmf.getPersistenceManager()

#create some persistent objects
from org.jpox.samples.company import Person
erik = Person(1,'Erik','Bengtson','erik@jpox.org')
andy = Person(2,'Andy','Jefferson','andy@jpox.org')
pm.currentTransaction().begin()
pm.makePersistent(erik)
pm.makePersistent(andy)
pm.currentTransaction().commit()

#query persistent objects
pm.currentTransaction().begin()
q = pm.newQuery(Person)
q.setResult("firstName,lastName")
result = q.execute()

#loop though results
print "First loop"
for r in result:
   print "first name:", r[0], "last name:", r[1]

#loop though results (another form)
from java.lang import System
System.out.println("Second loop")
for r in result:
   System.out.println("first name: "+r[0]+" last name: "+r[1])

#release resources
pm.currentTransaction().commit()
pm.close()
pmf.close()

The console have the below ouput after running the above script.

First loop
first name: Andy last name: Jefferson
first name: Erik last name: Bengtson
Second loop
first name: Andy last name: Jefferson
first name: Erik last name: Bengtson
Sample Script for running SQL

The below script exemplifies the execution of an SQL query where results are printed to the console.

#the JDO, JPOX Core, JPOX Enhancer, JPOX Shell and Jython libraries are in the classpath
#we set an additional classpath to load persistent classes and database drivers
classPath(['/home/user/classes','/home/lib/derby.jar'])

#imports
import java

#configuration
loadConfiguration('/home/user/pmf.properties')

#run sql
result = sql('SELECT * from PERSON') #result is printed to output

# iterating results and printing
print 'printing results for 2nd time.'
for r in result:
    java.lang.System.out.print('Name: ')
    for elm in r:
        java.lang.System.out.print(elm)
        java.lang.System.out.print(' ')
    java.lang.System.out.println('')

The console have the below ouput after running the above script.

Andy Jefferson
Erik Bengtson

print 'printing results for 2nd time.'
Name: Andy Jefferson
Name: Erik Bengtson
JPOX Shell Reference

JPOX Shell defines custom functions besides those that exists in Java and Jython. These functions are to facilitate the usage of JPOX. Below a list of functions:

loadConfiguration

This function loads the configuration for the database.

Syntax

void loadConfiguration(propertiesFile)

ArgumentDescrition
propertiesFileThe path to the properties file with the connection properties.

Examples

loadConfiguration('/home/user/pmf.properties')

dbInfo

This function displays the database information. You must have invoked loadConfiguration function before invoking dbInfo.

Syntax

void dbInfo()

Examples

loadConfiguration('/home/user/pmf.properties')
dbInfo()

schemaInfo

This function displays the schema information. You must have invoked loadConfiguration function before invoking schemaInfo

Syntax

void schemaInfo()

Examples

loadConfiguration('/home/user/pmf.properties')
schemaInfo()

getPersistenceManager

Accessor for the PersistenceManager. You must have invoked loadConfiguration function before invoking getPersistenceManager

Syntax

PersistenceManager getPersistenceManager()

Examples

loadConfiguration('/home/user/pmf.properties')
pm = getPersistenceManager()

sql

Execute a SQL statement. You must have invoked loadConfiguration function before invoking sql

Syntax

List sql(sql)

ArgumentDescrition
sqlThe sql statement.

Examples

loadConfiguration('/home/user/pmf.properties')
result = sql('SELECT * from TABLE') #result is printed to output and returned

help

This function displays a help list.

Syntax

void help(name)

ArgumentDescrition
nameOptional. The function name.

Examples

help()
help('dbInfo')

exit

Exits to the operating system.

Syntax

void exit()

Examples

exit()

classPath

This function sets a classpath for loading persistent classes, metadata files, dadtabase drivers.

Syntax

void classPath(files)

ArgumentDescrition
filesAn array of files or directories.

Examples

classPath(['file1.jar','/home/file2.jar','/dir'])

enhance

This function enhances persistent classes. The JPOX Enhancer and its dependencies must be in the classpath

Syntax

void enhance(files)

ArgumentDescrition
filesAn array of metadata files or classes.

Examples

enhance(['/home/user/package.jdo','/home/user/test.jdo'])
enhance(['/home/user/Person.class','/home/user/Manager.class'])