Files
appointment_tool/db/local_db_manager.py
T
2022-06-02 17:59:12 +02:00

45 lines
1.8 KiB
Python

import sqlalchemy
from sqlalchemy import MetaData, Column, String, Integer, DateTime, Table
from sqlalchemy.orm import Session
from sqlalchemy_utils import database_exists, create_database
from pojo.captcha_error_contact_pojo import ContactInErrorPojo
class LocalDbManager:
def __init__(self, path: str):
self.session = Session(self.init_db(path))
def init_db(self, path: str):
uri_for_db = "sqlite:///{}/{}.db".format(path, "appointment")
print(uri_for_db)
# 2.-Turn on database engine
db_engine = sqlalchemy.create_engine(uri_for_db) # ensure this is the correct path for the sqlite file.
if not database_exists(uri_for_db):
create_database(uri_for_db)
connextion = db_engine.connect()
if not db_engine.dialect.has_table(connextion,
ContactInErrorPojo.__tablename__): # If table don't exist, Create.
metadata = MetaData(db_engine)
# Create a table with the appropriate Columns
Table(ContactInErrorPojo.__tablename__, metadata,
Column('mail', String, primary_key=True, nullable=False),
Column('phone', String),
Column('passport', String),
Column('last_name', String),
Column('first_name', String),
Column('ccid', String),
Column('position', Integer),
Column('error_type', Integer),
Column('update_at', DateTime),
Column('create_at', DateTime))
# Implement the creation
metadata.create_all()
return db_engine
def insert_or_update(self, instance: ContactInErrorPojo):
self.session.merge(instance)
self.session.commit()