# Path: beeswarm/shared/helpers.py
# def database_exists(url):
#     """Check if a database exists.
# 
#     :param url: A SQLAlchemy engine URL.
# 
#     Performs backend-specific testing to quickly determine if a database
#     exists on the server. ::
# 
#         database_exists('postgres://postgres@localhost/name')  #=> False
#         create_database('postgres://postgres@localhost/name')
#         database_exists('postgres://postgres@localhost/name')  #=> True
# 
#     Supports checking against a constructed URL as well. ::
# 
#         engine = create_engine('postgres://postgres@localhost/name')
#         database_exists(engine.url)  #=> False
#         create_database(engine.url)
#         database_exists(engine.url)  #=> True
# 
#     """
# 
#     url = copy(make_url(url))
#     database = url.database
#     if url.drivername.startswith('postgresql'):
#         url.database = 'template1'
#     else:
#         url.database = None
# 
#     engine = sa.create_engine(url)
# 
#     if engine.dialect.name == 'postgresql':
#         text = "SELECT 1 FROM pg_database WHERE datname='%s'" % database
#         return bool(engine.execute(text).scalar())
# 
#     elif engine.dialect.name == 'mysql':
#         text = ("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA "
#                 "WHERE SCHEMA_NAME = '%s'" % database)
#         return bool(engine.execute(text).scalar())
# 
#     elif engine.dialect.name == 'sqlite':
#         return database == ':memory:' or os.path.exists(database)
# 
#     else:
#         text = 'SELECT 1'
#         try:
#             url.database = database
#             engine = sa.create_engine(url)
#             engine.execute(text)
#             return True
# 
#         except (ProgrammingError, OperationalError):
#             return False


import os
import json
import logging
import sys
import beeswarm.server.db
import entities
from beeswarm.shared.helpers import database_exists
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from entities import Classification
from entities import BaitUser
# Copyright (C) 2013 Johnny Vestergaard <jkv@unixcluster.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.




# these will go when database actor is finished
DB_Session = None
engine = None

logger = logging.getLogger(__name__)



# Path: beeswarm/shared/helpers.py
# def database_exists(url):
#     """Check if a database exists.
# 
#     :param url: A SQLAlchemy engine URL.
# 
#     Performs backend-specific testing to quickly determine if a database
#     exists on the server. ::
# 
#         database_exists('postgres://postgres@localhost/name')  #=> False
#         create_database('postgres://postgres@localhost/name')
#         database_exists('postgres://postgres@localhost/name')  #=> True
# 
#     Supports checking against a constructed URL as well. ::
# 
#         engine = create_engine('postgres://postgres@localhost/name')
#         database_exists(engine.url)  #=> False
#         create_database(engine.url)
#         database_exists(engine.url)  #=> True
# 
#     """
# 
#     url = copy(make_url(url))
#     database = url.database
#     if url.drivername.startswith('postgresql'):
#         url.database = 'template1'
#     else:
#         url.database = None
# 
#     engine = sa.create_engine(url)
# 
#     if engine.dialect.name == 'postgresql':
#         text = "SELECT 1 FROM pg_database WHERE datname='%s'" % database
#         return bool(engine.execute(text).scalar())
# 
#     elif engine.dialect.name == 'mysql':
#         text = ("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA "
#                 "WHERE SCHEMA_NAME = '%s'" % database)
#         return bool(engine.execute(text).scalar())
# 
#     elif engine.dialect.name == 'sqlite':
#         return database == ':memory:' or os.path.exists(database)
# 
#     else:
#         text = 'SELECT 1'
#         try:
#             url.database = database
#             engine = sa.create_engine(url)
#             engine.execute(text)
#             return True
# 
#         except (ProgrammingError, OperationalError):
#             return False


# Path: beeswarm/server/db/database_setup.py
import os
import json
import logging
import sys
import beeswarm.server.db
import entities
from beeswarm.shared.helpers import database_exists
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from entities import Classification
from entities import BaitUser

# Copyright (C) 2013 Johnny Vestergaard <jkv@unixcluster.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.




# these will go when database actor is finished
DB_Session = None
engine = None

logger = logging.getLogger(__name__)



def setup_db(connection_string):