Skip to content
Merged
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
4 changes: 4 additions & 0 deletions api/src/main/java/me/zort/sqllib/api/Query.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.zort.sqllib.api;

import java.sql.SQLException;

/**
* This class represents a query.
* @author ZorTik
Expand All @@ -21,4 +23,6 @@ default boolean isAncestor() {
return getAncestor() == this;
}

default void errorSignal(SQLException e) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import lombok.Getter;

import java.util.ArrayList;
import java.util.LinkedList;

@Getter
public class QueryRowsResult<T> extends ArrayList<T> implements QueryResult {
public class QueryRowsResult<T> extends LinkedList<T> implements QueryResult {

private final boolean successful;
private String rejectMessage = null;
Expand Down
31 changes: 13 additions & 18 deletions core/src/main/java/me/zort/sqllib/SQLConnectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.zort.sqllib.internal.factory.SQLConnectionFactory;
import me.zort.sqllib.internal.impl.DefaultSQLEndpoint;
import me.zort.sqllib.internal.impl.SQLEndpointImpl;
import me.zort.sqllib.pool.SQLConnectionPool;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -16,6 +17,7 @@
import java.sql.SQLException;
import java.util.Objects;

@SuppressWarnings("unused")
public final class SQLConnectionBuilder implements Cloneable {

public static @NotNull SQLConnectionBuilder of(String address, int port, String database, String username, String password) {
Expand All @@ -27,15 +29,11 @@ public final class SQLConnectionBuilder implements Cloneable {
}

public static @NotNull SQLConnectionBuilder ofSQLite(String path) {
SQLConnectionBuilder builder = of(new SQLEndpointImpl("jdbc:sqlite:" + path, null, null));
builder.withDriver("org.sqlite.JDBC");
return builder;
return of(new SQLEndpointImpl("jdbc:sqlite:" + path, null, null)).withDriver("org.sqlite.JDBC");
}

public static SQLConnectionBuilder of(SQLEndpoint endpoint) {
if(!endpoint.isValid()) {
throw new SQLEndpointNotValidException(endpoint);
}
if(!endpoint.isValid()) throw new SQLEndpointNotValidException(endpoint);
return new SQLConnectionBuilder(endpoint);
}

Expand All @@ -53,9 +51,7 @@ public SQLConnectionBuilder(@NotNull String address, int port, @NotNull String d

public SQLConnectionBuilder(@Nullable SQLEndpoint endpoint) {
this.endpoint = endpoint;
this.jdbc = endpoint != null
? endpoint.buildJdbc()
: null;
this.jdbc = endpoint != null ? endpoint.buildJdbc() : null;
}

public @NotNull SQLConnectionBuilder withEndpoint(final SQLEndpoint endpoint) {
Expand All @@ -65,10 +61,7 @@ public SQLConnectionBuilder(@Nullable SQLEndpoint endpoint) {
}

public @NotNull SQLConnectionBuilder withParam(final @NotNull String key, final @NotNull String value) {
if (endpoint != null) {
jdbc += (jdbc.contains("?") ? "&" : "?");
jdbc += key + "=" + value;
}
if (endpoint != null) jdbc += (jdbc.contains("?") ? "&" : "?") + (key + "=" + value);
return this;
}

Expand All @@ -92,11 +85,13 @@ public SQLConnectionBuilder(@Nullable SQLEndpoint endpoint) {
driver = Constants.DEFAULT_DRIVER;
}
SQLConnectionFactory connectionFactory = new BuilderSQLConnectionFactory(this, driver);
if(jdbc.contains("jdbc:sqlite")) {
return new SQLiteDatabaseConnectionImpl(connectionFactory, options);
} else {
return new SQLDatabaseConnectionImpl(connectionFactory, options);
}
return jdbc.contains("jdbc:sqlite")
? new SQLiteDatabaseConnectionImpl(connectionFactory, options)
: new SQLDatabaseConnectionImpl(connectionFactory, options);
}

public @NotNull SQLConnectionPool createPool(final @NotNull SQLConnectionPool.Options options) {
return new SQLConnectionPool(this, options);
}

@Override
Expand Down
47 changes: 28 additions & 19 deletions core/src/main/java/me/zort/sqllib/SQLDatabaseConnection.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.zort.sqllib;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import me.zort.sqllib.api.Query;
import me.zort.sqllib.api.SQLConnection;
Expand All @@ -12,6 +13,8 @@
import me.zort.sqllib.internal.impl.QueryResultImpl;
import me.zort.sqllib.internal.query.*;
import me.zort.sqllib.internal.query.part.SetStatement;
import me.zort.sqllib.transaction.Transaction;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -41,6 +44,12 @@ public SQLDatabaseConnection(final @NotNull SQLConnectionFactory connectionFacto
SQLConnectionRegistry.register(this);
}

/**
* @deprecated Use {@link SQLDatabaseConnection#createProxy(Class)} instead.
*/
@Deprecated
public abstract <T> T createGate(Class<T> mappingInterface);

/**
* Constructs a mapping repository based on provided interface.
* The interface should follow rules for creating mapping repositories
Expand Down Expand Up @@ -72,8 +81,8 @@ public SQLDatabaseConnection(final @NotNull SQLConnectionFactory connectionFacto
* @return Mapping repository.
* @param <T> Type of mapping repository.
*/
public abstract <T> T createGate(Class<T> mappingInterface);
public abstract <T> T createGate(Class<T> mappingInterface, @NotNull StatementMappingOptions options);
public abstract <T> T createProxy(Class<T> mappingInterface);
public abstract <T> T createProxy(Class<T> mappingInterface, @NotNull StatementMappingOptions options);
public abstract boolean buildEntitySchema(String tableName, Class<?> entityClass);

/**
Expand Down Expand Up @@ -112,27 +121,21 @@ public SQLDatabaseConnection(final @NotNull SQLConnectionFactory connectionFacto
*/
public abstract QueryResult exec(Query query);
public abstract QueryResult exec(String query);
@ApiStatus.Experimental
public abstract Transaction beginTransaction();
@ApiStatus.Experimental
public abstract void closeTransaction();
@ApiStatus.Experimental
@Nullable
public abstract Transaction getTransaction();
public abstract boolean isTransactionActive();
protected abstract DefsVals buildDefsVals(Object obj);
public abstract boolean isLogSqlErrors();
public abstract boolean isDebug();

/**
* Saves this mapping object into database using upsert query.
* <p>
* All mapping strategies are described in:
* {@link SQLDatabaseConnection#query(Query, Class)}.
*
* @param table Table to save into.
* @param obj The object to save.
* @return Result of the query.
*/
// by default, it creates and upsert request.
public QueryResult save(final @NotNull String table, final @NotNull Object obj) {
DefsVals defsVals = buildDefsVals(obj);

if(defsVals == null) return new QueryResultImpl(false);

return save(obj).table(table).execute();
public UpsertQuery save(final @NotNull String table, final @NotNull Object obj) {
if(buildDefsVals(obj) == null) throw new IllegalArgumentException("Cannot create save query! (defsVals == null)");
return save(obj).table(table);
}

public UpsertQuery save(final @NotNull Object obj) {
Expand Down Expand Up @@ -240,4 +243,10 @@ protected static class DefsVals {
private final String[] defs;
private final SQLDatabaseConnectionImpl.UnknownValueWrapper[] vals;
}

@AllArgsConstructor
@Data
public static class UnknownValueWrapper {
private Object object;
}
}
Loading