当前位置: 首页>>代码示例>>Python>>正文


Python DDL.execute_at方法代码示例

本文整理汇总了Python中sqlalchemy.DDL.execute_at方法的典型用法代码示例。如果您正苦于以下问题:Python DDL.execute_at方法的具体用法?Python DDL.execute_at怎么用?Python DDL.execute_at使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sqlalchemy.DDL的用法示例。


在下文中一共展示了DDL.execute_at方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: after_table

# 需要导入模块: from sqlalchemy import DDL [as 别名]
# 或者: from sqlalchemy.DDL import execute_at [as 别名]
 def after_table(self):
     statement = self.statement
     if hasattr(statement, '__call__'):
         statement = statement()
     if not isinstance(statement, list):
         statement = [statement]
     for s in statement:
         ddl = DDL(s, self.on, self.context)
         ddl.execute_at(self.when, self.entity.table)
开发者ID:gjhiggins,项目名称:elixir,代码行数:11,代码来源:perform_ddl.py

示例2: create_triggers

# 需要导入模块: from sqlalchemy import DDL [as 别名]
# 或者: from sqlalchemy.DDL import execute_at [as 别名]
def create_triggers(db, tables):


    db = create_engine(engine_name)
    db.echo = True
    db.connect()
    metadata = MetaData(db)


    insp = reflection.Inspector.from_engine(db)

    tables = []
    for table_name in insp.get_table_names():
        if not table_name.endswith('_aud'):
            table = Table(table_name, metadata, autoload=True, autoload_with=db)
            tables.append(table)
            #print("TABLE: %s"%table)
            #print table.__repr__
        else:
            table = Table(table_name, metadata, autoload=True, autoload_with=db)
            table.drop(db)
            metadata.remove(table)        


    drop_trigger_text = """DROP TRIGGER IF EXISTS %(trigger_name)s;"""
    for table in tables:
        pk_cols = [c.name for c in table.primary_key]
        for pk_col in pk_cols:
            try:
                db.execute(drop_trigger_text % {
                    'trigger_name' : table.name + "_ins_trig",
                })
            except:
                pass

        for pk_col in pk_cols:
            try:
                db.execute(drop_trigger_text % {
                    'trigger_name' : table.name + "_upd_trig",
                })
            except:
                pass
    #metadata.create_all()

    trigger_text = """
                    CREATE TRIGGER
                        %(trigger_name)s
                    AFTER %(action)s ON
                        %(table_name)s
                    FOR EACH ROW
                        BEGIN
                            INSERT INTO %(table_name)s_aud
                            SELECT
                                d.*,
                                '%(action)s',
                                NULL,
                                date('now')
                            FROM
                                %(table_name)s
                                AS d
                            WHERE
                                %(pkd)s;
                        END
                        """
    
    for table in tables:


        pk_cols = [c.name for c in table.primary_key]
        pkd = []
        
        for pk_col in pk_cols:
            pkd.append("d.%s = NEW.%s"%(pk_col, pk_col))

        text_dict = {
            'action'       : 'INSERT',
            'trigger_name' : table.name + "_ins_trig",
            'table_name'   : table.name,
            'pkd'           : ' and '.join(pkd),
        }

        logging.info(trigger_text % text_dict)
        trig_ddl = DDL(trigger_text % text_dict)
        trig_ddl.execute_at('after-create', table.metadata)  

        text_dict['action'] = 'UPDATE'
        text_dict['trigger_name'] = table.name + "_upd_trig"
        trig_ddl = DDL(trigger_text % text_dict)
        trig_ddl.execute_at('after-create', table.metadata)  

    metadata.create_all()
开发者ID:UMWRG,项目名称:HydraPlatform,代码行数:93,代码来源:audit.py


注:本文中的sqlalchemy.DDL.execute_at方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。