changeset 532:506156a9915e tip

add history file support
author zhangmingyuan240@gmail.com
date Mon, 18 Jun 2012 16:06:42 +0800
parents 5c49b5a32687
children
files sqlpython/sqlpython.py
diffstat 1 files changed, 15 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/sqlpython.py	Fri Jul 08 23:51:28 2011 -0400
+++ b/sqlpython/sqlpython.py	Mon Jun 18 16:06:42 2012 +0800
@@ -10,6 +10,8 @@
 
 import cmd2,getpass,binascii,re,os,platform
 import pyparsing, connections
+import readline
+
 __version__ = '1.7.2'
 try:
     import cx_Oracle
@@ -20,6 +22,9 @@
 except ImportError:
     psycopg2 = None
 
+homedir = os.path.expandvars('$HOME')
+history_file = os.path.join(homedir,'.sqlplus_history')
+
 class Parser(object):
     comment_def = "--" + pyparsing.NotAny('-' + pyparsing.CaselessKeyword('begin')) + pyparsing.ZeroOrMore(pyparsing.CharsNotIn("\n"))    
     def __init__(self, scanner, retainSeparator=True):
@@ -55,6 +60,7 @@
         self.timeout = 30
         self.commit_on_exit = True
         self.instances = {}
+	self.do_loadhistory()
         
     def no_instance(self):
         self.prompt = 'SQL.No_Connection> '
@@ -76,6 +82,7 @@
                                     for (k,v) in sorted(self.instances.items())) + '\n')
         
     def disconnect(self, arg):
+	self.do_writehistory()
         try:
             instance_number = int(arg)
             instance = self.instances[instance_number]
@@ -318,6 +325,7 @@
         return cmd2.Cmd.do_quit(self, None)
     do_exit = do_quit
     do_q = do_quit
+    do_eof = do_quit
     def colorize(self, val, color):
         if color not in self.colorcodes:
             if (color % 2):
@@ -377,8 +385,10 @@
             toprint.insert(0, sepcols)
             toprint.append(sepcols)
         return '\n'.join(toprint)
-    
-    
-    
-    
-
+    def do_loadhistory(self, arg=''):
+        readline.read_history_file(history_file)
+        for i in open(history_file,'r'):
+            self.history.append(i)
+    def do_writehistory(self, arg=''):
+        readline.set_history_length(1000)
+        readline.write_history_file(history_file)