Skip to content

Commit

Permalink
[#5638] Add org.jooq.Index
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Jun 2, 2017
1 parent cb0a924 commit a8ee7b4
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 23 deletions.
18 changes: 15 additions & 3 deletions jOOQ/src/main/java/org/jooq/AlterIndexStep.java
Expand Up @@ -47,6 +47,18 @@
*/ */
public interface AlterIndexStep { public interface AlterIndexStep {


/**
* Add a <code>RENAME TO</code> clause to the <code>ALTER INDEX</code>
* statement.
* <p>
* Note that in some databases, including MySQL and SQL Server, the index
* namespace is tied to a table, not a schema. In those databases, it is
* recommended to call {@link DSLContext#alterTable(String)} with
* {@link AlterTableStep#renameIndex(String)} instead.
*/
@Support({ H2, HSQLDB, POSTGRES })
AlterIndexFinalStep renameTo(String newName);

/** /**
* Add a <code>RENAME TO</code> clause to the <code>ALTER INDEX</code> * Add a <code>RENAME TO</code> clause to the <code>ALTER INDEX</code>
* statement. * statement.
Expand All @@ -65,9 +77,9 @@ public interface AlterIndexStep {
* <p> * <p>
* Note that in some databases, including MySQL and SQL Server, the index * Note that in some databases, including MySQL and SQL Server, the index
* namespace is tied to a table, not a schema. In those databases, it is * namespace is tied to a table, not a schema. In those databases, it is
* recommended to call {@link DSLContext#alterTable(String)} with * recommended to call {@link DSLContext#alterTable(Name)} with
* {@link AlterTableStep#renameIndex(String)} instead. * {@link AlterTableStep#renameIndex(Index)} instead.
*/ */
@Support({ H2, HSQLDB, POSTGRES }) @Support({ H2, HSQLDB, POSTGRES })
AlterIndexFinalStep renameTo(String newName); AlterIndexFinalStep renameTo(Index newName);
} }
9 changes: 8 additions & 1 deletion jOOQ/src/main/java/org/jooq/AlterTableRenameIndexToStep.java
Expand Up @@ -49,6 +49,12 @@
*/ */
public interface AlterTableRenameIndexToStep { public interface AlterTableRenameIndexToStep {


/**
* Specify a new index name.
*/
@Support({ H2, HSQLDB, MYSQL, POSTGRES })
AlterTableFinalStep to(String newName);

/** /**
* Specify a new index name. * Specify a new index name.
*/ */
Expand All @@ -59,5 +65,6 @@ public interface AlterTableRenameIndexToStep {
* Specify a new index name. * Specify a new index name.
*/ */
@Support({ H2, HSQLDB, MYSQL, POSTGRES }) @Support({ H2, HSQLDB, MYSQL, POSTGRES })
AlterTableFinalStep to(String newName); AlterTableFinalStep to(Index newName);

} }
7 changes: 7 additions & 0 deletions jOOQ/src/main/java/org/jooq/AlterTableStep.java
Expand Up @@ -114,6 +114,13 @@ public interface AlterTableStep {
@Support({ H2, HSQLDB, MYSQL, POSTGRES }) @Support({ H2, HSQLDB, MYSQL, POSTGRES })
AlterTableRenameIndexToStep renameIndex(Name oldName); AlterTableRenameIndexToStep renameIndex(Name oldName);


/**
* Add a <code>RENAME INDEX</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
@Support({ H2, HSQLDB, MYSQL, POSTGRES })
AlterTableRenameIndexToStep renameIndex(Index oldName);

/** /**
* Add a <code>RENAME INDEX</code> clause to the <code>ALTER TABLE</code> * Add a <code>RENAME INDEX</code> clause to the <code>ALTER TABLE</code>
* statement. * statement.
Expand Down
67 changes: 67 additions & 0 deletions jOOQ/src/main/java/org/jooq/DSLContext.java
Expand Up @@ -8488,6 +8488,14 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateIndexStep createIndex(Name index); CreateIndexStep createIndex(Name index);


/**
* Create a new DSL <code>CREATE INDEX</code> statement.
*
* @see DSL#createIndex(Index)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateIndexStep createIndex(Index index);

/** /**
* Create a new DSL <code>CREATE INDEX IF NOT EXISTS</code> statement. * Create a new DSL <code>CREATE INDEX IF NOT EXISTS</code> statement.
* *
Expand All @@ -8504,6 +8512,14 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES, SQLITE }) @Support({ FIREBIRD, H2, HSQLDB, POSTGRES, SQLITE })
CreateIndexStep createIndexIfNotExists(Name index); CreateIndexStep createIndexIfNotExists(Name index);


/**
* Create a new DSL <code>CREATE INDEX IF NOT EXISTS</code> statement.
*
* @see DSL#createIndexIfNotExists(Index)
*/
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES, SQLITE })
CreateIndexStep createIndexIfNotExists(Index index);

/** /**
* Create a new DSL <code>CREATE UNIQUE INDEX</code> statement. * Create a new DSL <code>CREATE UNIQUE INDEX</code> statement.
* *
Expand All @@ -8520,6 +8536,14 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateIndexStep createUniqueIndex(Name index); CreateIndexStep createUniqueIndex(Name index);


/**
* Create a new DSL <code>CREATE UNIQUE INDEX</code> statement.
*
* @see DSL#createIndex(Index)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateIndexStep createUniqueIndex(Index index);

/** /**
* Create a new DSL <code>CREATE UNIQUE INDEX</code> statement. * Create a new DSL <code>CREATE UNIQUE INDEX</code> statement.
* *
Expand All @@ -8536,6 +8560,14 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES, SQLITE }) @Support({ FIREBIRD, H2, HSQLDB, POSTGRES, SQLITE })
CreateIndexStep createUniqueIndexIfNotExists(Name index); CreateIndexStep createUniqueIndexIfNotExists(Name index);


/**
* Create a new DSL <code>CREATE UNIQUE INDEX</code> statement.
*
* @see DSL#createIndex(Index)
*/
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES, SQLITE })
CreateIndexStep createUniqueIndexIfNotExists(Index index);

/** /**
* Create a new DSL <code>CREATE SEQUENCE</code> statement. * Create a new DSL <code>CREATE SEQUENCE</code> statement.
* *
Expand Down Expand Up @@ -8792,6 +8824,14 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ H2, HSQLDB, POSTGRES }) @Support({ H2, HSQLDB, POSTGRES })
AlterIndexStep alterIndex(Name index); AlterIndexStep alterIndex(Name index);


/**
* Create a new DSL <code>ALTER INDEX</code> statement.
*
* @see DSL#alterIndex(Name)
*/
@Support({ H2, HSQLDB, POSTGRES })
AlterIndexStep alterIndex(Index index);

/** /**
* Create a new DSL <code>ALTER INDEX</code> statement. * Create a new DSL <code>ALTER INDEX</code> statement.
* *
Expand All @@ -8808,6 +8848,14 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ H2, POSTGRES }) @Support({ H2, POSTGRES })
AlterIndexStep alterIndexIfExists(Name index); AlterIndexStep alterIndexIfExists(Name index);


/**
* Create a new DSL <code>ALTER INDEX</code> statement.
*
* @see DSL#alterIndexIfExists(Name)
*/
@Support({ H2, POSTGRES })
AlterIndexStep alterIndexIfExists(Index index);

/** /**
* Create a new DSL <code>DROP SCHEMA</code> statement. * Create a new DSL <code>DROP SCHEMA</code> statement.
* *
Expand Down Expand Up @@ -8986,6 +9034,14 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
DropIndexOnStep dropIndex(Name index); DropIndexOnStep dropIndex(Name index);


/**
* Create a new DSL <code>DROP INDEX</code> statement.
*
* @see DSL#dropIndex(Name)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
DropIndexOnStep dropIndex(Index index);

/** /**
* Create a new DSL <code>DROP INDEX IF EXISTS</code> statement. * Create a new DSL <code>DROP INDEX IF EXISTS</code> statement.
* <p> * <p>
Expand All @@ -9008,6 +9064,17 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
DropIndexOnStep dropIndexIfExists(Name index); DropIndexOnStep dropIndexIfExists(Name index);


/**
* Create a new DSL <code>DROP INDEX IF EXISTS</code> statement.
* <p>
* If your database doesn't natively support <code>IF EXISTS</code>, this is
* emulated by catching (and ignoring) the relevant {@link SQLException}.
*
* @see DSL#dropIndexIfExists(Name)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
DropIndexOnStep dropIndexIfExists(Index index);

/** /**
* Create a new DSL <code>DROP SEQUENCE</code> statement. * Create a new DSL <code>DROP SEQUENCE</code> statement.
* *
Expand Down
82 changes: 82 additions & 0 deletions jOOQ/src/main/java/org/jooq/Index.java
@@ -0,0 +1,82 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;

import java.util.List;

/**
* A DDL index definition.
*
* @author Lukas Eder
*/
public interface Index extends QueryPart {

/**
* The name of the index.
*/
String getName();

/**
* The qualified name of this field.
*/
Name getQualifiedName();

/**
* The unqualified name of this field.
*/
Name getUnqualifiedName();

/**
* The table on which this index is defined.
*/
Table<?> getTable();

/**
* The sort field expressions on which this index is defined.
*/
List<SortField<?>> getFields();

/**
* The condition of a filtered / partial index, or <code>null</code>, if
* this is an ordinary index.
*/
Condition getWhere();

/**
* Whether this is a <code>UNIQUE</code> index.
*/
boolean getUnique();

}
23 changes: 15 additions & 8 deletions jOOQ/src/main/java/org/jooq/impl/AlterIndexImpl.java
Expand Up @@ -46,6 +46,7 @@
import static org.jooq.SQLDialect.FIREBIRD; import static org.jooq.SQLDialect.FIREBIRD;
// ... // ...
// ... // ...
import static org.jooq.impl.DSL.index;
import static org.jooq.impl.DSL.name; import static org.jooq.impl.DSL.name;
import static org.jooq.impl.Keywords.K_ALTER_INDEX; import static org.jooq.impl.Keywords.K_ALTER_INDEX;
import static org.jooq.impl.Keywords.K_IF_EXISTS; import static org.jooq.impl.Keywords.K_IF_EXISTS;
Expand All @@ -58,6 +59,7 @@
import org.jooq.Clause; import org.jooq.Clause;
import org.jooq.Configuration; import org.jooq.Configuration;
import org.jooq.Context; import org.jooq.Context;
import org.jooq.Index;
import org.jooq.Name; import org.jooq.Name;


/** /**
Expand All @@ -75,15 +77,15 @@ final class AlterIndexImpl extends AbstractQuery implements
private static final long serialVersionUID = 8904572826501186329L; private static final long serialVersionUID = 8904572826501186329L;
private static final Clause[] CLAUSES = { ALTER_INDEX }; private static final Clause[] CLAUSES = { ALTER_INDEX };


private final Name index; private final Index index;
private final boolean ifExists; private final boolean ifExists;
private Name renameTo; private Index renameTo;


AlterIndexImpl(Configuration configuration, Name index) { AlterIndexImpl(Configuration configuration, Index index) {
this(configuration, index, false); this(configuration, index, false);
} }


AlterIndexImpl(Configuration configuration, Name index, boolean ifExists) { AlterIndexImpl(Configuration configuration, Index index, boolean ifExists) {
super(configuration); super(configuration);


this.index = index; this.index = index;
Expand All @@ -94,15 +96,20 @@ final class AlterIndexImpl extends AbstractQuery implements
// XXX: DSL API // XXX: DSL API
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------


@Override
public final AlterIndexImpl renameTo(String newName) {
return renameTo(name(newName));
}

@Override @Override
public final AlterIndexImpl renameTo(Name newName) { public final AlterIndexImpl renameTo(Name newName) {
this.renameTo = newName; return renameTo(index(newName));
return this;
} }


@Override @Override
public final AlterIndexImpl renameTo(String newName) { public final AlterIndexImpl renameTo(Index newName) {
return renameTo(name(newName)); this.renameTo = newName;
return this;
} }


// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Expand Down
9 changes: 7 additions & 2 deletions jOOQ/src/main/java/org/jooq/impl/CreateIndexImpl.java
Expand Up @@ -55,6 +55,7 @@
import static org.jooq.impl.Keywords.K_ON; import static org.jooq.impl.Keywords.K_ON;
import static org.jooq.impl.Keywords.K_UNIQUE; import static org.jooq.impl.Keywords.K_UNIQUE;
import static org.jooq.impl.Keywords.K_WHERE; import static org.jooq.impl.Keywords.K_WHERE;
import static org.jooq.impl.Tools.EMPTY_SORTFIELD;


import java.util.Collection; import java.util.Collection;


Expand All @@ -65,6 +66,7 @@
import org.jooq.CreateIndexStep; import org.jooq.CreateIndexStep;
import org.jooq.CreateIndexWhereStep; import org.jooq.CreateIndexWhereStep;
import org.jooq.Field; import org.jooq.Field;
import org.jooq.Index;
import org.jooq.Name; import org.jooq.Name;
import org.jooq.QueryPart; import org.jooq.QueryPart;
import org.jooq.SQL; import org.jooq.SQL;
Expand All @@ -86,20 +88,23 @@ final class CreateIndexImpl extends AbstractQuery implements
private static final long serialVersionUID = 8904572826501186329L; private static final long serialVersionUID = 8904572826501186329L;
private static final Clause[] CLAUSES = { CREATE_INDEX }; private static final Clause[] CLAUSES = { CREATE_INDEX };


private final Name index; private final Index index;
private final boolean unique; private final boolean unique;
private final boolean ifNotExists; private final boolean ifNotExists;
private Table<?> table; private Table<?> table;
private Field<?>[] fields; private Field<?>[] fields;
private SortField<?>[] sortFields; private SortField<?>[] sortFields;
private Condition where; private Condition where;


CreateIndexImpl(Configuration configuration, Name index, boolean unique, boolean ifNotExists) { CreateIndexImpl(Configuration configuration, Index index, boolean unique, boolean ifNotExists) {
super(configuration); super(configuration);


this.index = index; this.index = index;
this.unique = unique; this.unique = unique;
this.ifNotExists = ifNotExists; this.ifNotExists = ifNotExists;
this.table = index.getTable();
this.sortFields = index.getFields().toArray(EMPTY_SORTFIELD);
this.where = index.getWhere();
} }


// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Expand Down

0 comments on commit a8ee7b4

Please sign in to comment.