Saturday, December 29, 2012

Build database tool using Jython

Build database tool using Jython
1. Doesn't need Python, but need Java Runtime Environment.
2. Get jython.jar and ojdbc14.jar (zxJDBC.jar is not necessary since it is already contained in jython.jar).
3. Write the python/jython programme and run as: java -jar jython test.py
Example1: Connect to Oracle server and print the dbname (Oracle)

from com.ziclix.python.sql import zxJDBC
d, u, p, v = "jdbc:oracle:thin:@192.168.2.1:1521:ORCL", "stone", "stone","oracle.jdbc.driver.OracleDriver"
db = zxJDBC.connect(d, u, p, v)
##print db.dbversion
print db.dbname
db.close()
Example2: A SQL*Plus like command line client tool
import os
import cmd
import ConfigParser
from dbexts import dbexts

class Console(cmd.Cmd):
    def do_conn(self,conninfo):
        """Connect to the database just like Oracle's SQL*Plus. Example: conn user/password@dbname"""
        if conninfo == "":
            conninfo = raw_input("Enter conn info like stone/stone@orcl: ");
        print 'Connecting...'
        global db
        try:
            db.close()
        except (NameError):
            pass
        db = dbexts(conninfo, "dbexts.ini")        
        db.isql("SELECT * FROM v$version")

    def do_select(self,select):
        destSelect = 'select ' + select
        destSelect = destSelect.strip()
        while destSelect[-1] == ';':
            destSelect=destSelect[:-1]
        db.isql(destSelect)
       
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.prompt = "SQL> "
        self.intro  = "Welcome to SQL*Plus toy by xxxx@gmail.com!"  ## defaults to None

    ## Command definitions ##
    def do_hist(self, args):
        """Print a list of commands that have been entered"""
        print self._hist

    def do_exit(self, args):
        """Exits from the console"""
        try:
            db.close()
        except (NameError):
            pass   
        return -1

    ## Command definitions to support Cmd object functionality ##
    def do_EOF(self, args):
        """Exit on system end of file character"""
        return self.do_exit(args)

    def do_shell(self, args):
        """Pass command to a system shell when line begins with '!'"""
        os.system(args)

    def do_help(self, args):
        """Get help on commands
           'help' or '?' with no arguments prints a list of commands for which help is available
           'help <command>' or '? <command>' gives help on <command>
        """
        ## The only reason to define this method is for the help text in the doc string
        cmd.Cmd.do_help(self, args)

    ## Override methods in Cmd object ##
    def preloop(self):
        """Initialization before prompting user for commands.
           Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
        """
        cmd.Cmd.preloop(self)   ## sets up command completion
        self._hist    = []      ## No history yet
        self._locals  = {}      ## Initialize execution namespace for user
        self._globals = {}

    def postloop(self):
        """Take care of any unfinished business.
           Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
        """
        cmd.Cmd.postloop(self)   ## Clean up command completion
        print "Exiting..."

    def precmd(self, line):
        """ This method is called after the line has been input but before
            it has been interpreted. If you want to modifdy the input line
            before execution (for example, variable substitution) do it here.
        """
        self._hist += [ line.strip() ]
        return line

    def postcmd(self, stop, line):
        """If you want to stop the console, return something that evaluates to true.
           If you want to do some post command processing, do it here.
        """
        return stop

    def emptyline(self):   
        """Do nothing on empty input line"""
        pass

    def default(self, line):      
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        try:
            exec(line) in self._locals, self._globals
        except Exception, e:
            print e.__class__, ":", e

if __name__ == '__main__':
    console = Console()
    console . cmdloop()
A kshell can be added to invoke the sqlplus.py programme (Need extract jython.jar to jython or cmd module or oracle jdbc driver can not be found).
java -Dpython.home="./jython" -classpath ".:./jython:ojdbc14.jar" org.python.util.jython sqlplus.py
Snapshot is as follows; it looks like SQL*Plus

No comments:

Post a Comment