示例#1
0
 def exec_sql_script(self, sql_script, ignore_duplicates=None):
     with closing(self.cursor()) as cursor:
         statement = ''
         for line in open(sql_script):
             stripped = line.strip()
             if stripped == '' or stripped[:2] == '--':  # ignore sql comment lines
                 continue
             statement += line
             if stripped[
                     -1] == ';':  # keep appending lines that don't end with ';'
                 logger.debug('SQL Script: {}'.format(sql_script))
                 logger.debug('SQL Statement: {}'.format(statement))
                 try:
                     cursor.execute(statement)
                 except MySQLdb.OperationalError as e:
                     # 1050 - Table X already exists
                     # 1060 - Duplicate column name X
                     # 1061 - Duplicate key name X
                     if ignore_duplicates:
                         if e.args[0] not in (1050, 1060, 1061):
                             raise
                 statement = ''
         if statement.strip() != '':
             logger.debug('SQL Script: {}'.format(sql_script))
             logger.debug('SQL Statement: {}'.format(statement))
             cursor.execute(statement)
         self.commit()
示例#2
0
 def exec_sql_script(self, sql_script, ignore_duplicates=None):
     with closing(self.cursor()) as cursor:
         statement = ''
         for line in open(sql_script):
             stripped = line.strip()
             if stripped == '' or stripped[:2] == '--':  # ignore sql comment lines
                 continue
             statement += line
             if stripped[-1] == ';':  # keep appending lines that don't end with ';'
                 logger.debug('SQL Script: {}'.format(sql_script))
                 logger.debug('SQL Statement: {}'.format(statement))
                 try:
                     cursor.execute(statement)
                 except MySQLdb.OperationalError as e:
                     # 1050 - Table X already exists
                     # 1060 - Duplicate column name X
                     # 1061 - Duplicate key name X
                     if ignore_duplicates:
                         if e.args[0] not in (1050, 1060, 1061):
                             raise
                 statement = ''
         if statement.strip() != '':
             logger.debug('SQL Script: {}'.format(sql_script))
             logger.debug('SQL Statement: {}'.format(statement))
             cursor.execute(statement)
         self.commit()
示例#3
0
    def query(self, query, *args, **kwargs):
        log_text = 'SQL Query: {}'.format(query)
        if 'args' in kwargs and len(kwargs['args']) > 0:
            if "\n" in query:
                log_text += "\n"
            log_text += ' (with args: {})'.format(kwargs['args'])
        logger.debug(log_text)

        def do_exec_query(cursor):
            time_started = time.time()

            returned = cursor.execute(query, *args, **kwargs)
            affected = cursor.rowcount
            rows = cursor.fetchall()

            # log SQL - sampled at 1%
            if random.random() < 0.01:
                extra = {}
                try:
                    extra.update(self.connection_info.__dict__)
                except Exception:
                    pass
                extra.pop('password', None)
                extra['num_rows'] = affected
                extra['elapsed'] = time.time() - time_started

                try:
                    extra['script'] = sys.modules['__main__'].__file__
                except Exception:
                    extra['script'] = 'interactive?'

                self.logger.info('SQL {}'.format(query), extra=extra)

            return QueryResult(query, args, kwargs, affected,
                               cursor.description, rows)

        if 'cursor' in kwargs:
            return do_exec_query(kwargs.pop('cursor'))
        else:
            with closing(self.cursor()) as cursor:
                return do_exec_query(cursor)
示例#4
0
    def query(self, query, *args, **kwargs):
        log_text = 'SQL Query: {}'.format(query)
        if 'args' in kwargs and len(kwargs['args']) > 0:
            if "\n" in query:
                log_text += "\n"
            log_text += ' (with args: {})'.format(kwargs['args'])
        logger.debug(log_text)
        def do_exec_query(cursor):
            time_started = time.time()

            returned = cursor.execute(query, *args, **kwargs)
            affected = cursor.rowcount
            rows = cursor.fetchall()

            # log SQL - sampled at 1%
            if random.random() < 0.01:
                extra = {}
                try:
                    extra.update(self.connection_info.__dict__)
                except Exception:
                    pass
                extra.pop('password', None)
                extra['num_rows'] = affected
                extra['elapsed'] = time.time() - time_started

                try:
                    extra['script'] = sys.modules['__main__'].__file__
                except Exception:
                    extra['script'] = 'interactive?'

                self.logger.info('SQL {}'.format(query), extra=extra)

            return QueryResult(query, args, kwargs, affected, cursor.description, rows)

        if 'cursor' in kwargs:
            return do_exec_query(kwargs.pop('cursor'))
        else:
            with closing(self.cursor()) as cursor:
                return do_exec_query(cursor)