/* * 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.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; /** * Created by aalexeev on 11/13/16. */ public class BaseJdbcService { @Inject JdbcTemplate jdbcTemplate; @Inject NamedParameterJdbcTemplate namedParameterJdbcTemplate; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlserver')}") private boolean omitRecursiveKeyword; @Value("#{('${spring.sql.init.platform}' == 'sqlite') or ('${spring.sql.init.platform}' == 'mysql')}") // Added in MariaDB 10.6 private boolean haveNoANSIFetch; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") private boolean haveNoDates; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") private boolean haveNoGreatest; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") private boolean haveNoForUpdate; @Value("#{new Boolean('${spring.sql.init.platform}' == 'mysql')}") private boolean haveNoOffsetDateTime; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlserver')}") private boolean haveOutput; @Value("#{new Boolean('${spring.sql.init.platform}' == 'h2')}") private boolean haveDeltaTables; public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { return namedParameterJdbcTemplate; } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public String withRecursive() { return omitRecursiveKeyword ? "WITH" : "WITH RECURSIVE"; } 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.getLong(columnIndex); if (date != 0) { return Instant.ofEpochMilli(date).atOffset(ZoneOffset.UTC); } 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 milliseconds; } if (haveNoOffsetDateTime) { return new Timestamp(milliseconds); } return Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC); } public Object toDateTime(OffsetDateTime now) { if (haveNoDates) { return now.toInstant().toEpochMilli(); } return now; } public int dateTimeType() { if (haveNoDates) { return Types.INTEGER; } if (haveNoOffsetDateTime) { return Types.TIMESTAMP; } return Types.TIMESTAMP_WITH_TIMEZONE; } public String returningId(String insertClause, String valuesClause, String idColumn) { if (haveOutput) { return insertClause + " OUTPUT inserted."+ idColumn + " " + valuesClause; } if (haveDeltaTables) { return "SELECT " + idColumn + " FROM FINAL TABLE (" + insertClause + " " + valuesClause + ")"; } return insertClause + " " + valuesClause + " RETURNING " + idColumn; } }