Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sqlite support #1770

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
https://stackoverflow.com/q/62263576/66686
-->
<oracle.version>23.3.0.23.09</oracle.version>
<sqlite.version>3.45.2.0</sqlite.version>

<!-- test utilities-->
<awaitility.version>4.2.0</awaitility.version>
Expand Down
7 changes: 7 additions & 0 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>${sqlite.version}</version>
<scope>test</scope>
</dependency>

<!-- Test dependencies -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
import org.springframework.data.jdbc.core.dialect.JdbcMySqlDialect;
import org.springframework.data.jdbc.core.dialect.JdbcPostgresDialect;
import org.springframework.data.jdbc.core.dialect.JdbcSqlServerDialect;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.H2Dialect;
import org.springframework.data.relational.core.dialect.HsqlDbDialect;
import org.springframework.data.relational.core.dialect.MariaDbDialect;
import org.springframework.data.relational.core.dialect.OracleDialect;
import org.springframework.data.relational.core.dialect.*;
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.util.Optionals;
import org.springframework.jdbc.core.ConnectionCallback;
Expand All @@ -50,6 +46,7 @@
* available {@link JdbcDialectProvider extensions}.
*
* @author Jens Schauder
* @author Rudolf Schmidt
* @since 2.0
* @see Dialect
* @see SpringFactoriesLoader
Expand Down Expand Up @@ -121,6 +118,9 @@ private static Dialect getDialect(Connection connection) throws SQLException {
if (name.contains("h2")) {
return H2Dialect.INSTANCE;
}
if (name.contains("sqlite")) {
return SQLiteDialect.INSTANCE;
}
if (name.contains("mysql")) {
return new JdbcMySqlDialect(getIdentifierProcessing(metaData));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.springframework.data.relational.core.dialect;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.relational.core.sql.LockOptions;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;

/**
* SQL Dialect for SQLite.
*
* @author Rudolf Schmidt
* @since 3.3.0
*/
public class SQLiteDialect extends AbstractDialect {

public static final SQLiteDialect INSTANCE = new SQLiteDialect();

private static final LimitClause LIMIT_CLAUSE = new LimitClause() {
@Override
public String getLimit(long limit) {
return String.format("limit %d", limit);
}

@Override
public String getOffset(long offset) {
throw new UnsupportedOperationException("offset alone not supported");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do better, like LIMIT -1 OFFSET %d. According to official documentation it is fine to set LIMIT to the negative value.

}

@Override
public String getLimitOffset(long limit, long offset) {
return String.format("limit %d offset %d", limit, offset);
}

@Override
public Position getClausePosition() {
return Position.AFTER_ORDER_BY;
}
};

private static final LockClause LOCK_CLAUSE = new LockClause() {
@Override
public String getLock(LockOptions lockOptions) {
return "";
}

@Override
public Position getClausePosition() {
return Position.AFTER_ORDER_BY;
}
};

private SQLiteDialect() {
}

@Override
public LimitClause limit() {
return LIMIT_CLAUSE;
}

@Override
public LockClause lock() {
return LOCK_CLAUSE;
}

@Override
public Collection<Object> getConverters() {
Collection<Object> converters = new ArrayList<>(super.getConverters());
converters.add(LocalDateTimeToNumericConverter.INSTANCE);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't tried this dialect, but since sqlite, doesn't support boolean tye the same as everyone else, we should probably add a conversion for this too

converters.add(NumericToLocalDateTimeConverter.INSTANCE);
return converters;
}

@WritingConverter
private enum LocalDateTimeToNumericConverter implements Converter<LocalDateTime, Long> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We need to handle other JSR310 types as well.
  2. Can we safely assign UTC as a timezone to the LocalDateTime? I do not think so. I think we should not introduce the converters for LocalDate/LocalTime/LocalDateTime here, since we can cause bugs for users. It is better for them that the framework would not handle these types out of the box, rather than saving them assuming the UTC


INSTANCE;

@Override
public Long convert(LocalDateTime source) {
return source.atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
}
}

@ReadingConverter
private enum NumericToLocalDateTimeConverter implements Converter<Long, LocalDateTime> {

INSTANCE;

@Override
public LocalDateTime convert(Long source) {
return Instant.ofEpochMilli(source).atZone(ZoneOffset.UTC).toLocalDateTime();
}
}
}