# Copyright (c) 2015, Xhive Global Ltd. and Contributors
# License: MIT. See LICENSE

# Database Module
# --------------------

from xhiveframework.database.database import savepoint


def setup_database(force, source_sql=None, verbose=None, no_mariadb_socket=False):
	import xhiveframework

	if xhiveframework.conf.db_type == "postgres":
		import xhiveframework.database.postgres.setup_db

		return xhiveframework.database.postgres.setup_db.setup_database(force, source_sql, verbose)
	else:
		import xhiveframework.database.mariadb.setup_db

		return xhiveframework.database.mariadb.setup_db.setup_database(
			force, source_sql, verbose, no_mariadb_socket=no_mariadb_socket
		)


def drop_user_and_database(db_name, root_login=None, root_password=None):
	import xhiveframework

	if xhiveframework.conf.db_type == "postgres":
		import xhiveframework.database.postgres.setup_db

		return xhiveframework.database.postgres.setup_db.drop_user_and_database(
			db_name, root_login, root_password
		)
	else:
		import xhiveframework.database.mariadb.setup_db

		return xhiveframework.database.mariadb.setup_db.drop_user_and_database(
			db_name, root_login, root_password
		)


def get_db(host=None, user=None, password=None, port=None):
	import xhiveframework

	if xhiveframework.conf.db_type == "postgres":
		import xhiveframework.database.postgres.database

		return xhiveframework.database.postgres.database.PostgresDatabase(host, user, password, port=port)
	else:
		import xhiveframework.database.mariadb.database

		return xhiveframework.database.mariadb.database.MariaDBDatabase(host, user, password, port=port)


def setup_help_database(help_db_name):
	import xhiveframework

	if xhiveframework.conf.db_type == "postgres":
		import xhiveframework.database.postgres.setup_db

		return xhiveframework.database.postgres.setup_db.setup_help_database(help_db_name)
	else:
		import xhiveframework.database.mariadb.setup_db

		return xhiveframework.database.mariadb.setup_db.setup_help_database(help_db_name)
