"""Commit SQL transaction."""
self.conn.commit()
- def exec(self, code: str, inputs: tuple[Any, ...] = tuple()) -> Cursor:
- """Add commands to SQL transaction."""
- return self.conn.execute(code, inputs)
-
- def exec_on_vals(self, code: str, inputs: tuple[Any, ...]) -> Cursor:
- """Wrapper around .exec appending adequate " (?, …)" to code."""
- q_marks_from_values = '(' + ','.join(['?'] * len(inputs)) + ')'
- return self.exec(f'{code} {q_marks_from_values}', inputs)
+ def exec(self,
+ code: str,
+ inputs: tuple[Any, ...] = tuple(),
+ build_q_marks: bool = True
+ ) -> Cursor:
+ """Wrapper around sqlite3.Connection.execute, building '?' if inputs"""
+ if len(inputs) > 0:
+ if build_q_marks:
+ q_marks = ('?' if len(inputs) == 1
+ else '(' + ','.join(['?'] * len(inputs)) + ')')
+ return self.conn.execute(f'{code} {q_marks}', inputs)
+ return self.conn.execute(code, inputs)
+ return self.conn.execute(code)
def close(self) -> None:
"""Close DB connection."""
self.delete_where(table_name, key, target)
for row in rows:
values = tuple(row[:key_index] + [target] + row[key_index:])
- self.exec_on_vals(f'INSERT INTO {table_name} VALUES', values)
+ self.exec(f'INSERT INTO {table_name} VALUES', values)
def row_where(self, table_name: str, key: str,
target: int | str) -> list[Row]:
"""Return list of Rows at table where key == target."""
- return list(self.exec(f'SELECT * FROM {table_name} WHERE {key} = ?',
+ return list(self.exec(f'SELECT * FROM {table_name} WHERE {key} =',
(target,)))
# def column_where_pattern(self,
"""Return column of table where key == target."""
return [row[0] for row in
self.exec(f'SELECT {column} FROM {table_name} '
- f'WHERE {key} = ?', (target,))]
+ f'WHERE {key} =', (target,))]
def column_all(self, table_name: str, column: str) -> list[Any]:
"""Return complete column of table."""
def delete_where(self, table_name: str, key: str,
target: int | str) -> None:
"""Delete from table where key == target."""
- self.exec(f'DELETE FROM {table_name} WHERE {key} = ?', (target,))
+ self.exec(f'DELETE FROM {table_name} WHERE {key} =', (target,))
BaseModelId = TypeVar('BaseModelId', int, str)
items = []
sql = f'SELECT id FROM {cls.table_name} '
sql += f'WHERE {date_col} >= ? AND {date_col} <= ?'
- for row in db_conn.exec(sql, (start_date, end_date)):
+ for row in db_conn.exec(sql, (start_date, end_date),
+ build_q_marks=False):
items += [cls.by_id(db_conn, row[0])]
return items, start_date, end_date
values = tuple([self.id_] + [getattr(self, key)
for key in self.to_save_simples])
table_name = self.table_name
- cursor = db_conn.exec_on_vals(f'REPLACE INTO {table_name} VALUES',
- values)
+ cursor = db_conn.exec(f'REPLACE INTO {table_name} VALUES', values)
if not isinstance(self.id_, str):
self.id_ = cursor.lastrowid # type: ignore[assignment]
self.cache()