Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

抽象化对SQL语句的操作 #26

Closed
LeoQuote opened this issue Dec 29, 2018 · 2 comments
Closed

抽象化对SQL语句的操作 #26

LeoQuote opened this issue Dec 29, 2018 · 2 comments
Assignees

Comments

@LeoQuote
Copy link
Collaborator

目前涉及到SQL语句的操作, 都是函数, 可以把这些操作抽象成一个个的 Engine

相当于 java 里的 Interface , 然后再继承这个Interface , 实现一个 MysqlEngine , 这样方便其它数据库比如 PG , oracle等的接入

class EngineBase:
    def __init__(self, instance=None, workflow=None):
        self.instance = instance
        self.workflow = workflow
    @property
    def Connection(self):
        """返回一个conn实例"""
        pass
    def get_all_databases(self):
        """获取数据库列表, 返回一个list"""
        pass
    def get_all_tables(self, db_name):
        """获取table 列表, 返回一个list"""
        pass
    def get_all_columns_by_tb(self, db_name, table_name):
        """获取所有字段, 返回一个列表"""
        pass
    def query_check(self, db_name, sqlcontent):
        """查询语句的检查"""
        pass
    def query(self, db_name, sqlcontent):
        """实际查询"""
        pass
    def execute_check(self, db_name, sqlcontent):
        """执行语句的检查"""
        pass
    def execute(self, db_name, sqlcontent):
        """执行语句"""
        pass

统一了interface之后, 只要继承interface , 统一返回对象, 实现出来的对象就一定能适配到系统中.

比如有 查询方法叫 Query , 那么代码里只要 get_engine , 然后 SomeEngine.query ,就一定能得到预期的结果.

适配数据库时, 也就不需要更改主逻辑部分的代码. 更棒的是, 如果你不用这个引擎, 就不会import到那些库, 那么在你不用pg的时候, 不装pg相关的依赖也是可以的. 测试功能会更加简单, 尤其在一些依赖很难装的windows 机器上.

@hhyo hhyo assigned hhyo and LeoQuote and unassigned hhyo Dec 29, 2018
@hhyo
Copy link
Owner

hhyo commented Dec 29, 2018

帮忙改造一下,顺便让也学习一下~

@LeoQuote
Copy link
Collaborator Author

LeoQuote commented Jan 2, 2019

审核结果的定义

class EngineResult:
    def __init__(self, inception_result=[], **kwargs):
        if inception_result:
            column_list = ['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence',
                   'backup_dbname', 'execute_time', 'sqlsha1']
            self.id = inception_result[0]
            self.stage = inception_result[1]
            self.errlevel = inception_result[2]
            self.stagestatus = inception_result[3]
            self.errormessage = inception_result[4]
            self.sql = inception_result[5]
            self.affected_rows = inception_result[6]
            self.sequence = inception_result[7]
            self.backup_dbname = inception_result[8]
            self.execute_time = inception_result[9]
            self.sqlsha1 = inception_result[10]
        self.id = kwargs['id']
        self.stage = kwargs['stage']
        self.errlevel = kwargs['errlevel']
        self.stagestatus = kwargs['stagestatus']
        self.errormessage = kwargs['errormessage']
        self.sql = kwargs['sql']
        self.affected_rows = kwargs['affected_rows']
        self.sequence = kwargs['sequence']
        self.backup_dbname = kwargs['backup_dbname']
        self.execute_time = kwargs['execute_time']
        self.sqlsha1 = kwargs['sqlsha1']

审核结果集的定义

class ResultSet:
    def __init__(self, query='', resultset = [], status=None,**kwargs):
        self.query = query
        self.resultset = []
        self.status = status
    def __repr__(self):
        for i in self.resultset:
            yield i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants