Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item selection is case-sensitive #45

Closed
jedmund opened this issue May 22, 2020 · 2 comments · Fixed by #88 or #93
Closed

Item selection is case-sensitive #45

jedmund opened this issue May 22, 2020 · 2 comments · Fixed by #88 or #93
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jedmund
Copy link
Owner

jedmund commented May 22, 2020

When querying items with Siero (e.g. to set rate ups, spark targets, etc), Siero looks for exact matches in the database. As such, these commands are case-sensitive. This is inconvenient for most users, especially those on mobile.

@jedmund jedmund added enhancement New feature or request good first issue Good for newcomers labels May 22, 2020
@mafdoesthings
Copy link
Collaborator

mafdoesthings commented May 25, 2020

So I wrote a better SQL query generation function to generate fuzzy matched queries, but I can't implement it into spark.js and gacha.js without some weird bugs cropping up. (running queries twice, mishandling unsanitized inputs.)

Features of the improved query/builder.

  1. Improved accuracy of results. No more 30+ results for "io grand"
  2. Hard cap at 9 results.
  3. Input sanitization and case insensitivity
  4. Sorting of most confident matches.

Things to be aware of:

ORDER BY is not compatible with the COUNT() function by itself. As such, when passing queries to the array to count rows, the first index in the array ( sql[0] ) needs to be 'SELECT COUNT(*)'

This can be improved by additional logic on the query inputs towards the last if/else statement. but wasn't necessary for current implementation.

Here's the function:

  buildQuery(name, sql) {

        var san = name.replace(/[^a-zA-z ]/g, "")

        var words = san.split(' ')
        for (var i in words) {
                if (i == 0) {
                    if (words[i].length >= 6) {
                        var op = '%'
                    } else {
                        var op = '~*'
                    }
                    sql.push(`(name ${op} '${words[i]}' OR recruits ${op} '${words[i]}')`)

                } else {
                    if (words[i].length >= 6) {
                        var op = '%'
                    } else {
                        var op = '~*'
                    }
                    sql.push(`AND (name ${op} '${words[i]}' OR recruits ${op} '${words[i]}')`)
                }
        }
        if (sql[0] != "SELECT COUNT(*)") {
            sql.push(`ORDER BY recruits <-> '${san}', name <-> '${san}'`)
            sql.push("LIMIT 9")
        } else {
            sql.push("LIMIT 9")
        }
        console.log(sql.join(" "))
        return sql.join(" ")
    }

And a usage example:

async fetchSuppliedTarget(name) {

        let sql = [
            "SELECT *",
            "FROM gacha WHERE"
        ]

        let query = this.buildQuery(name, sql)

        return await Client.one(query, [name])
            .then(res => {
                return res
            })
            .catch(error => {
                let text = 'Sorry, there was an error communicating with the database for your last request.'
                common.reportError(this.message, this.userId, this.context, error, text)
            })
    }

@mafdoesthings
Copy link
Collaborator

Ideally i'd like to wrap all query generation into this function, including a flag to run SELECT for specific columns (name, id ...) , and builtlins for common SQL functions like COUNT(). But until we know how it'll be implemented it gets a little more awkward to write.

@jedmund jedmund linked a pull request Jun 3, 2020 that will close this issue
4 tasks
@jedmund jedmund linked a pull request Jun 5, 2020 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
2 participants