FredyH / MySQLOO

MySQLOO
GNU Lesser General Public License v2.1
140 stars 55 forks source link

Prepared query : bad argument #1 to 'start' (table expected, got no value) #91

Closed Krelsis closed 3 years ago

Krelsis commented 3 years ago

Precursor: Connection to the local database has been succesfully established utilising :

MySQL = {};
local sqlip = "127.0.0.1";
MySQL.Database = mysqloo.connect(sqlip, "user", "pass", "table", 3306);

I have written a method to prepare a query based on the following object shown in JSON for reference.

       {
        QueryName = "A nice friendly name for the query",
        Query = "SELECT * FROM SomeTable WHERE SomeColumn = ?;",
        Payload = {
            1 = {
                Name: "Friendly parameter name",
                Type: 2
                Data: "Some data for the where clause"
            }
        },
        OnSuccess = "function: 0x.... some memory address",
        OnError = "function: 0x.... some memory address"
    }

Example of function being called, JSON representation of query parameter seen above.

function RunPreparedQuery(query)
    local preparedQuery = MySQL.Database:prepare(query.Query);

    for index, payload in ipairs(query.Payload) do
        if (payload.Type == 1) then
            preparedQuery:setNull(index);
        elseif (payload.Type == 2) then
            preparedQuery:setString(index, payload.Data);
        elseif (payload.Type == 3) then
            preparedQuery:setNumber(index, payload.Data);
        elseif (payload.Type == 4) then
            preparedQuery:setBoolean(index, payload.Data);
        end
    end

    if (query.OnSuccess) then
        preparedQuery.onSuccess = query.OnSuccess;
    end

    if (query.OnError) then
        preparedQuery.onError = query.OnError;
    end

    preparedQuery.start();
end

By utilisation of PrintTable and various prints throughout the above method, I can determine that each parameter is being set properly, OnSuccess and OnError successfully being set. However as soon as start() is invoked, it errors with bad argument #1 to 'start' (table expected, got no value).

For reference, if I were to provide an empty table start({}) the error is [MySQLOO] Expected MySQLOO table.

Utilising PrintTable I can see that preparedQuery at the time of invocation is :

__CppObject     =       userdata: 0x27cc8d50
onError =       function: 0x275fb638
onSuccess       =       function: 0x27cc8c20

All examples indicate that no parameters are required for the start() method. Am I approaching this incorrectly?

Krelsis commented 3 years ago

Using version 9.7.0-beta-7, However, issue was replicable on 9.6.1.

FredyH commented 3 years ago

You are missing a : in the query.start() line. i.e. it should be

preparedQuery:start()

Otherwise the preparedQuery does not get passed to the start function and mysqloo does not know which query you started.

Krelsis commented 3 years ago

Apologies, that's my fault, didn't realise I wasn't using it as a meta method, to have tried so many things but not check whether I was using a metamethod and not a method, for shame... This is why you shouldn't do any dev work 16 hours straight :D

I appreciate the swift reply, silly mistake though it may be, probably should have slept on it rather than rushing to a github issue.