root / tags / v1_0_2_Build_915 / extensions / extScripting / scripts / jython / Lib / isql.py @ 12217
History | View | Annotate | Download (3.51 KB)
1 |
# $Id: isql.py 5782 2006-06-12 07:56:49Z jmvivo $
|
---|---|
2 |
|
3 |
import dbexts, cmd, sys |
4 |
|
5 |
"""
|
6 |
Isql works in conjunction with dbexts to provide an interactive environment
|
7 |
for database work.
|
8 |
"""
|
9 |
|
10 |
__version__ = "$Revision: 5782 $"[11:-2] |
11 |
|
12 |
class Prompt: |
13 |
"""
|
14 |
This class fixes a problem with the cmd.Cmd class since it uses an ivar 'prompt'
|
15 |
as opposed to a method 'prompt()'. To get around this, this class is plugged in
|
16 |
as a 'prompt' attribute and when invoked the '__str__' method is called which
|
17 |
figures out the appropriate prompt to display. I still think, even though this
|
18 |
is clever, the attribute version of 'prompt' is poor design.
|
19 |
"""
|
20 |
def __init__(self, isql): |
21 |
self.isql = isql
|
22 |
def __str__(self): |
23 |
prompt = "%s> " % (self.isql.db.dbname) |
24 |
if len(self.isql.sqlbuffer) > 0: |
25 |
prompt = "... "
|
26 |
return prompt
|
27 |
|
28 |
class IsqlCmd(cmd.Cmd): |
29 |
|
30 |
def __init__(self, db=None, delimiter=";"): |
31 |
cmd.Cmd.__init__(self)
|
32 |
if db is None or type(db) == type(""): |
33 |
self.db = dbexts.dbexts(db)
|
34 |
else:
|
35 |
self.db = db
|
36 |
self.kw = {}
|
37 |
self.sqlbuffer = []
|
38 |
self.delimiter = delimiter
|
39 |
self.prompt = Prompt(self) |
40 |
|
41 |
def do_which(self, arg): |
42 |
"""\nPrints the current db connection parameters.\n"""
|
43 |
print self.db |
44 |
return None |
45 |
|
46 |
def do_EOF(self, arg): |
47 |
return None |
48 |
|
49 |
def do_p(self, arg): |
50 |
"""\nExecute a python expression.\n"""
|
51 |
try:
|
52 |
exec arg.strip() in globals() |
53 |
except:
|
54 |
print sys.exc_info()[1] |
55 |
return None |
56 |
|
57 |
def do_use(self, arg): |
58 |
"""\nUse a new database connection.\n"""
|
59 |
self.db = dbexts.dbexts(arg.strip())
|
60 |
return None |
61 |
|
62 |
def do_table(self, arg): |
63 |
"""\nPrints table meta-data. If no table name, prints all tables.\n"""
|
64 |
if len(arg.strip()): |
65 |
apply(self.db.table, (arg,), self.kw) |
66 |
else:
|
67 |
apply(self.db.table, (None,), self.kw) |
68 |
return None |
69 |
|
70 |
def do_proc(self, arg): |
71 |
"""\nPrints store procedure meta-data.\n"""
|
72 |
if len(arg.strip()): |
73 |
apply(self.db.proc, (arg,), self.kw) |
74 |
else:
|
75 |
apply(self.db.proc, (None,), self.kw) |
76 |
return None |
77 |
|
78 |
def do_schema(self, arg): |
79 |
"""\nPrints schema information.\n"""
|
80 |
print
|
81 |
self.db.schema(arg)
|
82 |
print
|
83 |
return None |
84 |
|
85 |
def do_delimiter(self, arg): |
86 |
"""\nChange the delimiter.\n"""
|
87 |
delimiter = arg.strip() |
88 |
if len(delimiter) > 0: |
89 |
self.delimiter = delimiter
|
90 |
|
91 |
def do_q(self, arg): |
92 |
"""\nQuit.\n"""
|
93 |
return 1 |
94 |
|
95 |
def do_set(self, arg): |
96 |
"""\nSet a parameter. Some examples:\n set owner = 'informix'\n set types = ['VIEW', 'TABLE']\nThe right hand side is evaluated using `eval()`\n"""
|
97 |
d = filter(lambda x: len(x) > 0, map(lambda x: x.strip(), arg.split("="))) |
98 |
if len(d) == 1: |
99 |
if self.kw.has_key(d[0]): |
100 |
del self.kw[d[0]] |
101 |
else:
|
102 |
self.kw[d[0]] = eval(d[1]) |
103 |
|
104 |
def default(self, arg): |
105 |
try:
|
106 |
token = arg.strip() |
107 |
# is it possible the line contains the delimiter
|
108 |
if len(token) >= len(self.delimiter): |
109 |
# does the line end with the delimiter
|
110 |
if token[-1 * len(self.delimiter):] == self.delimiter: |
111 |
# now add all up to the delimiter
|
112 |
self.sqlbuffer.append(token[:-1 * len(self.delimiter)]) |
113 |
if self.sqlbuffer: |
114 |
self.db.isql(" ".join(self.sqlbuffer)) |
115 |
self.sqlbuffer = []
|
116 |
return None |
117 |
if token:
|
118 |
self.sqlbuffer.append(token)
|
119 |
except:
|
120 |
self.sqlbuffer = []
|
121 |
print sys.exc_info()[1] |
122 |
return None |
123 |
|
124 |
def emptyline(self): |
125 |
return None |
126 |
|
127 |
if __name__ == '__main__': |
128 |
import getopt |
129 |
|
130 |
try:
|
131 |
opts, args = getopt.getopt(sys.argv[1:], "b:", []) |
132 |
except getopt.error, msg:
|
133 |
print
|
134 |
print msg
|
135 |
print "Try `%s --help` for more information." % (sys.argv[0]) |
136 |
sys.exit(0)
|
137 |
|
138 |
dbname = None
|
139 |
for opt, arg in opts: |
140 |
if opt == '-b': |
141 |
dbname = arg |
142 |
|
143 |
intro = "\nisql - interactive sql (%s)\n" % (__version__)
|
144 |
|
145 |
IsqlCmd(dbname).cmdloop(intro) |