leafo / pgmoon

A pure Lua Postgres driver for use in OpenResty & more
MIT License
391 stars 93 forks source link

Handle NULL in arrays. #56

Closed moteus closed 2 years ago

moteus commented 7 years ago

Arrays like {NULL,"NULL"} should be handeled to `{pg.NULL, 'NULL'}. I use yours lpeg parser with simple patch.

local literal = function(name)
  if name == 'NULL' then return NULL end
  return name
end

local g = P{"array",
....
   literal = C((P(1) - S("},")) ^ 1) / literal,
}

Test case: sql - select ARRAY[NULL,'1','NULL1','NULL',NULL,'2',NULL], result in text mode {NULL,1,NULL1,"NULL",NULL,2,NULL}

I think same cahnges need in Array building.

ttfkam commented 7 years ago

Note: the (insane?) way Lua handles array tables when implementing a solution

    local arr = { Nil, 3 }
    print(#arr)  -- prints 2
    arr = { 2, 3, Nil }
    print(#arr)  -- prints 2
    arr = { Nil, 3, Nil, 3 }
    print(#arr)  -- prints 4
    arr = { Nil, Nil, Nil, Nil, Nil, Nil, Nil }
    print(#arr)  -- prints 0
    arr = { 1, Nil, Nil, Nil, Nil, Nil, Nil }
    print(#arr)  -- prints 1

Any solution will require some extra effort on the part of the user to ensure correctness and safety.

leafo commented 2 years ago

I'm closing out very old issues. Feel free to comment if you still feel something should be addressed.