dabo Commit
Revision 4715
Date: 2008-11-25 18:00:28 -0800 (Tue, 25 Nov 2008)
Author: Ed
Trac: http://svn.dabodev.com/trac/dabo/changeset/4715

Changed:
U   trunk/dabo/dException.py
U   trunk/dabo/dPref.py
U   trunk/dabo/db/dConnection.py
U   trunk/dabo/db/dbSQLite.py
U   trunk/dabo/settings.py

Log:
Changed the default for SQLite to not create the database file if it doesn't 
already exist. This setting is controlled by dabo.createDbFiles, which defaults 
to False. 

Added dException.DBFileDoesNotExistException class. This is raised if you try 
to connect to an SQLite file that does not exist and dabo.createDbFiles is 
False. This exception can be caught in the calling routines and handled 
appropriately.

dConnection now takes an optional constructor parameter 'forceCreate'; if that 
is True (default=False), the db file will be created no matter what the system 
setting is. 

dbSQLite.getConnection() takes that same parameter, and passes it to the 
connection constructor.

dPref, which is supposed to create the DaboPreferences.db file no matter what, 
has been modified to pass  forceCreate=True to the connection.


Diff:
Modified: trunk/dabo/dException.py
===================================================================
--- trunk/dabo/dException.py    2008-11-25 19:10:45 UTC (rev 4714)
+++ trunk/dabo/dException.py    2008-11-26 02:00:28 UTC (rev 4715)
@@ -67,6 +67,9 @@
 class DBNoDBOnHostException(DatabaseException):
        pass
 
+class DBFileDoesNotExistException(DatabaseException):
+       pass
+
 class DBQueryException(DatabaseException):
        def __init__(self, err, sql=None):
                if sql is None:

Modified: trunk/dabo/dPref.py
===================================================================
--- trunk/dabo/dPref.py 2008-11-25 19:10:45 UTC (rev 4714)
+++ trunk/dabo/dPref.py 2008-11-26 02:00:28 UTC (rev 4715)
@@ -74,7 +74,8 @@
                                #      chdir() accordingly)..
                                prefdir = os.getcwd()
                        db = os.path.join(prefdir, "DaboPreferences.db")
-                       self._cxn = dabo.db.dConnection(connectInfo={"DbType": 
"SQLite", "Database": db})
+                       self._cxn = dabo.db.dConnection(connectInfo={"DbType": 
"SQLite", "Database": db},
+                                       forceCreate=True)
                        self._cursor = self._cxn.getDaboCursor()
                        self._cursor.IsPrefCursor = True
                        # Make sure that the table exists

Modified: trunk/dabo/db/dConnection.py
===================================================================
--- trunk/dabo/db/dConnection.py        2008-11-25 19:10:45 UTC (rev 4714)
+++ trunk/dabo/db/dConnection.py        2008-11-26 02:00:28 UTC (rev 4715)
@@ -7,8 +7,9 @@
 
 class dConnection(dObject):
        """ Hold a connection to a backend database. """
-       def __init__(self, connectInfo=None, parent=None, **kwargs):
+       def __init__(self, connectInfo=None, parent=None, forceCreate=False, 
**kwargs):
                self._baseClass = dConnection
+               self._forceCreate = forceCreate
                super(dConnection, self).__init__()
                # Store a reference to the parent object (bizobj maybe; app 
                # object connection collection most likely)
@@ -72,7 +73,7 @@
 
        def _openConnection(self, **kwargs):
                """ Open a connection to the database and store it for future 
use. """
-               return self._connectInfo.getConnection(**kwargs)
+               return 
self._connectInfo.getConnection(forceCreate=self._forceCreate, **kwargs)
                
        
        def getBackendObject(self):

Modified: trunk/dabo/db/dbSQLite.py
===================================================================
--- trunk/dabo/db/dbSQLite.py   2008-11-25 19:10:45 UTC (rev 4714)
+++ trunk/dabo/db/dbSQLite.py   2008-11-26 02:00:28 UTC (rev 4715)
@@ -4,7 +4,7 @@
 import re
 import dabo
 from dabo.dLocalize import _
-from dabo.dException import dException
+from dabo.dException import dException, DBFileDoesNotExistException
 from dBackend import dBackend
 from dNoEscQuoteStr import dNoEscQuoteStr as dNoEQ
 from dCursorMixin import dCursorMixin
@@ -23,7 +23,7 @@
                self._alreadyCorrectedFieldTypes = True
                
 
-       def getConnection(self, connectInfo, **kwargs):
+       def getConnection(self, connectInfo, forceCreate=False, **kwargs):
                ## Mods to sqlite to return DictCursors by default, so that 
dCursor doesn't
                ## need to do the conversion:
                dbapi = self.dbapi
@@ -53,6 +53,10 @@
 
                self._dictCursorClass = DictCursor
                pth = os.path.expanduser(connectInfo.Database)
+               if not forceCreate and not dabo.createDbFiles:
+                       if not os.path.exists(pth):
+                               # Database file does not exist; raise an error
+                               raise DBFileDoesNotExistException, _("Database 
file '%s' does not exist") % pth
                pth = pth.decode(sys.getfilesystemencoding()).encode("utf-8")
                # Need to specify "isolation_level=None" to have transactions 
working correctly.
                self._connection = self.dbapi.connect(pth, 
factory=DictConnection, isolation_level=None)

Modified: trunk/dabo/settings.py
===================================================================
--- trunk/dabo/settings.py      2008-11-25 19:10:45 UTC (rev 4714)
+++ trunk/dabo/settings.py      2008-11-26 02:00:28 UTC (rev 4715)
@@ -171,6 +171,10 @@
                "mpeg": "MPEG Videos",
                "mp3": "mp3 Audio Files",
 }
+
+# For file-based data backends such as SQLite, do we allow creating a 
connection to
+# a non-existent file, which SQLite will then create? 
+createDbFiles = False
                
 
 ### Settings - end




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: http://leafe.com/archives/byMID/[EMAIL PROTECTED]

Reply via email to