<commit_before>from asyncio import coroutine
from functools import wraps
from enum import Enum

from aiopg import create_pool, Pool, Cursor
import psycopg2

_CursorType = Enum('CursorType', 'PLAIN, DICT, NAMEDTUPLE')


class PostgresStore:
    _pool = None
    _connection_params = {}
    _insert_string = "insert into {} ({}) values ({});"
    _update_string = "update {} set ({}) = ({}) where {} = %s;"
    _select_all_string_with_condition = "select * from {} where ({}) limit {} offset %s order by {}"
    _select_all_string = "select * from {} limit {} offset {} order by {}"
    _select_selective_column = "select {} from {} limit {} offset {} order by {}"
    _select_selective_column_with_condition = "select {} from {} where ({}) limit {} offset {} order by {}"
    _delete_query = "delete from {} where ({})"

    @classmethod
    def connect(cls, database:str, user:str, password:str, host:str, port:int):
        """
        Sets connection parameters
        """
        cls._connection_params['database'] = database
        cls._connection_params['user'] = user
        cls._connection_params['password'] = password
        cls._connection_params['host'] = host
        cls._connection_params['port'] = port

    @classmethod
    def use_pool(cls, pool:Pool):
        """
        Sets an existing connection pool instead of using connect() to make one
        """
        cls._pool = pool

    @classmethod
    @coroutine
    def get_pool(cls) -> Pool:
        """
        Yields:
            existing db connection pool
        """
        if len(cls._connection_params) < 5:
            raise ConnectionError('Please call SQLStore.connect before calling this method')
        if not cls._pool:
            cls._pool = yield from create_pool(**cls._connection_params)
        return cls._pool

    @classmethod
    @coroutine
    def get_cursor(cls, cursor_type=_CursorType.PLAIN) -> Cursor:
        """
        Yields:
            new client-side cursor from existing db connection pool
        """
        pool = yield from cls.get_pool()
        if cursor_type == _CursorType.PLAIN:
            c = yield from pool.cursor()
            return c
        if cursor_type == _CursorType.NAMEDTUPLE:
            return (yield from pool.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor))
        if cursor_type == _CursorType.DICT:
            return (yield from pool.cursor(cursor_factory=psycopg2.extras.DictCursor))

    @classmethod
    def make_insert_query(cls, table:str, values:dict):
        """
        Creates an insert statement with only chosen fields

        Args:
            table: a string indicating the name of the table
            values: a dict of fields and values to be inserted

        Returns:
            query: a SQL string with
            values: a tuple of values to replace placeholder(%s) tokens in query

        """
        keys = ', '.join(values.keys())
        value_place_holder = ' %s,' * len(values)
        query = cls._insert_string.format(table, keys, value_place_holder[:-1])
        return query, tuple(values.values())

    @classmethod
    def make_update_query(cls, table:str, values:dict, where_key:str):
        """
        Creates an update query with only chosen fields
        Supports only a single field where clause

        Args:
            table: a string indicating the name of the table
            values: a dict of fields and values to be inserted
            where_key: the 'where' clause field

        Returns:
            query: a SQL string with
            values: a tuple of values to replace placeholder(%s) tokens in query - except the where clause value

        """
        keys = ', '.join(values.keys())
        value_place_holder = ' %s,' * len(values)
        query = cls._update_string.format(table, keys, value_place_holder[:-1], where_key)
        return query, tuple(values.values())

    @classmethod
    def _get_where_clause_with_values(cls, where_keys):
        vals = []

        def make_and_query(ele: dict):
            and_query = ' and '.join(['{} = %s'.format(e) for e in ele.keys()])
            vals.extend(ele.values())
            return '(' + and_query + ')'<commit_msg>Accepting operator in where clause<commit_after>from asyncio import coroutine
from functools import wraps
from enum import Enum

from aiopg import create_pool, Pool, Cursor
import psycopg2

_CursorType = Enum('CursorType', 'PLAIN, DICT, NAMEDTUPLE')


class PostgresStore:
    _pool = None
    _connection_params = {}
    _insert_string = "insert into {} ({}) values ({});"
    _update_string = "update {} set ({}) = ({}) where {} = %s;"
    _select_all_string_with_condition = "select * from {} where ({}) limit {} offset %s order by {}"
    _select_all_string = "select * from {} limit {} offset {} order by {}"
    _select_selective_column = "select {} from {} limit {} offset {} order by {}"
    _select_selective_column_with_condition = "select {} from {} where ({}) limit {} offset {} order by {}"
    _delete_query = "delete from {} where ({})"

    @classmethod
    def connect(cls, database:str, user:str, password:str, host:str, port:int):
        """
        Sets connection parameters
        """
        cls._connection_params['database'] = database
        cls._connection_params['user'] = user
        cls._connection_params['password'] = password
        cls._connection_params['host'] = host
        cls._connection_params['port'] = port

    @classmethod
    def use_pool(cls, pool:Pool):
        """
        Sets an existing connection pool instead of using connect() to make one
        """
        cls._pool = pool

    @classmethod
    @coroutine
    def get_pool(cls) -> Pool:
        """
        Yields:
            existing db connection pool
        """
        if len(cls._connection_params) < 5:
            raise ConnectionError('Please call SQLStore.connect before calling this method')
        if not cls._pool:
            cls._pool = yield from create_pool(**cls._connection_params)
        return cls._pool

    @classmethod
    @coroutine
    def get_cursor(cls, cursor_type=_CursorType.PLAIN) -> Cursor:
        """
        Yields:
            new client-side cursor from existing db connection pool
        """
        pool = yield from cls.get_pool()
        if cursor_type == _CursorType.PLAIN:
            c = yield from pool.cursor()
            return c
        if cursor_type == _CursorType.NAMEDTUPLE:
            return (yield from pool.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor))
        if cursor_type == _CursorType.DICT:
            return (yield from pool.cursor(cursor_factory=psycopg2.extras.DictCursor))

    @classmethod
    def make_insert_query(cls, table:str, values:dict):
        """
        Creates an insert statement with only chosen fields

        Args:
            table: a string indicating the name of the table
            values: a dict of fields and values to be inserted

        Returns:
            query: a SQL string with
            values: a tuple of values to replace placeholder(%s) tokens in query

        """
        keys = ', '.join(values.keys())
        value_place_holder = ' %s,' * len(values)
        query = cls._insert_string.format(table, keys, value_place_holder[:-1])
        return query, tuple(values.values())

    @classmethod
    def make_update_query(cls, table:str, values:dict, where_key:str):
        """
        Creates an update query with only chosen fields
        Supports only a single field where clause

        Args:
            table: a string indicating the name of the table
            values: a dict of fields and values to be inserted
            where_key: the 'where' clause field

        Returns:
            query: a SQL string with
            values: a tuple of values to replace placeholder(%s) tokens in query - except the where clause value

        """
        keys = ', '.join(values.keys())
        value_place_holder = ' %s,' * len(values)
        query = cls._update_string.format(table, keys, value_place_holder[:-1], where_key)
        return query, tuple(values.values())

    @classmethod
    def _get_where_clause_with_values(cls, where_keys):
        vals = []

        def make_and_query(ele: dict):
            and_query = ' and '.join(['{} {} %s'.format(e[0], e[1][0]) for e in ele.items()])
            vals.extend([val[1] for val in ele.values()])
            return '(' + and_query + ')'
{'fraction_non_alphanumeric': 0.10624925886398671, 'fraction_numerical': 0.0018973081939997628, 'mean_word_length': 2.473640856672158, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6458136', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2321, 'n_words': 703}