Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ebean-orm/avaje-ebeanorm
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Jul 29, 2014
2 parents 2ad3997 + 664e95d commit 7f8e4be
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 110 deletions.
24 changes: 24 additions & 0 deletions src/main/java/com/avaje/ebean/annotation/Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.avaje.ebean.annotation;

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

/**
* An annotation for declaring an index on a single column.
*
* @author rvbiljouw
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Index {

/**
* Name of the index
*
* @return index name
*/
String value() default "";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.avaje.ebeaninternal.server.ddl;

import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound;

/**
* A visitor that creates indexes for columns annotated with ColumnIndex
*
* @author rvbiljouw
* @see com.avaje.ebean.annotation.Index
*/
public class CreateIndexVisitor extends AbstractBeanVisitor {
private final IndexPropertyVisitor iv;

public CreateIndexVisitor(DdlGenContext ctx) {
this.iv = new IndexPropertyVisitor(ctx);
}

@Override
public void visitBegin() {

}

@Override
public boolean visitBean(BeanDescriptor<?> descriptor) {
return descriptor.isInheritanceRoot();
}

@Override
public PropertyVisitor visitProperty(BeanProperty p) {
return iv;
}

@Override
public void visitBeanEnd(BeanDescriptor<?> descriptor) {
visitInheritanceProperties(descriptor, iv);
}

@Override
public void visitEnd() {

}

private static final class IndexPropertyVisitor extends BaseTablePropertyVisitor {
private final DdlGenContext ctx;

public IndexPropertyVisitor(DdlGenContext ctx) {
this.ctx = ctx;
}

@Override
public void visitEmbeddedScalar(BeanProperty p, BeanPropertyAssocOne<?> embedded) {

}

@Override
public void visitOneImported(BeanPropertyAssocOne<?> p) {

}

@Override
public void visitScalar(BeanProperty p) {
String baseTable = p.getBeanDescriptor().getBaseTable();
if (p.isIndexed()) {
String indexName = p.getIndexName();
if (indexName.length() == 0) {
indexName = ctx.getDdlSyntax().getIndexName(baseTable, p.getDbColumn(), ctx.incrementIxCount());
}
ctx.write("create index ")
.write(indexName)
.write(" on ")
.write(baseTable)
.write("(")
.write(p.getDbColumn())
.write(");")
.writeNewLine();
}
}

@Override
public void visitCompoundScalar(BeanPropertyCompound compound, BeanProperty p) {

}
}
}
21 changes: 12 additions & 9 deletions src/main/java/com/avaje/ebeaninternal/server/ddl/DdlGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ public void runDdl() {
}
}

protected void writeDrop(String dropFile) {
protected void writeDrop(String dropFile) {

try {
String c = generateDropDdl();
writeFile(dropFile, c);
try {
String c = generateDropDdl();
writeFile(dropFile, c);

} catch (IOException e) {
String msg = "Error generating Drop DDL";
throw new PersistenceException(msg, e);
}
}
} catch (IOException e) {
String msg = "Error generating Drop DDL";
throw new PersistenceException(msg, e);
}
}

protected void writeCreate(String createFile) {

Expand Down Expand Up @@ -149,6 +149,9 @@ public String generateCreateDdl() {
AddForeignKeysVisitor fkeys = new AddForeignKeysVisitor(ctx);
VisitorUtil.visit(server, fkeys);

CreateIndexVisitor indexes = new CreateIndexVisitor(ctx);
VisitorUtil.visit(server, indexes);

ctx.flush();
createContent = ctx.getContent();
return createContent;
Expand Down
Loading

0 comments on commit 7f8e4be

Please sign in to comment.