aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service/BaseJdbcService.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/service/BaseJdbcService.java')
-rw-r--r--src/main/java/com/juick/service/BaseJdbcService.java80
1 files changed, 79 insertions, 1 deletions
diff --git a/src/main/java/com/juick/service/BaseJdbcService.java b/src/main/java/com/juick/service/BaseJdbcService.java
index 51c41251..d6f29283 100644
--- a/src/main/java/com/juick/service/BaseJdbcService.java
+++ b/src/main/java/com/juick/service/BaseJdbcService.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2020, Juick
+ * 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
@@ -17,10 +17,20 @@
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.
@@ -30,6 +40,19 @@ public class BaseJdbcService {
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;
@@ -38,4 +61,59 @@ public class BaseJdbcService {
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;
+ }
}