/*
* 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.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.modeshape.sequencer.ddl.StandardDdlLexicon;
import org.modeshape.sequencer.ddl.dialect.mysql.MySqlDdlLexicon;
import org.modeshape.sequencer.ddl.node.AstNode;
import org.teiid.designer.relational.model.RelationalIndex;
import org.teiid.designer.relational.model.RelationalModel;
import org.teiid.designer.relational.model.RelationalReference;
import org.teiid.designer.relational.model.RelationalSchema;
import org.teiid.designer.relational.model.RelationalTable;
/**
*
*/
public class MySQLImporter extends StandardImporter {
/**
* Create RelationalReference objects
* @param node the provided AstNode
* @param model the RelationalModel being created
* @param schema the schema
* @return the map of AstNodes which need to be deferred
* @throws Exception
*/
@Override
protected Map<AstNode,RelationalReference> createObject(AstNode node, RelationalModel model, RelationalSchema schema) throws Exception {
Map<AstNode,RelationalReference> deferredMap = new HashMap<AstNode,RelationalReference>();
if (is(node, MySqlDdlLexicon.TYPE_CREATE_INDEX_STATEMENT)) {
deferredMap.put(node, null);
} else if (is(node, MySqlDdlLexicon.TYPE_CREATE_PROCEDURE_STATEMENT)) {
createProcedure(node, model);
} else if (is(node, MySqlDdlLexicon.TYPE_CREATE_FUNCTION_STATEMENT)) {
createProcedure(node, model).setFunction(true);
} else
return super.createObject(node, model, schema);
return deferredMap;
}
/**
* Create deferred objects using the supplied map
* @param deferredNodes the map of deferred AstNodes
* @param model the RelationalModel being created
* @throws Exception
*/
@Override
protected void createDeferredObjects(Map<AstNode,RelationalReference> deferredNodes, RelationalModel model) throws Exception {
Collection<RelationalReference> allRefs = model.getAllReferences();
// Make first pass to create the PKs
Set<AstNode> astNodes = deferredNodes.keySet();
for(AstNode node:astNodes) {
if (is(node, StandardDdlLexicon.TYPE_TABLE_CONSTRAINT)) {
RelationalTable table = (RelationalTable)deferredNodes.get(node);
createPrimaryKey(node, table, allRefs);
} else if (is(node, StandardDdlLexicon.TYPE_ALTER_TABLE_STATEMENT)) {
RelationalTable table = find(RelationalTable.class, node, null, allRefs);
for (AstNode node1 : node) {
if (is(node1, StandardDdlLexicon.TYPE_ADD_TABLE_CONSTRAINT_DEFINITION))
createPrimaryKey(node1, table, allRefs);
}
}
}
// Second pass create other constraints
for(AstNode node:astNodes) {
if (is(node, MySqlDdlLexicon.TYPE_CREATE_INDEX_STATEMENT)) {
RelationalIndex index = getFactory().createIndex();
Info info = createInfo(node, model);
if (info.getSchema() == null)
model.addChild(index);
else
info.getSchema().getIndexes().add(index);
initialize(index, node, info.getName());
} else if (is(node, StandardDdlLexicon.TYPE_TABLE_CONSTRAINT)) {
RelationalTable table = (RelationalTable)deferredNodes.get(node);
createConstraint(node, table, allRefs);
} else if (is(node, StandardDdlLexicon.TYPE_ALTER_TABLE_STATEMENT)) {
RelationalTable table = find(RelationalTable.class, node, null, allRefs);
for (AstNode node1 : node) {
if (is(node1, StandardDdlLexicon.TYPE_ADD_TABLE_CONSTRAINT_DEFINITION))
createConstraint(node1, table, allRefs);
else if (is(node1, StandardDdlLexicon.TYPE_ADD_COLUMN_DEFINITION))
createColumn(node1, table);
}
}
}
}
}