/* * Copyright 2011, Mysema Ltd * * 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. */ package com.mysema.query.sql; import static com.google.common.collect.ImmutableList.copyOf; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import javax.annotation.Nullable; import com.google.common.collect.Maps; import com.mysema.query.types.FactoryExpression; import com.mysema.query.types.Path; import com.mysema.query.types.PathMetadata; import com.mysema.query.types.PathMetadataFactory; import com.mysema.query.types.path.BeanPath; /** * RelationalPathBase is a base class for {@link RelationalPath} implementations * * @author tiwe * * @param * entity type */ public class RelationalPathBase extends BeanPath implements RelationalPath { private static final long serialVersionUID = -7031357250283629202L; @Nullable private PrimaryKey primaryKey; private final List> columns = new ArrayList>(); private final Map, ColumnMetadata> columnMetadata = Maps.newHashMap(); private final List> foreignKeys = new ArrayList>(); private final List> inverseForeignKeys = new ArrayList>(); private final String schema, table; private transient FactoryExpression projection; public RelationalPathBase(Class type, String variable, String schema, String table) { this(type, PathMetadataFactory.forVariable(variable), schema, table); } public RelationalPathBase(Class type, PathMetadata metadata, String schema, String table) { super(type, metadata); this.schema = schema; this.table = table; } protected PrimaryKey createPrimaryKey(Path... columns) { primaryKey = new PrimaryKey(this, columns); return primaryKey; } protected ForeignKey createForeignKey(Path local, String foreign) { ForeignKey foreignKey = new ForeignKey(this, local, foreign); foreignKeys.add(foreignKey); return foreignKey; } protected ForeignKey createForeignKey(List> local, List foreign) { ForeignKey foreignKey = new ForeignKey(this, copyOf(local), copyOf(foreign)); foreignKeys.add(foreignKey); return foreignKey; } protected ForeignKey createInvForeignKey(Path local, String foreign) { ForeignKey foreignKey = new ForeignKey(this, local, foreign); inverseForeignKeys.add(foreignKey); return foreignKey; } protected ForeignKey createInvForeignKey(List> local, List foreign) { ForeignKey foreignKey = new ForeignKey(this, copyOf(local), copyOf(foreign)); inverseForeignKeys.add(foreignKey); return foreignKey; } protected

> P addMetadata(P path, ColumnMetadata metadata) { columnMetadata.put(path, metadata); return path; } @Override public FactoryExpression getProjection() { if (projection == null) { projection = RelationalPathUtils.createProjection(this); } return projection; } public Path[] all() { Path[] all = new Path[columns.size()]; columns.toArray(all); return all; } @Override protected

> P add(P path) { columns.add(path); return path; } @Override public List> getColumns() { return columns; } @Override public Collection> getForeignKeys() { return foreignKeys; } @Override public Collection> getInverseForeignKeys() { return inverseForeignKeys; } @Override public PrimaryKey getPrimaryKey() { return primaryKey; } @Override public String getSchemaName() { return schema; } @Override public String getTableName() { return table; } @Override public ColumnMetadata getMetadata(Path column) { return columnMetadata.get(column); } }