webmcp

changeset 549:77355fe3b1cc

Array support for more types
author jbe
date Tue Oct 22 13:45:45 2019 +0200 (13 months ago)
parents a006593b747c
children 59d2aaa6df07
files libraries/mondelefant/mondelefant_atom_connector.lua
line diff
     1.1 --- a/libraries/mondelefant/mondelefant_atom_connector.lua	Mon Oct 21 19:55:13 2019 +0200
     1.2 +++ b/libraries/mondelefant/mondelefant_atom_connector.lua	Tue Oct 22 13:45:45 2019 +0200
     1.3 @@ -38,11 +38,15 @@
     1.4  
     1.5  input_converters = setmetatable({}, { __mode = "k" })
     1.6  
     1.7 -input_converters["boolean"] = function(conn, value)
     1.8 -  if value then return "TRUE" else return "FALSE" end
     1.9 +input_converters["boolean"] = function(conn, value, rawtext_mode)
    1.10 +  if rawtext_mode then
    1.11 +    if value then return "t" else return "f" end
    1.12 +  else
    1.13 +    if value then return "TRUE" else return "FALSE" end
    1.14 +  end
    1.15  end
    1.16  
    1.17 -input_converters["number"] = function(conn, value)
    1.18 +input_converters["number"] = function(conn, value, rawtext_mode)
    1.19    if _VERSION == "Lua 5.2" then
    1.20      -- TODO: remove following compatibility hack to allow large integers (e.g. 1e14) in Lua 5.2
    1.21      local integer_string = string.format("%i", value)
    1.22 @@ -53,7 +57,7 @@
    1.23        if string.find(number_string, "^[0-9.e+-]+$") then
    1.24          return number_string
    1.25        else
    1.26 -        return "'NaN'"
    1.27 +        if rawtext_mode then return "NaN" else return "'NaN'" end
    1.28        end
    1.29      end
    1.30    end
    1.31 @@ -65,41 +69,65 @@
    1.32    if string.find(str, "^[0-9.e+-]+$") then
    1.33      return str
    1.34    end
    1.35 -  return "'NaN'"
    1.36 +  if rawtext_mode then return "NaN" else return "'NaN'" end
    1.37  end
    1.38  
    1.39 -input_converters[atom.fraction] = function(conn, value)
    1.40 +input_converters[atom.fraction] = function(conn, value, rawtext_mode)
    1.41    if value.invalid then
    1.42 -    return "'NaN'"
    1.43 +    if rawtext_mode then return "NaN" else return "'NaN'" end
    1.44    else
    1.45      local n, d = tostring(value.numerator), tostring(value.denominator)
    1.46      if string.find(n, "^%-?[0-9]+$") and string.find(d, "^%-?[0-9]+$") then
    1.47 -      return "(" .. n .. "::numeric / " .. d .. "::numeric)"
    1.48 +      if rawtext_mode then
    1.49 +        return n .. "/" .. d
    1.50 +      else
    1.51 +        return "(" .. n .. "::numeric / " .. d .. "::numeric)"
    1.52 +      end
    1.53      else
    1.54 -      return "'NaN'"
    1.55 +      if rawtext_mode then return "NaN" else return "'NaN'" end
    1.56      end
    1.57    end
    1.58  end
    1.59  
    1.60 -input_converters[atom.date] = function(conn, value)
    1.61 -  return conn:quote_string(tostring(value)) .. "::date"
    1.62 +input_converters[atom.date] = function(conn, value, rawtext_mode)
    1.63 +  if rawtext_mode then
    1.64 +    return tostring(value)
    1.65 +  else
    1.66 +    return conn:quote_string(tostring(value)) .. "::date"
    1.67 +  end
    1.68  end
    1.69  
    1.70 -input_converters[atom.timestamp] = function(conn, value)
    1.71 -  return conn:quote_string(tostring(value))  -- don't define type
    1.72 +input_converters[atom.timestamp] = function(conn, value, rawtext_mode)
    1.73 +  if rawtext_mode then
    1.74 +    return tostring(value)
    1.75 +  else
    1.76 +    return conn:quote_string(tostring(value))  -- don't define type
    1.77 +  end
    1.78  end
    1.79  
    1.80 -input_converters[atom.time] = function(conn, value)
    1.81 -  return conn:quote_string(tostring(value)) .. "::time"
    1.82 +input_converters[atom.time] = function(conn, value, rawtext_mode)
    1.83 +  if rawtext_mode then
    1.84 +    return tostring(value)
    1.85 +  else
    1.86 +    return conn:quote_string(tostring(value)) .. "::time"
    1.87 +  end
    1.88  end
    1.89  
    1.90 -input_converters["table"] = function(conn, value)  -- integer and text arrays
    1.91 -  -- TODO: support more types
    1.92 +input_converters["table"] = function(conn, value, rawtext_mode)
    1.93 +  -- treat tables as arrays
    1.94    local parts = { "{" }
    1.95    for i, v in ipairs(value) do
    1.96      if i > 1 then parts[#parts+1] = "," end
    1.97 +    local converter =
    1.98 +      input_converters[getmetatable(v)] or
    1.99 +      input_converters[type(v)]
   1.100 +    if converter then
   1.101 +      v = converter(conn, v, true)
   1.102 +    else
   1.103 +      v = tostring(v)
   1.104 +    end
   1.105      parts[#parts+1] = '"'
   1.106 -    parts[#parts+1] = string.gsub(tostring(v), '[\\"]', '\\%0')
   1.107 +    parts[#parts+1] = string.gsub(v, '[\\"]', '\\%0')
   1.108      parts[#parts+1] = '"'
   1.109    end
   1.110    parts[#parts+1] = "}"

Impressum / About Us