Skip to content

Commit

Permalink
TEIIDDES-1739: Refactors the DDL importer
Browse files Browse the repository at this point in the history
* Separates out the ddl importer into specialised node sub-importers

* Node Importers keys against the parser id returned from modeshape

* spi plugin provides the node importer registry and extension point
  • Loading branch information
Paul Richardson committed Jun 10, 2013
1 parent 1165869 commit 65b9194
Show file tree
Hide file tree
Showing 28 changed files with 2,273 additions and 1,051 deletions.
7 changes: 7 additions & 0 deletions features/org.teiid.designer.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,11 @@
version="0.0.0"
unpack="false"/>

<plugin
id="org.teiid.designer.ddl.spi"
download-size="0"
install-size="0"
version="0.0.0"
unpack="false"/>

</feature>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.teiid.core.designer.ModelerCoreException;
import org.teiid.designer.core.util.ModelContents;
import org.teiid.designer.core.workspace.ModelWorkspaceException;
Expand All @@ -28,7 +29,7 @@ public interface ModelSelector {

public void setLabel( String label );

public List getRootObjects() throws ModelerCoreException;
public List<EObject> getRootObjects() throws ModelerCoreException;

public URI getUri();

Expand Down
3 changes: 2 additions & 1 deletion plugins/org.teiid.designer.ddl.importer/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Require-Bundle: org.teiid.designer.compare;bundle-version="[8.1.0,9.0.0)",
org.eclipse.emf.ecore;bundle-version="[2.8.0,3.0.0)",
org.eclipse.core.resources;bundle-version="[3.8.0,4.0.0)",
org.eclipse.core.runtime;bundle-version="[3.8.0,4.0.0)",
org.teiid.designer.modeshape;bundle-version="[8.1.0,9.0.0)"
org.teiid.designer.modeshape;bundle-version="[8.1.0,9.0.0)",
org.teiid.designer.ddl.spi;bundle-version="[8.2.0,9.0.0)"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
Expand Down
3 changes: 2 additions & 1 deletion plugins/org.teiid.designer.ddl.importer/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ bin.includes = META-INF/,\
.,\
OSGI-INF/,\
OSGI-INF/l10n/,\
LEGAL.txt
LEGAL.txt,\
plugin.xml
28 changes: 28 additions & 0 deletions plugins/org.teiid.designer.ddl.importer/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.teiid.designer.ddl.nodeImporter">
<importer
class="org.teiid.designer.ddl.importer.node.StandardImporter"
dialect="Standard">
</importer>
<importer
class="org.teiid.designer.ddl.importer.node.OracleImporter"
dialect="Oracle">
</importer>
<importer
class="org.teiid.designer.ddl.importer.node.DerbyImporter"
dialect="Derby">
</importer>
<importer
class="org.teiid.designer.ddl.importer.node.PostgresImporter"
dialect="Postgres">
</importer>
<importer
class="org.teiid.designer.ddl.importer.node.TeiidDdlImporter"
dialect="Teiid">
</importer>
</extension>

</plugin>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@

import static org.teiid.designer.ddl.importer.DdlImporterPlugin.i18n;

class DdlImporterI18n {
static final String CREATING_CHANGE_REPORT_MSG = i18n("creatingChangeReportMsg"); //$NON-NLS-1$
static final String CREATING_MODEL_MSG = i18n("creatingModelMsg"); //$NON-NLS-1$
static final String DDL_FILE_NOT_FOUND_MSG = i18n("ddlFileNotFoundMsg"); //$NON-NLS-1$
static final String ENTITY_NOT_FOUND_MSG = i18n("entityNotFoundMsg"); //$NON-NLS-1$
static final String INVALID_MODEL_FOLDER_MSG = i18n("invalidModelFolderMsg"); //$NON-NLS-1$
static final String INVALID_MODEL_NAME_MSG = i18n("invalidModelNameMsg"); //$NON-NLS-1$
static final String MODEL = i18n("model"); //$NON-NLS-1$
static final String MODEL_FOLDER_IN_NON_MODEL_PROJECT_MSG = i18n("modelFolderInNonModelProjectMsg"); //$NON-NLS-1$
static final String MODEL_FOLDER_IS_FILE_MSG = i18n("modelFolderIsFileMsg"); //$NON-NLS-1$
static final String MODEL_NAME_IS_FOLDER_MSG = i18n("modelNameIsFolderMsg"); //$NON-NLS-1$
static final String MODEL_NAME_IS_NON_MODEL_FILE_MSG = i18n("modelNameIsNonModelFileMsg"); //$NON-NLS-1$
static final String MODEL_NAME_IS_NON_RELATIONAL_MODEL_MSG = i18n("modelNameIsNonRelationalModelMsg"); //$NON-NLS-1$
static final String PARSING_DDL_MSG = i18n("parsingDdlMsg"); //$NON-NLS-1$
static final String SAVING_MODEL_MSG = i18n("savingModelMsg"); //$NON-NLS-1$
static final String FK_TABLE_REF_NOT_FOUND_MSG = i18n("fkTableRefNotFoundMsg"); //$NON-NLS-1$

private DdlImporterI18n() {
}
/**
* i18n constants
*/
public interface DdlImporterI18n {
String CREATING_CHANGE_REPORT_MSG = i18n("creatingChangeReportMsg"); //$NON-NLS-1$
String CREATING_MODEL_MSG = i18n("creatingModelMsg"); //$NON-NLS-1$
String DDL_FILE_NOT_FOUND_MSG = i18n("ddlFileNotFoundMsg"); //$NON-NLS-1$
String ENTITY_NOT_FOUND_MSG = i18n("entityNotFoundMsg"); //$NON-NLS-1$
String INVALID_MODEL_FOLDER_MSG = i18n("invalidModelFolderMsg"); //$NON-NLS-1$
String INVALID_MODEL_NAME_MSG = i18n("invalidModelNameMsg"); //$NON-NLS-1$
String MODEL = i18n("model"); //$NON-NLS-1$
String MODEL_FOLDER_IN_NON_MODEL_PROJECT_MSG = i18n("modelFolderInNonModelProjectMsg"); //$NON-NLS-1$
String MODEL_FOLDER_IS_FILE_MSG = i18n("modelFolderIsFileMsg"); //$NON-NLS-1$
String MODEL_NAME_IS_FOLDER_MSG = i18n("modelNameIsFolderMsg"); //$NON-NLS-1$
String MODEL_NAME_IS_NON_MODEL_FILE_MSG = i18n("modelNameIsNonModelFileMsg"); //$NON-NLS-1$
String MODEL_NAME_IS_NON_RELATIONAL_MODEL_MSG = i18n("modelNameIsNonRelationalModelMsg"); //$NON-NLS-1$
String PARSING_DDL_MSG = i18n("parsingDdlMsg"); //$NON-NLS-1$
String SAVING_MODEL_MSG = i18n("savingModelMsg"); //$NON-NLS-1$
String FK_TABLE_REF_NOT_FOUND_MSG = i18n("fkTableRefNotFoundMsg"); //$NON-NLS-1$
String FAILURE_IMPORT_MSG = i18n("importFailureMsg"); //$NON-NLS-1$
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ static String i18n( final String key ) {
return UTIL.getString(key);
}

/**
* Not intended for use by any class other than {@link DdlImporterI18n}.
*
* @param key
* @param parameters
* @return The i18n template associated with the supplied key
*/
static String i18n( final String key, Object... parameters) {
return UTIL.getString(key, parameters);
}

/**
* @return the singleton instance of this type
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ modelNameIsNonRelationalModelMsg = The supplied model name refers to an existing
parsingDdlMsg = Parsing DDL...
savingModelMsg = Saving model...
fkTableRefNotFoundMsg = Table reference not found for Foreign Key
importFailureMsg = Failed to import node
noDDLImporterRegisteredMsg = No ddl node importer registered for the {0} parser
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.designer.ddl.importer.node;

import org.modeshape.sequencer.ddl.node.AstNode;
import org.teiid.designer.ddl.DdlImporterModel;
import org.teiid.designer.ddl.DdlNodeImporter;
import org.teiid.designer.metamodels.relational.RelationalFactory;

/**
*
*/
public abstract class AbstractImporter implements DdlNodeImporter {

/**
* Entity Not Found Exception
*/
protected class EntityNotFoundException extends Exception {

private static final long serialVersionUID = 1L;

/**
* @param message
*/
public EntityNotFoundException(String message) {
super(message);
}
}

private DdlImporterModel importerModel;

/**
* @param importerModel
*/
protected void setImporterModel(DdlImporterModel importerModel) {
this.importerModel = importerModel;
}

/**
* @return the importerModel
*/
protected DdlImporterModel getImporterModel() {
return this.importerModel;
}

/**
* @return relational factory
*/
protected RelationalFactory getFactory() {
return importerModel.getFactory();
}

/**
* @param message
*/
protected void addProgressMessage(String message) {
importerModel.getProgressMessages().add(message);
}

/**
* Gets boolean value for the provided text string
*
* @param text a text string
* @return 'true' if provided string is "true", otherwise 'false'
*/
protected boolean isTrue(String text) {
return Boolean.valueOf(text);
}

/**
* Is given node of the given dialect
*
* @param node
* @param dialectType
* @return true if node has dialect
*/
protected boolean is(AstNode node, String dialectType) {
return node.hasMixin(dialectType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.designer.ddl.importer.node;

import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.modeshape.sequencer.ddl.StandardDdlLexicon;
import org.modeshape.sequencer.ddl.dialect.derby.DerbyDdlLexicon;
import org.modeshape.sequencer.ddl.node.AstNode;
import org.teiid.designer.metamodels.relational.Column;
import org.teiid.designer.metamodels.relational.Index;
import org.teiid.designer.metamodels.relational.NullableType;
import org.teiid.designer.metamodels.relational.Procedure;
import org.teiid.designer.metamodels.relational.ProcedureParameter;
import org.teiid.designer.metamodels.relational.Schema;
import org.teiid.designer.metamodels.relational.Table;

/**
*
*/
public class DerbyImporter extends StandardImporter {


@Override
protected Procedure createProcedure(AstNode procedureNode, List<EObject> roots) throws Exception {
Procedure procedure = super.createProcedure(procedureNode, roots);

for (AstNode child : procedureNode) {
if (! is(child, DerbyDdlLexicon.TYPE_FUNCTION_PARAMETER))
continue;

ProcedureParameter prm = getFactory().createProcedureParameter();
procedure.getParameters().add(prm);
initialize(prm, child);
String datatype = child.getProperty(StandardDdlLexicon.DATATYPE_NAME).toString();
prm.setNativeType(datatype);

EObject type = getDataType(datatype);
prm.setType(type);

Object prop = child.getProperty(StandardDdlLexicon.DATATYPE_LENGTH);
if (prop != null)
prm.setLength(Integer.parseInt(prop.toString()));

prop = child.getProperty(StandardDdlLexicon.DATATYPE_PRECISION);
if (prop != null)
prm.setPrecision(Integer.parseInt(prop.toString()));

prop = child.getProperty(StandardDdlLexicon.DATATYPE_SCALE);
if (prop != null)
prm.setScale(Integer.parseInt(prop.toString()));

prop = child.getProperty(StandardDdlLexicon.NULLABLE);
if (prop != null)
prm.setNullable(prop.toString().equals("NULL") ? NullableType.NULLABLE_LITERAL : NullableType.NO_NULLS_LITERAL); //$NON-NLS-1$

prop = child.getProperty(StandardDdlLexicon.DEFAULT_VALUE);
if (prop != null)
prm.setDefaultValue(prop.toString());
}

return procedure;
}

@Override
protected void create(AstNode node, List<EObject> roots, Schema schema) throws Exception {
if (is(node, DerbyDdlLexicon.TYPE_CREATE_INDEX_STATEMENT)) {
Index index = getFactory().createIndex();
Info info = createInfo(node, roots);
if (info.getSchema() == null)
roots.add(index);
else
info.getSchema().getIndexes().add(index);

initialize(index, node, info.getName());
Object prop = node.getProperty(DerbyDdlLexicon.UNIQUE_INDEX);
if (prop != null)
index.setUnique((Boolean) prop);

prop = node.getProperty(DerbyDdlLexicon.TABLE_NAME);
if (prop == null)
return;

try {
Table table = find(Table.class, prop.toString(), node, null, roots);
for (AstNode node1 : node) {
// Probably need to check for a simple column reference for Oracle
if (node1.hasMixin(DerbyDdlLexicon.TYPE_INDEX_COLUMN_REFERENCE)) {
try {
index.getColumns().add(find(Column.class, node1, table, roots));
} catch (EntityNotFoundException error) {
addProgressMessage(error.getMessage());
}
}
}
} catch (EntityNotFoundException error) {
addProgressMessage(error.getMessage());
}
} else if (is(node, DerbyDdlLexicon.TYPE_CREATE_PROCEDURE_STATEMENT)) {
createProcedure(node, roots);
} else if (is(node, DerbyDdlLexicon.TYPE_CREATE_FUNCTION_STATEMENT)) {
createProcedure(node, roots).setFunction(true);
} else {
super.create(node, roots, schema);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.designer.ddl.importer.node;

import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.modeshape.sequencer.ddl.dialect.mysql.MySqlDdlLexicon;
import org.modeshape.sequencer.ddl.node.AstNode;
import org.teiid.designer.metamodels.relational.Index;
import org.teiid.designer.metamodels.relational.Schema;

/**
*
*/
public class MySQLImporter extends StandardImporter {

@Override
protected void create(AstNode node, List<EObject> roots, Schema schema) throws Exception {
if (is(node, MySqlDdlLexicon.TYPE_CREATE_INDEX_STATEMENT)) {
Index index = getFactory().createIndex();
Info info = createInfo(node, roots);
if (info.getSchema() == null)
roots.add(index);
else
info.getSchema().getIndexes().add(index);

initialize(index, node, info.getName());
} else if (is(node, MySqlDdlLexicon.TYPE_CREATE_PROCEDURE_STATEMENT)) {
createProcedure(node, roots);
} else if (is(node, MySqlDdlLexicon.TYPE_CREATE_FUNCTION_STATEMENT)) {
createProcedure(node, roots).setFunction(true);
} else
super.create(node, roots, schema);
}
}

0 comments on commit 65b9194

Please sign in to comment.