webmcp

diff libraries/mondelefant/mondelefant_atom_connector.lua @ 106:bbfbbddf13ad

Support for intervals
author jbe
date Sun Nov 04 04:55:22 2012 +0100 (2012-11-04)
parents 3d43a5cf17c1
children 6b435d3c0b14
line diff
     1.1 --- a/libraries/mondelefant/mondelefant_atom_connector.lua	Sun Nov 04 04:54:54 2012 +0100
     1.2 +++ b/libraries/mondelefant/mondelefant_atom_connector.lua	Sun Nov 04 04:55:22 2012 +0100
     1.3 @@ -75,6 +75,24 @@
     1.4    return conn:quote_string(tostring(value)) .. "::time"
     1.5  end
     1.6  
     1.7 +input_converters[atom.interval] = function(conn, value)
     1.8 +  return (
     1.9 +    conn:quote_string(
    1.10 +      table.concat(
    1.11 +        {
    1.12 +          tostring(value.years),   "years",
    1.13 +          tostring(value.months),  "months",
    1.14 +          tostring(value.days),    "days",
    1.15 +          tostring(value.hours),   "hours",
    1.16 +          tostring(value.minutes), "minutes",
    1.17 +          tostring(value.seconds), "seconds"
    1.18 +        },
    1.19 +        " "
    1.20 +      )
    1.21 +    ) .. "::interval"
    1.22 +  )
    1.23 +end
    1.24 +
    1.25  
    1.26  output_converters = setmetatable({}, { __mode = "k" })
    1.27  
    1.28 @@ -129,6 +147,59 @@
    1.29  output_converters.time = time_loader_func
    1.30  output_converters.timetz = time_loader_func
    1.31  
    1.32 +output_converters.interval = function(str)
    1.33 +  local years, months, days, hours, minutes, seconds = 0, 0, 0, 0, 0, 0
    1.34 +  local any_match = false
    1.35 +  for amount, unit in string.gmatch(str, "(%-?[0-9]+)%s*([A-Za-z]+)") do
    1.36 +    local unit = string.lower(unit)
    1.37 +    if string.match(unit, "^y") then
    1.38 +      years = years + tonumber(amount)
    1.39 +      any_match = true
    1.40 +    elseif string.match(unit, "^mo") then
    1.41 +      months = months + tonumber(amount)
    1.42 +      any_match = true
    1.43 +    elseif string.match(unit, "^d") then
    1.44 +      days = days + tonumber(amount)
    1.45 +      any_match = true
    1.46 +    elseif string.match(unit, "^h") then
    1.47 +      hours = hours + tonumber(amount)
    1.48 +      any_match = true
    1.49 +    elseif string.match(unit, "^mi") then
    1.50 +      minutes = minutes + tonumber(amount)
    1.51 +      any_match = true
    1.52 +    elseif string.match(unit, "^s") then
    1.53 +      seconds = seconds + tonumber(amount)
    1.54 +      any_match = true
    1.55 +    else
    1.56 +      return atom.interval.invalid
    1.57 +    end
    1.58 +  end
    1.59 +  local sign, h, m, s = string.match(str, "(%-?)([0-9]+):([0-9]+):([0-9]+)")
    1.60 +  if h then
    1.61 +    if sign == "-" then
    1.62 +      hours   = hours   - tonumber(h)
    1.63 +      minutes = minutes - tonumber(m)
    1.64 +      seconds = seconds - tonumber(s)
    1.65 +    else
    1.66 +      hours   = hours   + tonumber(h)
    1.67 +      minutes = minutes + tonumber(m)
    1.68 +      seconds = seconds + tonumber(s)
    1.69 +    end
    1.70 +    any_match = true
    1.71 +  end
    1.72 +  if not any_match then
    1.73 +    return atom.interval.invalid
    1.74 +  end
    1.75 +  if string.match(str, "%sago%s*$") then
    1.76 +    years, months, days = -years, -months, -days
    1.77 +    hours, minutes, seconds = -hours, -minutes, -seconds
    1.78 +  end
    1.79 +  return atom.interval:new{
    1.80 +    years = years, months = months, days = days,
    1.81 +    hours = hours, minutes = minutes, seconds = seconds
    1.82 +  }
    1.83 +end
    1.84 +
    1.85  mondelefant.postgresql_connection_prototype.type_mappings = {
    1.86    int8 = atom.integer,
    1.87    int4 = atom.integer,

Impressum / About Us