A
download isql.py
Language: Python
LOC: 118
Project Info
NetMage
Server: SourceForge
Type: cvs
...ge\netmage\netmage\lib\Lib\
   .cvsignore
   __future__.py
   anydbm.py
   atexit.py
   base64.py
   BaseHTTPServer.py
   bdb.py
   binhex.py
   bisect.py
   calendar.py
   cgi.py
   CGIHTTPServer.py
   cmd.py
   code.py
   codecs.py
   colorsys.py
   commands.py
   compileall.py
   ConfigParser.py
   Cookie.py
   copy.py
   copy_reg.py
   dbexts.py
   difflib.py
   dircache.py
   doctest.py
   dospath.py
   dumbdbm.py
   fileinput.py
   fnmatch.py
   formatter.py
   fpformat.py
   ftplib.py
   getopt.py
   glob.py
   gopherlib.py
   gzip.py
   htmlentitydefs.py
   htmllib.py
   httplib.py
   imaplib.py
   imghdr.py
   isql.py
   javaos.py
   javapath.py
   jreload.py
   keyword.py
   linecache.py
   macpath.py
   macurl2path.py
   mailbox.py
   mailcap.py
   marshal.py
   mhlib.py
   mimetools.py
   mimetypes.py
   MimeWriter.py
   mimify.py
   multifile.py
   mutex.py
   nntplib.py
   ntpath.py
   nturl2path.py
   pdb.py
   pickle.py
   pipes.py
   popen2.py
   poplib.py
   posixfile.py
   posixpath.py
   pprint.py
   profile.py
   pstats.py
   pyclbr.py
   Queue.py
   quopri.py
   random.py
   re.py
   reconvert.py
   repr.py
   rfc822.py
   sched.py
   sgmllib.py
   shelve.py
   shutil.py
   SimpleHTTPServer.py
   site.py
   smtplib.py
   sndhdr.py
   socket.py
   SocketServer.py
   sre.py
   sre_compile.py
   sre_constants.py
   sre_parse.py
   stat.py
   string.py
   StringIO.py
   symbol.py
   telnetlib.py
   tempfile.py
   threading.py
   token.py
   tokenize.py
   traceback.py
   tzparse.py
   unittest.py
   urllib.py
   urlparse.py
   user.py
   UserDict.py
   UserList.py
   UserString.py
   warnings.py
   weakref.py
   whichdb.py
   whrandom.py
   xdrlib.py
   xmllib.py
   zipfile.py
   zlib.py

# $Id: isql.py,v 1.1 2002/09/29 01:45:26 jturner2 Exp $

import dbexts, cmd, sys

"""
Isql works in conjunction with dbexts to provide an interactive environment
for database work.
"""

__version__ = "$Revision: 1.1 $"[11:-2]

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

class IsqlCmd(cmd.Cmd):

	def __init__(self, db=None, delimiter=";"):
		cmd.Cmd.__init__(self)
		if db is None or type(db) == type(""):
			self.db = dbexts.dbexts(db)
		else:
			self.db = db
		self.kw = {}
		self.sqlbuffer = []
		self.delimiter = delimiter
		self.prompt = Prompt(self)

	def do_which(self, arg):
		"""\nPrints the current db connection parameters.\n"""
		print self.db
		return None

	def do_EOF(self, arg):
		return None

	def do_p(self, arg):
		"""\nExecute a python expression.\n"""
		try:
			exec arg.strip() in globals()
		except:
			print sys.exc_info()[1]
		return None

	def do_use(self, arg):
		"""\nUse a new database connection.\n"""
		self.db = dbexts.dbexts(arg.strip())
		return None

	def do_table(self, arg):
		"""\nPrints table meta-data.  If no table name, prints all tables.\n"""
		if len(arg.strip()):
			apply(self.db.table, (arg,), self.kw)
		else:
			apply(self.db.table, (None,), self.kw)
		return None

	def do_proc(self, arg):
		"""\nPrints store procedure meta-data.\n"""
		if len(arg.strip()):
			apply(self.db.proc, (arg,), self.kw)
		else:
			apply(self.db.proc, (None,), self.kw)
		return None

	def do_schema(self, arg):
		"""\nPrints schema information.\n"""
		print
		self.db.schema(arg)
		print
		return None

	def do_delimiter(self, arg):
		"""\nChange the delimiter.\n"""
		delimiter = arg.strip()
		if len(delimiter) > 0:
			self.delimiter = delimiter

	def do_q(self, arg):
		"""\nQuit.\n"""
		return 1

	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"""
		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 default(self, arg):
		try:
			token = arg.strip()
			# 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:
						self.db.isql(" ".join(self.sqlbuffer))
						self.sqlbuffer = []
						return None
			if token:
				self.sqlbuffer.append(token)
		except:
			self.sqlbuffer = []
			print sys.exc_info()[1]
		return None

	def emptyline(self):
		return 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__)

	IsqlCmd(dbname).cmdloop(intro)

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us