comparison sqlpython.py @ 102:c5f601abc993

login with AS SYSDBA, easy connect
author catherine@localhost
date Thu, 29 May 2008 13:14:06 -0400
parents fa8c9eb8908f
children 4aa28dffe658
comparison
equal deleted inserted replaced
101:9f4b13d23b6e 102:c5f601abc993
6 # A python module to reproduce Oracle's command line 'sqlplus-like' within python 6 # A python module to reproduce Oracle's command line 'sqlplus-like' within python
7 # Intended to allow easy customizations and extentions 7 # Intended to allow easy customizations and extentions
8 # Best used with the companion modules sqlpyPlus and mysqlpy 8 # Best used with the companion modules sqlpyPlus and mysqlpy
9 # See also http://twiki.cern.ch/twiki/bin/view/PSSGroup/SqlPython 9 # See also http://twiki.cern.ch/twiki/bin/view/PSSGroup/SqlPython
10 10
11 import cmd2,getpass,binascii,cx_Oracle,re 11 import cmd2,getpass,binascii,cx_Oracle,re,os
12 import pexpecter, sqlpyPlus 12 import pexpecter, sqlpyPlus
13 13
14 # complication! separate sessions -> 14 # complication! separate sessions ->
15 # separate transactions !!!!! 15 # separate transactions !!!!!
16 # also: timeouts, other session failures 16 # also: timeouts, other session failures
23 self.prompt = 'SQL.No_Connection> ' 23 self.prompt = 'SQL.No_Connection> '
24 self.maxfetch = 1000 24 self.maxfetch = 1000
25 self.failoverSessions = [] 25 self.failoverSessions = []
26 self.terminator = ';' 26 self.terminator = ';'
27 self.timeout = 30 27 self.timeout = 30
28 28
29 connection_modes = {re.compile(' AS SYSDBA', re.IGNORECASE): cx_Oracle.SYSDBA,
30 re.compile(' AS SYSOPER', re.IGNORECASE): cx_Oracle.SYSOPER}
29 def do_connect(self, arg): 31 def do_connect(self, arg):
30 '''Opens the DB connection''' 32 '''Opens the DB connection'''
33 modeval = 0
34 for modere, modevalue in self.connection_modes.items():
35 if modere.search(arg):
36 arg = modere.sub('', arg)
37 modeval = modevalue
31 try: 38 try:
32 if arg.find('/') == -1: 39 orauser, oraserv = arg.split('@')
33 orapass = getpass.getpass('Password: ') 40 except ValueError:
34 orauser = arg.split('@')[0] 41 try:
35 oraserv = arg.split('@')[1] 42 oraserv = os.environ['ORACLE_SID']
36 self.orcl = cx_Oracle.connect(orauser,orapass,oraserv) 43 except KeyError:
37 arg = '%s/%s@%s' % (orauser, orapass, oraserv) 44 print 'instance not specified and environment variable ORACLE_SID not set'
38 else: 45 return
39 self.orcl = cx_Oracle.connect(arg) 46 orauser = arg
47 sid = oraserv
48 try:
49 host, sid = oraserv.split('/')
50 try:
51 host, port = host.split(':')
52 port = int(port)
53 except ValueError:
54 port = 1521
55 oraserv = cx_Oracle.makedsn(host, port, sid)
56 except ValueError:
57 pass
58 try:
59 orauser, orapass = orauser.split('/')
60 except ValueError:
61 orapass = getpass.getpass('Password: ')
62 if orauser.upper() == 'SYS' and not modeval:
63 print 'Privilege not specified for SYS, assuming SYSOPER'
64 modeval = cx_Oracle.SYSOPER
65 try:
66 self.orcl = cx_Oracle.connect(orauser,orapass,oraserv,modeval)
40 self.curs = self.orcl.cursor() 67 self.curs = self.orcl.cursor()
41 self.prompt = 'SQL.'+self.orcl.tnsentry+'> ' 68 self.prompt = '%s@%s> ' % (orauser, sid)
42 self.failoverSessions = [f for f in [fbs(arg) for fbs in pexpecter.available] if f.available] 69 self.failoverSessions = [f for f in [fbs(arg) for fbs in pexpecter.available] if f.available]
43
44 except Exception, e: 70 except Exception, e:
45 print e 71 print e
72
46 73
47 def emptyline(self): 74 def emptyline(self):
48 pass 75 pass
49 76
50 def fail(self, arg, do_everywhere=False): 77 def fail(self, arg, do_everywhere=False):