net.sf.farrago.ddl.gen
Class DdlGenerator

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

public abstract class DdlGenerator
extends Object
implements ReflectiveVisitor

Base class for DDL generators which use the visitor pattern to generate DDL given a catalog object.

Escape rules:

  1. In a SET SCHEMA command, apostrophes (') and quotes (") enclose the schema name, like this: '"Foo"'. In this context, apostrophes and quotes must be escaped.
  2. CREATE and DROP commands use quotes (") to enclose the object name. Only quotes are escaped.

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

Nested Class Summary
private static class DdlGenerator.MyComparator
          Comparator for schema elements to ensure that export file occurs in an intuitive order.
private static class DdlGenerator.NamerImpl
          Implementation of JmiObjUtil.Namer which generates names for objects based on their position in the CWM catalog-schema-object hierarchy.
 
Field Summary
private static List<Class> ADDITIONAL_PARAMETER_TYPES
           
protected  boolean dropCascade
           
protected static String NL
           
protected  String previousSetSchema
           
private  boolean schemaQualified
           
protected static String SEP
           
protected static SqlDialect sqlDialect
           
protected static String VALUE_NULL
           
private  ReflectiveVisitDispatcher<DdlGenerator,CwmModelElement> visitDispatcher
           
 
Constructor Summary
DdlGenerator()
           
 
Method Summary
abstract  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.
private  void generate(String method, CwmModelElement e, GeneratedDdlStmt stmt)
           
 void generateCreate(CwmModelElement e, GeneratedDdlStmt stmt)
           
 void generateDrop(CwmModelElement e, GeneratedDdlStmt stmt)
           
 boolean generateSetSchema(GeneratedDdlStmt stmt, String schemaName, boolean evenIfUnchanged)
          Appends a 'SET SCHEMA' command to stmt if schemaName is not null.
 String getExportText(List<CwmModelElement> exportList, boolean sort)
          Converts a set of elements to a string using this generator.
protected abstract  JmiModelView getModelView()
           
protected static SqlTypeName getSqlTypeName(CwmClassifier classifier)
           
protected static boolean hasPrimaryKeyConstraint(FemStoredColumn col)
           
static String literal(String str)
           
private  RefAssociation lookupAssoc(String assocName, JmiModelGraph modelGraph)
          Looks up a named association in the model, fails if not found.
private  RefClass lookupClass(String className, JmiModelGraph modelGraph)
          Looks up a named class in the model, fails if not found.
protected  void name(StringBuilder sb, CwmNamespace schema, String objectName)
          Outputs the name of an object, optionally qualified by a schema name.
static String quote(String str)
           
 void setDropCascade(boolean dropCascade)
          Sets whether DROP statements should include a CASCADE directive at the end.
 void setSchemaQualified(boolean schemaQualified)
          Sets whether object names should be qualified with a schema name, if they have one.
protected abstract  boolean typeSupportsReplace(String typeName)
          Returns whether an object type supports CREATE OR REPLACE operation.
static String unquoteLiteral(String str)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sqlDialect

protected static final SqlDialect sqlDialect

VALUE_NULL

protected static final String VALUE_NULL
See Also:
Constant Field Values

NL

protected static final String NL

SEP

protected static final String SEP

ADDITIONAL_PARAMETER_TYPES

private static final List<Class> ADDITIONAL_PARAMETER_TYPES

schemaQualified

private boolean schemaQualified

dropCascade

protected boolean dropCascade

previousSetSchema

protected String previousSetSchema

visitDispatcher

private final ReflectiveVisitDispatcher<DdlGenerator,CwmModelElement> visitDispatcher
Constructor Detail

DdlGenerator

public DdlGenerator()
Method Detail

getModelView

protected abstract JmiModelView getModelView()

setSchemaQualified

public void setSchemaQualified(boolean schemaQualified)
Sets whether object names should be qualified with a schema name, if they have one. Default is false.

Parameters:
schemaQualified - whether to qualify object names with schema name

setDropCascade

public void setDropCascade(boolean dropCascade)
Sets whether DROP statements should include a CASCADE directive at the end. Default is false, so you must explicitly request CASCADE.

Parameters:
dropCascade - whether to append CASCADE to DROP statements

generateSetSchema

public boolean generateSetSchema(GeneratedDdlStmt stmt,
                                 String schemaName,
                                 boolean evenIfUnchanged)
Appends a 'SET SCHEMA' command to stmt if schemaName is not null. If evenIfUnchanged is true, does so even if the schema is the same as the previous call to this method.

Parameters:
stmt - Statement to append to
schemaName - Name of schema
evenIfUnchanged - Whether to generate again for same schema name
Returns:
whether SET SCHEMA command was generated

generateCreate

public void generateCreate(CwmModelElement e,
                           GeneratedDdlStmt stmt)

generateDrop

public void generateDrop(CwmModelElement e,
                         GeneratedDdlStmt stmt)

generate

private void generate(String method,
                      CwmModelElement e,
                      GeneratedDdlStmt stmt)

quote

public static String quote(String str)

literal

public static String literal(String str)

unquoteLiteral

public static String unquoteLiteral(String str)

getSqlTypeName

protected static SqlTypeName getSqlTypeName(CwmClassifier classifier)

hasPrimaryKeyConstraint

protected static boolean hasPrimaryKeyConstraint(FemStoredColumn col)

getExportText

public String getExportText(List<CwmModelElement> exportList,
                            boolean sort)
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.

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

lookupClass

private RefClass lookupClass(String className,
                             JmiModelGraph modelGraph)
Looks up a named class in the model, fails if not found.

Parameters:
className - Association name
modelGraph - Model graph
Returns:
Class, never null

lookupAssoc

private RefAssociation lookupAssoc(String assocName,
                                   JmiModelGraph modelGraph)
Looks up a named association in the model, fails if not found.

Parameters:
assocName - Association name
modelGraph - Model graph
Returns:
Association, never null

typeSupportsReplace

protected abstract boolean typeSupportsReplace(String typeName)
Returns whether an object type supports CREATE OR REPLACE operation.

Parameters:
typeName - Name of object type, e.g. "CLUSTERED INDEX"
Returns:
whether type supports REPLACE

gatherElements

public abstract 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.

Parameters:
list - List to populate
schemaName - Name of schema
includeNonSchemaElements - Whether to include elements which do not belong to a schema
catalog - Catalog

name

protected void name(StringBuilder sb,
                    CwmNamespace schema,
                    String objectName)
Outputs the name of an object, optionally qualified by a schema name.

Parameters:
sb - StringBuilder to write to
schema - Schema object belongs to, or null if object does not belong to a schema
objectName - Name of object