lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Monday, March 14, 2011 11:02 AM, Doug Currie cast light into the darkness

> 
> You might find this example helpful: dbvm_bind_names in lsqlite3.c
>

That's one of the pieces of code that I was looking at this weekend. It didn't make sense to me because I'd approached the problem from the other direction - having the parameters drive the binding instead of the statement.

dbvm_bind_names expects to have a table as the second argument and ?userdata? as the first argument.

It calls sqlite3 to count the number of parameters in the statement. I can do that on my query string and get the number of positional and named parameters.

For each of the parameters, it gets the name of the parameter.

If the parameter starts with : or $, it is treated as a named parameter. Otherwise, it is treated as a positional parameter.

For named parameter, it pushes the name onto the stack and then calls gettable to get the value (using name as the key into the table on the stack). The value is now on the top of the stack.

For positional parameter, it pushes the position onto the stack and then calls gettable to get the value (using position as the index into the table on the stack). The value is now on the top of the stack.

Either way, a function to bind it is called. That function gets the current Lua state and the index of the parameter and the index of the value on the stack (-1 meaning that the value is on top of the stack).

The function looks at the type of the value on the stack. Based on the type, it calls the appropriate sqlite3 bind function. 

Then it pops the name or position from the stack so that the next parameter can be processed.

Did I understand that correctly?

What I'm doing/trying to do differently is have the parameter table drive this instead of the statement. Reflecting on this, I don't know why I decided to do it that way. I should stop fighting the tide and go with this approach.

Mike