|
| 1 | +/* |
| 2 | + * Copyright 2013 Maurício Linhares |
| 3 | + * Copyright 2013 Dylan Simon |
| 4 | + * |
| 5 | + * Maurício Linhares licenses this file to you under the Apache License, |
| 6 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 7 | + * with the License. You may obtain a copy of the License at: |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.github.mauricio.async.db.postgresql.column |
| 19 | + |
| 20 | +import com.github.mauricio.async.db.column.ColumnEncoderDecoder |
| 21 | +import com.github.mauricio.async.db.exceptions.DateEncoderNotAvailableException |
| 22 | +import com.github.mauricio.async.db.util.Log |
| 23 | +import org.joda.time.{Period, ReadablePeriod, ReadableDuration} |
| 24 | +import org.joda.time.format.{ISOPeriodFormat, PeriodFormatterBuilder} |
| 25 | + |
| 26 | +object PostgreSQLIntervalEncoderDecoder extends ColumnEncoderDecoder { |
| 27 | + |
| 28 | + private val log = Log.getByName(this.getClass.getName) |
| 29 | + |
| 30 | + /* Postgres accepts all ISO8601 formats. */ |
| 31 | + private val formatter = ISOPeriodFormat.standard |
| 32 | + |
| 33 | + override def encode(value : Any) : String = { |
| 34 | + value match { |
| 35 | + case t : ReadablePeriod => formatter.print(t) |
| 36 | + case t : ReadableDuration => t.toString // defaults to ISO8601 |
| 37 | + case _ => throw new DateEncoderNotAvailableException(value) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /* these should only be used for parsing: */ |
| 42 | + private def postgresYMDBuilder(builder : PeriodFormatterBuilder) = builder |
| 43 | + .appendYears .appendSuffix(" year", " years").appendSeparator(" ") |
| 44 | + .appendMonths .appendSuffix(" mon", " mons" ).appendSeparator(" ") |
| 45 | + .appendDays .appendSuffix(" day", " days" ).appendSeparator(" ") |
| 46 | + |
| 47 | + private val postgres_verboseParser = |
| 48 | + postgresYMDBuilder(new PeriodFormatterBuilder().appendLiteral("@ ")) |
| 49 | + .appendHours .appendSuffix(" hour", " hours").appendSeparator(" ") |
| 50 | + .appendMinutes.appendSuffix(" min", " mins" ).appendSeparator(" ") |
| 51 | + .appendSecondsWithOptionalMillis.appendSuffix(" sec", " secs") |
| 52 | + .toFormatter |
| 53 | + |
| 54 | + private def postgresHMSBuilder(builder : PeriodFormatterBuilder) = builder |
| 55 | + // .printZeroAlways // really all-or-nothing |
| 56 | + .rejectSignedValues(true) // XXX: sign should apply to all |
| 57 | + .appendHours .appendSuffix(":") |
| 58 | + .appendMinutes.appendSuffix(":") |
| 59 | + .appendSecondsWithOptionalMillis |
| 60 | + |
| 61 | + private val hmsParser = |
| 62 | + postgresHMSBuilder(new PeriodFormatterBuilder()) |
| 63 | + .toFormatter |
| 64 | + |
| 65 | + private val postgresParser = |
| 66 | + postgresHMSBuilder(postgresYMDBuilder(new PeriodFormatterBuilder())) |
| 67 | + .toFormatter |
| 68 | + |
| 69 | + /* These sql_standard parsers don't handle negative signs correctly. */ |
| 70 | + private def sqlDTBuilder(builder : PeriodFormatterBuilder) = |
| 71 | + postgresHMSBuilder(builder |
| 72 | + .appendDays.appendSeparator(" ")) |
| 73 | + |
| 74 | + private val sqlDTParser = |
| 75 | + sqlDTBuilder(new PeriodFormatterBuilder()) |
| 76 | + .toFormatter |
| 77 | + |
| 78 | + private val sqlParser = |
| 79 | + sqlDTBuilder(new PeriodFormatterBuilder() |
| 80 | + .printZeroAlways |
| 81 | + .rejectSignedValues(true) // XXX: sign should apply to both |
| 82 | + .appendYears.appendSeparator("-").appendMonths |
| 83 | + .rejectSignedValues(false) |
| 84 | + .printZeroNever |
| 85 | + .appendSeparator(" ")) |
| 86 | + .toFormatter |
| 87 | + |
| 88 | + /* This supports all positive intervals, and intervalstyle of postgres_verbose, and iso_8601 perfectly. |
| 89 | + * If intervalstyle is set to postgres or sql_standard, some negative intervals may be rejected. |
| 90 | + */ |
| 91 | + def decode(value : String) : Period = { |
| 92 | + if (value.isEmpty) /* huh? */ |
| 93 | + Period.ZERO |
| 94 | + else { |
| 95 | + val format = ( |
| 96 | + if (value(0).equals('P')) /* iso_8601 */ |
| 97 | + formatter |
| 98 | + else if (value.startsWith("@ ")) |
| 99 | + postgres_verboseParser |
| 100 | + else { |
| 101 | + /* try to guess based on what comes after the first number */ |
| 102 | + val i = value.indexWhere(!_.isDigit, if ("-+".contains(value(0))) 1 else 0) |
| 103 | + if (i < 0 || ":.".contains(value(i))) /* simple HMS (to support group negation) */ |
| 104 | + hmsParser |
| 105 | + else if (value(i).equals('-')) /* sql_standard: Y-M */ |
| 106 | + sqlParser |
| 107 | + else if (value(i).equals(' ') && i+1 < value.length && value(i+1).isDigit) /* sql_standard: D H:M:S */ |
| 108 | + sqlDTParser |
| 109 | + else |
| 110 | + postgresParser |
| 111 | + } |
| 112 | + ) |
| 113 | + if ((format eq hmsParser) && value(0).equals('-')) |
| 114 | + format.parsePeriod(value.substring(1)).negated |
| 115 | + else if (value.endsWith(" ago")) /* only really applies to postgres_verbose, but shouldn't hurt */ |
| 116 | + format.parsePeriod(value.stripSuffix(" ago")).negated |
| 117 | + else |
| 118 | + format.parsePeriod(value) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments