Convert a LaTeX formula to the database index.

    Parameters
    ----------
    formula_str : string
        The formula as LaTeX code.
    backslash_fix : boolean
        If this is set to true, then it will be checked if the same formula
        exists with a preceeding backslash.

    Returns
    -------
    int :
        The database index.

def formula_to_dbid(formula_str, backslash_fix=False):
    """
    Convert a LaTeX formula to the database index.

    Parameters
    ----------
    formula_str : string
        The formula as LaTeX code.
    backslash_fix : boolean
        If this is set to true, then it will be checked if the same formula
        exists with a preceeding backslash.

    Returns
    -------
    int :
        The database index.
    """
    global __formula_to_dbid_cache
    if __formula_to_dbid_cache is None:
        mysql = utils.get_mysql_cfg()
        connection = pymysql.connect(host=mysql['host'],
                                     user=mysql['user'],
                                     passwd=mysql['passwd'],
                                     db=mysql['db'],
                                     charset='utf8mb4',
                                     cursorclass=pymysql.cursors.DictCursor)
        cursor = connection.cursor()

        # Get all formulas that should get examined
        sql = ("SELECT `id`, `formula_in_latex` FROM `wm_formula` ")
        cursor.execute(sql)
        formulas = cursor.fetchall()
        __formula_to_dbid_cache = {}
        for fm in formulas:
            __formula_to_dbid_cache[fm['formula_in_latex']] = fm['id']
    if formula_str in __formula_to_dbid_cache:
        return __formula_to_dbid_cache[formula_str]
    elif backslash_fix and ('\\%s' % formula_str) in __formula_to_dbid_cache:
        return __formula_to_dbid_cache['\\%s' % formula_str]
    else:
        logging.info("Symbol '%s' was not found. Add it to write-math.com.",
                     formula_str)
        mysql = utils.get_mysql_cfg()
        connection = pymysql.connect(host=mysql['host'],
                                     user=mysql['user'],
                                     passwd=mysql['passwd'],
                                     db=mysql['db'],
                                     charset='utf8mb4',
                                     cursorclass=pymysql.cursors.DictCursor)
        cursor = connection.cursor()
        sql = ("INSERT INTO `wm_formula` (`user_id`, `formula_name`, "
               "`formula_in_latex`, "
               "`mode`, `package`) VALUES ("
               "'10', %s, %s, 'bothmodes', NULL);")
        if len(formula_str) < 20:
            logging.info("Insert formula %s.", formula_str)
        cursor.execute(sql, (formula_str, formula_str))
        connection.commit()
        __formula_to_dbid_cache[formula_str] = connection.insert_id()
        return __formula_to_dbid_cache[formula_str]