net.sf.farrago.ddl.gen
Class FarragoDdlGenerator

java.lang.Object
  extended by net.sf.farrago.ddl.gen.DdlGenerator
      extended by net.sf.farrago.ddl.gen.FarragoDdlGenerator
All Implemented Interfaces:
ReflectiveVisitor

public class FarragoDdlGenerator
extends DdlGenerator

Generates DDL statements from catalog objects.

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/ddl/gen/FarragoDdlGenerator.java#22 $
Author:
Jason Ouellette

Nested Class Summary
protected static class FarragoDdlGenerator.FemStorageOptionNameComparator
           
 
Field Summary
protected  JmiModelView modelView
           
private static List<String> NON_REPLACEABLE_TYPE_NAMES
           
 
Fields inherited from class net.sf.farrago.ddl.gen.DdlGenerator
dropCascade, NL, previousSetSchema, SEP, sqlDialect, VALUE_NULL
 
Constructor Summary
FarragoDdlGenerator(JmiModelView modelView)
          Creates a DDL generator.
 
Method Summary
private  void add(FemRoutineParameter parameter, ProcedureType routineType, StringBuilder sb)
          Generates DDL for a routine parameter.
protected  void addColumns(StringBuilder sb, List<CwmColumn> columns)
           
protected  void addColumns(StringBuilder sb, List<CwmColumn> columns, boolean skipDefaults, boolean skipNullable)
           
protected  void addDescription(StringBuilder sb, FemAnnotatedElement element)
           
private  void addOperations(StringBuilder sb, List<CwmOperation> operations)
           
protected  void addOptions(StringBuilder sb, Collection<FemStorageOption> options)
           
protected  void addOptions(StringBuilder sb, Collection<FemStorageOption> options, int indent)
           
private  void addPrimaryKeyConstraint(StringBuilder sb, List<String> keyColumns)
           
<T extends RefObject>
Collection<T>
allOfClass(Class<T> clazz)
           
<T extends RefObject>
Collection<T>
allOfType(Class<T> clazz)
           
private  void appendType(StringBuilder sb, CwmClassifier type, Integer precision, Integer scale, Integer length, NullableType nullable, CwmExpression defaultValue, boolean qualifyType)
           
 void create(CwmDependency dependency, GeneratedDdlStmt stmt)
           
 void create(CwmOperation operation, GeneratedDdlStmt stmt)
           
 void create(FemDataServer server, GeneratedDdlStmt stmt)
           
 void create(FemDataWrapper wrapper, GeneratedDdlStmt stmt)
           
 void create(FemForeignTable table, GeneratedDdlStmt stmt)
           
 void create(FemLabel label, GeneratedDdlStmt stmt)
           
 void create(FemLocalIndex index, GeneratedDdlStmt stmt)
           
 void create(FemLocalSchema schema, GeneratedDdlStmt stmt)
           
 void create(FemLocalTable table, GeneratedDdlStmt stmt)
           
 void create(FemLocalView view, GeneratedDdlStmt stmt)
           
 void create(FemRoutine routine, GeneratedDdlStmt stmt)
           
 void create(FemSqldistinguishedType type, GeneratedDdlStmt stmt)
           
 void create(FemSqlobjectType type, GeneratedDdlStmt stmt)
           
protected  void createHeader(StringBuilder sb, String typeName, boolean replace, String newName)
           
protected  void createHeader(StringBuilder sb, String typeName, GeneratedDdlStmt stmt)
           
protected  void drop(CwmModelElement e, String elementType, GeneratedDdlStmt stmt)
           
 void drop(CwmSchema schema, GeneratedDdlStmt stmt)
           
 void drop(CwmTable table, GeneratedDdlStmt stmt)
           
 void drop(CwmView view, GeneratedDdlStmt stmt)
           
 void drop(FemDataServer server, GeneratedDdlStmt stmt)
           
 void drop(FemDataWrapper plugin, GeneratedDdlStmt stmt)
           
 void drop(FemLabel label, GeneratedDdlStmt stmt)
           
 void drop(FemLocalIndex index, GeneratedDdlStmt stmt)
           
private  RefClass findRefClass(Class<? extends RefObject> clazz)
           
static String formatTypeInfo(CwmColumn col)
          Format the core elements of a column's type (type name, precision, scale, length) into SQL format.
static void formatTypeInfo(StringBuilder sb, CwmClassifier type, Integer precision, Integer scale, Integer length)
          Format the core elements of a column's type (type name, precision, scale, length) into SQL format.
 void gatherElements(List<CwmModelElement> list, String schemaName, boolean includeNonSchemaElements, CwmCatalog catalog)
          Gathers a list of elements in a schema, optionally including elements which don't belong to any schema.
 void generateColumnsAndKeys(StringBuilder sb, List<CwmColumn> columns, boolean skipDefaults, boolean skipNullable, List<String> imposedPrimaryKey)
          Generates the column and key definitions of a table as a SQL string (enclosed in parentheses unless there are no columns).
 String getExportText(List<CwmModelElement> exportList, boolean sort)
          Converts a set of elements to a string using this generator.
protected  JmiModelView getModelView()
           
private static void indent(StringBuilder sb, int indent)
           
protected static String maybeNot(boolean b, String s)
          Generates either "s" or "NOT s", depending on b.
private  void methodBody(FemRoutine routine, StringBuilder sb)
          Generates a parameters, keyword list and body of a method.
protected  boolean typeSupportsReplace(String typeName)
          Returns whether an object type supports CREATE OR REPLACE operation.
 
Methods inherited from class net.sf.farrago.ddl.gen.DdlGenerator
generateCreate, generateDrop, generateSetSchema, getSqlTypeName, hasPrimaryKeyConstraint, literal, name, quote, setDropCascade, setSchemaQualified, unquoteLiteral
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NON_REPLACEABLE_TYPE_NAMES

private static final List<String> NON_REPLACEABLE_TYPE_NAMES

modelView

protected final JmiModelView modelView
Constructor Detail

FarragoDdlGenerator

public FarragoDdlGenerator(JmiModelView modelView)
Creates a DDL generator.

The modelView parameter can be null if you are generating DDL for a single object. A model view is required if calling DdlGenerator.gatherElements(java.util.List, java.lang.String, boolean, net.sf.farrago.cwm.relational.CwmCatalog) or getExportText(java.util.List, boolean) with sort=true.

Parameters:
modelView - Model graph
Method Detail

findRefClass

private RefClass findRefClass(Class<? extends RefObject> clazz)

allOfClass

public <T extends RefObject> Collection<T> allOfClass(Class<T> clazz)

allOfType

public <T extends RefObject> Collection<T> allOfType(Class<T> clazz)

getExportText

public String getExportText(List<CwmModelElement> exportList,
                            boolean sort)
Description copied from class: DdlGenerator
Converts a set of elements to a string using this generator.

If sort is specified, sorts list first so that dependent elements are created after their dependencies.

Overrides:
getExportText in class DdlGenerator
Parameters:
exportList - List of elements to export
sort - Whether to sort list in dependency order
Returns:
DDL script

typeSupportsReplace

protected boolean typeSupportsReplace(String typeName)
Description copied from class: DdlGenerator
Returns whether an object type supports CREATE OR REPLACE operation.

Specified by:
typeSupportsReplace in class DdlGenerator
Parameters:
typeName - Name of object type, e.g. "CLUSTERED INDEX"
Returns:
whether type supports REPLACE

getModelView

protected JmiModelView getModelView()
Specified by:
getModelView in class DdlGenerator

gatherElements

public void gatherElements(List<CwmModelElement> list,
                           String schemaName,
                           boolean includeNonSchemaElements,
                           CwmCatalog catalog)
Description copied from class: DdlGenerator
Gathers a list of elements in a schema, optionally including elements which don't belong to any schema.

Specified by:
gatherElements in class DdlGenerator
Parameters:
list - List to populate
schemaName - Name of schema
includeNonSchemaElements - Whether to include elements which do not belong to a schema
catalog - Catalog

createHeader

protected void createHeader(StringBuilder sb,
                            String typeName,
                            GeneratedDdlStmt stmt)

createHeader

protected void createHeader(StringBuilder sb,
                            String typeName,
                            boolean replace,
                            String newName)

create

public void create(FemLocalView view,
                   GeneratedDdlStmt stmt)

create

public void create(FemLocalSchema schema,
                   GeneratedDdlStmt stmt)

create

public void create(FemLocalTable table,
                   GeneratedDdlStmt stmt)

create

public void create(FemForeignTable table,
                   GeneratedDdlStmt stmt)

create

public void create(FemDataWrapper wrapper,
                   GeneratedDdlStmt stmt)

create

public void create(CwmOperation operation,
                   GeneratedDdlStmt stmt)

create

public void create(FemRoutine routine,
                   GeneratedDdlStmt stmt)

methodBody

private void methodBody(FemRoutine routine,
                        StringBuilder sb)
Generates a parameters, keyword list and body of a method.

Parameters:
routine - Routine
sb - String buffer to write to

add

private void add(FemRoutineParameter parameter,
                 ProcedureType routineType,
                 StringBuilder sb)
Generates DDL for a routine parameter.

Parameters:
parameter - Parameter
routineType - Type of routine parameter belongs to
sb - StringBuilder to append to

appendType

private void appendType(StringBuilder sb,
                        CwmClassifier type,
                        Integer precision,
                        Integer scale,
                        Integer length,
                        NullableType nullable,
                        CwmExpression defaultValue,
                        boolean qualifyType)

formatTypeInfo

public static String formatTypeInfo(CwmColumn col)
Format the core elements of a column's type (type name, precision, scale, length) into SQL format.

Parameters:
col - CwmColumn object we want type info for
Returns:
String containing formatted type info

formatTypeInfo

public static void formatTypeInfo(StringBuilder sb,
                                  CwmClassifier type,
                                  Integer precision,
                                  Integer scale,
                                  Integer length)
Format the core elements of a column's type (type name, precision, scale, length) into SQL format.

Note that this was refactored out of appendType(StringBuilder, CwmClassifier, Integer, Integer, Integer, NullableType, CwmExpression, boolean) to allow separate access.

Parameters:
sb - StringBuilder to hold the formatted type information
type - CwmClassifier object representing the column type
precision - Integer specifying the column's precision
scale - Integer specifying the column's scale
length - Integer specifying the column's length

maybeNot

protected static String maybeNot(boolean b,
                                 String s)
Generates either "s" or "NOT s", depending on b.

Parameters:
b - Condition
s - Flag string
Returns:
string containing s or not s

create

public void create(FemSqlobjectType type,
                   GeneratedDdlStmt stmt)

addOperations

private void addOperations(StringBuilder sb,
                           List<CwmOperation> operations)

create

public void create(FemSqldistinguishedType type,
                   GeneratedDdlStmt stmt)

create

public void create(FemDataServer server,
                   GeneratedDdlStmt stmt)

create

public void create(CwmDependency dependency,
                   GeneratedDdlStmt stmt)

drop

public void drop(CwmSchema schema,
                 GeneratedDdlStmt stmt)

drop

public void drop(CwmView view,
                 GeneratedDdlStmt stmt)

drop

public void drop(FemDataServer server,
                 GeneratedDdlStmt stmt)

drop

public void drop(CwmTable table,
                 GeneratedDdlStmt stmt)

drop

public void drop(FemDataWrapper plugin,
                 GeneratedDdlStmt stmt)

create

public void create(FemLocalIndex index,
                   GeneratedDdlStmt stmt)

drop

public void drop(FemLocalIndex index,
                 GeneratedDdlStmt stmt)

create

public void create(FemLabel label,
                   GeneratedDdlStmt stmt)

drop

public void drop(FemLabel label,
                 GeneratedDdlStmt stmt)

addColumns

protected void addColumns(StringBuilder sb,
                          List<CwmColumn> columns)

addColumns

protected void addColumns(StringBuilder sb,
                          List<CwmColumn> columns,
                          boolean skipDefaults,
                          boolean skipNullable)

generateColumnsAndKeys

public void generateColumnsAndKeys(StringBuilder sb,
                                   List<CwmColumn> columns,
                                   boolean skipDefaults,
                                   boolean skipNullable,
                                   List<String> imposedPrimaryKey)
Generates the column and key definitions of a table as a SQL string (enclosed in parentheses unless there are no columns).

Parameters:
sb - receives generated string
columns - list of column definitions to generate
skipDefaults - whether to omit default value definitions
skipNullable - whether to omit NOT NULL constraint definitions
imposedPrimaryKey - if not null, use as PRIMARY KEY

addOptions

protected void addOptions(StringBuilder sb,
                          Collection<FemStorageOption> options)

addOptions

protected void addOptions(StringBuilder sb,
                          Collection<FemStorageOption> options,
                          int indent)

indent

private static void indent(StringBuilder sb,
                           int indent)

addPrimaryKeyConstraint

private void addPrimaryKeyConstraint(StringBuilder sb,
                                     List<String> keyColumns)

addDescription

protected void addDescription(StringBuilder sb,
                              FemAnnotatedElement element)

drop

protected void drop(CwmModelElement e,
                    String elementType,
                    GeneratedDdlStmt stmt)