src/pds_doi_service/core/db/doi_database.py
def _form_query_with_wildcards(column_name, search_tokens):
        """
        Helper method to form a portion of an SQL WHERE clause that returns
        matches from the specified column using the provided list of tokens.

        The list of tokens may either contain fully specified identifiers, or
        identifiers containing Unix-style wildcards (*), aka globs. The method
        partitions the tokens accordingly, and forms the appropriate clause
        to capture all results.

        Parameters
        ----------
        column_name : str
            Name of the SQL table column name that will be searched by the
            returned query.
        search_tokens : list of str
            List of tokens to search for. Tokens may either be full identifiers,
            or contain one or more wildcards (*).

        Returns
        -------
        where_subclause : str
            Query portion which can be used with a WHERE clause to find the
            requested set of tokens. This subclause is parameterized, and should
            be used with the returned named parameter dictionary.
        named_parameter_values : dict
            The dictionary mapping the named parameters in the returned subclause
            with the actual values to use.

        """
        # Partition the tokens containing wildcards from the fully specified ones
        wildcard_tokens = list(filter(lambda token: "*" in token or "?" in token, search_tokens))
        full_tokens = list(set(search_tokens) - set(wildcard_tokens))

        # Clean up the column name provided so it can be used as a suitable
        # named parameter placeholder token
        filter_chars = [" ", "'", ":", "|"]
        named_param_id = column_name

        for filter_char in filter_chars:
            named_param_id = named_param_id.replace(filter_char, "")

        # Set up the named parameters for the IN portion of the WHERE used
        # to find fully specified tokens
        named_parameters = ",".join([f":{named_param_id}_{i}" for i in range(len(full_tokens))])
        named_parameter_values = {f"{named_param_id}_{i}": full_tokens[i] for i in range(len(full_tokens))}

        # Next, because we use actually use LIKE and not GLOB (for the case-insensitivity),
        # we need to convert wildcards from Unix style (*,?) to SQLite style (%,_),
        # but we first need to escape any existing characters reserved by LIKE (& and _)
        like_chars = ["%", "_"]
        glob_chars = ["*", "?"]

        for index, wildcard_token in enumerate(wildcard_tokens):
            for like_char, glob_char in zip(like_chars, glob_chars):
                # Escape reserved wildcards used by LIKE
                wildcard_token = wildcard_token.replace(like_char, f"\\{like_char}")

                # Replace wildcards used by GLOB with equivalent for LIKE
                wildcard_token = wildcard_token.replace(glob_char, like_char)

                # Update the list of wildcards
                wildcard_tokens[index] = wildcard_token

        # Set up the named parameters for the LIKE portion of the WHERE used
        # find tokens containing wildcards
        like_parameters = " OR ".join(
            [f"{column_name} LIKE :{named_param_id}_like_{i}" for i in range(len(wildcard_tokens))]
        )

        named_parameter_values.update(
            {f"{named_param_id}_like_{i}": wildcard_tokens[i] for i in range(len(wildcard_tokens))}
        )

        # Build the portion of the WHERE clause combining the necessary
        # parameters needed to search for all the tokens we were provided
        where_subclause = "AND ("

        if full_tokens:
            where_subclause += f"{column_name} IN ({named_parameters}) "

        if full_tokens and wildcard_tokens:
            where_subclause += " OR "

        if wildcard_tokens:
            where_subclause += f"{like_parameters}"

            # Make sure Sqlite knows were using backslash for escaped chars
            where_subclause += " ESCAPE '\\'"

        where_subclause += ")"

        logger.debug("WHERE subclause: %s", where_subclause)

        return where_subclause, named_parameter_values

        Helper method to form a portion of an SQL WHERE clause that returns
        matches from the specified column using the provided list of tokens.

        The list of tokens may either contain fully specified identifiers, or
        identifiers containing Unix-style wildcards (*), aka globs. The method
        partitions the tokens accordingly, and forms the appropriate clause
        to capture all results.

        Parameters
        ----------
        column_name : str
            Name of the SQL table column name that will be searched by the
            returned query.
        search_tokens : list of str
            List of tokens to search for. Tokens may either be full identifiers,
            or contain one or more wildcards (*).

        Returns
        -------
        where_subclause : str
            Query portion which can be used with a WHERE clause to find the
            requested set of tokens. This subclause is parameterized, and should
            be used with the returned named parameter dictionary.
        named_parameter_values : dict
            The dictionary mapping the named parameters in the returned subclause
            with the actual values to use.

        
Helper method to form a portion of an SQL WHERE clause that returns
matches from the specified column using the provided list of tokens.

The list of tokens may either contain fully specified identifiers, or
identifiers containing Unix-style wildcards (*), aka globs. The method
partitions the tokens accordingly, and forms the appropriate clause
to capture all results.

Parameters

column_name : str
Name of the SQL table column name that will be searched by the
returned query.
search_tokens : list of str
List of tokens to search for. Tokens may either be full identifiers,
or contain one or more wildcards (*).

Returns

where_subclause : str
Query portion which can be used with a WHERE clause to find the
requested set of tokens. This subclause is parameterized, and should
be used with the returned named parameter dictionary.
named_parameter_values : dict
The dictionary mapping the named parameters in the returned subclause
with the actual values to use.
Helper method to form a portion of an SQL WHERE clause that returns matches from the specified column using the provided list of tokens . The list of tokens may either contain fully specified identifiers or identifiers containing Unix - style wildcards ( * ) aka globs . The method partitions the tokens accordingly and forms the appropriate clause to capture all results . Parameters column_name : str Name of the SQL table column name that will be searched by the returned query . search_tokens : list of str List of tokens to search for . Tokens may either be full identifiers or contain one or more wildcards ( * ) . Returns where_subclause : str Query portion which can be used with a WHERE clause to find the requested set of tokens . This subclause is parameterized and should be used with the returned named parameter dictionary . named_parameter_values : dict The dictionary mapping the named parameters in the returned subclause with the actual values to use .
def _form_query_with_wildcards(column_name, search_tokens):
        wildcard_tokens = list(filter(lambda token: "*" in token or "?" in token, search_tokens))
        full_tokens = list(set(search_tokens) - set(wildcard_tokens))
        filter_chars = [" ", "'", ":", "|"]
        named_param_id = column_name
        for filter_char in filter_chars:
            named_param_id = named_param_id.replace(filter_char, "")
        named_parameters = ",".join([f":{named_param_id}_{i}" for i in range(len(full_tokens))])
        named_parameter_values = {f"{named_param_id}_{i}": full_tokens[i] for i in range(len(full_tokens))}
        like_chars = ["%", "_"]
        glob_chars = ["*", "?"]
        for index, wildcard_token in enumerate(wildcard_tokens):
            for like_char, glob_char in zip(like_chars, glob_chars):
                wildcard_token = wildcard_token.replace(like_char, f"\\{like_char}")
                wildcard_token = wildcard_token.replace(glob_char, like_char)
                wildcard_tokens[index] = wildcard_token
        like_parameters = " OR ".join(
            [f"{column_name} LIKE :{named_param_id}_like_{i}" for i in range(len(wildcard_tokens))]
        )
        named_parameter_values.update(
            {f"{named_param_id}_like_{i}": wildcard_tokens[i] for i in range(len(wildcard_tokens))}
        )
        where_subclause = "AND ("
        if full_tokens:
            where_subclause += f"{column_name} IN ({named_parameters}) "
        if full_tokens and wildcard_tokens:
            where_subclause += " OR "
        if wildcard_tokens:
            where_subclause += f"{like_parameters}"
            where_subclause += " ESCAPE '\\'"
        where_subclause += ")"
        logger.debug("WHERE subclause: %s", where_subclause)
        return where_subclause, named_parameter_values
def _form_query_with_wildcards ( column_name , search_tokens ) : wildcard_tokens = list ( filter ( lambda token : "*" in token or "?" in token , search_tokens ) ) full_tokens = list ( set ( search_tokens ) - set ( wildcard_tokens ) ) filter_chars = [ " " , "'" , ":" , "|" ] named_param_id = column_name for filter_char in filter_chars : named_param_id = named_param_id . replace ( filter_char , "" ) named_parameters = "," . join ( [ f":{named_param_id}_{i}" for i in range ( len ( full_tokens ) ) ] ) named_parameter_values = { f"{named_param_id}_{i}" : full_tokens [ i ] for i in range ( len ( full_tokens ) ) } like_chars = [ "%" , "_" ] glob_chars = [ "*" , "?" ] for index , wildcard_token in enumerate ( wildcard_tokens ) : for like_char , glob_char in zip ( like_chars , glob_chars ) : wildcard_token = wildcard_token . replace ( like_char , f"\\{like_char}" ) wildcard_token = wildcard_token . replace ( glob_char , like_char ) wildcard_tokens [ index ] = wildcard_token like_parameters = " OR " . join ( [ f"{column_name} LIKE :{named_param_id}_like_{i}" for i in range ( len ( wildcard_tokens ) ) ] ) named_parameter_values . update ( { f"{named_param_id}_like_{i}" : wildcard_tokens [ i ] for i in range ( len ( wildcard_tokens ) ) } ) where_subclause = "AND (" if full_tokens : where_subclause += f"{column_name} IN ({named_parameters}) " if full_tokens and wildcard_tokens : where_subclause += " OR " if wildcard_tokens : where_subclause += f"{like_parameters}" where_subclause += " ESCAPE '\\'" where_subclause += ")" logger . debug ( "WHERE subclause: %s" , where_subclause ) return where_subclause , named_parameter_values
Helper method to form a portion of an SQL WHERE clause that returns
matches from the specified column using the provided list of tokens.
Helper method to form a portion of an SQL WHERE clause that returns matches from the specified column using the provided list of tokens .
"""
        Helper method to form a portion of an SQL WHERE clause that returns
        matches from the specified column using the provided list of tokens.

        The list of tokens may either contain fully specified identifiers, or
        identifiers containing Unix-style wildcards (*), aka globs. The method
        partitions the tokens accordingly, and forms the appropriate clause
        to capture all results.

        Parameters
        ----------
        column_name : str
            Name of the SQL table column name that will be searched by the
            returned query.
        search_tokens : list of str
            List of tokens to search for. Tokens may either be full identifiers,
            or contain one or more wildcards (*).

        Returns
        -------
        where_subclause : str
            Query portion which can be used with a WHERE clause to find the
            requested set of tokens. This subclause is parameterized, and should
            be used with the returned named parameter dictionary.
        named_parameter_values : dict
            The dictionary mapping the named parameters in the returned subclause
            with the actual values to use.

        """ # Partition the tokens containing wildcards from the fully specified ones # Clean up the column name provided so it can be used as a suitable # named parameter placeholder token # Set up the named parameters for the IN portion of the WHERE used # to find fully specified tokens # Next, because we use actually use LIKE and not GLOB (for the case-insensitivity), # we need to convert wildcards from Unix style (*,?) to SQLite style (%,_), # but we first need to escape any existing characters reserved by LIKE (& and _) # Escape reserved wildcards used by LIKE # Replace wildcards used by GLOB with equivalent for LIKE # Update the list of wildcards # Set up the named parameters for the LIKE portion of the WHERE used # find tokens containing wildcards # Build the portion of the WHERE clause combining the necessary # parameters needed to search for all the tokens we were provided # Make sure Sqlite knows were using backslash for escaped chars
column_name
search_tokens