1
|
import sqlite3
|
2
|
from sqlite3 import Connection
|
3
|
|
4
|
from injector import Module, provider, singleton
|
5
|
|
6
|
from src.config.configuration import Configuration
|
7
|
from src.db.init_queries import DEFAULT_VALUES_SQL
|
8
|
from src.db.setup_database import SCHEMA_SQL
|
9
|
|
10
|
|
11
|
class ConnectionProvider(Module):
|
12
|
|
13
|
@singleton
|
14
|
@provider
|
15
|
def connect(self, configuration: Configuration) -> Connection:
|
16
|
co = sqlite3.connect(database=configuration.connection_string, check_same_thread=False)
|
17
|
cu = co.cursor()
|
18
|
cu.executescript(SCHEMA_SQL) # TODO change setup_database not to drop tables if they exist
|
19
|
cu.executescript(DEFAULT_VALUES_SQL)
|
20
|
return co
|