예제 #1
0
    def delete_by_template(self, template, limit=None):
        '''

        :param template: Template to determine rows to delete.
        :return: Number of rows deleted.
        '''
        if not Helper.is_empty(template):
            if not Helper.is_template_valid(template, self.get_columns()):
                self._logger.error(
                    'Some columns in the specified template don\'t match table columns'
                )
                raise Exception

        rows = self.get_rows()
        r_indexes = []
        for i in range(0, len(rows)):
            if limit is not None:
                if len(r_indexes) == limit:
                    break

            if Helper.matches_template(rows[i], template):
                r_indexes.append(i)

        if len(r_indexes) == 0:
            return 0
        elif len(r_indexes) == 1:
            self._delete_row(r_indexes[0])
            return 1
        else:
            self._delete_rows(r_indexes)
            return len(r_indexes)
예제 #2
0
    def update_by_template(self, template, new_values, limit=None):
        '''

        :param template: Template for rows to match.
        :param new_values: New values to set for matching fields.
        :return: Number of rows updated.
        '''
        if Helper.is_empty(new_values):
            return 0

        if not Helper.is_empty(template):
            if not Helper.is_template_valid(template, self.get_columns()):
                self._logger.error(
                    'Some columns in the specified template don\'t match table columns'
                )
                raise Exception

        # Extract key_fields from template if any
        changed_keys = Helper.extract_key_columns_and_values_from_template(
            new_values, self._data['key_columns'])

        rows = self.get_rows()
        r_indexes = []
        for i in range(0, len(rows)):
            if limit is not None:
                if len(r_indexes) == limit:
                    break

            if Helper.matches_template(rows[i], template):
                # Apply changed_keys and check if modification would result in duplicate primary key
                if not Helper.is_empty(changed_keys):
                    # Very important to make copy of rows[i] so that it will not be altered
                    new_keys_template = Helper.change_keys(
                        copy.copy(rows[i]), changed_keys)

                    if self._violate_primary_key_constraint(new_keys_template):
                        self._logger.error('Violates primary key constraint')
                        raise Exception

                r_indexes.append(i)

        if len(r_indexes) == 0:
            return 0
        elif len(r_indexes) == 1:
            self._modify_row(r_indexes[0], new_values)
            return 1
        else:
            self._modify_rows(r_indexes, new_values)
            return len(r_indexes)
예제 #3
0
    def find_by_template(self,
                         template,
                         field_list=None,
                         limit=None,
                         offset=None,
                         order_by=None):
        '''

        :param template: A dictionary of the form { 'field1' : value1, 'field2': value2, ...}
        :param field_list: A list of request fields of the form, ['fielda', 'fieldb', ...]
        :param limit: Do not worry about this for now.
        :param offset: Do not worry about this for now.
        :param order_by: Do not worry about this for now.
        :return: A list containing dictionaries. A dictionary is in the list representing each record
            that matches the template. The dictionary only contains the requested fields.
        '''
        if not Helper.is_empty(template):
            if not Helper.is_template_valid(template, self.get_columns()):
                self._logger.error(
                    'Some columns in the specified template don\'t match table columns'
                )
                raise Exception

        if not Helper.is_empty(field_list):
            if not Helper.is_column_list_valid(field_list, self.get_columns()):
                self._logger.error(
                    'Some columns in the specified field_list don\'t match table columns'
                )
                raise Exception

        matching_rows = []
        for row in self.get_rows():
            if limit is not None:
                if len(matching_rows) == limit:
                    break

            if Helper.matches_template(row, template):
                matching_rows.append(
                    Helper.extract_needed_fields(field_list, row))

        return matching_rows