<commit_before>from __future__ import absolute_import

import vertica_python.errors as errors

import vertica_python.vertica.messages as messages
from vertica_python.vertica.column import Column


class Cursor(object):
    def __init__(self, connection, cursor_type=None):
        self.connection = connection
        self.cursor_type = cursor_type
        self._closed = False
        self._message = None

        self.last_execution = None
        self.error = None

        #
        # dbApi properties
        #
        self.description = None
        self.rowcount = -1
        self.arraysize = 1

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        self.close()

    #
    # dbApi methods
    #

    def callproc(procname, parameters=None):
        raise errors.NotSupportedError('Cursor.callproc() is not implemented')

    def close(self):
        self._closed = True

    def execute(self, operation, parameters=None):
        if self.closed():
            raise errors.Error('Cursor is closed')

        if parameters:
            # optional requirement
            from psycopg2.extensions import adapt

            if isinstance(parameters, dict):
                for key in parameters:
                    v = adapt(parameters[key]).getquoted()
                    operation = operation.replace(':' + key, v)
            elif isinstance(parameters, tuple):
                operation = operation % tuple(adapt(p).getquoted() for p in parameters)
            else:
                raise errors.Error("Argument 'parameters' must be dict or tuple")

        self.rowcount = 0
        if self.last_execution:
            # ToDo: can just empty the message buffer in connection if easy to do
            self.connection.reset_connection()
        self.last_execution = operation
        self.connection.write(messages.Query(operation))

        # Fetch the schema
        message = self.connection.read_message()
        if isinstance(message, messages.ErrorResponse):
            raise errors.QueryError.from_error_response(message, self.last_execution)
        elif not isinstance(message, messages.RowDescription):
            print("Message type: {0}".format(type(message)))
            print(message)
            raise errors.QueryError("Unexpected message type", self.last_execution)
        self.description = map(lambda fd: Column(fd), message.fields)

        # Read until data rows or end of stream
        while True:
            message = self.connection.read_message()
            if isinstance(message, messages.DataRow) or isinstance(message, messages.ReadyForQuery):
                self._message = message  # cache the message because there's no way to undo the read
                break
            else:
                self.connection.process_message(message)

    def fetchone(self):
        if isinstance(self._message, messages.DataRow):
            self.rowcount += 1
            row = self.row_formatter(self._message)<commit_msg>fix condition when the query returns 0 row<commit_after>from __future__ import absolute_import

import vertica_python.errors as errors

import vertica_python.vertica.messages as messages
from vertica_python.vertica.column import Column


class Cursor(object):
    def __init__(self, connection, cursor_type=None):
        self.connection = connection
        self.cursor_type = cursor_type
        self._closed = False
        self._message = None

        self.last_execution = None
        self.error = None

        #
        # dbApi properties
        #
        self.description = None
        self.rowcount = -1
        self.arraysize = 1

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        self.close()

    #
    # dbApi methods
    #

    def callproc(procname, parameters=None):
        raise errors.NotSupportedError('Cursor.callproc() is not implemented')

    def close(self):
        self._closed = True

    def execute(self, operation, parameters=None):
        if self.closed():
            raise errors.Error('Cursor is closed')

        if parameters:
            # optional requirement
            from psycopg2.extensions import adapt

            if isinstance(parameters, dict):
                for key in parameters:
                    v = adapt(parameters[key]).getquoted()
                    operation = operation.replace(':' + key, v)
            elif isinstance(parameters, tuple):
                operation = operation % tuple(adapt(p).getquoted() for p in parameters)
            else:
                raise errors.Error("Argument 'parameters' must be dict or tuple")

        self.rowcount = 0
        if self.last_execution:
            # ToDo: can just empty the message buffer in connection if easy to do
            self.connection.reset_connection()
        self.last_execution = operation
        self.connection.write(messages.Query(operation))

        # Fetch the schema
        message = self.connection.read_message()
        if isinstance(message, messages.ErrorResponse):
            raise errors.QueryError.from_error_response(message, self.last_execution)
        elif not isinstance(message, messages.RowDescription):
            print("Message type: {0}".format(type(message)))
            print(message)
            raise errors.QueryError("Unexpected message type", self.last_execution)
        self.description = map(lambda fd: Column(fd), message.fields)

        # Read until data rows or end of stream
        while True:
            message = self.connection.read_message()
            if isinstance(message, messages.DataRow) \
                    or isinstance(message, messages.CommandComplete) \
                    or isinstance(message, messages.ReadyForQuery):
                self._message = message  # cache the message because there's no way to undo the read
                break
            else:
                self.connection.process_message(message)

    def fetchone(self):
        if isinstance(self._message, messages.DataRow):
            self.rowcount += 1
            row = self.row_formatter(self._message)
{'fraction_non_alphanumeric': 0.08097560975609756, 'fraction_numerical': 0.002113821138211382, 'mean_word_length': 2.1804550155118925, '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': '6938528', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1478, 'n_words': 427}