/* * Copyright (C) 2008-2023, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package com.juick.service; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import javax.inject.Inject; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Types; import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; /** * Created by aalexeev on 11/13/16. */ public class BaseJdbcService { @Inject JdbcTemplate jdbcTemplate; @Inject NamedParameterJdbcTemplate namedParameterJdbcTemplate; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlserver')}") protected boolean omitRecursiveKeyword; @Value("#{('${spring.sql.init.platform}' == 'sqlite') or ('${spring.sql.init.platform}' == 'mysql')}") // Added in MariaDB 10.6 protected boolean haveNoANSIFetch; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") protected boolean haveNoDates; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") protected boolean haveNoGreatest; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") protected boolean haveNoForUpdate; @Value("#{new Boolean('${spring.sql.init.platform}' == 'mysql')}") protected boolean haveNoOffsetDateTime; public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { return namedParameterJdbcTemplate; } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } protected String limit(int rows) { if (haveNoANSIFetch) { return "LIMIT " + rows; } else { return "OFFSET 0 ROWS FETCH NEXT " + rows + " ROWS ONLY"; } } protected String greatest() { if (haveNoGreatest) { return "MAX"; } return "GREATEST"; } protected String forUpdate() { if (haveNoForUpdate) { return ""; } return "FOR UPDATE"; } public OffsetDateTime getOffsetDateTime(ResultSet rs, int columnIndex) throws SQLException { if (haveNoDates) { var date = rs.getString(columnIndex); if (StringUtils.isNotEmpty(date)) { return OffsetDateTime.parse(date); } return null; } if (haveNoOffsetDateTime) { var date = rs.getTimestamp(columnIndex); if (date != null) { return date.toInstant().atOffset(ZoneOffset.UTC); } return null; } return rs.getObject(columnIndex, OffsetDateTime.class); } public Object fromEpochMilli(Long milliseconds) { if (haveNoDates) { return Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC) .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); } if (haveNoOffsetDateTime) { return new Timestamp(milliseconds); } return Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC); } public int dateTimeType() { if (haveNoDates) { return Types.VARCHAR; } if (haveNoOffsetDateTime) { return Types.TIMESTAMP; } return Types.TIMESTAMP_WITH_TIMEZONE; } }