def fetchmany(self, *arg): """Fetch next row of a query result set. arg sets the arraysize The number of rows to fetch per call is specified by the parameter. If it is not given, the cursor's arraysize determines the number of rows to be fetched. The method should try to fetch as many rows as indicated by the size parameter. If this is not possible due to the specified number of rows not being available, fewer rows may be returned. Arraysize is retained after each call to fetchmany. """ values = [] return_tuple = () self.__check_if_open() self.__check_for_transaction() if (not self.__mimcursor): self.__raise_exception(-25014) # If arg is provided, arraysize is set if (len(arg) > 0): self.arraysize = arg[0] fetch_length = self.arraysize rc_value = mimerapi.mimerFetch(self.__statement) fetch_value = rc_value while (fetch_value != 100 and fetch_length > 0): self.__check_mimerapi_error(fetch_value, self.__statement) return_tuple = () # Column number starts a 1 for cur_column in range(1, self._number_of_columns + 1): rc_value = mimerapi.mimerColumnType(self.__statement, cur_column) self.__check_mimerapi_error(rc_value, self.__statement) func_tuple = get_funcs[rc_value](self.__statement, cur_column) self.__check_mimerapi_error(func_tuple[0], self.__statement) # Conversion from C int to Python boolean if (rc_value == 42 and not func_tuple[1] == None): if (func_tuple[1] == 0): return_tuple = return_tuple + (False, ) else: return_tuple = return_tuple + (True, ) else: return_tuple = return_tuple + (func_tuple[1], ) values.append(return_tuple) fetch_length = fetch_length - 1 fetch_value = mimerapi.mimerFetch(self.__statement) return values
def fetchall(self): """ Fetch all (remaining) row of a query result set. The rows are returned as a list of tuples. If no more data is available, an empty list is returned. If fetchall is called and the previous call to execute did not produce a result set, a ProgrammingError is raised. """ self.__check_if_open() self.__check_for_transaction() if (not self.__mimcursor): self.__raise_exception(-25014) values = [] rc_value = mimerapi.mimerFetch(self.__statement) fetch_value = rc_value while (fetch_value != 100): self.__check_mimerapi_error(fetch_value, self.__statement) return_tuple = () # Column number starts a 1 for cur_column in range(1, self._number_of_columns + 1): rc_value = mimerapi.mimerColumnType(self.__statement, cur_column) self.__check_mimerapi_error(rc_value, self.__statement) func_tuple = get_funcs[rc_value](self.__statement, cur_column) self.__check_mimerapi_error(func_tuple[0], self.__statement) # Conversion from C int to Python boolean if (rc_value == 42 and not func_tuple[1] == None): if (func_tuple[1] == 0): return_tuple = return_tuple + (False, ) else: return_tuple = return_tuple + (True, ) else: return_tuple = return_tuple + (func_tuple[1], ) values.append(return_tuple) fetch_value = mimerapi.mimerFetch(self.__statement) return values
def fetchone(self): """ Fetch next row of a query result set. The row is returned as a tuple. If no more data is available, None is returned. If fetchone is called and the previous call to execute did not produce a result set, a ProgrammingError is raised. """ self.__check_if_open() self.__check_for_transaction() if (not self.__mimcursor): self.__raise_exception(-25014) rc_value = mimerapi.mimerFetch(self.__statement) self.__check_mimerapi_error(rc_value, self.__statement) return_tuple = () # Return value of mimerFetch == 100 implies end of result set if (rc_value == 100): return [] for cur_column in range(1, self._number_of_columns + 1): rc_value = mimerapi.mimerColumnType(self.__statement, cur_column) self.__check_mimerapi_error(rc_value, self.__statement) func_tuple = get_funcs[rc_value](self.__statement, cur_column) self.__check_mimerapi_error(func_tuple[0], self.__statement) # Conversion from C int to Python boolean if (rc_value == 42 and not func_tuple[1] == None): if (func_tuple[1] == 0): return_tuple = return_tuple + (False, ) else: return_tuple = return_tuple + (True, ) else: return_tuple = return_tuple + (func_tuple[1], ) return return_tuple
def execute(self, *arg): """ Executes a database operation. arg query to execute Executes a database operation. """ rc_value = 0 self.__check_if_open() self.__check_for_transaction() parameter_markers = () # I would like to look over this at some point, the type control is not ideal - Erik 2018-10 if (len(arg) > 1): if (isinstance(arg[1], dict)): parameter_markers = arg[1] elif (not isinstance(arg[1], tuple) and (not isinstance(arg[1], list))): parameter_markers = [arg[1]] else: parameter_markers = arg[1] query = arg[0] # If same query is used twice there is not need for a new statement if (query != self._last_query or self.__mimcursor): self.__close_statement() values = mimerapi.mimerBeginStatement8(self.__session, query, 0) rc_value = values[0] self._DDL_rc_value = values[0] # -24005 indicates a DDL statement if (self._DDL_rc_value != -24005): self.__check_mimerapi_error(rc_value, self.__session) self.__statement = values[1] self._last_query = query # Return value -24005 is given when a DDL query query is passed through # mimerBeginStatementC. if (self._DDL_rc_value == -24005): self.connection.transaction = False self.messages = [] rc_value = mimerapi.mimerExecuteStatement8(self.__session, query) self.__check_mimerapi_error(rc_value, self.__session) else: rc_value = mimerapi.mimerParameterCount(self.__statement) self.__check_mimerapi_error(rc_value, self.__statement) # Return value of mimerParameterCount = 0 implies a query with no # parameters. if (rc_value > 0): self._number_of_parameters = rc_value try: if (len(parameter_markers) < self._number_of_parameters): self.__raise_exception(-25013) # Column number starts a 1 for cur_column in range(1, self._number_of_parameters + 1): parameter_type = mimerapi.mimerParameterType( self.__statement, cur_column) self.__check_mimerapi_error( parameter_type, self.__statement) if (isinstance(parameter_markers, dict)): rc_value, parameter_name = mimerapi.mimerParameterName8( self.__statement, cur_column) self.__check_mimerapi_error( rc_value, self.__statement) if parameter_name in parameter_markers: parameter = parameter_markers.get( parameter_name) if (parameter == None): parameter_type = 501 rc_value = set_funcs[parameter_type]( self.__statement, cur_column, parameter) else: # self.__raise_exception(-25012,str(parameter_name)) pass # Skipping keys in dictionary that does not match with any column else: # If the parameter marker is None, we use mimerSetNull try: if (parameter_markers[cur_column - 1] == None): parameter_type = 501 except TypeError: # End up here when invalid parameters are used self.__raise_exception(-25013) rc_value = set_funcs[parameter_type](self.__statement, cur_column, parameter_markers[cur_column - 1]) self.__check_mimerapi_error(rc_value, self.__statement) # Catching error for errorhandler except KeyError as e: self.__raise_exception(-25020, exception=e) # &&&& ?? # Catching error for errorhandler except TypeError as e: # End up here when invalid parameters are used¨ self.__raise_exception(-25020, exception=e) # Catching error for errorhandler except OverflowError as e: self.__raise_exception(-25020, exception=e) self.__check_mimerapi_error(rc_value, self.__statement) rc_value = mimerapi.mimerColumnCount(self.__statement) # Return value of mimerColumnCount <= 0 implies a query with no # result set. if (rc_value <= 0): self.__check_mimerapi_error(rc_value, self.__statement) self.messages = [] rc_value = mimerapi.mimerExecute(self.__statement) self.__check_mimerapi_error(rc_value, self.__statement) self.rowcount = rc_value else: # Return value of mimerColumnCount > 0 implies a query with a # result set. self.rowcount = rc_value self._number_of_columns = rc_value rc_value = mimerapi.mimerOpenCursor(self.__statement) self.__check_mimerapi_error(rc_value, self.__statement) self.__mimcursor = True description = collections.namedtuple('Column_description', 'name type_code display_size internal_size precision scale null_ok') self.description = () self._column_type = [] for cur_column in range(1, self._number_of_columns + 1): func_tuple = mimerapi.mimerColumnName8( self.__statement, cur_column) rc_value = func_tuple[0] self.__check_mimerapi_error(rc_value, self.__statement) name = func_tuple[1] rc_value = mimerapi.mimerColumnType( self.__statement, cur_column) self.__check_mimerapi_error(rc_value, self.__statement) self._column_type.append(rc_value) type_code = rc_value self.description = self.description + (description(name=name, type_code=type_code, display_size=None, internal_size=None, precision=None, scale=None, null_ok=None),)