Last active
May 1, 2017 18:11
-
-
Save LifeMoroz/d7788851a5ffd8a5ad0299b5e6b1cc6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Database1: | |
def get_connection(self, host, port, db, user='', password=''): | |
pass | |
def execute(self, conn, sql, params, unescaped=None): | |
pass | |
class MysqlDatabase(Database1): | |
def get_connection(self, host, port='', db='', user='', password=''): | |
return MySQLdb.connect(password=password, host=host, port=port, db=db, user=user) | |
def execute(self, conn, sql, params, unescaped=None): | |
return conn.execute(sql, params) | |
class SqlLite3(Database1): | |
def get_connection(self, host, port='', db='', user='', password=''): | |
return sqlite3.connect(host) | |
def execute(self, conn, sql, params, unescaped=None): | |
sql = sql.format(unescaped) if unescaped else sql | |
try: | |
if params: | |
return conn.cursor().execute(sql, params) | |
else: | |
return conn.cursor().execute(sql) | |
finally: | |
conn.commit() | |
class DatabaseFactory: | |
def get_db(self) -> Database1: | |
pass | |
class MysqlFactory(DatabaseFactory): | |
def get_db(self) -> Database1: | |
return MysqlDatabase | |
class SqlLiteFactory(DatabaseFactory): | |
def get_db(self) -> Database1: | |
return SqlLite3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment