dabo Commit
Revision 2810
Date: 2007-02-13 12:39:34 -0800 (Tue, 13 Feb 2007)
Author: Ed

Changed:
U   trunk/dabo/__init__.py
U   trunk/dabo/dApp.py
U   trunk/dabo/dPref.py
U   trunk/dabo/db/dCursorMixin.py
U   trunk/dabo/lib/logger.py

Log:
Added the option of logging all database execution. By default, no logging is 
done. You can set the application's 'DatabaseActivityLog' property to either a 
file path or a file-like object, and all database execution will be written to 
that log.

Since there is a lot of internal Dabo database execution reading/writing 
preferences and saved settings, those actions will not be logged.


Diff:
Modified: trunk/dabo/__init__.py
===================================================================
--- trunk/dabo/__init__.py      2007-02-13 18:04:12 UTC (rev 2809)
+++ trunk/dabo/__init__.py      2007-02-13 20:39:34 UTC (rev 2810)
@@ -129,6 +129,11 @@
 errorLog = Log()
 errorLog.Caption = "Dabo Error Log"
 errorLog.LogObject = sys.stderr
+# This log is set to None by default. It must be manually activated 
+# via the Application object.
+dbActivityLog = Log()
+dbActivityLog.Caption = "Database Activity Log"
+dbActivityLog.LogObject = None
 
 # Import global settings (do this first, as other imports may rely on it):
 from settings import *

Modified: trunk/dabo/dApp.py
===================================================================
--- trunk/dabo/dApp.py  2007-02-13 18:04:12 UTC (rev 2809)
+++ trunk/dabo/dApp.py  2007-02-13 20:39:34 UTC (rev 2810)
@@ -673,6 +673,21 @@
                self._cryptoProvider = val
 
 
+       def _getDatabaseActivityLog(self):
+               return dabo.dbActivityLog.LogObject
+
+       def _setDatabaseActivityLog(self, val):
+               if isinstance(val, basestring):
+                       try:
+                               f = open(val, "a")
+                       except:
+                               dabo.errorLog.write(_("Could not open file: 
'%s'") % val)
+                               return
+               else:
+                       f = val
+               dabo.dbActivityLog.LogObject = f
+
+
        def _getDrawSizerOutlines(self):
                return self.uiApp.DrawSizerOutlines
        
@@ -862,6 +877,10 @@
        Crypto = property(_getCrypto, _setCrypto, None, 
                        _("Reference to the object that provides cryptographic 
services.  (varies)" ) )
        
+       DatabaseActivityLog = property(_getDatabaseActivityLog, 
_setDatabaseActivityLog, None,
+                       _("""Path to the file (or file-like object) to be used 
for logging all database 
+                       activity. Default=None, which means no log is kept.   
(file or str)"""))
+       
        DrawSizerOutlines = property(_getDrawSizerOutlines, 
_setDrawSizerOutlines, None,
                        _("Determines if sizer outlines are drawn on the 
ActiveForm.  (bool)"))
        

Modified: trunk/dabo/dPref.py
===================================================================
--- trunk/dabo/dPref.py 2007-02-13 18:04:12 UTC (rev 2809)
+++ trunk/dabo/dPref.py 2007-02-13 20:39:34 UTC (rev 2810)
@@ -67,6 +67,7 @@
                        self._cxn = dabo.db.dConnection(connectInfo={"dbType": 
"SQLite",
                                        "database": os.path.join(prefdir, 
"DaboPreferences.db")})
                        self._cursor = self._cxn.getDaboCursor()
+                       self._cursor.IsPrefCursor = True
                        # Make sure that the table exists
                        if not  "daboprefs" in self._cursor.getTables():
                                self._cursor.execute("create table daboprefs 
(ckey text not null, ctype text not null, cvalue text not null)")
@@ -290,21 +291,25 @@
                                del self._cache[key]
        
        
-       def getPrefs(self, returnNested=False, key=None):
+       def getPrefs(self, returnNested=False, key=None, asDataSet=False):
                """Returns all the preferences set for this object. If 
returnNested is True,
                returns any sub-preferences too.
                """
                crs = self._cursor
                if key is None:
                        key = self._getKey()
-               sql = "select * from daboprefs where ckey like ?"
-               crs.execute(sql, ("%s.%%" % key, ))
+               key = key.replace("_", r"\_")
+               param = "%(key)s%%" % locals()
+               sql = "select * from daboprefs where ckey like ? escape '\\' "
+               crs.execute(sql, (param, ))
                ds = crs.getDataSet()
                if not returnNested:
                        # Filter out all the results that are not first-level 
prefs
                        keylen = len(key)+1
                        ds = [rec for rec in ds
                                        if len(rec["ckey"][keylen:].split(".")) 
== 1]
+               if asDataSet:
+                       return ds
                ret = {}
                for rec in ds:
                        ret[rec["ckey"]] = self._decodeType(rec)

Modified: trunk/dabo/db/dCursorMixin.py
===================================================================
--- trunk/dabo/db/dCursorMixin.py       2007-02-13 18:04:12 UTC (rev 2809)
+++ trunk/dabo/db/dCursorMixin.py       2007-02-13 20:39:34 UTC (rev 2810)
@@ -110,6 +110,9 @@
                # mementos and new records, keyed on record object ids:
                self._mementos = {}
                self._newRecords = {}
+               
+               # Flag preference cursors so that they don't fill up the logs
+               self._isPrefCursor = False
 
                self.initProperties()
 
@@ -253,18 +256,15 @@
                        sql = unicode(sql, self.Encoding)
                sql = self.processFields(sql)
                
-               # Make sure all Unicode characters are properly encoded.
-#              if isinstance(sql, unicode):
-#                      sqlEX = sql.encode(self.Encoding)
-#              else:
-#                      sqlEX = sql
-               sqlEX = sql
-               
                try:
                        if params is None or len(params) == 0:
-                               res = self.superCursor.execute(self, sqlEX)
+                               res = self.superCursor.execute(self, sql)
+                               if not self.IsPrefCursor:
+                                       dabo.dbActivityLog.write("SQL: %s" % 
sql.replace("\n", " "))
                        else:
-                               res = self.superCursor.execute(self, sqlEX, 
params)
+                               res = self.superCursor.execute(self, sql, 
params)
+                               if not self.IsPrefCursor:
+                                       dabo.dbActivityLog.write("SQL: %s, 
PARAMS: %s" % (sql.replace("\n", " "), ", ".join(params)))
                except Exception, e:
                        # If this is due to a broken connection, let the user 
know.
                        # Different backends have different messages, but they
@@ -2003,6 +2003,13 @@
                return self._newRecords.has_key(recKey)
        
 
+       def _getIsPrefCursor(self):
+               return self._isPrefCursor
+
+       def _setIsPrefCursor(self, val):
+               self._isPrefCursor = val
+
+
        def _getKeyField(self):
                try:
                        return self._keyField
@@ -2148,6 +2155,10 @@
        IsAdding = property(_getIsAdding, None, None,
                        _("Returns True if the current record is new and 
unsaved"))
                        
+       IsPrefCursor = property(_getIsPrefCursor, _setIsPrefCursor, None,
+                       _("""Returns True if this cursor is used for managing 
internal 
+                       Dabo preferences and settings. Default=False.  
(bool)"""))
+       
        LastSQL = property(_getLastSQL, None, None,
                        _("Returns the last executed SQL statement."))
 

Modified: trunk/dabo/lib/logger.py
===================================================================
--- trunk/dabo/lib/logger.py    2007-02-13 18:04:12 UTC (rev 2809)
+++ trunk/dabo/lib/logger.py    2007-02-13 20:39:34 UTC (rev 2810)
@@ -29,6 +29,9 @@
        
        def write(self, message):
                """Writes the passed message to the log."""
+               if self.LogObject is None:
+                       # Send messages to the bit bucket...
+                       return
                if self.LogTimeStamp:
                        timestamp = "%s: " % time.asctime()
                else:




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev

Reply via email to