Skip to content

Commit

Permalink
Merge pull request #43 from ZorTik/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
ZorTik authored Sep 16, 2023
2 parents 82e495e + 0434e9a commit 99c396c
Show file tree
Hide file tree
Showing 45 changed files with 720 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.zort.sqllib.internal.annotation;

import org.jetbrains.annotations.ApiStatus;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -25,6 +27,7 @@
*
* @author ZorTik
*/
@ApiStatus.Experimental
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface LinkedOne {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static void debug(Connection connection, String message) {
private static class RegistryCodeHandler implements SQLDatabaseConnection.CodeObserver {
private final SQLDatabaseConnection connection;

// This will ensure that only connected instances are present in the registry
@Override
public void onNotified(int code) {
if (code == SQLDatabaseConnection.Code.CONNECTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import me.zort.sqllib.mapping.DefaultStatementMappingFactory;
import me.zort.sqllib.mapping.MappingRegistryImpl;
import me.zort.sqllib.mapping.ProxyInstanceImpl;
import me.zort.sqllib.model.schema.DatabaseSchemaBuilder;
import me.zort.sqllib.model.schema.EntitySchemaBuilder;
import me.zort.sqllib.model.schema.SQLSchemaSynchronizer;
import me.zort.sqllib.model.builder.DatabaseSchemaBuilder;
import me.zort.sqllib.model.builder.EntitySchemaBuilder;
import me.zort.sqllib.model.SQLSchemaSynchronizer;
import me.zort.sqllib.pool.PooledSQLDatabaseConnection;
import me.zort.sqllib.transaction.Transaction;
import me.zort.sqllib.util.Validator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import me.zort.sqllib.api.ISQLDatabaseOptions;
import me.zort.sqllib.api.options.NamingStrategy;
import me.zort.sqllib.internal.Defaults;
import me.zort.sqllib.naming.SymbolSeparatedNamingStrategy;
import org.jetbrains.annotations.NotNull;

@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import me.zort.sqllib.internal.factory.SQLConnectionFactory;
import me.zort.sqllib.internal.query.*;
import me.zort.sqllib.internal.query.part.SetStatement;
import me.zort.sqllib.model.column.SQLiteColumnQueryBuilder;
import me.zort.sqllib.model.column.SQLiteColumnTypeAdjuster;
import me.zort.sqllib.model.schema.SQLSchemaSynchronizer;
import me.zort.sqllib.model.builder.SQLiteColumnQueryBuilder;
import me.zort.sqllib.model.adjuster.SQLiteColumnTypeAdjuster;
import me.zort.sqllib.model.SQLSchemaSynchronizer;
import me.zort.sqllib.util.PrimaryKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
4 changes: 2 additions & 2 deletions asql-core/src/main/java/me/zort/sqllib/internal/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.zort.sqllib.api.options.NamingStrategy;
import me.zort.sqllib.naming.SymbolSeparatedNamingStrategy;
import me.zort.sqllib.naming.SnakeCaseNamingStrategy;

public final class Defaults {

public static final String DEFAULT_DRIVER = "com.mysql.jdbc.Driver";
public static final boolean DEFAULT_AUTO_RECONNECT = true;
public static final boolean DEFAULT_DEBUG = false;
public static final boolean DEFAULT_LOG_SQL_ERRORS = true;
public static final NamingStrategy DEFAULT_NAMING_STRATEGY = new SymbolSeparatedNamingStrategy('_');
public static final NamingStrategy DEFAULT_NAMING_STRATEGY = new SnakeCaseNamingStrategy('_');

public static final Gson DEFAULT_GSON = new GsonBuilder()
.enableComplexMapKeySerialization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public Object obtainValue(
String convertedName,
Type type
) {
if (!(element instanceof Parameter) || !(((Parameter) element).getDeclaringExecutable() instanceof Constructor))
if (!(element instanceof Parameter) || !(((Parameter) element).getDeclaringExecutable() instanceof Constructor)) {
return null;
}

Parameter p = (Parameter) element;
Matcher matcher = argumentPattern.matcher(p.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.zort.sqllib.internal.query;

import java.util.ArrayList;
import java.util.List;

/**
* Top-level query node that is used to build a query.
*
* @author ZorTik
*/
public abstract class AncestorQueryNode extends QueryNode<QueryNode<?>> {
public AncestorQueryNode() {
this(new ArrayList<>());
}
public AncestorQueryNode(List<QueryNode<?>> initial) {
super(null, initial, QueryPriority.GENERAL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import me.zort.sqllib.api.Executive;
import me.zort.sqllib.SQLDatabaseConnection;
import me.zort.sqllib.internal.query.part.LimitStatement;
import me.zort.sqllib.internal.query.part.OffsetStatement;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Objects;

public class DeleteQuery extends QueryNode<QueryNode<?>> implements Executive, Conditional<DeleteQuery>, Limitable<DeleteQuery> {
public class DeleteQuery extends AncestorQueryNode implements Executive, Conditional<DeleteQuery>, Limitable<DeleteQuery> {

private String table;

Expand All @@ -25,7 +26,7 @@ public DeleteQuery(@Nullable SQLDatabaseConnection connection) {
}

public DeleteQuery(@Nullable SQLDatabaseConnection connection, @Nullable String table) {
super(null, new ArrayList<>(), QueryPriority.GENERAL);
super(new ArrayList<>());
this.table = table;
this.connection = connection;
}
Expand All @@ -35,6 +36,12 @@ public DeleteQuery from(String table) {
return this;
}

@Override
public DeleteQuery offset(int offset) {
then(new OffsetStatement<>(this, new ArrayList<>(), offset));
return this;
}

public DeleteQuery limit(int limit) {
then(new LimitStatement<>(this, new ArrayList<>(), limit));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.ArrayList;
import java.util.Objects;

public class InsertQuery extends QueryNode<QueryNode<?>> implements Executive, Conditional<InsertQuery> {
public class InsertQuery extends AncestorQueryNode implements Executive, Conditional<InsertQuery> {

@Getter
private String table;
Expand All @@ -31,7 +31,7 @@ public InsertQuery(@Nullable SQLDatabaseConnection connection) {
}

public InsertQuery(@Nullable SQLDatabaseConnection connection, @Nullable String table) {
super(null, new ArrayList<>(), QueryPriority.GENERAL);
super(new ArrayList<>());
this.table = table;
this.connection = connection;
this.defs = new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public interface Limitable<P extends QueryNode<?> & Limitable<P>> { // P = self

P offset(int offset);
P limit(int limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import me.zort.sqllib.api.data.Row;
import me.zort.sqllib.internal.exception.InvalidConnectionInstanceException;
import me.zort.sqllib.internal.exception.NoLinkedConnectionException;
import me.zort.sqllib.util.Pair;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.sql.Connection;
Expand All @@ -19,8 +21,9 @@
import java.util.function.Function;

/**
* Represents a query builder node, a part of a
* query builder flow.
* Represents a query builder node, a part of a query builder flow.
* This is a tree structure node, each node can represent part in a query that is prepared
* to be joined in a final query.
*
* @param <P> Parent node type.
* @author ZorTik
Expand Down Expand Up @@ -61,6 +64,7 @@ public PreparedStatement prepare(Connection connection) throws SQLException {
return details.remove(buildQuery()).prepare(connection);
}

@ApiStatus.Internal
@Override
public String buildQuery() {
QueryDetails queryDetails = buildQueryDetails();
Expand All @@ -73,6 +77,7 @@ public String buildQuery() {
return uuid;
}

@ApiStatus.Internal
public QueryDetails buildInnerQuery() {
List<QueryNode<?>> children = new ArrayList<>(this.children);
children.sort(Comparator.comparingInt(QueryNode::getPriority));
Expand Down Expand Up @@ -194,6 +199,11 @@ private void debug(String message) {
}
}

@SuppressWarnings("unused")
public Pair<String, Object[]> toPreparedQuery() {
return getAncestor().buildQueryDetails().buildStatementDetails();
}

public String toString() {
return "QueryNode{details=" + buildQueryDetails().toString() + "}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import me.zort.sqllib.api.Executive;
import me.zort.sqllib.SQLDatabaseConnection;
import me.zort.sqllib.internal.query.part.LimitStatement;
import me.zort.sqllib.internal.query.part.OffsetStatement;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class SelectQuery extends QueryNode<QueryNode<?>> implements Executive, Conditional<SelectQuery>, Limitable<SelectQuery>, ResultSetAware {
/**
* Represents a SELECT query.
*/
public class SelectQuery extends AncestorQueryNode implements
Executive, Conditional<SelectQuery>, Limitable<SelectQuery>, ResultSetAware {

private final List<String> cols;
private String table;
Expand All @@ -28,7 +33,7 @@ public SelectQuery(@Nullable SQLDatabaseConnection connection, String... cols) {
}

public SelectQuery(@Nullable SQLDatabaseConnection connection, @Nullable String table, List<String> cols) {
super(null, new ArrayList<>(), QueryPriority.GENERAL);
super(new ArrayList<>());
this.table = table;
this.cols = cols;
this.connection = connection;
Expand All @@ -39,6 +44,12 @@ public SelectQuery from(String table) {
return this;
}

@Override
public SelectQuery offset(int offset) {
then(new OffsetStatement<>(this, new ArrayList<>(), offset));
return this;
}

public SelectQuery limit(int limit) {
then(new LimitStatement<>(this, new ArrayList<>(), limit));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import java.util.ArrayList;
import java.util.Objects;

public class UpdateQuery extends QueryNode<QueryNode<?>> implements Executive, Conditional<UpdateQuery> {
/**
* Represents a UPDATE query.
*/
public class UpdateQuery extends AncestorQueryNode implements Executive, Conditional<UpdateQuery> {

private String table;

Expand All @@ -26,7 +29,7 @@ public UpdateQuery(@Nullable SQLDatabaseConnection connection) {
}

public UpdateQuery(@Nullable SQLDatabaseConnection connection, @Nullable String table) {
super(null, new ArrayList<>(), QueryPriority.GENERAL.getPrior());
super(new ArrayList<>());
this.table = table;
this.connection = connection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import java.util.HashMap;

/**
* Represents an upsert (update or insert) query.
*/
public class UpsertQuery extends InsertQuery {

@Setter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.zort.sqllib.internal.query.part;

import me.zort.sqllib.internal.query.QueryDetails;
import me.zort.sqllib.internal.query.QueryNode;
import me.zort.sqllib.internal.query.ResultSetAware;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;

public class OffsetStatement<P extends QueryNode<?>> extends QueryNode<P> implements ResultSetAware {

private final int offset;

public OffsetStatement(@Nullable P parent, List<QueryNode<?>> initial, int offset) {
super(parent, initial, Integer.MAX_VALUE);
this.offset = offset;
}

@Override
public QueryDetails buildQueryDetails() {
return new QueryDetails(" OFFSET " + Math.max(offset, 0), new HashMap<>());
}

@SuppressWarnings("unchecked")
@Override
public OffsetStatement<P> then(String part) {
return (OffsetStatement<P>) super.then(part);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import me.zort.sqllib.api.model.TableSchema;
import me.zort.sqllib.api.options.NamingStrategy;
import me.zort.sqllib.mapping.annotation.Table;
import me.zort.sqllib.model.schema.EntitySchemaBuilder;
import me.zort.sqllib.model.builder.EntitySchemaBuilder;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
Expand All @@ -19,11 +20,15 @@

class Util {
@Nullable
public static String getFromContext(Method method, @Nullable ParameterPair[] parameters) {
public static String getFromContext(AnnotatedElement element, @Nullable ParameterPair[] parameters) {
PlaceholderMapper mapper = new PlaceholderMapper(parameters != null ? parameters : new ParameterPair[0]);
if (method.isAnnotationPresent(Table.class)) {
return mapper.assignValues(method.getAnnotation(Table.class).value());
} else if (method.getDeclaringClass().isAnnotationPresent(Table.class)) {
if (element.isAnnotationPresent(Table.class)) {
return mapper.assignValues(element.getAnnotation(Table.class).value());
} else if (!(element instanceof Method)) {
throw new SQLMappingException("Element " + element.toString() + " is not suitable for @Table check!", null, null);
}
Method method = (Method) element;
if (method.getDeclaringClass().isAnnotationPresent(Table.class)) {
return mapper.assignValues(method.getDeclaringClass().getAnnotation(Table.class).value());
} else {
throw new SQLMappingException("Method " + method.getName() + " in class " + method.getDeclaringClass().getSimpleName() + " requires @Table annotation", method, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.zort.sqllib.model.column;
package me.zort.sqllib.model;

import me.zort.sqllib.api.model.ColumnDefinition;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.zort.sqllib.model.column;
package me.zort.sqllib.model;

public interface SQLColumnTypeAdjuster {

Expand Down
Loading

0 comments on commit 99c396c

Please sign in to comment.