lua-users home
lua-l archive

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


On Jun 28, 2013 12:35 AM, "Tim Hill" <drtimhill@gmail.com> wrote:
> To take a canonical example, what is a clean way to surface a database result set that might include NULLs? In a standard library that many people are likely to use? Yes, there are a dozen ways, but I don't regard any of them as clean, and when I analyzed the problem, it really came down to this lack of a storable empty value and the overloading of nil.

Personally I haven't yet encountered a case where I needed a null value in a database to correspond to anything other than nil in a table. I suppose that's because I generally abstract each table (or sometimes sets of tables) to functions like:

function add_foo(foo)
  db:query("INSERT INTO foos (name, type, value) VALUES (?, ?, ?)",
    foo.name, foo.type, foo.value)
end

in which db:query() uses its vararg params to form a parameterized query; a nil argument gets translated to NULL automatically. Similarly get_foo() would look up a row by some criteria and return it (or perhaps a list of rows) with fields like {name='a', type='t', value=7}; if one of those fields were null in the database, it would just be missing from the returned row, and when the application reads that field from the table, it gets nil just as you'd expect.

There are probably cases where this won't do, but I usually feel like if the best solution to a problem involves modifying Lua itself, what I really need to do is rethink the design so that the problem no longer exists, as it's usually reflecting a fundamental weakness in the design itself and not in the language.