Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions komodo-core/src/main/resources/config/TeiidSql.cnd
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@
+ tsql:from (tsql:from)
+ tsql:criteria (tsql:criteria)
+ tsql:select (tsql:select)

[tsql:create] > tsql:command mixin
- tsql:onCommit (string)
+ tsql:table (tsql:groupSymbol)
+ tsql:tableColumn (tsql:multipleElementSymbol)
+ tsql:primaryKeyColumn (tsql:multipleElementSymbol)

[tsql:setQuery] > tsql:queryCommand mixin
- tsql:all (boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.Test;
import org.komodo.modeshape.AbstractTSqlSequencerTest;
import org.komodo.modeshape.teiid.language.SortSpecification.NullOrdering;
import org.komodo.osgi.PluginService;
import org.komodo.repository.KSequencerController.SequencerType;
import org.komodo.spi.lexicon.TeiidSqlLexicon.AbstractCompareCriteria;
import org.komodo.spi.lexicon.TeiidSqlLexicon.AbstractSetCriteria;
Expand All @@ -37,6 +36,7 @@
import org.komodo.spi.lexicon.TeiidSqlLexicon.CommandStatement;
import org.komodo.spi.lexicon.TeiidSqlLexicon.CompareCriteria;
import org.komodo.spi.lexicon.TeiidSqlLexicon.Constant;
import org.komodo.spi.lexicon.TeiidSqlLexicon.Create;
import org.komodo.spi.lexicon.TeiidSqlLexicon.CreateProcedureCommand;
import org.komodo.spi.lexicon.TeiidSqlLexicon.DeclareStatement;
import org.komodo.spi.lexicon.TeiidSqlLexicon.DerivedColumn;
Expand Down Expand Up @@ -80,7 +80,6 @@
import org.komodo.spi.query.CriteriaOperator;
import org.komodo.spi.query.JoinTypeTypes;
import org.komodo.spi.query.Operation;
import org.komodo.spi.query.TeiidService;
import org.komodo.spi.runtime.version.TeiidVersion;
import org.komodo.spi.type.DataTypeManager.DataTypeName;

Expand Down Expand Up @@ -1775,4 +1774,40 @@ public void testWindowFunction() throws Exception {
verifySql("SELECT ROW_NUMBER() OVER (PARTITION BY x ORDER BY y) FROM g", fileNode);
}

@Test
public void testLocalTable() throws Exception {
String sql = "Create local TEMPORARY table x(c1 boolean, c2 byte, c3 string);";
Node fileNode = sequenceSql(sql, TSQL_QUERY);

Node createNode = verify(fileNode, Create.ID, Create.ID);

Node tableNode = verify(createNode, Create.TABLE_REF_NAME, GroupSymbol.ID);
verifyProperty(tableNode, Symbol.SHORT_NAME_PROP_NAME, "x");

Node columnNode=verify(createNode,Create.COLUMNS_REF_NAME,ElementSymbol.ID);
verifyProperty(columnNode, Symbol.SHORT_NAME_PROP_NAME, "c1");
verifyProperty(columnNode,Expression.TYPE_CLASS_PROP_NAME,"BOOLEAN");

verifySql("CREATE LOCAL TEMPORARY TABLE x(c1 BOOLEAN, c2 BYTE, c3 STRING)", fileNode);
}

@Test
public void testLocalTablePreserveRows() throws Exception {
String sql = "Create local TEMPORARY table x(c1 boolean, c2 byte, c3 string, primary key (c2, c3)) ON COMMIT PRESERVE ROWS;";
Node fileNode = sequenceSql(sql, TSQL_QUERY);

Node createNode = verify(fileNode, Create.ID, Create.ID);

Node tableNode = verify(createNode, Create.TABLE_REF_NAME, GroupSymbol.ID);
verifyProperty(tableNode, Symbol.SHORT_NAME_PROP_NAME, "x");

Node columnNode=verify(createNode,Create.COLUMNS_REF_NAME,ElementSymbol.ID);
verifyProperty(columnNode, Symbol.SHORT_NAME_PROP_NAME, "c1");
verifyProperty(columnNode,Expression.TYPE_CLASS_PROP_NAME,"BOOLEAN");

verify(createNode,Create.PRIMARY_KEY_COLUMNS_REF_NAME,ElementSymbol.ID);

verifySql("CREATE LOCAL TEMPORARY TABLE x(c1 BOOLEAN, c2 BYTE, c3 STRING, PRIMARY KEY(c2, c3)) ON COMMIT PRESERVE ROWS", fileNode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import org.komodo.modeshape.teiid.language.SortSpecification.NullOrdering;
import org.komodo.spi.constants.StringConstants;
import org.komodo.spi.lexicon.TeiidSqlConstants;
import org.komodo.spi.lexicon.TeiidSqlContext;
import org.komodo.spi.lexicon.TeiidSqlConstants.NonReserved;
import org.komodo.spi.lexicon.TeiidSqlConstants.Reserved;
import org.komodo.spi.lexicon.TeiidSqlConstants.Tokens;
import org.komodo.spi.lexicon.TeiidSqlContext;
import org.komodo.spi.lexicon.TeiidSqlLexicon;
import org.komodo.spi.lexicon.TeiidSqlLexicon.*;
import org.komodo.spi.query.AggregateFunctions;
Expand All @@ -53,10 +53,10 @@
import org.komodo.spi.query.JoinTypeTypes;
import org.komodo.spi.query.LogicalOperator;
import org.komodo.spi.query.MatchMode;
import org.komodo.spi.query.PredicateQuantifier;
import org.komodo.spi.query.TriggerEvent;
import org.komodo.spi.query.Operation;
import org.komodo.spi.query.ParameterInfo;
import org.komodo.spi.query.PredicateQuantifier;
import org.komodo.spi.query.TriggerEvent;
import org.komodo.spi.runtime.version.DefaultTeiidVersion.Version;
import org.komodo.spi.runtime.version.TeiidVersion;
import org.komodo.spi.type.DataTypeManager.DataTypeName;
Expand Down Expand Up @@ -742,6 +742,9 @@ protected void visit(Node node, TeiidSqlContext context) throws RepositoryExcept
break;
case INSERT:
insert(context);
break;
case CREATE:
createLocalTempTable(context);
break;
case STORED_PROCEDURE:
storedProcedure(context);
Expand Down Expand Up @@ -1680,6 +1683,77 @@ public Object dynamicCommand(TeiidSqlContext context) throws Exception {
return null;
}

public Object createLocalTempTable(TeiidSqlContext context) throws Exception {
Node node = (Node)context.get(NODE_KEY);

append(CREATE);
append(SPACE);
append(LOCAL);
append(SPACE);
append(TEMPORARY);
append(SPACE);
append(TABLE);
append(SPACE);

Node table = reference(node, Create.TABLE_REF_NAME);
visit(table);

append(OPEN_BRACKET);

tableColumns(context);
primaryKey(context);

append(CLOSE_BRACKET);
String onCommit = propertyString(node, Create.ON_COMMIT_PROP_NAME);
if (onCommit != null && onCommit.equals(Create.ON_COMMIT_PROP_VALUE)) {
append(SPACE);
append(ON);
append(SPACE);
append(COMMIT);
append(SPACE);
append(PRESERVE);
append(SPACE);
append(ROWS);
}

return null;
}

public Object primaryKey(TeiidSqlContext context) throws Exception {
Node node = (Node) context.get(NODE_KEY);
Iterator<Node> pkColumns = references(node, Create.PRIMARY_KEY_COLUMNS_REF_NAME);
if(pkColumns.hasNext()){
append(COMMA);
append(SPACE);
append(PRIMARY);
append(SPACE);
append(KEY);
append(OPEN_BRACKET);
for (int i = 0; pkColumns.hasNext(); ++i) {
if (i > 0)
append(COMMA + SPACE);
Node pkColumn = pkColumns.next();
append(propertyString(pkColumn,Symbol.SHORT_NAME_PROP_NAME));
}
append(CLOSE_BRACKET);
}
return null;
}

public Object tableColumns(TeiidSqlContext context) throws Exception {
Node node = (Node) context.get(NODE_KEY);
Iterator<Node> columns = references(node, Create.COLUMNS_REF_NAME);
for (int i = 0; columns.hasNext(); ++i) {
if (i > 0)
append(COMMA + SPACE);
Node column = columns.next();
append(propertyString(column,Symbol.SHORT_NAME_PROP_NAME));
append(SPACE);
append(propertyString(column,Expression.TYPE_CLASS_PROP_NAME));
}
return null;
}

public Object query(TeiidSqlContext context) throws Exception {
Node node = (Node) context.get(NODE_KEY);
addWithClause(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@
+ tsql:into (tsql:into)
+ tsql:criteria (tsql:criteria)
+ tsql:groupBy (tsql:groupBy)


[tsql:create] > tsql:command mixin
- tsql:onCommit (string)
+ tsql:table (tsql:groupSymbol)
+ tsql:tableColumn (tsql:multipleElementSymbol)
+ tsql:primaryKeyColumn (tsql:multipleElementSymbol)

[tsql:setQuery] > tsql:queryCommand mixin
- tsql:operation (string) < "UNION", "INTERSECT", "EXCEPT"
- tsql:all (boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.nodetype.NodeType;

import org.komodo.modeshape.AbstractNodeVisitor;
import org.komodo.modeshape.teiid.TeiidSqlNodeVisitor;
import org.komodo.spi.constants.StringConstants;
import org.komodo.spi.ddl.TeiidDDLConstants;
import org.komodo.spi.lexicon.TeiidSqlConstants;
import org.komodo.spi.lexicon.TeiidSqlConstants.NonReserved;
import org.komodo.spi.lexicon.TeiidSqlConstants.Reserved;
import org.komodo.spi.ddl.TeiidDDLConstants;
import org.komodo.spi.metadata.MetadataNamespaces;
import org.komodo.spi.runtime.version.TeiidVersion;
import org.komodo.spi.type.DataTypeManager.DataTypeName;
Expand Down Expand Up @@ -111,6 +110,10 @@ private enum MixinTypeName {

CREATE_FUNCTION(TeiidDdlLexicon.CreateProcedure.FUNCTION_STATEMENT),

CREATE_GLOBAL_TEMP_TABLE(TeiidDdlLexicon.CreateTable.GLOBAL_TEMP_TABLE_STATEMENT),

CREATE_FOREIGN_TEMP_TABLE(TeiidDdlLexicon.CreateTable.FOREIGN_TEMP_TABLE_STATEMENT),

UNKNOWN(UNDEFINED);

private String nodeTypeId;
Expand Down Expand Up @@ -140,15 +143,9 @@ public static MixinTypeName findName(NodeType nodeType) {

}

private enum TableType {
TABLE,
VIEW,
GLOBAL_TEMP_TABLE;
}

private class CreateObjectContext {

private TableType tableType = TableType.TABLE;
private MixinTypeName tableType=MixinTypeName.UNKNOWN;

private boolean virtual = false;

Expand All @@ -168,11 +165,11 @@ public void setVirtual(boolean virtual) {
this.virtual = virtual;
}

public TableType getTableType() {
public MixinTypeName getTableType() {
return tableType;
}

public void setTableType(TableType tableType) {
public void setTableType(MixinTypeName tableType) {
this.tableType = tableType;
}
}
Expand Down Expand Up @@ -567,7 +564,7 @@ private void tableElement(Node tableElement, CreateObjectContext context) throws

ColumnContext columnContext = createColumnContext(tableElement);

if (TableType.GLOBAL_TEMP_TABLE == context.getTableType() && columnContext.isAutoIncremented() &&
if (MixinTypeName.CREATE_GLOBAL_TEMP_TABLE == context.getTableType() && columnContext.isAutoIncremented() &&
columnContext.isNotNull() &&
DataTypeName.INTEGER.equals(columnContext.getDataTypeName())) {
append(escapeSinglePart(tableElement.getName()));
Expand Down Expand Up @@ -735,10 +732,10 @@ private String schemaElementType(Node node) throws Exception {

private void tabulation(Node tabulation, CreateObjectContext context) throws Exception {
append(SPACE);

addTableBody(tabulation, context);

if (TableType.GLOBAL_TEMP_TABLE != context.getTableType()) {
if (
MixinTypeName.CREATE_FOREIGN_TEMP_TABLE !=context.getTableType()) {
if (context.isVirtual()) {
TeiidSqlNodeVisitor visitor = new TeiidSqlNodeVisitor(getVersion());
String teiidSql = visitor.getTeiidSql(tabulation);
Expand All @@ -763,17 +760,63 @@ private void table(Node table) throws Exception {
append(CREATE).append(SPACE);

if (context.isPhysical()) {
context.setTableType(TableType.TABLE);
context.setTableType(MixinTypeName.CREATE_TABLE);
append(FOREIGN).append(SPACE).append(TABLE);
}
else {
context.setTableType(TableType.GLOBAL_TEMP_TABLE);
append(GLOBAL).append(SPACE).append(TEMPORARY).append(SPACE).append(TABLE);
}

tabulation(table, context);
}

/**
* @param node
* @throws RepositoryException
*/
private void globalTempTable(Node temptable) throws Exception {
if (! includeTables)
return;

if (!hasMixinType(temptable, TeiidDdlLexicon.CreateTable.GLOBAL_TEMP_TABLE_STATEMENT))
return;

append(NEW_LINE);
append(CREATE).append(SPACE).append(GLOBAL).append(SPACE).append(TEMPORARY).append(SPACE).append(TABLE);
CreateObjectContext context = new CreateObjectContext();
context.setPhysical(false);
context.setTableType(MixinTypeName.CREATE_GLOBAL_TEMP_TABLE);
context.setPhysical(true);
tabulation(temptable,context);
}

/**
* @param node
* @throws RepositoryException
*/
private void foreignTempTable(Node temptable) throws Exception {
if (! includeTables)
return;

if (!hasMixinType(temptable, TeiidDdlLexicon.CreateTable.FOREIGN_TEMP_TABLE_STATEMENT))
return;

append(NEW_LINE);
append(CREATE).append(SPACE).append(FOREIGN).append(SPACE).append(TEMPORARY).append(SPACE).append(TABLE);
CreateObjectContext context = new CreateObjectContext();
context.setPhysical(false);
context.setTableType(MixinTypeName.CREATE_FOREIGN_TEMP_TABLE);
context.setPhysical(true);
tabulation(temptable,context);
PropertyIterator props=temptable.getProperties();
Property property;
String schemaRef=null;
while(props.hasNext()){
property= props.nextProperty();
if(property.getName().equals(TeiidDdlLexicon.CreateTable.SCHEMA_REFERENCE)){
schemaRef=property.getValue().getString();
}
}
append(SPACE).append(ON).append(SPACE).append(schemaRef).append(SEMI_COLON);
}

private void view(Node view) throws Exception {
if (! includeTables)
return;
Expand All @@ -785,7 +828,7 @@ private void view(Node view) throws Exception {

CreateObjectContext context = new CreateObjectContext();
context.setVirtual(true);
context.setTableType(TableType.VIEW);
context.setTableType(MixinTypeName.CREATE_VIEW);

append(CREATE).append(SPACE).append(VIEW);

Expand Down Expand Up @@ -982,6 +1025,12 @@ public void visit(Node node) throws RepositoryException {
function(node);
append(NEW_LINE);
break;
case CREATE_GLOBAL_TEMP_TABLE:
globalTempTable(node);
break;
case CREATE_FOREIGN_TEMP_TABLE:
foreignTempTable(node);
break;
case UNKNOWN:
default:
// Not a node we are interested in but may contain such nodes
Expand All @@ -992,6 +1041,8 @@ public void visit(Node node) throws RepositoryException {
}
}



@Override
public void visit(Property property) {
// Not used
Expand Down
Loading