1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2026-02-06 01:32:54 +00:00

Greatly improves source code documentation

This commit is contained in:
2011-12-27 16:42:05 -02:00
parent 998a93f9d2
commit 92c72ff155
149 changed files with 3260 additions and 256 deletions

View File

@@ -16,7 +16,6 @@
*/
package com.l2jserver.model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -72,9 +71,9 @@ public abstract class AbstractModel<T extends ID<?>> implements Model<T> {
}
/**
* Set this object desire to {@link ObjectDesire#UPDATE}. If the desire is
* {@link ObjectDesire#INSERT} or {@link ObjectDesire#DELETE} the desire
* will not be changed.
* Set this object desire to {@link Model.ObjectDesire#UPDATE}. If the
* desire is {@link Model.ObjectDesire#INSERT} or
* {@link Model.ObjectDesire#DELETE} the desire will not be changed.
*/
protected void desireUpdate() {
if (this.desire != ObjectDesire.INSERT
@@ -85,8 +84,8 @@ public abstract class AbstractModel<T extends ID<?>> implements Model<T> {
}
/**
* Set this object desire to {@link ObjectDesire#INSERT}. If the desire is
* {@link ObjectDesire#DELETE} the desire will not be changed.
* Set this object desire to {@link Model.ObjectDesire#INSERT}. If the desire is
* {@link Model.ObjectDesire#DELETE} the desire will not be changed.
*/
protected void desireInsert() {
if (this.desire != ObjectDesire.DELETE) {

View File

@@ -45,10 +45,10 @@ public abstract class AbstractDAO<T extends Model<?>, I extends ID<?>>
*/
protected final DatabaseService database;
@Inject
/**
* The ThreadService used to execute operations asynchronously.
*/
@Inject
protected ThreadService threadService;
/**

View File

@@ -3,6 +3,12 @@ package com.l2jserver.service.database;
import com.l2jserver.model.Model;
import com.l2jserver.model.id.ID;
/**
* The {@link DAOResolver} resolves the {@link DataAccessObject} that provides
* database operations for an given {@link Model} instance
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface DAOResolver {
/**
* Returns the {@link DataAccessObject} used to retrieve and save objects of

View File

@@ -20,8 +20,14 @@ package com.l2jserver.service.database;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DatabaseException extends RuntimeException {
/**
* The Java Serialization API ID
*/
private static final long serialVersionUID = 1L;
/**
* Creates a new instane
*/
public DatabaseException() {
super();
}

View File

@@ -20,8 +20,14 @@ package com.l2jserver.service.database;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DatabaseMappingException extends DatabaseException {
/**
* The Java Serialization API ID
*/
private static final long serialVersionUID = 1L;
/**
* Creates a new instane
*/
public DatabaseMappingException() {
super();
}

View File

@@ -31,7 +31,13 @@ import com.mysema.query.types.Path;
*/
public class SelectPrimaryKeyMapper<R, I extends ID<? super R>, E extends RelationalPathBase<R>>
implements SelectMapper<I, R, I, E> {
/**
* The primary key mapper
*/
private final PrimaryKeyMapper<I, R> mapper;
/**
* The primary key path
*/
private final Path<R> path;
/**

View File

@@ -30,6 +30,15 @@ import com.l2jserver.util.factory.CollectionFactory;
*
*/
public class QueryFactory {
/**
* Creates an <code>CREATE TABLE</code> query from an {@link Table} object
*
* @param table
* the table object
* @param template
* the query template
* @return the <code>CREATE TABLE</code> query as string
*/
public static String createTableQuery(Table table, QueryTemplate template) {
final StringBuilder builder = new StringBuilder();
builder.append(template.getCreateTable())
@@ -59,6 +68,18 @@ public class QueryFactory {
return builder.toString();
}
/**
* Creates an <code>ALTER TABLE</code> query from the difference between two
* {@link Table} objects
*
* @param expected
* the desired table model
* @param current
* the current table model
* @param template
* the query template
* @return the <code>ALTER TABLE</code> query as string
*/
public static String alterTableQueryDelta(Table expected, Table current,
QueryTemplate template) {
// detect missing columns
@@ -108,6 +129,20 @@ public class QueryFactory {
return builder.toString();
}
/**
* Creates an <code>ALTER TABLE</code> query from the difference between two
* {@link Table} objects.
* <p>
* This method does not delete any column.
*
* @param expected
* the desired table model
* @param current
* the current table model
* @param template
* the query template
* @return the <code>ALTER TABLE</code> query as string
*/
public static String alterTableQueryUpdate(Table expected, Table current,
QueryTemplate template) {
// detect missing columns
@@ -144,6 +179,19 @@ public class QueryFactory {
return builder.toString();
}
/**
* Creates an <code>ALTER TABLE</code> query from the difference between two
* {@link Table} objects. Note that this method will only add missing
* columns, but won't update their types.
*
* @param expected
* the desired table model
* @param current
* the current table model
* @param template
* the query template
* @return the <code>ALTER TABLE</code> query as string
*/
public static String alterTableQueryMissing(Table expected, Table current,
QueryTemplate template) {
// detect missing columns
@@ -172,6 +220,16 @@ public class QueryFactory {
return builder.toString();
}
/**
* @param builder
* the {@link StringBuilder}
* @param template
* the query template
* @param column
* the column
* @param alter
* whether it is an alter table or create table
*/
private static void createColumnDefinition(StringBuilder builder,
QueryTemplate template, Column column, boolean alter) {
builder.append(template.quoteIdentifier(column.getName())).append(" ");
@@ -215,6 +273,16 @@ public class QueryFactory {
}
}
/**
* @param builder
* the {@link StringBuilder}
* @param template
* the query template
* @param table
* the table
* @param pk
* the primary key
*/
private static void generatePrimaryKeyDefinition(StringBuilder builder,
QueryTemplate template, Table table, PrimaryKey pk) {
builder.append("CONSTRAINT ")
@@ -227,6 +295,14 @@ public class QueryFactory {
// .append(")");
}
/**
* @param builder
* the {@link StringBuilder}
* @param template
* the query template
* @param fk
* the foreign key
*/
private static void generateForeignKeyDefinition(StringBuilder builder,
QueryTemplate template, ForeignKey fk) {
builder.append("CONSTRAINT ")

View File

@@ -44,39 +44,85 @@ public abstract class QueryTemplate extends SQLTemplates {
// String getColumnDefinition(String name, ColumnType type, int size,
// boolean nullable);
/**
* @param type
* the column type
* @return the database specific type
*/
public abstract String getDatabaseType(ColumnType type);
/**
* @param type
* the column type
* @return true if the database requires an size parameter for the column
* type
*/
public abstract boolean getTypeSizeRequirement(ColumnType type);
/**
* @return true if the database supports enums
*/
public abstract boolean supportsEnum();
/**
* @return true if the database supports auto increment
*/
public abstract boolean supportsAutoIncrement();
/**
* @return true if the database supports foreign keys
*/
public abstract boolean supportsForeignKeys();
/**
* @return true if the database supports changing column types
*/
public abstract boolean supportsColumnChangeTypes();
/**
* @return true if the database supports renaming columns
*/
public abstract boolean supportsColumnRename();
/**
* @return true if the database supports altering tables
*/
public abstract boolean supportsAlterTable();
/**
* @param defaultValue
* the value
* @return the quoted value
*/
public String quoteValue(String defaultValue) {
return new StringBuilder("'").append(defaultValue).append("'")
.toString();
}
/**
* @return the <code>ALTER TABLE</code> statement
*/
public String getAlterTable() {
return "alter table ";
}
/**
* @return the <code>ADD COLUMN</code> statement
*/
public String getAddColumn() {
return "add column ";
}
/**
* @return the <code>DROP COLUMN</code> statement
*/
public String getDropColumn() {
return "drop column ";
}
/**
* @return the <code>ALTER COLUMN</code> statement
*/
public String getAlterColumn() {
return "alter column ";
}

View File

@@ -131,6 +131,13 @@ public class TableFactory {
return fks;
}
/**
* @param tablePath
* the query entity
* @param columns
* the columns
* @return the primary key object
*/
private static PrimaryKey createPK(RelationalPath<?> tablePath,
Map<String, Column> columns) {
return new PrimaryKey(columns.get(tablePath.getPrimaryKey()
@@ -138,6 +145,13 @@ public class TableFactory {
.toString()));
}
/**
* @param tablePath
* the query entity
* @param path
* the path
* @return the column object
*/
private static Column createColumn(RelationalPath<?> tablePath, Path<?> path) {
final String columnName = path.getMetadata().getExpression().toString();
final ColumnType columnType = getColumnType(path.getType());
@@ -197,6 +211,11 @@ public class TableFactory {
hasDefaultValue, defaultValue);
}
/**
* @param type
* the java type
* @return the database column type
*/
private static ColumnType getColumnType(Class<?> type) {
if (ClassUtils.isSubclass(type, String.class))
return ColumnType.STRING;
@@ -215,6 +234,11 @@ public class TableFactory {
return null;
}
/**
* @param jdbcType
* the JDBC type
* @return the database column type
*/
private static ColumnType getColumnType(int jdbcType) {
switch (jdbcType) {
case Types.INTEGER:

View File

@@ -33,5 +33,8 @@ import com.mysema.query.sql.RelationalPath;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ColumnDefault {
/**
* @return the default column value
*/
String value() default "NULL";
}

View File

@@ -33,5 +33,8 @@ import com.mysema.query.sql.RelationalPath;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ColumnSize {
/**
* @return the maximum column value
*/
int value();
}

View File

@@ -16,6 +16,7 @@
*/
package com.l2jserver.service.database.ddl.struct;
import java.sql.Date;
import java.util.List;
import com.l2jserver.util.factory.CollectionFactory;
@@ -24,18 +25,67 @@ import com.l2jserver.util.factory.CollectionFactory;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class Column {
/**
* The column name
*/
private final String name;
/**
* The column type
*/
private final ColumnType type;
/**
* Define the supported column types
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public enum ColumnType {
STRING, ENUM, INTEGER, DOUBLE, TIMESTAMP;
/**
* {@link String} type
*/
STRING,
/**
* {@link Enum} type
*/
ENUM,
/**
* {@link Integer} type
*/
INTEGER,
/**
* {@link Double} type
*/
DOUBLE,
/**
* {@link Date} type
*/
TIMESTAMP;
}
/**
* Whether the column is nullable or not
*/
private boolean nullable = true;
/**
* The maximum size of the column
*/
private int size = 0;
/**
* Whether the column has a default value
*/
private boolean hasDefaultValue = false;
/**
* The column default value
*/
private String defaultValue = null;
/**
* The enum values
*/
private List<String> enumValues = CollectionFactory.newList();
/**
* Whether the column requires ID generation
*/
private boolean autoIncrement;
/**

View File

@@ -24,7 +24,13 @@ import java.util.List;
*
*/
public class ForeignKey {
/**
* The key name
*/
private final String name;
/**
* The key columns
*/
private final List<Column> columns;
/**

View File

@@ -20,6 +20,9 @@ package com.l2jserver.service.database.ddl.struct;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class PrimaryKey {
/**
* The primary key column
*/
private final Column column;
/**

View File

@@ -28,12 +28,30 @@ import com.l2jserver.util.factory.CollectionFactory;
*
*/
public class Table {
/**
* The table name
*/
private final String name;
/**
* The columns
*/
private final Map<String, Column> columns = CollectionFactory.newMap();
/**
* The primary key
*/
private final PrimaryKey primaryKey;
/**
* The foreign keys
*/
private final List<ForeignKey> foreignKeys = CollectionFactory.newList();
/**
* @param name the table name
* @param columns the column
* @param primaryKey the primary key
* @param foreignKeys the foreign keys
*/
public Table(String name, Map<String, Column> columns,
PrimaryKey primaryKey, List<ForeignKey> foreignKeys) {
this.name = name;
@@ -43,6 +61,10 @@ public class Table {
this.foreignKeys.addAll(foreignKeys);
}
/**
* @param name the table name
* @param primaryKey the primary key
*/
public Table(String name, PrimaryKey primaryKey) {
this.name = name;
this.primaryKey = primaryKey;
@@ -81,6 +103,10 @@ public class Table {
return column;
}
/**
* @param name the column name
* @return the column represented by <code>name</code>
*/
public Column getColumn(String name) {
for (final Column column : columns.values()) {
if (name.equals(column.getName()))

View File

@@ -27,12 +27,24 @@ import com.mysema.query.types.Ops;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DerbyTemplate extends QueryTemplate {
/**
* The limit offset template string
*/
private String limitOffsetTemplate = "\noffset {1s} rows fetch next {0s} rows only";
/**
* The limit template string
*/
private String limitTemplate = "\nfetch first {0s} rows only";
/**
* The offset template string
*/
private String offsetTemplate = "\noffset {0s} rows";
/**
* Creates a new instance
*/
public DerbyTemplate() {
super("\"", '\\', true);
addClass2TypeMappings("smallint", Byte.class);

View File

@@ -24,6 +24,9 @@ import com.mysema.query.types.Ops;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class H2Template extends QueryTemplate {
/**
* Creates a new instance
*/
public H2Template() {
super("\"", '\\', true);
setNativeMerge(true);

View File

@@ -26,6 +26,9 @@ import com.mysema.query.types.Ops;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class MySQLTemplate extends QueryTemplate {
/**
* Creates a new instance
*/
public MySQLTemplate() {
super("`", '\\', true);
addClass2TypeMappings("bool", Boolean.class);

View File

@@ -496,7 +496,24 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
R query(ODatabaseDocumentTx database, DatabaseService service);
}
public static abstract class AbstractQuery<R> implements Query<R> {
/**
* An base abstract query. For internal use only.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <R>
* the query return type
*/
private static abstract class AbstractQuery<R> implements Query<R> {
/**
* Tries to update the object desire if it currently is equal to
* <code>expected</code>
*
* @param object
* the object to update desire
* @param expected
* the expected desire
*/
protected void updateDesire(Object object, ObjectDesire expected) {
if (object instanceof Model) {
if (((Model<?>) object).getObjectDesire() == expected) {
@@ -505,18 +522,53 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
}
}
/**
* Returns the parameter name for the given <code>path</code>
*
* @param path
* the path
* @return the parameter name
*/
protected String name(Path<?> path) {
return path.getMetadata().getExpression().toString();
}
}
/**
* An query implementation designed to insert new objects into the database.
* Optionally, it can use an pseudo primary key generator that maps the
* OrientDB document id as the generated ID.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the object type used in this query
* @param <RI>
* the raw ID type
* @param <I>
* the ID type
* @param <E>
* the entity type
*/
public static class InsertQuery<O, RI, I extends ID<? super RI>, E extends RelationalPathBase<?>>
extends AbstractQuery<Integer> {
/**
* The row mapper
*/
private final InsertMapper<O, RI, I, E> mapper;
/**
* The query object iterator
*/
private final Iterator<O> iterator;
/**
* The query primary key column. Only set if want auto generated IDs
*/
private final Path<RI> primaryKey;
protected final E e;
/**
* The query entity
*/
protected final E entity;
/**
* @param entity
@@ -533,7 +585,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
Path<RI> primaryKey, Iterator<O> iterator) {
this.iterator = iterator;
this.mapper = mapper;
this.e = entity;
this.entity = entity;
this.primaryKey = primaryKey;
}
@@ -589,9 +641,9 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
final DocumentDatabaseRow row = new DocumentDatabaseRow();
while (iterator.hasNext()) {
final O object = iterator.next();
row.setDocument(new ODocument(database, e.getTableName()));
row.setDocument(new ODocument(database, entity.getTableName()));
mapper.insert(e, object, row);
mapper.insert(entity, object, row);
row.getDocument().save();
if (primaryKey != null && object instanceof Model) {
@@ -614,11 +666,30 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
}
}
/**
* An query implementation designed to update objects in the database
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the query object type
* @param <E>
* the query entity type
*/
public static abstract class UpdateQuery<O, E extends RelationalPathBase<?>>
extends AbstractQuery<Integer> {
/**
* The row mapper
*/
private final UpdateMapper<O, E> mapper;
/**
* The object iterator for this query
*/
private final Iterator<O> iterator;
protected final E e;
/**
* The query entity
*/
protected final E entity;
/**
* @param entity
@@ -632,7 +703,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
Iterator<O> iterator) {
this.iterator = iterator;
this.mapper = mapper;
this.e = entity;
this.entity = entity;
}
/**
@@ -658,7 +729,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
List<ODocument> documents = database
.query(new ONativeSynchQuery<OQueryContextNative>(
database, e.getTableName(),
database, entity.getTableName(),
new OQueryContextNative()) {
private static final long serialVersionUID = 1L;
@@ -670,7 +741,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
if (documents.size() < 1)
continue;
row.setDocument(documents.get(0));
mapper.update(e, object, row);
mapper.update(entity, object, row);
row.getDocument().save();
rows++;
@@ -680,14 +751,40 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
return rows;
}
/**
* Performs the OrientDB document filtering. If all results are wanted,
* <code>null</code> should be returned.
*
* @param record
* the document record
* @param o
* the object instance
* @return the record instance or <code>null</code>
*/
protected abstract OQueryContextNative query(
OQueryContextNative record, O o);
}
/**
* An query implementation designed for deleting objects in the database.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the query object type
* @param <E>
* the query entity type
*/
public static abstract class DeleteQuery<O, E extends RelationalPathBase<?>>
extends AbstractQuery<Integer> {
/**
* The object iterator for this query
*/
private final Iterator<O> iterator;
protected final E e;
/**
* This query entity
*/
protected final E entity;
/**
* @param entity
@@ -697,7 +794,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
*/
public DeleteQuery(E entity, Iterator<O> iterator) {
this.iterator = iterator;
this.e = entity;
this.entity = entity;
}
/**
@@ -720,7 +817,7 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
List<ODocument> documents = database
.query(new ONativeSynchQuery<OQueryContextNative>(
database, e.getTableName(),
database, entity.getTableName(),
new OQueryContextNative()) {
private static final long serialVersionUID = 1L;
@@ -739,13 +836,46 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
return rows;
}
/**
* Performs the OrientDB document filtering. If all results are wanted,
* <code>null</code> should be returned.
*
* @param record
* the document record
* @param o
* the object instance
* @return the record instance or <code>null</code>
*/
protected abstract OQueryContextNative query(
OQueryContextNative record, O o);
}
public static abstract class AbstractSelectQuery<R, O, RI, I extends ID<? super RI>, E extends RelationalPathBase<RI>>
/**
* Abstract query implementation designed for selecting database objects.
* Internal use only.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <R>
* the query return type
* @param <O>
* the query object type
* @param <RI>
* the raw ID type
* @param <I>
* the ID type
* @param <E>
* the query entity type
*/
private static abstract class AbstractSelectQuery<R, O, RI, I extends ID<? super RI>, E extends RelationalPathBase<RI>>
extends AbstractQuery<R> {
/**
* This query entity type
*/
protected final E entity;
/**
* The row mapper
*/
protected final SelectMapper<O, RI, I, E> mapper;
/**
@@ -778,12 +908,41 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
return perform(documents, service);
}
/**
* Performs the OrientDB document filtering. If all results are wanted,
* <code>null</code> should be returned.
*
* @param record
* the document record
* @param e
* the query entity
* @return the record instance or <code>null</code>
*/
protected abstract OQueryContextNative query(
OQueryContextNative record, E e);
/**
* Effectively performs the query executing and mapping process
*
* @param documents
* the list of documens returned
* @param service
* the database service
* @return the query result, returned directly to the user
*/
protected abstract R perform(List<ODocument> documents,
DatabaseService service);
/**
* Checks if the object is on the cache. Returns it if available,
* <code>null</code> otherwise.
*
* @param row
* the row
* @param database
* the database service
* @return the object on cache, if exists.
*/
@SuppressWarnings("unchecked")
protected O lookupCache(DatabaseRow row, DatabaseService database) {
final I id = mapper.getPrimaryKeyMapper().createID(
@@ -797,6 +956,14 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
return null;
}
/**
* Updates the cache instance
*
* @param instance
* the object instance
* @param database
* the database service
*/
protected void updateCache(O instance, DatabaseService database) {
if (instance == null)
return;
@@ -806,6 +973,21 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
}
}
/**
* An query implementation designed for selecting a single object in the
* database
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the object type
* @param <RI>
* the raw ID type
* @param <I>
* the ID type
* @param <E>
* the query entity type
*/
public static abstract class SelectSingleQuery<O, RI, I extends ID<? super RI>, E extends RelationalPathBase<RI>>
extends AbstractSelectQuery<O, O, RI, I, E> {
/**
@@ -838,6 +1020,21 @@ public abstract class AbstractOrientDatabaseService extends AbstractService
}
}
/**
* An query implementation designed for selecting several objects in the
* database
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the object type
* @param <RI>
* the raw ID type
* @param <I>
* the ID type
* @param <E>
* the query entity type
*/
public static abstract class SelectListQuery<O, RI, I extends ID<? super RI>, E extends RelationalPathBase<RI>>
extends AbstractSelectQuery<List<O>, O, RI, I, E> {
/**

View File

@@ -26,6 +26,9 @@ import com.orientechnologies.orient.core.record.impl.ODocument;
*
*/
public class DocumentDatabaseRow implements DatabaseRow, WritableDatabaseRow {
/**
* The OrientDB {@link ODocument} instance
*/
private ODocument document;
/**
@@ -36,6 +39,9 @@ public class DocumentDatabaseRow implements DatabaseRow, WritableDatabaseRow {
this.document = document;
}
/**
* Creates a new instance
*/
public DocumentDatabaseRow() {
}

View File

@@ -649,7 +649,24 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
DatabaseService database);
}
/**
* An base abstract query. For internal use only.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <R>
* the query return type
*/
public static abstract class AbstractQuery<R> implements Query<R> {
/**
* Tries to update the object desire if it currently is equal to
* <code>expected</code>
*
* @param object
* the object to update desire
* @param expected
* the expected desire
*/
protected void updateDesire(Object object, ObjectDesire expected) {
if (object instanceof Model) {
if (((Model<?>) object).getObjectDesire() == expected) {
@@ -659,13 +676,39 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
}
}
/**
* An query implementation designed to insert new objects into the database.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the object type used in this query
* @param <RI>
* the raw ID type
* @param <I>
* the ID type
* @param <E>
* the entity type
*/
public static class InsertQuery<O, RI, I extends ID<? super RI>, E extends RelationalPathBase<?>>
extends AbstractQuery<Integer> {
/**
* The row mapper
*/
private final InsertMapper<O, RI, I, E> mapper;
/**
* The query object iterator
*/
private final Iterator<O> iterator;
/**
* The query primary key column. Only set if want auto generated IDs
*/
private final Path<RI> primaryKey;
protected final E e;
/**
* The query entity
*/
protected final E entity;
/**
* @param entity
@@ -682,7 +725,7 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
Path<RI> primaryKey, Iterator<O> iterator) {
this.iterator = iterator;
this.mapper = mapper;
this.e = entity;
this.entity = entity;
this.primaryKey = primaryKey;
}
@@ -739,8 +782,8 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
while (iterator.hasNext()) {
final O object = iterator.next();
final SQLInsertWritableDatabaseRow row = new SQLInsertWritableDatabaseRow(
factory.insert(e));
mapper.insert(e, object, row);
factory.insert(entity));
mapper.insert(entity, object, row);
if (primaryKey == null) {
row.getClause().execute();
@@ -759,11 +802,30 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
}
}
/**
* An query implementation designed to update objects in the database
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the query object type
* @param <E>
* the query entity type
*/
public static abstract class UpdateQuery<O, E extends RelationalPathBase<?>>
extends AbstractQuery<Integer> {
/**
* The row mapper
*/
private final UpdateMapper<O, E> mapper;
/**
* The object iterator for this query
*/
private final Iterator<O> iterator;
protected final E e;
/**
* The query entity
*/
protected final E entity;
/**
* @param entity
@@ -777,7 +839,7 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
Iterator<O> iterator) {
this.iterator = iterator;
this.mapper = mapper;
this.e = entity;
this.entity = entity;
}
/**
@@ -801,10 +863,10 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
while (iterator.hasNext()) {
final O object = iterator.next();
final SQLUpdateWritableDatabaseRow row = new SQLUpdateWritableDatabaseRow(
factory.update(e));
factory.update(entity));
// maps query to the values
query(row.getClause(), object);
mapper.update(e, object, row);
mapper.update(entity, object, row);
rows += row.getClause().execute();
@@ -813,13 +875,37 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
return rows;
}
/**
* Performs the query filtering
*
* @param q
* the query clause
* @param o
* the object
*/
protected abstract void query(SQLUpdateClause q, O o);
}
/**
* An query implementation designed for deleting objects in the database.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the query object type
* @param <E>
* the query entity type
*/
public static abstract class DeleteQuery<O, E extends RelationalPathBase<?>>
extends AbstractQuery<Integer> {
/**
* The object iterator for this query
*/
private final Iterator<O> iterator;
protected final E e;
/**
* This query entity
*/
protected final E entity;
/**
* @param entity
@@ -829,7 +915,7 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
*/
public DeleteQuery(E entity, Iterator<O> iterator) {
this.iterator = iterator;
this.e = entity;
this.entity = entity;
}
/**
@@ -850,7 +936,7 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
int rows = 0;
while (iterator.hasNext()) {
final O object = iterator.next();
final SQLDeleteClause delete = factory.delete(e);
final SQLDeleteClause delete = factory.delete(entity);
// maps query to the values
query(delete, object);
@@ -861,12 +947,43 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
return rows;
}
/**
* Performs the query filtering
*
* @param q
* the query clause
* @param o
* the object
*/
protected abstract void query(SQLDeleteClause q, O o);
}
/**
* Abstract query implementation designed for selecting database objects.
* Internal use only.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <R>
* the query return type
* @param <O>
* the query object type
* @param <RI>
* the raw ID type
* @param <I>
* the ID type
* @param <E>
* the query entity type
*/
public static abstract class AbstractSelectQuery<R, O, RI, I extends ID<? super RI>, E extends RelationalPathBase<RI>>
extends AbstractQuery<R> {
/**
* This query entity type
*/
protected final E entity;
/**
* The row mapper
*/
protected final SelectMapper<O, RI, I, E> mapper;
/**
@@ -891,11 +1008,39 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
return perform(select, database);
}
/**
* Performs the query filtering
*
* @param q
* the query clause
* @param e
* the query entity
*/
protected abstract void query(AbstractSQLQuery<?> q, E e);
/**
* Effectively performs the query executing and mapping process
*
* @param select
* the query clause ready to be executed (can be modified if
* needed)
* @param database
* the database service
* @return the query result, returned directly to the user
*/
protected abstract R perform(AbstractSQLQuery<?> select,
DatabaseService database);
/**
* Checks if the object is on the cache. Returns it if available,
* <code>null</code> otherwise.
*
* @param row
* the row
* @param database
* the database service
* @return the object on cache, if exists.
*/
@SuppressWarnings("unchecked")
protected O lookupCache(DatabaseRow row, DatabaseService database) {
final I id = mapper.getPrimaryKeyMapper().createID(
@@ -909,6 +1054,14 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
return null;
}
/**
* Updates the cache instance
*
* @param instance
* the object instance
* @param database
* the database service
*/
protected void updateCache(O instance, DatabaseService database) {
if (instance == null)
return;
@@ -918,6 +1071,21 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
}
}
/**
* An query implementation designed for selecting a single object in the
* database
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the object type
* @param <RI>
* the raw ID type
* @param <I>
* the ID type
* @param <E>
* the query entity type
*/
public static abstract class SelectSingleQuery<O, RI, I extends ID<? super RI>, E extends RelationalPathBase<RI>>
extends AbstractSelectQuery<O, O, RI, I, E> {
/**
@@ -950,6 +1118,21 @@ public abstract class AbstractSQLDatabaseService extends AbstractService
}
}
/**
* An query implementation designed for selecting several objects in the
* database
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the object type
* @param <RI>
* the raw ID type
* @param <I>
* the ID type
* @param <E>
* the query entity type
*/
public static abstract class SelectListQuery<O, RI, I extends ID<? super RI>, E extends RelationalPathBase<RI>>
extends AbstractSelectQuery<List<O>, O, RI, I, E> {
/**

View File

@@ -38,18 +38,25 @@ import com.mysema.query.sql.types.Type;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DerbyDatabaseEngine implements DatabaseEngine {
/**
* The {@link DerbyTemplate} instance
*/
private final DerbyTemplate template = new DerbyTemplate();
/**
* The querydsl configuration
*/
private final Configuration configuration = new Configuration(template);
@Override
public SQLQueryFactory<? extends AbstractSQLQuery<?>, ?, ?, ?, ?, ?> createSQLQueryFactory(
final Connection conn) {
return new SQLQueryFactoryImpl(configuration, new Provider<Connection>() {
@Override
public Connection get() {
return conn;
}
});
return new SQLQueryFactoryImpl(configuration,
new Provider<Connection>() {
@Override
public Connection get() {
return conn;
}
});
}
@Override

View File

@@ -37,7 +37,13 @@ import com.mysema.query.sql.types.Type;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class H2DatabaseEngine implements DatabaseEngine {
/**
* The {@link H2Template} instance
*/
private final H2Template template = new H2Template();
/**
* The querydsl configuration
*/
private final Configuration configuration = new Configuration(template);
@Override

View File

@@ -36,7 +36,13 @@ import com.mysema.query.sql.types.Type;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class MySQLDatabaseEngine implements DatabaseEngine {
/**
* The {@link MySQLTemplate} instance
*/
private final QueryTemplate template = new MySQLTemplate();
/**
* The querydsl configuration
*/
private final Configuration configuration = new Configuration(template);
@Override

View File

@@ -27,10 +27,18 @@ import com.mysema.query.types.Path;
*
*/
public class SQLDatabaseRow implements DatabaseRow {
/**
* The query entity
*/
@SuppressWarnings("unused")
private final RelationalPathBase<?> entity;
/**
* The cached list of paths
*/
private final List<Path<?>> paths;
/**
* The database data row
*/
private Object[] row;
/**
@@ -66,10 +74,19 @@ public class SQLDatabaseRow implements DatabaseRow {
return row[indexOf(path)] == null;
}
/**
* @param path
* the path
* @return the index of <code>path</code>
*/
private int indexOf(Path<?> path) {
return paths.indexOf(path);
}
/**
* @param row
* the new row data
*/
public void setRow(Object[] row) {
this.row = row;
}

View File

@@ -27,6 +27,17 @@ import com.l2jserver.util.factory.CollectionFactory;
*
*/
public class ArrayUtils {
/**
* Copy an entire array except objects in <code>except</code> array.
*
* @param type
* the array type
* @param array
* the source array
* @param except
* the objects to not be copied
* @return the copied array
*/
@SafeVarargs
public final static <T> T[] copyArrayExcept(Class<T[]> type, T[] array,
T... except) {

View File

@@ -197,6 +197,11 @@ public class CSVUtils {
return process(map);
}
/**
* @param map
* the CSV row mapped into an {@link Map}
* @return the row processed object
*/
public abstract R process(Map<String, String> map);
}
}

View File

@@ -21,8 +21,14 @@ package com.l2jserver.util.transformer;
*
*/
public class TransformException extends RuntimeException {
/**
* The Java Serialization API ID
*/
private static final long serialVersionUID = 1L;
/**
* Creates a new instane
*/
public TransformException() {
}