forked from restlet/restlet-framework-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisql.py
More file actions
237 lines (207 loc) · 6.99 KB
/
isql.py
File metadata and controls
237 lines (207 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# $Id: isql.py 6639 2009-08-10 17:06:51Z fwierzbicki $
import dbexts, cmd, sys, os
"""
Isql works in conjunction with dbexts to provide an interactive environment
for database work.
"""
__version__ = "$Revision: 6639 $"[11:-2]
class IsqlExit(Exception): pass
class Prompt:
"""
This class fixes a problem with the cmd.Cmd class since it uses an ivar 'prompt'
as opposed to a method 'prompt()'. To get around this, this class is plugged in
as a 'prompt' attribute and when invoked the '__str__' method is called which
figures out the appropriate prompt to display. I still think, even though this
is clever, the attribute version of 'prompt' is poor design.
"""
def __init__(self, isql):
self.isql = isql
def __str__(self):
prompt = "%s> " % (self.isql.db.dbname)
if len(self.isql.sqlbuffer) > 0:
prompt = "... "
return prompt
if os.name == 'java':
def __tojava__(self, cls):
import java.lang.String
if cls == java.lang.String:
return self.__str__()
return False
class IsqlCmd(cmd.Cmd):
def __init__(self, db=None, delimiter=";", comment=('#', '--')):
cmd.Cmd.__init__(self, completekey=None)
if db is None or type(db) == type(""):
self.db = dbexts.dbexts(db)
else:
self.db = db
self.kw = {}
self.sqlbuffer = []
self.comment = comment
self.delimiter = delimiter
self.prompt = Prompt(self)
def parseline(self, line):
command, arg, line = cmd.Cmd.parseline(self, line)
if command and command <> "EOF":
command = command.lower()
return command, arg, line
def do_which(self, arg):
"""\nPrints the current db connection parameters.\n"""
print self.db
return False
def do_EOF(self, arg):
return False
def do_p(self, arg):
"""\nExecute a python expression.\n"""
try:
exec arg.strip() in globals()
except:
print sys.exc_info()[1]
return False
def do_column(self, arg):
"""\nInstructions for column display.\n"""
return False
def do_use(self, arg):
"""\nUse a new database connection.\n"""
# this allows custom dbexts
self.db = self.db.__class__(arg.strip())
return False
def do_table(self, arg):
"""\nPrints table meta-data. If no table name, prints all tables.\n"""
if len(arg.strip()):
self.db.table(arg, **self.kw)
else:
self.db.table(None, **self.kw)
return False
def do_proc(self, arg):
"""\nPrints store procedure meta-data.\n"""
if len(arg.strip()):
self.db.proc(arg, **self.kw)
else:
self.db.proc(None, **self.kw)
return False
def do_schema(self, arg):
"""\nPrints schema information.\n"""
print
self.db.schema(arg)
print
return False
def do_delimiter(self, arg):
"""\nChange the delimiter.\n"""
delimiter = arg.strip()
if len(delimiter) > 0:
self.delimiter = delimiter
def do_o(self, arg):
"""\nSet the output.\n"""
if not arg:
fp = self.db.out
try:
if fp:
fp.close()
finally:
self.db.out = None
else:
fp = open(arg, "w")
self.db.out = fp
def do_q(self, arg):
"""\nQuit.\n"""
try:
if self.db.out:
self.db.out.close()
finally:
return True
def do_set(self, arg):
"""\nSet a parameter. Some examples:\n set owner = 'informix'\n set types = ['VIEW', 'TABLE']\nThe right hand side is evaluated using `eval()`\n"""
if len(arg.strip()) == 0:
items = self.kw.items()
if len(items):
print
# format the results but don't include how many rows affected
for a in dbexts.console(items, ("key", "value"))[:-1]:
print a
print
return False
d = filter(lambda x: len(x) > 0, map(lambda x: x.strip(), arg.split("=")))
if len(d) == 1:
if self.kw.has_key(d[0]):
del self.kw[d[0]]
else:
self.kw[d[0]] = eval(d[1])
def do_i(self, arg):
fp = open(arg)
try:
print
for line in fp.readlines():
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
finally:
fp.close()
return False
def default(self, arg):
try:
token = arg.strip()
if not token:
return False
comment = [token.startswith(x) for x in self.comment]
if reduce(lambda x,y: x or y, comment):
return False
if token[0] == '\\':
token = token[1:]
# is it possible the line contains the delimiter
if len(token) >= len(self.delimiter):
# does the line end with the delimiter
if token[-1 * len(self.delimiter):] == self.delimiter:
# now add all up to the delimiter
self.sqlbuffer.append(token[:-1 * len(self.delimiter)])
if self.sqlbuffer:
q = " ".join(self.sqlbuffer)
print q
self.db.isql(q, **self.kw)
self.sqlbuffer = []
if self.db.updatecount:
print
if self.db.updatecount == 1:
print "1 row affected"
else:
print "%d rows affected" % (self.db.updatecount)
print
return False
if token:
self.sqlbuffer.append(token)
except:
self.sqlbuffer = []
print
print sys.exc_info()[1]
print
return False
def emptyline(self):
return False
def postloop(self):
raise IsqlExit()
def cmdloop(self, intro=None):
while 1:
try:
cmd.Cmd.cmdloop(self, intro)
except IsqlExit, e:
break
except Exception, e:
print
print e
print
intro = None
if __name__ == '__main__':
import getopt
try:
opts, args = getopt.getopt(sys.argv[1:], "b:", [])
except getopt.error, msg:
print
print msg
print "Try `%s --help` for more information." % (sys.argv[0])
sys.exit(0)
dbname = None
for opt, arg in opts:
if opt == '-b':
dbname = arg
intro = "\nisql - interactive sql (%s)\n" % (__version__)
isql = IsqlCmd(dbname)
isql.cmdloop()