def get_latest_state_change_by_data_field(
            self,
            filters: Dict[str, Any],
    ) -> StateChangeRecord:
        """ Return all state changes filtered by a named field and value."""
        cursor = self.conn.cursor()

        where_clauses = []
        args = []
        filters = _filter_from_dict(filters)
        for field, value in filters.items():
            where_clauses.append('json_extract(data, ?)=?')
            args.append(f'$.{field}')
            args.append(value)

        where = ' AND '.join(where_clauses)
        sql = (
            f'SELECT identifier, data '
            f'FROM state_changes '
            f'WHERE {where} '
            f'ORDER BY identifier '
            f'DESC LIMIT 1'
        )
        cursor.execute(sql, args)

        result = StateChangeRecord(state_change_identifier=0, data=None)
        row = cursor.fetchone()
        if row:
            state_change_identifier = row[0]
            state_change = row[1]
            result = StateChangeRecord(
                state_change_identifier=state_change_identifier,
                data=state_change,
            )

        return result
def get_latest_state_change_by_data_field ( self , filters : Dict [ str , Any ] , ) -> StateChangeRecord : cursor = self . conn . cursor ( ) where_clauses = [ ] args = [ ] filters = _filter_from_dict ( filters ) for field , value in filters . items ( ) : where_clauses . append ( 'json_extract(data, ?)=?' ) args . append ( f'$.{field}' ) args . append ( value ) where = ' AND ' . join ( where_clauses ) sql = ( f'SELECT identifier, data ' f'FROM state_changes ' f'WHERE {where} ' f'ORDER BY identifier ' f'DESC LIMIT 1' ) cursor . execute ( sql , args ) result = StateChangeRecord ( state_change_identifier = 0 , data = None ) row = cursor . fetchone ( ) if row : state_change_identifier = row [ 0 ] state_change = row [ 1 ] result = StateChangeRecord ( state_change_identifier = state_change_identifier , data = state_change , ) return result
Return all state changes filtered by a named field and value.