Beispiel #1
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 #2
0
 def executemany(self, operation, seq_of_parameters):
     # PEP 249
     log.info('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 #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 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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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_handle = rpc.get_table_schema(self.service,
                 self.session_handle, table_name, database_name)
     self._execute_sync(op)
     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 #16
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 #17
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)
Beispiel #18
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 #19
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 #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)