/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* 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 org.jkiss.dbeaver.ext.generic.model;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSEntityConstraintType;
import org.jkiss.utils.CommonUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* GenericTableConstraint
*/
public class GenericPrimaryKey extends GenericTableConstraint
{
private List<GenericTableConstraintColumn> columns;
public GenericPrimaryKey(GenericTable table, String name, @Nullable String remarks, DBSEntityConstraintType constraintType, boolean persisted)
{
super(table, name, remarks, constraintType, persisted);
}
/**
* Copy constructor
* @param constraint
*/
GenericPrimaryKey(GenericPrimaryKey constraint)
{
super(constraint.getTable(), constraint.getName(), constraint.getDescription(), constraint.getConstraintType(), constraint.isPersisted());
if (constraint.columns != null) {
this.columns = new ArrayList<>(constraint.columns.size());
for (GenericTableConstraintColumn sourceColumn : constraint.columns) {
this.columns.add(new GenericTableConstraintColumn(this, sourceColumn));
}
}
}
@Override
public List<GenericTableConstraintColumn> getAttributeReferences(DBRProgressMonitor monitor)
{
return columns;
}
public void addColumn(GenericTableConstraintColumn column)
{
if (columns == null) {
columns = new ArrayList<>();
}
this.columns.add(column);
}
void setColumns(List<GenericTableConstraintColumn> columns)
{
this.columns = columns;
if (!CommonUtils.isEmpty(this.columns) && this.columns.size() > 1) {
Collections.sort(columns, new Comparator<GenericTableConstraintColumn>() {
@Override
public int compare(GenericTableConstraintColumn o1, GenericTableConstraintColumn o2)
{
return o1.getOrdinalPosition() - o2.getOrdinalPosition();
}
});
}
}
public boolean hasColumn(GenericTableColumn column)
{
if (this.columns != null) {
for (GenericTableConstraintColumn constColumn : columns) {
if (constColumn.getAttribute() == column) {
return true;
}
}
}
return false;
}
}