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 1 commit
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
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 Down Expand Up @@ -121,6 +117,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
Expand Up @@ -33,6 +33,7 @@
* resolution uses Spring's {@link SpringFactoriesLoader spring.factories} to determine available extensions.
*
* @author Mark Paluch
* @author Rudolf Schmidt
* @see R2dbcDialect
* @see SpringFactoriesLoader
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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 SQLiteDialect() {
}

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

@Override
public String getOffset(final long offset) {
throw new UnsupportedOperationException("offset alone not supported");
}

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

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

@Override
public LockClause lock() {
return new LockClause() {
@Override
public String getLock(final LockOptions lockOptions) {
return "with lock";
}

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

@Override
public Collection<Object> getConverters() {
final var 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(final LocalDateTime source) {
return source.atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
}
}

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

INSTANCE;

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