capacitor-community / sqlite

Community plugin for native & electron SQLite databases
MIT License
426 stars 104 forks source link

Multi row statement should fail if no values are replaced #551

Open lasher23 opened 2 weeks ago

lasher23 commented 2 weeks ago

Is your feature request related to a problem? Please describe. Currently it is not easy visible if you make a mistake while using the multi row statements.

Describe the solution you'd like If nothing is replaced with the regex i would like that an error is thrown. Replace the following: image With something like this:

    public String replacePlaceholders(String stmt, String sqlBuilder) {
        return replaceWithCheck(stmt, "(?i)VALUES \\((\\?(?:,\\s*\\?\\s*)*)\\)",  "VALUES " + sqlBuilder)
    }

    public static String replaceWithCheck(String input, String regex, String replacement) throws Exception {
        String newString = input.replaceAll(regex, replacement);
        if (input.equals(newString)) {
            throw new Exception("No values found to replace for multi row statements");
        }
        return newString;
    }
jepiqueau commented 2 weeks ago

@lasher23 not really it is for the case where you have "INSERT INTO TABLE (name, email) VALUES ('Jeep','jeep@example.com') " this should pass through

lasher23 commented 2 weeks ago

@jepiqueau but then you dont provide a two dimensional array as parameter. The logic is only triggered if the array is a two dimensional array.

jepiqueau commented 2 weeks ago

@lasher23 i will have an other look give me an example of statements that you want me to add to my test program thanks

lasher23 commented 1 week ago

@jepiqueau If i call executeSet with a two dimensional array. I always want to get into the multi statement logic. A simple example is i dont get it that i have to pass ? as parameter. What you are doing now is just execute the statement with an empty array as args which makes it replace it with null.

For example the following code raises no error: executeSet("insert into table", [['a'],['b']])

This for sure is an error in my statement. But it would be very nice if the Plugin tells me that i am not allowed to execute this. For example it was very hard to spot the bugs with the case insensitive 'VALUES' because there was never an error from the plugin, it just did not replace the values.