1111 import sqlite3
1212
1313from winevtrc import definitions
14+ from winevtrc import errors
1415from winevtrc import py2to3
1516from winevtrc import resources
1617
@@ -56,6 +57,7 @@ def CreateTable(self, table_name, column_definitions):
5657 column_definitions (list[str]): column definitions.
5758
5859 Raises:
60+ BackendError: if the database back-end raises an exception.
5961 IOError: if the database is not opened or
6062 if the database is in read-only mode.
6163 """
@@ -68,7 +70,10 @@ def CreateTable(self, table_name, column_definitions):
6870 sql_query = u'CREATE TABLE {0:s} ( {1:s} )' .format (
6971 table_name , u', ' .join (column_definitions ))
7072
71- self ._cursor .execute (sql_query )
73+ try :
74+ self ._cursor .execute (sql_query )
75+ except sqlite3 .OperationalError as exception :
76+ raise errors .BackendError (exception )
7277
7378 def GetValues (self , table_names , column_names , condition ):
7479 """Retrieves values from a table.
@@ -82,6 +87,7 @@ def GetValues(self, table_names, column_names, condition):
8287 generator: values generator.
8388
8489 Raises:
90+ BackendError: if the database back-end raises an exception.
8591 IOError: if the database is not opened.
8692 """
8793 def _GetValues (cursor , table_names , column_names , condition ):
@@ -102,7 +108,10 @@ def _GetValues(cursor, table_names, column_names, condition):
102108 sql_query = u'SELECT {1:s} FROM {0:s}{2:s}' .format (
103109 u', ' .join (table_names ), u', ' .join (column_names ), condition )
104110
105- cursor .execute (sql_query )
111+ try :
112+ cursor .execute (sql_query )
113+ except sqlite3 .OperationalError as exception :
114+ raise errors .BackendError (exception )
106115
107116 for row in cursor :
108117 values = {}
@@ -126,18 +135,25 @@ def HasTable(self, table_name):
126135 bool: True if the table exists, false otheriwse.
127136
128137 Raises:
138+ BackendError: if the database back-end raises an exception.
129139 IOError: if the database is not opened.
130140 """
131141 if not self ._connection :
132142 raise IOError (u'Cannot determine if table exists database not opened.' )
133143
134144 sql_query = self ._HAS_TABLE_QUERY .format (table_name )
135145
136- self ._cursor .execute (sql_query )
137- if self ._cursor .fetchone ():
138- has_table = True
139- else :
140- has_table = False
146+ try :
147+ self ._cursor .execute (sql_query )
148+
149+ if self ._cursor .fetchone ():
150+ has_table = True
151+ else :
152+ has_table = False
153+
154+ except sqlite3 .OperationalError as exception :
155+ raise errors .BackendError (exception )
156+
141157 return has_table
142158
143159 def InsertValues (self , table_name , column_names , values ):
@@ -149,6 +165,7 @@ def InsertValues(self, table_name, column_names, values):
149165 values (list[str]): values formatted as a string.
150166
151167 Raises:
168+ BackendError: if the database back-end raises an exception.
152169 IOError: if the database is not opened or
153170 if the database is in read-only mode or
154171 if an unsupported value type is encountered.
@@ -181,7 +198,10 @@ def InsertValues(self, table_name, column_names, values):
181198 sql_query = u'INSERT INTO {0:s} ( {1:s} ) VALUES ( {2:s} )' .format (
182199 table_name , u', ' .join (column_names ), u', ' .join (sql_values ))
183200
184- self ._cursor .execute (sql_query )
201+ try :
202+ self ._cursor .execute (sql_query )
203+ except sqlite3 .OperationalError as exception :
204+ raise errors .BackendError (exception )
185205
186206 def Open (self , filename , read_only = False ):
187207 """Opens the database file.
@@ -196,6 +216,7 @@ def Open(self, filename, read_only=False):
196216 bool: True if successful or False if not.
197217
198218 Raises:
219+ BackendError: if the database back-end raises an exception.
199220 IOError: if the database is already opened.
200221 """
201222 if self ._connection :
@@ -208,7 +229,11 @@ def Open(self, filename, read_only=False):
208229 if not self ._connection :
209230 return False
210231
211- self ._cursor = self ._connection .cursor ()
232+ try :
233+ self ._cursor = self ._connection .cursor ()
234+ except sqlite3 .OperationalError as exception :
235+ raise errors .BackendError (exception )
236+
212237 if not self ._cursor :
213238 return False
214239
0 commit comments