3
import dbexts, cmd, sys
6
Isql works in conjunction with dbexts to provide an interactive environment
10
__version__ = "$Revision$"[11:-2]
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.
20
def __init__(self, isql):
23
prompt = "%s> " % (self.isql.db.dbname)
24
if len(self.isql.sqlbuffer) > 0:
28
class IsqlCmd(cmd.Cmd):
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)
38
self.delimiter = delimiter
39
self.prompt = Prompt(self)
41
def do_which(self, arg):
42
"""\nPrints the current db connection parameters.\n"""
46
def do_EOF(self, arg):
50
"""\nExecute a python expression.\n"""
52
exec arg.strip() in globals()
54
print sys.exc_info()[1]
57
def do_use(self, arg):
58
"""\nUse a new database connection.\n"""
59
self.db = dbexts.dbexts(arg.strip())
62
def do_table(self, arg):
63
"""\nPrints table meta-data. If no table name, prints all tables.\n"""
65
apply(self.db.table, (arg,), self.kw)
67
apply(self.db.table, (None,), self.kw)
70
def do_proc(self, arg):
71
"""\nPrints store procedure meta-data.\n"""
73
apply(self.db.proc, (arg,), self.kw)
75
apply(self.db.proc, (None,), self.kw)
78
def do_schema(self, arg):
79
"""\nPrints schema information.\n"""
85
def do_delimiter(self, arg):
86
"""\nChange the delimiter.\n"""
87
delimiter = arg.strip()
88
if len(delimiter) > 0:
89
self.delimiter = delimiter
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("=")))
99
if self.kw.has_key(d[0]):
102
self.kw[d[0]] = eval(d[1])
104
def default(self, arg):
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)])
114
self.db.isql(" ".join(self.sqlbuffer))
118
self.sqlbuffer.append(token)
121
print sys.exc_info()[1]
127
if __name__ == '__main__':
131
opts, args = getopt.getopt(sys.argv[1:], "b:", [])
132
except getopt.error, msg:
135
print "Try `%s --help` for more information." % (sys.argv[0])
139
for opt, arg in opts:
143
intro = "\nisql - interactive sql (%s)\n" % (__version__)
145
IsqlCmd(dbname).cmdloop(intro)