/*
*
* SchemaCrawler
* http://sourceforge.net/projects/schemacrawler
* Copyright (c) 2000-2010, Sualeh Fatehi.
*
* 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 schemacrawler.build;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import schemacrawler.schema.Procedure;
import schemacrawler.schema.ProcedureColumn;
import schemacrawler.schema.ProcedureType;
import schemacrawler.schema.RoutineBodyType;
import schemacrawler.schema.Schema;
/**
* Represents a database procedure. Created from metadata returned by a
* JDBC call.
*
* @author Sualeh Fatehi
*/
final class MutableProcedure
extends BaseDatabaseObjectMetadata
implements Procedure
{
public final class ProcedureBuilder
extends BaseDatabaseObjectBuilder
{
private ProcedureType procedureType;
private final Set<MutableProcedureColumn> columns = new HashSet<MutableProcedureColumn>();
private RoutineBodyType routineBodyType;
private StringBuilder definition;
protected ProcedureBuilder(final Schema schema, final String name)
{
super(schema, name);
}
public void addColumn(final MutableProcedureColumn column)
{
columns.add(column);
}
public void appendDefinition(final String definition)
{
this.definition.append(definition);
}
public Procedure build()
{
return new MutableProcedure(this);
}
public ProcedureColumn[] getColumns()
{
final ProcedureColumn[] columnsArray = columns
.toArray(new ProcedureColumn[columns.size()]);
Arrays.sort(columnsArray);
return columnsArray;
}
public String getDefinition()
{
return definition.toString();
}
public RoutineBodyType getRoutineBodyType()
{
return routineBodyType;
}
public ProcedureType getType()
{
return procedureType;
}
public void setRoutineBodyType(final RoutineBodyType routineBodyType)
{
if (routineBodyType == null)
{
throw new IllegalArgumentException("Null procedure type");
}
this.routineBodyType = routineBodyType;
}
public void setType(final ProcedureType type)
{
if (type == null)
{
throw new IllegalArgumentException("Null procedure type");
}
procedureType = type;
}
}
private static final long serialVersionUID = 3906925686089134130L;
private final ProcedureType procedureType;
private final ProcedureColumn[] columns;
private final RoutineBodyType routineBodyType;
private final String definition;
MutableProcedure(final ProcedureBuilder builder)
{
super(builder);
columns = builder.getColumns();
procedureType = builder.procedureType;
routineBodyType = builder.routineBodyType;
definition = builder.getDefinition();
}
/**
* {@inheritDoc}
*
* @see schemacrawler.schema.Procedure#getColumn(java.lang.String)
*/
public ProcedureColumn getColumn(final String name)
{
ProcedureColumn column = null;
for (final ProcedureColumn currentColumn: columns)
{
if (currentColumn.getName().equals(name))
{
column = currentColumn;
break;
}
}
return column;
}
/**
* {@inheritDoc}
*
* @see Procedure#getColumns()
*/
public ProcedureColumn[] getColumns()
{
return columns;
}
/**
* {@inheritDoc}
*
* @see Procedure#getDefinition()
*/
public String getDefinition()
{
return definition.toString();
}
/**
* {@inheritDoc}
*
* @see Procedure#getRoutineBodyType()
*/
public RoutineBodyType getRoutineBodyType()
{
return routineBodyType;
}
/**
* {@inheritDoc}
*
* @see Procedure#getType()
*/
public ProcedureType getType()
{
return procedureType;
}
}