<commit_before>import psycopg2, psycopg2.extras, psycopg2.extensions
import re

from . import dtuple

# Receive strings from the database in unicode
# http://initd.org/psycopg/docs/usage.html#unicode-handling
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)

class ExpressionValue(str):
    pass

class ExpressionValueAdapter(object):
    def __init__(self, value):
        self.value = value
    
    def getquoted(self):
        return str(self.value)

psycopg2.extensions.register_adapter(ExpressionValue, ExpressionValueAdapter)

# Quoting of table/schema names in psycopg2:
# http://osdir.com/ml/python.db.psycopg.devel/2004-10/msg00012.html
class SchemaName(str):
    pass

class SchemaNameAdapter(object):
    def __init__(self, value):
        self.value = value
    
    def getquoted(self):
        return '"' + self.value.replace('"', '""') + '"'

psycopg2.extensions.register_adapter(SchemaName, SchemaNameAdapter)

# Use this for obtaining original psycopg2 tuple/list escaping behavior
class SqlArray(list):
    pass

#psycopg2.extensions.register_adapter(list, psycopg2.extensions.SQL_IN)
psycopg2.extensions.register_adapter(SqlArray, psycopg2._psycopg.List)

WHITESPACE_REGEXP = re.compile(r'^\s+', re.M)

class DatabaseError(StandardError):
    pass

class NotFoundError(DatabaseError):
    pass

class TransactionStateError(DatabaseError):
    pass

class MissingCursorDescriptionError(DatabaseError):
    pass

def _lists_to_tuples(arg):
    if isinstance(arg, list):
        arg = tuple(arg)
    return arg

class CursorWrapper(object):
    def __init__(self, cursor, conn,
        debug_queries=False, debug_transactions=False,
    ):
        self.cursor = cursor
        self.conn = conn
        self._debug_queries = debug_queries
        self._debug_transactions = debug_transactions
    
    def execute(self, sql, *args):
        return self.execute2(sql, args)
    
    def execute2(self, sql, args):
        sql = sql.replace('?', '%s')
        
        convert_lists = False
        
        if args is None:
            args = ()
        elif isinstance(args, dict):
            # keep as a dict
            # convert lists to tuples
            for key in args:
                value = args[key]
                if isinstance(value, list):
                    args[key] = tuple(value)
        elif isinstance(args, basestring) or getattr(args, '__len__', None) is None:
            args = (args,)
            convert_lists = True
        else:
            # a tuple or a list
            convert_lists = True
        
        if convert_lists:
            args = map(_lists_to_tuples, args)
        
        try:
            if self._debug_queries:
                debug_sql = self.cursor.mogrify(sql, args)
                #debug_sql = sql.strip()
                #if args:
                    #debug_sql += ', ' + repr(args)
                debug_sql = WHITESPACE_REGEXP.sub('     ', debug_sql.strip())
                print 'SQL:', debug_sql
            return self.cursor.execute(sql, args)
        except psycopg2.OperationalError, e:
            if str(e).startswith('server closed the connection unexpectedly'):
                if self.conn._transaction_depth == 0:
                    self.conn.reconnect()
                    self.cursor = self.conn.cursor()
                    self.cursor.execute(sql, args)
            else:
                raise
        
        #if self.conn._transaction_depth_request:
            #self.conn._transaction_depth += 1
            #self.conn._transaction_depth_request -= 1
    
    def execute_many(self, sql_commands):
        for sql in sql_commands.split(';'):
            sql = sql.strip()
            if sql:
                self.execute2(sql, ())
    <commit_msg>Add missing else path with a raise<commit_after>import psycopg2, psycopg2.extras, psycopg2.extensions
import re

from . import dtuple

# Receive strings from the database in unicode
# http://initd.org/psycopg/docs/usage.html#unicode-handling
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)

class ExpressionValue(str):
    pass

class ExpressionValueAdapter(object):
    def __init__(self, value):
        self.value = value
    
    def getquoted(self):
        return str(self.value)

psycopg2.extensions.register_adapter(ExpressionValue, ExpressionValueAdapter)

# Quoting of table/schema names in psycopg2:
# http://osdir.com/ml/python.db.psycopg.devel/2004-10/msg00012.html
class SchemaName(str):
    pass

class SchemaNameAdapter(object):
    def __init__(self, value):
        self.value = value
    
    def getquoted(self):
        return '"' + self.value.replace('"', '""') + '"'

psycopg2.extensions.register_adapter(SchemaName, SchemaNameAdapter)

# Use this for obtaining original psycopg2 tuple/list escaping behavior
class SqlArray(list):
    pass

#psycopg2.extensions.register_adapter(list, psycopg2.extensions.SQL_IN)
psycopg2.extensions.register_adapter(SqlArray, psycopg2._psycopg.List)

WHITESPACE_REGEXP = re.compile(r'^\s+', re.M)

class DatabaseError(StandardError):
    pass

class NotFoundError(DatabaseError):
    pass

class TransactionStateError(DatabaseError):
    pass

class MissingCursorDescriptionError(DatabaseError):
    pass

def _lists_to_tuples(arg):
    if isinstance(arg, list):
        arg = tuple(arg)
    return arg

class CursorWrapper(object):
    def __init__(self, cursor, conn,
        debug_queries=False, debug_transactions=False,
    ):
        self.cursor = cursor
        self.conn = conn
        self._debug_queries = debug_queries
        self._debug_transactions = debug_transactions
    
    def execute(self, sql, *args):
        return self.execute2(sql, args)
    
    def execute2(self, sql, args):
        sql = sql.replace('?', '%s')
        
        convert_lists = False
        
        if args is None:
            args = ()
        elif isinstance(args, dict):
            # keep as a dict
            # convert lists to tuples
            for key in args:
                value = args[key]
                if isinstance(value, list):
                    args[key] = tuple(value)
        elif isinstance(args, basestring) or getattr(args, '__len__', None) is None:
            args = (args,)
            convert_lists = True
        else:
            # a tuple or a list
            convert_lists = True
        
        if convert_lists:
            args = map(_lists_to_tuples, args)
        
        try:
            if self._debug_queries:
                debug_sql = self.cursor.mogrify(sql, args)
                #debug_sql = sql.strip()
                #if args:
                    #debug_sql += ', ' + repr(args)
                debug_sql = WHITESPACE_REGEXP.sub('     ', debug_sql.strip())
                print 'SQL:', debug_sql
            return self.cursor.execute(sql, args)
        except psycopg2.OperationalError, e:
            if str(e).startswith('server closed the connection unexpectedly'):
                if self.conn._transaction_depth == 0:
                    self.conn.reconnect()
                    self.cursor = self.conn.cursor()
                    self.cursor.execute(sql, args)
                else:
                    raise
            else:
                raise
        
        #if self.conn._transaction_depth_request:
            #self.conn._transaction_depth += 1
            #self.conn._transaction_depth_request -= 1
    
    def execute_many(self, sql_commands):
        for sql in sql_commands.split(';'):
            sql = sql.strip()
            if sql:
                self.execute2(sql, ())
    
{'fraction_non_alphanumeric': 0.09613145296933627, 'fraction_numerical': 0.008539267693103895, 'mean_word_length': 2.206138531729573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '711889', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2231, 'n_words': 490}