11# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
22# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
33
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."""
85
96import collections
107import datetime
3128os = isolate_module (os )
3229
3330# 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" .
3532
3633SCHEMA_VERSION = 7
3734
@@ -390,8 +387,10 @@ def _file_id(self, filename, add=False):
390387 if filename not in self ._file_map :
391388 if add :
392389 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+ )
395394 return self ._file_map .get (filename )
396395
397396 def _context_id (self , context ):
@@ -428,8 +427,10 @@ def _set_context_id(self):
428427 self ._current_context_id = context_id
429428 else :
430429 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+ )
433434
434435 def base_filename (self ):
435436 """The base filename for storing data.
@@ -1126,6 +1127,14 @@ def execute(self, sql, parameters=()):
11261127 self .debug .write (f"EXCEPTION from execute: { msg } " )
11271128 raise DataError (f"Couldn't use data file { self .filename !r} : { msg } " ) from exc
11281129
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+
11291138 def execute_one (self , sql , parameters = ()):
11301139 """Execute a statement and return the one row that results.
11311140
@@ -1147,7 +1156,11 @@ def executemany(self, sql, data):
11471156 """Same as :meth:`python:sqlite3.Connection.executemany`."""
11481157 if self .debug .should ("sql" ):
11491158 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} " )
11511164 try :
11521165 return self .con .executemany (sql , data )
11531166 except Exception : # pragma: cant happen
0 commit comments