From d2a434f37a05ae189e985c7486994309f6ddef4c Mon Sep 17 00:00:00 2001 From: "Barry M. Caceres" Date: Thu, 29 Feb 2024 08:45:03 -0800 Subject: [PATCH] - Added `sqlLeast` and `sqlGreatest` functions to `com.senzing.sql.DatabaseType` (#85) - Updated depdendncies to newer versions: - Upgraded `junit-jupiter` from version `5.10.1` to `5.10.2` - Upgraded `maven-surefire-plugin` from version `3.2.3` to version `3.2.5` --- CHANGELOG.md | 7 ++ pom.xml | 6 +- .../java/com/senzing/sql/DatabaseType.java | 77 +++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4edd9f0..2c4b8fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), [markdownlint](https://dlaa.me/markdownlint/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.2.0] - 2024-02-29 + +### Changed in 3.2.0 + +- Added `sqlLeast` and `sqlGreatest` functions to `com.senzing.sql.DatabaseType` +- Updated depdendncies to newer versions: + ## [3.1.5] - 2024-01-10 ### Changed in 3.1.5 diff --git a/pom.xml b/pom.xml index aa9365d..92558f7 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.senzing senzing-commons jar - 3.1.5 + 3.2.0 Senzing Commons Utility classes and functions common to multiple Senzing projects. http://github.com/senzing-garage/senzing-commons-java @@ -71,7 +71,7 @@ org.junit.jupiter junit-jupiter - 5.10.1 + 5.10.2 test @@ -107,7 +107,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.2.3 + 3.2.5 ${project.build.directory} diff --git a/src/main/java/com/senzing/sql/DatabaseType.java b/src/main/java/com/senzing/sql/DatabaseType.java index 8af95d5..83e5df3 100644 --- a/src/main/java/com/senzing/sql/DatabaseType.java +++ b/src/main/java/com/senzing/sql/DatabaseType.java @@ -1,5 +1,6 @@ package com.senzing.sql; +import java.util.Objects; import java.sql.*; import java.time.Instant; import java.time.ZoneId; @@ -148,6 +149,82 @@ public void setTimestamp(CallableStatement cs, int index, Timestamp value) } } + /** + * Provides a generic way to format the equivalent of the SQL + * "least" function for the database type. + * + * @param first The first parameter to the LEAST function. + * @param second The second parameter to the LEAST function. + * @param other The optional other parameters to the LEAST + * function for variable length parameters. + * @return The formatted SQL "least" function for the respective + * database. + * @throws NullPointerException If either the first or second + * parameters is null. + */ + public String sqlLeast(String first, String second, String... other) + throws NullPointerException + { + Objects.requireNonNull(first, + "The first parameter to LEAST cannot be null"); + Objects.requireNonNull(first, + "The second parameter to LEAST cannot be null"); + StringBuilder sb = new StringBuilder(); + switch (this) { + case SQLITE: + sb.append("MIN"); + break; + default: + sb.append("LEAST"); + } + sb.append("(").append(first).append(", ").append(second); + if (other != null) { + for (String param: other) { + sb.append(", ").append(param); + } + } + sb.append(")"); + return sb.toString(); + } + + /** + * Provides a generic way to format the equivalent of the SQL + * "least" function for the database type. + * + * @param first The first parameter to the LEAST function. + * @param second The second parameter to the LEAST function. + * @param other The optional other parameters to the LEAST + * function for variable length parameters. + * @return The formatted SQL "least" function for the respective + * database. + * @throws NullPointerException If either the first or second + * parameters is null. + */ + public String sqlGreatest(String first, String second, String... other) + throws NullPointerException + { + Objects.requireNonNull(first, + "The first parameter to GREATEST cannot be null"); + Objects.requireNonNull(first, + "The second parameter to GREATEST cannot be null"); + StringBuilder sb = new StringBuilder(); + switch (this) { + case SQLITE: + sb.append("MAX"); + break; + default: + sb.append("GREATEST"); + } + sb.append("(").append(first).append(", ").append(second); + if (other != null) { + for (String param: other) { + sb.append(", ").append(param); + } + } + sb.append(")"); + return sb.toString(); + } + /** * The {@link Calendar} to use for retrieving timestamps from the database. */