Changeset 3413 for pykota/trunk/pykota/storages/sqlitestorage.py
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/storages/sqlitestorage.py
r3411 r3413 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 29 29 try : 30 30 from pysqlite2 import dbapi2 as sqlite 31 except ImportError : 31 except ImportError : 32 32 import sys 33 33 # TODO : to translate or not to translate ? 34 34 raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PySQLite module installed correctly." % sys.version.split()[0] 35 35 36 36 class Storage(BaseStorage, SQLStorage) : 37 37 def __init__(self, pykotatool, host, dbname, user, passwd) : 38 38 """Opens the SQLite database connection.""" 39 39 BaseStorage.__init__(self, pykotatool) 40 40 41 41 self.tool.logdebug("Trying to open database (dbname=%s)..." % dbname) 42 42 self.database = sqlite.connect(dbname, isolation_level=None) … … 44 44 self.closed = 0 45 45 self.tool.logdebug("Database opened (dbname=%s)" % dbname) 46 47 def close(self) : 46 47 def close(self) : 48 48 """Closes the database connection.""" 49 49 if not self.closed : … … 52 52 self.closed = 1 53 53 self.tool.logdebug("Database closed.") 54 55 def beginTransaction(self) : 54 55 def beginTransaction(self) : 56 56 """Starts a transaction.""" 57 57 self.cursor.execute("BEGIN;") 58 58 self.tool.logdebug("Transaction begins...") 59 60 def commitTransaction(self) : 59 60 def commitTransaction(self) : 61 61 """Commits a transaction.""" 62 62 self.cursor.execute("COMMIT;") 63 63 self.tool.logdebug("Transaction committed.") 64 65 def rollbackTransaction(self) : 64 65 def rollbackTransaction(self) : 66 66 """Rollbacks a transaction.""" 67 67 self.cursor.execute("ROLLBACK;") 68 68 self.tool.logdebug("Transaction aborted.") 69 69 70 70 def doRawSearch(self, query) : 71 71 """Does a raw search query.""" 72 query = query.strip() 73 if not query.endswith(';') : 72 query = query.strip() 73 if not query.endswith(';') : 74 74 query += ';' 75 75 try : 76 76 self.querydebug("QUERY : %s" % query) 77 77 self.cursor.execute(query) 78 except self.database.Error, msg : 78 except self.database.Error, msg : 79 79 raise PyKotaStorageError, repr(msg) 80 else : 80 else : 81 81 result = self.cursor.fetchall() 82 82 return result 83 84 def doSearch(self, query) : 83 84 def doSearch(self, query) : 85 85 """Does a search query.""" 86 86 result = self.doRawSearch(query) 87 if result : 87 if result : 88 88 rows = [] 89 89 fields = {} 90 90 for i in range(len(self.cursor.description)) : 91 91 fields[i] = self.cursor.description[i][0] 92 for row in result : 92 for row in result : 93 93 rowdict = {} 94 94 for field in fields.keys() : … … 99 99 pass 100 100 rowdict[fields[field]] = value 101 rows.append(rowdict) 102 return rows 103 101 rows.append(rowdict) 102 return rows 103 104 104 def doModify(self, query) : 105 105 """Does a (possibly multiple) modify query.""" 106 query = query.strip() 107 if not query.endswith(';') : 106 query = query.strip() 107 if not query.endswith(';') : 108 108 query += ';' 109 109 try : 110 110 self.querydebug("QUERY : %s" % query) 111 111 self.cursor.execute(query) 112 except self.database.Error, msg : 112 except self.database.Error, msg : 113 113 self.tool.logdebug("Query failed : %s" % repr(msg)) 114 114 raise PyKotaStorageError, repr(msg) 115 115 116 116 def doQuote(self, field) : 117 117 """Quotes a field for use as a string in SQL queries.""" 118 if type(field) == type(0.0) : 118 if type(field) == type(0.0) : 119 119 return field 120 elif type(field) == type(0) : 120 elif type(field) == type(0) : 121 121 return field 122 elif type(field) == type(0L) : 122 elif type(field) == type(0L) : 123 123 return field 124 124 elif field is not None : 125 125 return ("'%s'" % field.replace("'", "''")).decode("UTF-8") 126 else : 126 else : 127 127 return "NULL" 128 128 129 129 def prepareRawResult(self, result) : 130 130 """Prepares a raw result by including the headers.""" 131 131 if result : 132 132 entries = [tuple([f[0] for f in self.cursor.description])] 133 for entry in result : 133 for entry in result : 134 134 row = [] 135 135 for value in entry : … … 139 139 pass 140 140 row.append(value) 141 entries.append(tuple(row)) 142 return entries 143 141 entries.append(tuple(row)) 142 return entries 143