Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions src/main/java/org/javawebstack/orm/TableInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class TableInfo {
private String relationField;
private final Map<String, String> filterable = new HashMap<>();
private final List<String> searchable = new ArrayList<>();
private final List<Index> indices = new ArrayList<>();

private static final Class<?>[] appliesDefaultSize = {
String.class,
Expand Down Expand Up @@ -105,6 +106,14 @@ private void analyzeTable(Class<? extends Model> model) throws ORMConfigurationE
if (!fields.containsKey(dates.update()))
throw new ORMConfigurationException("Missing dates field '" + dates.update() + "'");
}
if (model.isAnnotationPresent(Index.class)) {
Index[] unfilteredIndices = model.getDeclaredAnnotationsByType(Index.class);
for (Index index : unfilteredIndices) {
if (index.value().length == 0)
continue;
indices.add(index);
}
}
}

private void analyzeColumns(Class<? extends Model> model) throws ORMConfigurationException {
Expand Down Expand Up @@ -292,4 +301,7 @@ public String getRelationField() {
return relationField;
}

public List<Index> getIndices() {
return indices;
}
}
19 changes: 19 additions & 0 deletions src/main/java/org/javawebstack/orm/annotation/Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.javawebstack.orm.annotation;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(Indices.class)
public @interface Index {
Type type() default Type.AUTO;
boolean unique() default false;
String id() default "";
String[] value() default {};

enum Type {
AUTO,
BTREE,
HASH
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/javawebstack/orm/annotation/Indices.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.javawebstack.orm.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Indices {
Comment thread
x7airworker marked this conversation as resolved.
Resolved
Index[] value();
}
41 changes: 41 additions & 0 deletions src/main/java/org/javawebstack/orm/migration/AutoMigrator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.javawebstack.orm.migration;

import jdk.internal.joptsimple.internal.Strings;
Comment thread
x7airworker marked this conversation as resolved.
Resolved
import org.javawebstack.orm.Repo;
import org.javawebstack.orm.TableInfo;
import org.javawebstack.orm.annotation.Index;
import org.javawebstack.orm.exception.ORMQueryException;
import org.javawebstack.orm.wrapper.SQL;

Expand All @@ -12,6 +14,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AutoMigrator {

Expand Down Expand Up @@ -122,6 +125,31 @@ private static void migrateTable(SQL sql, TableInfo info, boolean tableExists) {
}
}
}

List<String> existingIndices = getIndices(sql, info.getTableName());
for (Index index : info.getIndices()) {
String columns = Stream.of(index.value()).map(info::getColumnName).collect(Collectors.joining(","));
String id = index.id().length() > 0 ? index.id() : "idx_" + Strings.join(index.value(), "_");
if (existingIndices.contains(id))
continue;

StringBuilder sb = new StringBuilder("CREATE ");
if (index.unique())
sb.append("UNIQUE ");
sb.append("INDEX `").append(id).append("` ");
if (index.type() != Index.Type.AUTO)
sb.append("USING ").append(index.type().name()).append(" ");
sb.append("ON `")
.append(info.getTableName())
.append("` (")
.append(columns)
.append(");");
try {
sql.write(sb.toString());
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
}
}

private static Map<String, String> getColumnKeys(SQL sql, String tableName) {
Expand All @@ -137,6 +165,19 @@ private static Map<String, String> getColumnKeys(SQL sql, String tableName) {
}
}

private static List<String> getIndices(SQL sql, String tableName) {
try {
List<String> indices = new ArrayList<>();
ResultSet rs = sql.read("SHOW INDEX FROM `" + tableName + "`;");
while (rs.next()) {
indices.add(rs.getString(1));
}
return indices;
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
}

private static List<String> getTables(SQL sql) {
try {
List<String> tables = new ArrayList<>();
Expand Down