__import__('pkg_resources').declare_namespace(__name__)

import logging
logger = logging.getLogger("upstudy")
from upstudy import settings
from sqlalchemy import create_engine
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.exc import ProgrammingError, IntegrityError

class SQLBackend(object):

    def __init__(self, config):
        self.engine = None
        self._connection = None
        self._session = None
        self.config = config

        if not hasattr(SQLBackend, "_instance"):
            SQLBackend._instance = self

    def connect(self, user=None, password=None, host=None, database=None, type=None, driver=None):
        if self.engine:
            self._session = None
            self._connection.close()
            self._connection = None
            self.engine.dispose()
            self.engine = None

        user = user or self.config.get("user")
        password = password or self.config.get("user")
        database = database or self.config.get("database")
        host = host or self.config.get("host")
        type = type or self.config.get("type")
        driver = driver or self.config.get("driver")
        pool_size = self.config.get("pool_size", 5)
        params = {
                "user": user,
                "password": password,
                "database": database,
                "host": host,
                "type": type,
                "driver": driver,
        }

        connect_url = ""
        if user:
            if not password:
                connect_url = "{type}+{driver}://{user}@{host}/{database}"
            else:
                connect_url = "{type}+{driver}://{user}:{password}@{host}/{database}"
        else:
            connect_url = "{type}+{driver}://{host}/{database}"
        self.engine = create_engine(connect_url.format(**params), encoding='utf8', pool_size=pool_size)

        if self.config.get("use_threadlocal", False):
            self.engine.pool._use_threadlocal = True

        self._connection = self.engine.connect()
        self._connection.execute("COMMIT")
        Session = sessionmaker(bind=self.engine)
        self._session = Session()

    def __initialize__(self):
        logger.info("creating database");
        if self.config["type"] == "mysql":
            self.connect()
            self._connection.execute("CREATE DATABASE IF NOT EXISTS up_research CHARACTER SET utf8;")
        elif self.config["type"] == "postgresql":
            self.connect(database="postgres")
            try:
                self._connection.execute("CREATE DATABASE up_research;")
            except ProgrammingError, e:
                logging.warning(e)
            self.connect()

    def __drop__(self):
        logger.info("dropping database");
        database = self.config.get("database")
        if self.config["type"] == "postgresql":
            database = "postgres"
        self.connect(database=database)
        self._connection.execute("DROP DATABASE up_research;")

    def __create_tables__(self):
        from upstudy.data.models import create_tables
        create_tables(self.engine)

    def __load_categories__(self):
        logger.info("loading initial data");
        from upstudy.data.models import create_categories
        try:
            create_categories(self._session)
        except IntegrityError, e:
            logger.warning(e)

    @property
    def session(self):
        return self._session

    @classmethod
    def instance(cls):
        if hasattr(SQLBackend, "_instance"):
            return SQLBackend._instance
        else:
            return SQLBackend(settings.database)

@@ -2169,32 +2169,99 @@
 ialize__(self):%0A
+        database_name = self.config.get(%22database%22, %22up_research%22)%0A
         logger.i
@@ -2274,33 +2274,59 @@
 reating database
-%22
+ %7B0%7D%22.format(database_name)
 );%0A        if se
@@ -2450,27 +2450,19 @@
  EXISTS 
-up_research
+%7B0%7D
  CHARACT
@@ -2474,16 +2474,38 @@
 T utf8;%22
+.format(database_name)
 )%0A      
@@ -2669,29 +2669,43 @@
 ATABASE 
-up_research;%22
+%7B0%7D;%22.format(database_name)
 )%0A      
@@ -2817,32 +2817,99 @@
 __drop__(self):%0A
+        database_name = self.config.get(%22database%22, %22up_research%22)%0A
         logger.i
@@ -2930,17 +2930,43 @@
 database
-%22
+ %7B0%7D%22.format(database_name)
 );%0A     
@@ -3181,21 +3181,35 @@
 ASE 
-up_research;%22
+%7B0%7D;%22.format(database_name)
 )%0A%0A 

make sql database backend read from config on init