Index handling. Automatic addition and removal of indices not yet implemented.
This commit is contained in:
@@ -41,6 +41,7 @@ import com.samskivert.jdbc.depot.annotation.Computed;
|
|||||||
import com.samskivert.jdbc.depot.annotation.Entity;
|
import com.samskivert.jdbc.depot.annotation.Entity;
|
||||||
import com.samskivert.jdbc.depot.annotation.GeneratedValue;
|
import com.samskivert.jdbc.depot.annotation.GeneratedValue;
|
||||||
import com.samskivert.jdbc.depot.annotation.Id;
|
import com.samskivert.jdbc.depot.annotation.Id;
|
||||||
|
import com.samskivert.jdbc.depot.annotation.Index;
|
||||||
import com.samskivert.jdbc.depot.annotation.TableGenerator;
|
import com.samskivert.jdbc.depot.annotation.TableGenerator;
|
||||||
import com.samskivert.jdbc.depot.annotation.Transient;
|
import com.samskivert.jdbc.depot.annotation.Transient;
|
||||||
|
|
||||||
@@ -72,6 +73,7 @@ public class DepotMarshaller<T>
|
|||||||
_pclass = pclass;
|
_pclass = pclass;
|
||||||
|
|
||||||
// see if this is a computed entity
|
// see if this is a computed entity
|
||||||
|
Entity entity = pclass.getAnnotation(Entity.class);
|
||||||
Computed computed = pclass.getAnnotation(Computed.class);
|
Computed computed = pclass.getAnnotation(Computed.class);
|
||||||
if (computed == null) {
|
if (computed == null) {
|
||||||
// if not, this class has a corresponding SQL table
|
// if not, this class has a corresponding SQL table
|
||||||
@@ -79,7 +81,6 @@ public class DepotMarshaller<T>
|
|||||||
_tableName = _tableName.substring(_tableName.lastIndexOf(".")+1);
|
_tableName = _tableName.substring(_tableName.lastIndexOf(".")+1);
|
||||||
|
|
||||||
// see if there are Entity values specified
|
// see if there are Entity values specified
|
||||||
Entity entity = pclass.getAnnotation(Entity.class);
|
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
if (entity.name().length() > 0) {
|
if (entity.name().length() > 0) {
|
||||||
_tableName = entity.name();
|
_tableName = entity.name();
|
||||||
@@ -200,28 +201,34 @@ public class DepotMarshaller<T>
|
|||||||
// figure out the list of fields that correspond to actual table columns and generate the
|
// figure out the list of fields that correspond to actual table columns and generate the
|
||||||
// SQL used to create and migrate our table (unless we're a computed entity)
|
// SQL used to create and migrate our table (unless we're a computed entity)
|
||||||
_columnFields = new String[_allFields.length];
|
_columnFields = new String[_allFields.length];
|
||||||
_columnDefinitions = new String[_allFields.length];
|
|
||||||
int jj = 0;
|
int jj = 0;
|
||||||
for (int ii = 0; ii < _allFields.length; ii++) {
|
for (int ii = 0; ii < _allFields.length; ii++) {
|
||||||
// include all persistent non-computed fields
|
// include all persistent non-computed fields
|
||||||
String colDef = _fields.get(_allFields[ii]).getColumnDefinition();
|
String colDef = _fields.get(_allFields[ii]).getColumnDefinition();
|
||||||
if (colDef != null) {
|
if (colDef != null) {
|
||||||
_columnFields[jj] = _allFields[ii];
|
_columnFields[jj] = _allFields[ii];
|
||||||
_columnDefinitions[jj] = colDef;
|
_declarations.add(colDef);
|
||||||
jj ++;
|
jj ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_columnFields = ArrayUtil.splice(_columnFields, jj);
|
_columnFields = ArrayUtil.splice(_columnFields, jj);
|
||||||
_columnDefinitions = ArrayUtil.splice(_columnDefinitions, jj);
|
|
||||||
|
// determine whether we have any index definitions
|
||||||
|
if (entity != null) {
|
||||||
|
for (Index index : entity.indices()) {
|
||||||
|
// TODO: delegate this to a database specific SQL generator
|
||||||
|
_declarations.add("INDEX " + index.name() + " " + index.type() +
|
||||||
|
" (" + StringUtil.join(index.columns(), ", ") + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add the primary key, if we have one
|
// add the primary key, if we have one
|
||||||
if (hasPrimaryKey()) {
|
if (hasPrimaryKey()) {
|
||||||
String[] indices = new String[_pkColumns.size()];
|
String[] pkcols = new String[_pkColumns.size()];
|
||||||
for (int ii = 0; ii < indices.length; ii ++) {
|
for (int ii = 0; ii < pkcols.length; ii ++) {
|
||||||
indices[ii] = _pkColumns.get(ii).getColumnName();
|
pkcols[ii] = _pkColumns.get(ii).getColumnName();
|
||||||
}
|
}
|
||||||
_columnDefinitions = ArrayUtil.append(
|
_declarations.add("PRIMARY KEY (" + StringUtil.join(pkcols, ", ") + ")");
|
||||||
_columnDefinitions, "PRIMARY KEY (" + StringUtil.join(indices, ", ") + ")");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we did not find a schema version field, complain
|
// if we did not find a schema version field, complain
|
||||||
@@ -582,10 +589,10 @@ public class DepotMarshaller<T>
|
|||||||
ctx.invoke(new Modifier(null) {
|
ctx.invoke(new Modifier(null) {
|
||||||
public int invoke (Connection conn) throws SQLException {
|
public int invoke (Connection conn) throws SQLException {
|
||||||
if (!JDBCUtil.tableExists(conn, getTableName())) {
|
if (!JDBCUtil.tableExists(conn, getTableName())) {
|
||||||
log.fine("Creating table " + getTableName() + " (" +
|
log.info("Creating table " + getTableName() + " (" + _declarations + ") " +
|
||||||
StringUtil.join(_columnDefinitions, ", ") + ") " + _postamble);
|
_postamble);
|
||||||
JDBCUtil.createTableIfMissing(
|
String[] definition = _declarations.toArray(new String[_declarations.size()]);
|
||||||
conn, getTableName(), _columnDefinitions, _postamble);
|
JDBCUtil.createTableIfMissing(conn, getTableName(), definition, _postamble);
|
||||||
updateVersion(conn, 1);
|
updateVersion(conn, 1);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -752,7 +759,7 @@ public class DepotMarshaller<T>
|
|||||||
protected int _schemaVersion = -1;
|
protected int _schemaVersion = -1;
|
||||||
|
|
||||||
/** Used when creating and migrating our table schema. */
|
/** Used when creating and migrating our table schema. */
|
||||||
protected String[] _columnDefinitions;
|
protected ArrayList<String> _declarations = new ArrayList<String>();
|
||||||
|
|
||||||
/** Used when creating and migrating our table schema. */
|
/** Used when creating and migrating our table schema. */
|
||||||
protected String _postamble = "";
|
protected String _postamble = "";
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ public @interface Entity
|
|||||||
/** The name of an entity. Defaults to the unqualified name of the entity class. */
|
/** The name of an entity. Defaults to the unqualified name of the entity class. */
|
||||||
String name () default "";
|
String name () default "";
|
||||||
|
|
||||||
|
/** Defines indices to add to this entity's table. */
|
||||||
|
Index[] indices () default {};
|
||||||
|
|
||||||
/** Additional SQL to be added when creating this entity's table for the first time. You can
|
/** Additional SQL to be added when creating this entity's table for the first time. You can
|
||||||
* specify a MySQL table storage type for example. */
|
* specify a MySQL table storage type for example. */
|
||||||
String postamble () default "";
|
String postamble () default "";
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
//
|
||||||
|
// samskivert library - useful routines for java programs
|
||||||
|
// Copyright (C) 2006 Michael Bayne
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Lesser General Public License as published
|
||||||
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package com.samskivert.jdbc.depot.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines an index on an entity table.
|
||||||
|
*/
|
||||||
|
@Target(value={})
|
||||||
|
@Retention(value=RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Index
|
||||||
|
{
|
||||||
|
/** Defines the name of the index. */
|
||||||
|
String name () default "";
|
||||||
|
|
||||||
|
/** Configures the type of this index if necessary. MySQL supports FULLTEXT, SPATIAL and
|
||||||
|
* UNIQUE. */
|
||||||
|
String type () default "";
|
||||||
|
|
||||||
|
/** Defines the columns on which the index operates. */
|
||||||
|
String[] columns () default {};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user