# -*- coding: utf-8 -*-
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from config import settings

if sqlalchemy.__version__ >= '1.4':
    mysql_engine_url = sqlalchemy.engine.URL.create(**settings.mysql_dwd_config)
    mysql_engine_url = mysql_engine_url.update_query_dict({'charset': 'utf8mb4'})
else:
    mysql_engine_url = '{drivername}://{username}:{password}@{host}:{port}/{database}?charset=utf8mb4'.format(**settings.mysql_dwd_config)

engine = create_engine(mysql_engine_url, echo=False, pool_size=100, pool_recycle=3600, pool_pre_ping=True)
SessionLocal = sessionmaker(bind=engine)
Base = declarative_base()

# Dependency
def get_db():
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()

def get_db_local():
    return SessionLocal()

#from database import engine
#from models.oneshare_base import Base
#from models.knowledge_base import Base
#
# #使用Base的metadata和engine来创建所有表
#Base.metadata.create_all(bind=engine)
