import pymysql
from dbutils.pooled_db import PooledDB

from flyers.database import DatabaseConf
from flyers.logs import logger

# The max number of connections
MAX_CONNECTIONS = 10


def new_connection_pool(conf: DatabaseConf) -> PooledDB:
    """
    Create a new connection pool
    :param conf:
    :return:
    """
    logger.info('create new database connection pool, conf: {}'.format(conf))
    return PooledDB(
        pymysql,
        MAX_CONNECTIONS,
        host=conf.host,
        user=conf.user,
        port=conf.host,
        passwd=conf.password,
        db=conf.database,
        use_unicode=True)


def run_with(pool: PooledDB, consumer_func):
    conn, cursor = get_conn(pool)
    consumer_func(conn, cursor)
    close_conn(conn, cursor)


def get_conn(pool: PooledDB):
    conn = pool.connection()
    return conn, conn.cursor()


def close_conn(conn, cursor):
    conn.close()
    cursor.close()
