org.eclipse.persistence.tools.schemaframework.UniqueKeyConstraint Maven / Gradle / Ivy
Show all versions of eclipselink Show documentation
/*
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.tools.schemaframework;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
*
* Purpose: Define a unique key constraint for a table.
*/
public class UniqueKeyConstraint implements Serializable {
protected String name;
protected List sourceFields; // field names
public UniqueKeyConstraint() {
this.name = "";
this.sourceFields = new ArrayList<>();
}
public UniqueKeyConstraint(String name, String sourceField) {
this();
this.name = name;
sourceFields.add(sourceField);
}
public UniqueKeyConstraint(String name, String[] sourceFields) {
this();
this.name = name;
this.sourceFields.addAll(Arrays.asList(sourceFields));
}
/**
* PUBLIC:
*/
public void addSourceField(String sourceField) {
getSourceFields().add(sourceField);
}
/**
* INTERNAL:
* Append the database field definition string to the table creation statement.
*/
public void appendDBString(Writer writer, AbstractSession session) {
try {
writer.write("UNIQUE (");
for (Iterator iterator = getSourceFields().iterator();
iterator.hasNext();) {
writer.write(iterator.next());
if (iterator.hasNext()) {
writer.write(", ");
}
}
writer.write(")");
} catch (IOException ioException) {
throw ValidationException.fileError(ioException);
}
}
/**
* PUBLIC:
*/
public String getName() {
return name;
}
/**
* PUBLIC:
*/
public List getSourceFields() {
return sourceFields;
}
/**
* PUBLIC:
*/
public void setName(String name) {
this.name = name;
}
/**
* PUBLIC:
*/
public void setSourceFields(List sourceFields) {
this.sourceFields = sourceFields;
}
}