Skip to content

Commit

Permalink
Change to ansi-sql default limit syntax. MariaDB Customization (doing…
Browse files Browse the repository at this point in the history
… nothing)
  • Loading branch information
kagkarlsson committed Aug 27, 2023
1 parent f35cf2e commit 7e05fd0
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class AutodetectJdbcCustomization implements JdbcCustomization {
public static final String POSTGRESQL = "PostgreSQL";
public static final String ORACLE = "Oracle";
public static final String MYSQL = "MySQL";
public static final String MARIADB = "MariaDB";
private final JdbcCustomization jdbcCustomization;

public AutodetectJdbcCustomization(DataSource dataSource) {
Expand All @@ -50,26 +51,21 @@ public AutodetectJdbcCustomization(DataSource dataSource) {
} else if (databaseProductName.contains(ORACLE)) {
LOG.info("Using Oracle jdbc-overrides.");
detectedCustomization = new OracleJdbcCustomization();
} else if (databaseProductName.contains(MARIADB)) {
LOG.info("Using MariaDB jdbc-overrides.");
detectedCustomization = new MariaDBJdbcCustomization();
} else if (databaseProductName.contains(MYSQL)) {
int databaseMajorVersion = c.getMetaData().getDatabaseMajorVersion();
String databaseProductVersion = c.getMetaData().getDatabaseProductVersion();
// FIXLATER: fix syntax for FOR UPDATE SKIP LOCKED for mysql and enable Customization
// supporting it
// if (databaseMajorVersion >= 8) {
// LOG.info(
// "Using MySQL jdbc-overrides version 8 and later. (version is {})",
// databaseProductVersion);
// detectedCustomization = new MySQL8JdbcCustomization();
// } else {
LOG.info(
"Using MySQL jdbc-overrides for version older than 8. (version is {})",
databaseProductVersion);
detectedCustomization = new MySQLJdbcCustomization();
// }
String dbVersion = c.getMetaData().getDatabaseProductVersion();
if (databaseMajorVersion >= 8) {
LOG.info("Using MySQL jdbc-overrides version 8 and later. (v {})", dbVersion);
detectedCustomization = new MySQL8JdbcCustomization();
} else {
LOG.info("Using MySQL jdbc-overrides for version older than 8. (v {})", dbVersion);
detectedCustomization = new MySQLJdbcCustomization();
}
}

// TODO: add mariadb

} catch (SQLException e) {
LOG.error("Failed to detect database via getDatabaseMetadata. Using default.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public boolean supportsExplicitQueryLimitPart() {

@Override
public String getQueryLimitPart(int limit) {
return Queries.postgresSqlLimitPart(limit);
return Queries.ansiSqlLimitPart(limit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) Gustav Karlsson
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.kagkarlsson.scheduler.jdbc;

import static com.github.kagkarlsson.scheduler.jdbc.Queries.selectForUpdate;

public class MariaDBJdbcCustomization extends DefaultJdbcCustomization {

@Override
public String getName() {
return "MariaDB";
}

@Override
public String getQueryLimitPart(int limit) {
return Queries.postgresSqlLimitPart(limit);
}

@Override
public boolean supportsGenericLockAndFetch() {
// FIXLATER: enable for versions of MariaDB that supports it
return false;
}

@Override
public String createGenericSelectForUpdateQuery(
String tableName, int limit, String requiredAndCondition) {
return selectForUpdate(
tableName,
Queries.postgresSqlLimitPart(limit),
requiredAndCondition,
" FOR UPDATE SKIP LOCKED ",
null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public void setInstant(PreparedStatement p, int index, Instant value) throws SQL
Calendar.getInstance(TimeZone.getTimeZone("UTC")));
}

@Override
public String getQueryLimitPart(int limit) {
return Queries.ansiSqlLimitPart(limit);
}

@Override
public boolean supportsGenericLockAndFetch() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ public String getName() {
return "MySQL";
}

@Override
public String getQueryLimitPart(int limit) {
return Queries.ansiSqlLimitPart(limit);
}

@Override
public boolean supportsGenericLockAndFetch() {
return true;
// FIXLATER: enable when mysql-syntax fixed
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public class MySQLJdbcCustomization extends DefaultJdbcCustomization {
public String getName() {
return "MySQL";
}

@Override
public String getQueryLimitPart(int limit) {
return Queries.postgresSqlLimitPart(limit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package com.github.kagkarlsson.scheduler.jdbc;

import static com.github.kagkarlsson.scheduler.StringUtils.truncate;
import static com.github.kagkarlsson.scheduler.jdbc.Queries.postgresSqlLimitPart;
import static com.github.kagkarlsson.scheduler.jdbc.Queries.selectForUpdate;

import com.github.kagkarlsson.scheduler.task.Execution;
Expand All @@ -37,6 +36,11 @@ public String getName() {
return "PostgreSQL";
}

@Override
public String getQueryLimitPart(int limit) {
return Queries.postgresSqlLimitPart(limit);
}

@Override
public boolean supportsSingleStatementLockAndFetch() {
return !useGenericLockAndFetch;
Expand All @@ -52,7 +56,7 @@ public String createGenericSelectForUpdateQuery(
String tableName, int limit, String requiredAndCondition) {
return selectForUpdate(
tableName,
postgresSqlLimitPart(limit),
getQueryLimitPart(limit),
requiredAndCondition,
" FOR UPDATE SKIP LOCKED ",
null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Properties;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
Expand All @@ -16,7 +15,6 @@

@Tag("compatibility")
@Testcontainers
@Disabled // FIXLATER: enable when SKIP LOCKED is fixed for mysql
public class Mysql8CompatibilityTest extends CompatibilityTest {

@Container
Expand All @@ -25,8 +23,9 @@ public class Mysql8CompatibilityTest extends CompatibilityTest {

private static HikariDataSource pooledDatasource;

// FIXLATER: enable select-for-update tests when SKIP LOCKED is fixed for mysql
public Mysql8CompatibilityTest() {
super(true);
super(false);
}

@BeforeAll
Expand Down

0 comments on commit 7e05fd0

Please sign in to comment.