|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * This code is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms of the GNU General Public License version 2 only, as |
| 6 | + * published by the Free Software Foundation. Codename One designates this |
| 7 | + * particular file as subject to the "Classpath" exception as provided |
| 8 | + * by Oracle in the LICENSE file that accompanied this code. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Codename One through http://www.codenameone.com/ if you |
| 21 | + * need additional information or have any questions. |
| 22 | + */ |
| 23 | +package com.codename1.impl.orm; |
| 24 | + |
| 25 | +import com.codename1.orm.session.AttributeConverter; |
| 26 | + |
| 27 | +import java.time.Instant; |
| 28 | +import java.time.LocalDate; |
| 29 | +import java.time.LocalDateTime; |
| 30 | +import java.time.LocalTime; |
| 31 | + |
| 32 | +/// The storage of `java.time.Instant`, `LocalDate` and `LocalDateTime` fields. |
| 33 | +/// |
| 34 | +/// The defaults follow `java.util.Date`'s: a number in an integer column, which |
| 35 | +/// sorts, compares and indexes the same on every engine. An Instant or a |
| 36 | +/// LocalDateTime is milliseconds since 1970-01-01T00:00 -- UTC for the Instant, |
| 37 | +/// the wall clock itself for the LocalDateTime, which has no zone to convert |
| 38 | +/// through -- and a LocalDate is the day count since 1970-01-01. So an Instant |
| 39 | +/// reads a column a `long createdAt` or a `Date` wrote, unchanged. |
| 40 | +/// |
| 41 | +/// `@Temporal` opts into a native column instead, through the same UTC ISO text |
| 42 | +/// `TemporalConverter` writes: `TIMESTAMP` for an Instant or a LocalDateTime, |
| 43 | +/// `DATE` for a LocalDate. |
| 44 | +/// |
| 45 | +/// Precision is the millisecond, as for a Date: nanoseconds below it are dropped |
| 46 | +/// toward the earlier instant on write. |
| 47 | +/// |
| 48 | +/// Internal ORM runtime; not an application API. |
| 49 | +/// @hidden |
| 50 | +@com.codename1.impl.SharedWithBackend |
| 51 | +public final class JavaTimeConverter { |
| 52 | + private static final long DAY = 86400000L; |
| 53 | + |
| 54 | + private JavaTimeConverter() { |
| 55 | + } |
| 56 | + |
| 57 | + /// An Instant as epoch milliseconds, the default. |
| 58 | + /// @hidden |
| 59 | + public static final class InstantMillis implements AttributeConverter<Instant, Long> { |
| 60 | + @Override |
| 61 | + public Long toDatabase(Instant value) { |
| 62 | + return value == null ? null : Long.valueOf(value.toEpochMilli()); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Instant fromDatabase(Long value) { |
| 67 | + return value == null ? null : Instant.ofEpochMilli(value.longValue()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// An Instant in a native timestamp column, `@Temporal(TemporalType.TIMESTAMP)`. |
| 72 | + /// @hidden |
| 73 | + public static final class InstantTimestamp implements AttributeConverter<Instant, String> { |
| 74 | + private final TemporalConverter text = new TemporalConverter.Timestamp(); |
| 75 | + |
| 76 | + @Override |
| 77 | + public String toDatabase(Instant value) { |
| 78 | + return value == null ? null : text.toDatabase(new java.util.Date(value.toEpochMilli())); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Instant fromDatabase(String value) { |
| 83 | + return value == null ? null : Instant.ofEpochMilli(TemporalConverter.parse(value)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// A LocalDate as the day count since 1970-01-01, the default. |
| 88 | + /// @hidden |
| 89 | + public static final class LocalDateEpochDay implements AttributeConverter<LocalDate, Long> { |
| 90 | + @Override |
| 91 | + public Long toDatabase(LocalDate value) { |
| 92 | + return value == null ? null : Long.valueOf(value.toEpochDay()); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public LocalDate fromDatabase(Long value) { |
| 97 | + return value == null ? null : LocalDate.ofEpochDay(value.longValue()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// A LocalDate in a native date column, `@Temporal(TemporalType.DATE)`. |
| 102 | + /// @hidden |
| 103 | + public static final class LocalDateDay implements AttributeConverter<LocalDate, String> { |
| 104 | + private final TemporalConverter text = new TemporalConverter.Day(); |
| 105 | + |
| 106 | + @Override |
| 107 | + public String toDatabase(LocalDate value) { |
| 108 | + return value == null ? null : text.toDatabase(new java.util.Date(value.toEpochDay() * DAY)); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public LocalDate fromDatabase(String value) { |
| 113 | + return value == null ? null : LocalDate.ofEpochDay(floorDiv(TemporalConverter.parse(value), DAY)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// A LocalDateTime's wall clock as milliseconds since 1970-01-01T00:00, the |
| 118 | + /// default. |
| 119 | + /// @hidden |
| 120 | + public static final class LocalDateTimeMillis implements AttributeConverter<LocalDateTime, Long> { |
| 121 | + @Override |
| 122 | + public Long toDatabase(LocalDateTime value) { |
| 123 | + return value == null ? null : Long.valueOf(millis(value)); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public LocalDateTime fromDatabase(Long value) { |
| 128 | + return value == null ? null : wallClock(value.longValue()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// A LocalDateTime in a native timestamp column, |
| 133 | + /// `@Temporal(TemporalType.TIMESTAMP)`. The wall clock is written as is; the |
| 134 | + /// backend's UTC session means no zone ever shifts it. |
| 135 | + /// @hidden |
| 136 | + public static final class LocalDateTimeTimestamp implements AttributeConverter<LocalDateTime, String> { |
| 137 | + private final TemporalConverter text = new TemporalConverter.Timestamp(); |
| 138 | + |
| 139 | + @Override |
| 140 | + public String toDatabase(LocalDateTime value) { |
| 141 | + return value == null ? null : text.toDatabase(new java.util.Date(millis(value))); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public LocalDateTime fromDatabase(String value) { |
| 146 | + return value == null ? null : wallClock(TemporalConverter.parse(value)); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /// Milliseconds of a wall clock, refusing what a long cannot hold: a |
| 151 | + /// LocalDateTime reaches the year 999999999, and `LocalDateTime.MAX` as a |
| 152 | + /// "no bound" value would otherwise wrap to a plausible wrong time. An |
| 153 | + /// Instant needs no such check; its toEpochMilli throws by itself. |
| 154 | + static long millis(LocalDateTime value) { |
| 155 | + long days = value.toLocalDate().toEpochDay(); |
| 156 | + if (days > Long.MAX_VALUE / DAY - 1 || days < Long.MIN_VALUE / DAY + 1) { |
| 157 | + throw new IllegalArgumentException("A LocalDateTime does not fit in epoch milliseconds: " + value); |
| 158 | + } |
| 159 | + return days * DAY + value.toLocalTime().toNanoOfDay() / 1000000L; |
| 160 | + } |
| 161 | + |
| 162 | + static LocalDateTime wallClock(long millis) { |
| 163 | + long days = floorDiv(millis, DAY); |
| 164 | + return LocalDateTime.of(LocalDate.ofEpochDay(days), LocalTime.ofNanoOfDay((millis - days * DAY) * 1000000L)); |
| 165 | + } |
| 166 | + |
| 167 | + private static long floorDiv(long value, long divisor) { |
| 168 | + long quotient = value / divisor; |
| 169 | + return value % divisor != 0 && (value < 0) != (divisor < 0) ? quotient - 1 : quotient; |
| 170 | + } |
| 171 | +} |
0 commit comments