Beispiel #1
0
 def replace_markers(marker, op, parameters):
     param_count = len(parameters)
     marker_index = 0
     start_offset = 0
     while True:
         found_offset = op.find(marker, start_offset)
         if not found_offset > -1:
             break
         if marker_index < param_count:
             op = op[:found_offset] + op[found_offset:].replace(
                 marker, parameters[marker_index], 1)
             start_offset = found_offset + len(parameters[marker_index])
             marker_index += 1
         else:
             raise ProgrammingError("Incorrect number of bindings "
                                    "supplied. The current statement uses "
                                    "%d or more, and there are %d "
                                    "supplied." %
                                    (marker_index + 1, param_count))
     if marker_index != 0 and marker_index != param_count:
         raise ProgrammingError("Incorrect number of bindings "
                                "supplied. The current statement uses "
                                "%d or more, and there are %d supplied." %
                                (marker_index + 1, param_count))
     return op
Beispiel #2
0
def _replace_numeric_markers(operation, string_parameters):
    """
    Replaces qname and numeric markers in the given operation, from
    the string_parameters list.
    Raises ProgrammingError on wrong number of parameters or bindings
    when using qmark. There is no error checking on numeric parameters.
    """
    # replace qmark parameters
    param_count = len(string_parameters)
    qmark_index = 0
    while operation.find('?') > -1:
        if qmark_index < param_count:
            operation = operation.replace('?', string_parameters[qmark_index],
                                          1)
            qmark_index += 1
        else:
            raise ProgrammingError("Incorrect number of bindings " +
                                   "supplied. The current statement uses "
                                   "%d or more, and there are %d supplied." %
                                   (qmark_index + 1, param_count))
    if qmark_index != 0 and qmark_index != param_count:
        raise ProgrammingError("Incorrect number of bindings " +
                               "supplied. The current statement uses "
                               "%d or more, and there are %d supplied." %
                               (qmark_index + 1, param_count))

    # replace numbered parameters
    # Go through them backwards so smaller numbers don't replace
    # parts of larger ones
    for index in range(param_count, 0, -1):
        operation = operation.replace(':' + str(index),
                                      string_parameters[index - 1])
    return operation
Beispiel #3
0
 def executemany(self, operation, seq_of_parameters):
     # PEP 249
     for parameters in seq_of_parameters:
         self.execute(operation, parameters)
         if self.has_result_set:
             raise ProgrammingError("Operations that have result sets are "
                                    "not allowed with executemany.")
Beispiel #4
0
 def fetchcbatch(self):
     '''Return a CBatch object of any data currently in the buffer or
        if no data currently in buffer then fetch a batch'''
     if not self._last_operation.is_columnar:
         raise NotSupportedError("Server does not support columnar "
                                 "fetching")
     if not self.has_result_set:
         raise ProgrammingError(
             "Trying to fetch results on an operation with no results.")
     if len(self._buffer) > 0:
         log.debug(
             'fetchcbatch: buffer has data in. Returning it and wiping buffer'
         )
         batch = self._buffer
         self._buffer = Batch()
         return batch
     elif self._last_operation_active:
         log.debug('fetchcbatch: buffer empty and op is active => fetching '
                   'more data')
         batch = (self._last_operation.fetch(
             self.description,
             self.buffersize,
             convert_types=self.convert_types))
         if len(batch) == 0:
             return None
         return batch
     else:
         return None
Beispiel #5
0
    def get_table_schema(self, table_name, database_name=None):
        if database_name is None:
            database_name = '.*'

        def op():
            self._last_operation_string = "RPC_DESCRIBE_TABLE"
            self._last_operation = self.session.get_table_schema(
                table_name, database_name)

        self._execute_async(op)
        self._wait_to_finish()
        results = self.fetchall()
        if len(results) == 0:
            # TODO: the error raised here should be different
            raise OperationalError("no schema results for table %s.%s" %
                                   (database_name, table_name))
        # check that results are derived from a unique table
        tables = set()
        for col in results:
            tables.add((col[1], col[2]))
        if len(tables) > 1:
            # TODO: the error raised here should be different
            raise ProgrammingError("db: %s, table: %s is not unique" %
                                   (database_name, table_name))
        return [(r[3], r[5]) for r in results]
Beispiel #6
0
    def executemany(self, operation, seq_of_parameters):
        # PEP 249
        log.debug('Attempting to execute %s queries', len(seq_of_parameters))

        regex = r'^INSERT INTO TABLE.*VALUES'
        if re.search(regex, operation):
            # doing a bulk insert
            log.debug("bulk insert detected")
            row_per_operation = 1000
        else:
            row_per_operation = 1

        total_rows = len(seq_of_parameters)
        i = 0

        parameters = []
        for parameter in seq_of_parameters:
            parameters.append(parameter)
            i += 1
            if len(parameters) == row_per_operation or i == total_rows:
                self.execute(operation, parameters)
                parameters = []
                if self.has_result_set:
                    raise ProgrammingError(
                        "Operations that have result sets are "
                        "not allowed with executemany.")
Beispiel #7
0
 def fetchone(self):
     # PEP 249
     if not self.has_result_set:
         raise ProgrammingError("Tried to fetch but no results.")
     try:
         return self.next()
     except StopIteration:
         return None
Beispiel #8
0
 def executemany(self, operation, seq_of_parameters):
     # PEP 249
     log.debug('Attempting to execute %s queries', len(seq_of_parameters))
     for parameters in seq_of_parameters:
         self.execute(operation, parameters)
         if self.has_result_set:
             raise ProgrammingError("Operations that have result sets are "
                                    "not allowed with executemany.")
Beispiel #9
0
 def fetchone(self):
     # PEP 249
     if not self.has_result_set:
         raise ProgrammingError("Tried to fetch but no results.")
     log.debug('Fetching a single row')
     try:
         return next(self)
     except StopIteration:
         return None
Beispiel #10
0
def _bind_parameters(operation, parameters):
    # If parameters is a list, assume either qmark, format, or numeric
    # format. If not, assume either named or pyformat parameters
    if isinstance(parameters, (list, tuple)):
        return _bind_parameters_list(operation, parameters)
    elif isinstance(parameters, dict):
        return _bind_parameters_dict(operation, parameters)
    else:
        raise ProgrammingError("Query parameters argument should be a "
                               "list, tuple, or dict object")
Beispiel #11
0
 def replace_markers(marker, op, parameters):
     param_count = len(parameters)
     marker_index = 0
     while op.find(marker) > -1:
         if marker_index < param_count:
             op = op.replace(marker, parameters[marker_index], 1)
             marker_index += 1
         else:
             raise ProgrammingError("Incorrect number of bindings "
                                    "supplied. The current statement uses "
                                    "%d or more, and there are %d "
                                    "supplied." %
                                    (marker_index + 1, param_count))
     if marker_index != 0 and marker_index != param_count:
         raise ProgrammingError("Incorrect number of bindings "
                                "supplied. The current statement uses "
                                "%d or more, and there are %d supplied." %
                                (marker_index + 1, param_count))
     return op
Beispiel #12
0
def _replace_numeric_markers(operation, string_parameters, paramstyle):
    """
    Replaces qname, format, and numeric markers in the given operation, from
    the string_parameters list.

    Raises ProgrammingError on wrong number of parameters or bindings
    when using qmark. There is no error checking on numeric parameters.
    """
    def replace_markers(marker, op, parameters):
        param_count = len(parameters)
        marker_index = 0
        start_offset = 0
        while True:
            found_offset = op.find(marker, start_offset)
            if not found_offset > -1:
                break
            if marker_index < param_count:
                op = op[:found_offset]+op[found_offset:].replace(marker, parameters[marker_index], 1)
                start_offset = found_offset + len(parameters[marker_index])
                marker_index += 1
            else:
                raise ProgrammingError("Incorrect number of bindings "
                                       "supplied. The current statement uses "
                                       "%d or more, and there are %d "
                                       "supplied." % (marker_index + 1,
                                                      param_count))
        if marker_index != 0 and marker_index != param_count:
            raise ProgrammingError("Incorrect number of bindings "
                                   "supplied. The current statement uses "
                                   "%d or more, and there are %d supplied." %
                                   (marker_index + 1, param_count))
        return op

    # replace qmark parameters and format parameters
    # If paramstyle is explicitly specified don't try to substitue them all
    if paramstyle == 'qmark' or paramstyle is None:
        operation = replace_markers('?', operation, string_parameters)
    if paramstyle == 'format' or paramstyle is None:
        operation = replace_markers(r'%s', operation, string_parameters)

    # replace numbered parameters
    if paramstyle == 'numeric' or paramstyle is None:
        operation = re.sub(r'(:)(\d+)', r'{\2}', operation)
        # offset by one
        operation = operation.format(*[''] + string_parameters)

    if paramstyle in {'named', 'pyformat'}:
        raise ProgrammingError(
            "paramstyle '%s' is not compatible with parameters passed as List."
            "please you a dict for you parameters instead or specify"
            " a different paramstyle",
            paramstyle
        )

    return operation
Beispiel #13
0
def _bind_parameters(operation, parameters):
    # If parameters is a list, assume either qmark, format, or numeric
    # format. If not, assume either named or pyformat parameters

    # now parameter is always a list
    # if parameters is a list of dict, we are doing a bulk insert
    for parameter in parameters:
        if isinstance(parameter, (list, tuple)):
            return _bind_parameters_list(operation, parameters)
        elif isinstance(parameter, dict):
            return _bind_parameters_dict(operation, parameters)
        else:
            raise ProgrammingError("Query parameters argument should be a "
                                   "list, tuple, or dict object")
Beispiel #14
0
 def fetchmany(self, size=None):
     # PEP 249
     if not self.has_result_set:
         raise ProgrammingError("Tried to fetch but no results.")
     if size is None:
         size = self.arraysize
     local_buffer = []
     i = 0
     while i < size:
         try:
             local_buffer.append(self.next())
             i += 1
         except StopIteration:
             break
     return local_buffer
Beispiel #15
0
 def next(self):
     if not self.has_result_set:
         raise ProgrammingError(
             "Trying to fetch results on an operation with no results.")
     if len(self._buffer) > 0:
         return self._buffer.pop(0)
     elif self._last_operation_active:
         # self._buffer is empty here and op is active: try to pull more rows
         rows = rpc.fetch_results(self.service, self._last_operation_handle,
                                  self.description, self.buffersize)
         self._buffer.extend(rows)
         if len(self._buffer) == 0:
             raise StopIteration
         return self._buffer.pop(0)
     else:
         # buffer is already empty
         raise StopIteration
Beispiel #16
0
 def fetchmany(self, size=None):
     # PEP 249
     self._wait_to_finish()
     if not self.has_result_set:
         raise ProgrammingError("Tried to fetch but no results.")
     if size is None:
         size = self.arraysize
     log.debug('Fetching up to %s result rows', size)
     local_buffer = []
     i = 0
     while i < size:
         try:
             local_buffer.append(next(self))
             i += 1
         except StopIteration:
             break
     return local_buffer
Beispiel #17
0
 def next(self):
     if not self.has_result_set:
         raise ProgrammingError("Trying to fetch results on an operation with no results.")
     if len(self._buffer) > 0:
         return self._buffer.pop(0)
     elif self._last_operation_active:
         # self._buffer is empty here and op is active: try to pull more rows
         rows = rpc.fetch_internal(self.service,
                 self._last_operation_handle, self.buffersize)
         self._buffer.extend(rows)
         if len(self._buffer) == 0:
             self._last_operation_active = False
             rpc.close_query(self.service, self._last_operation_handle)
             raise StopIteration
         return self._buffer.pop(0)
     else:
         # empty buffer and op is now closed: raise StopIteration
         raise StopIteration
Beispiel #18
0
 def __next__(self):
     if not self.has_result_set:
         raise ProgrammingError(
             "Trying to fetch results on an operation with no results.")
     if len(self._buffer) > 0:
         log.debug('__next__: popping row out of buffer')
         return self._buffer.pop()
     elif self._last_operation_active:
         log.debug('__next__: buffer empty and op is active => fetching '
                   'more data')
         self._buffer = self._last_operation.fetch(self.description,
                                                   self.buffersize)
         if len(self._buffer) == 0:
             log.debug('__next__: no more data to fetch')
             raise StopIteration
         log.debug('__next__: popping row out of buffer')
         return self._buffer.pop()
     else:
         log.debug('__next__: buffer empty')
         raise StopIteration
Beispiel #19
0
 def __next__(self):
     if not self.has_result_set:
         raise ProgrammingError(
             "Trying to fetch results on an operation with no results.")
     if len(self._buffer) > 0:
         log.debug('__next__: popping row out of buffer')
         return self._buffer.pop(0)
     elif self._last_operation_active:
         # self._buffer is empty here and op is active: try to pull more
         # rows
         log.debug('__next__: buffer empty and op is active => fetching '
                   'more rows')
         rows = fetch_results(self.service, self._last_operation_handle,
                              self.hs2_protocol_version, self.description,
                              self.buffersize)
         self._buffer.extend(rows)
         if len(self._buffer) == 0:
             log.debug('__next__: no more rows to fetch')
             raise StopIteration
         log.debug('__next__: popping row out of buffer')
         return self._buffer.pop(0)
     else:
         # buffer is already empty
         raise StopIteration
Beispiel #20
0
 def is_executing(self):
     if self._last_operation_handle is None:
         raise ProgrammingError("Operation state is not available")
     operation_state = get_operation_status(self.service,
                                            self._last_operation_handle)
     return self._op_state_is_executing(operation_state)
Beispiel #21
0
 def get_log(self):
     if self._last_operation is None:
         raise ProgrammingError("Operation state is not available")
     return self._last_operation.get_log()
Beispiel #22
0
 def get_profile(self, profile_format=TRuntimeProfileFormat.STRING):
     if self._last_operation is None:
         raise ProgrammingError("Operation state is not available")
     return self._last_operation.get_profile(profile_format=profile_format)
Beispiel #23
0
 def is_executing(self):
     if self._last_operation is None:
         raise ProgrammingError("Operation state is not available")
     operation_state = self._last_operation.get_status()
     return self._op_state_is_executing(operation_state)