1
1
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2
2
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
3
3
4
- """Sqlite coverage data."""
5
-
6
- # TODO: factor out dataop debugging to a wrapper class?
7
- # TODO: make sure all dataop debugging is in place somehow
4
+ """SQLite coverage data."""
8
5
9
6
import collections
10
7
import datetime
31
28
os = isolate_module (os )
32
29
33
30
# If you change the schema, increment the SCHEMA_VERSION, and update the
34
- # docs in docs/dbschema.rst also .
31
+ # docs in docs/dbschema.rst by running "make cogdoc" .
35
32
36
33
SCHEMA_VERSION = 7
37
34
@@ -390,8 +387,10 @@ def _file_id(self, filename, add=False):
390
387
if filename not in self ._file_map :
391
388
if add :
392
389
with self ._connect () as con :
393
- cur = con .execute ("insert or replace into file (path) values (?)" , (filename ,))
394
- self ._file_map [filename ] = cur .lastrowid
390
+ self ._file_map [filename ] = con .execute_for_rowid (
391
+ "insert or replace into file (path) values (?)" ,
392
+ (filename ,)
393
+ )
395
394
return self ._file_map .get (filename )
396
395
397
396
def _context_id (self , context ):
@@ -428,8 +427,10 @@ def _set_context_id(self):
428
427
self ._current_context_id = context_id
429
428
else :
430
429
with self ._connect () as con :
431
- cur = con .execute ("insert into context (context) values (?)" , (context ,))
432
- self ._current_context_id = cur .lastrowid
430
+ self ._current_context_id = con .execute_for_rowid (
431
+ "insert into context (context) values (?)" ,
432
+ (context ,)
433
+ )
433
434
434
435
def base_filename (self ):
435
436
"""The base filename for storing data.
@@ -1126,6 +1127,14 @@ def execute(self, sql, parameters=()):
1126
1127
self .debug .write (f"EXCEPTION from execute: { msg } " )
1127
1128
raise DataError (f"Couldn't use data file { self .filename !r} : { msg } " ) from exc
1128
1129
1130
+ def execute_for_rowid (self , sql , parameters = ()):
1131
+ """Like execute, but returns the lastrowid."""
1132
+ con = self .execute (sql , parameters )
1133
+ rowid = con .lastrowid
1134
+ if self .debug .should ("sqldata" ):
1135
+ self .debug .write (f"Row id result: { rowid !r} " )
1136
+ return rowid
1137
+
1129
1138
def execute_one (self , sql , parameters = ()):
1130
1139
"""Execute a statement and return the one row that results.
1131
1140
@@ -1147,7 +1156,11 @@ def executemany(self, sql, data):
1147
1156
"""Same as :meth:`python:sqlite3.Connection.executemany`."""
1148
1157
if self .debug .should ("sql" ):
1149
1158
data = list (data )
1150
- self .debug .write (f"Executing many { sql !r} with { len (data )} rows" )
1159
+ final = ":" if self .debug .should ("sqldata" ) else ""
1160
+ self .debug .write (f"Executing many { sql !r} with { len (data )} rows{ final } " )
1161
+ if self .debug .should ("sqldata" ):
1162
+ for i , row in enumerate (data ):
1163
+ self .debug .write (f"{ i :4d} : { row !r} " )
1151
1164
try :
1152
1165
return self .con .executemany (sql , data )
1153
1166
except Exception : # pragma: cant happen
0 commit comments