use of org.teiid.query.sql.symbol.ElementSymbol in project teiid by teiid.
the class DynamicCommandResolver method resolveCommand.
/**
* @see org.teiid.query.resolver.CommandResolver#resolveCommand(org.teiid.query.sql.lang.Command, TempMetadataAdapter, boolean)
*/
public void resolveCommand(Command command, TempMetadataAdapter metadata, boolean resolveNullLiterals) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
DynamicCommand dynamicCmd = (DynamicCommand) command;
Iterator columns = dynamicCmd.getAsColumns().iterator();
Set groups = new HashSet();
boolean resolvedColumns = false;
// if there is no into group, just create temp metadata ids
if (dynamicCmd.getIntoGroup() == null) {
while (columns.hasNext()) {
ElementSymbol column = (ElementSymbol) columns.next();
column.setMetadataID(new TempMetadataID(column.getShortName(), column.getType()));
}
} else if (dynamicCmd.getIntoGroup().isTempGroupSymbol()) {
resolvedColumns = true;
while (columns.hasNext()) {
ElementSymbol column = (ElementSymbol) columns.next();
column.setGroupSymbol(new GroupSymbol(dynamicCmd.getIntoGroup().getName()));
}
}
ResolverVisitor.resolveLanguageObject(dynamicCmd, groups, dynamicCmd.getExternalGroupContexts(), metadata);
String sqlType = DataTypeManager.getDataTypeName(dynamicCmd.getSql().getType());
String targetType = DataTypeManager.DefaultDataTypes.CLOB;
if (!targetType.equals(sqlType) && !DataTypeManager.isImplicitConversion(sqlType, targetType)) {
throw new QueryResolverException(QueryPlugin.Event.TEIID30100, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30100, sqlType));
}
dynamicCmd.setSql(ResolverUtil.convertExpression(dynamicCmd.getSql(), targetType, metadata));
if (dynamicCmd.getUsing() != null && !dynamicCmd.getUsing().isEmpty()) {
for (SetClause clause : dynamicCmd.getUsing().getClauses()) {
ElementSymbol id = clause.getSymbol();
id.setGroupSymbol(new GroupSymbol(ProcedureReservedWords.DVARS));
id.setType(clause.getValue().getType());
id.setMetadataID(new TempMetadataID(id.getName(), id.getType()));
}
}
GroupSymbol intoSymbol = dynamicCmd.getIntoGroup();
if (intoSymbol != null) {
if (!intoSymbol.isImplicitTempGroupSymbol()) {
ResolverUtil.resolveGroup(intoSymbol, metadata);
if (!resolvedColumns) {
// must be a temp table from a higher scope
for (ElementSymbol column : (List<ElementSymbol>) dynamicCmd.getAsColumns()) {
column.setGroupSymbol(dynamicCmd.getIntoGroup().clone());
}
}
} else {
List symbols = dynamicCmd.getAsColumns();
ResolverUtil.resolveImplicitTempGroup(metadata, intoSymbol, symbols);
}
}
}
use of org.teiid.query.sql.symbol.ElementSymbol in project teiid by teiid.
the class InsertResolver method resolveProceduralCommand.
/**
* Resolve an INSERT. Need to resolve elements, constants, types, etc.
* @see org.teiid.query.resolver.ProcedureContainerResolver#resolveProceduralCommand(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.TempMetadataAdapter)
*/
public void resolveProceduralCommand(Command command, TempMetadataAdapter metadata) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
// Cast to known type
Insert insert = (Insert) command;
if (insert.getValues() != null) {
QueryResolver.resolveSubqueries(command, metadata, null);
// variables and values must be resolved separately to account for implicitly defined temp groups
resolveList(insert.getValues(), metadata, insert.getExternalGroupContexts(), null);
}
boolean usingQuery = insert.getQueryExpression() != null;
// resolve subquery if there
if (usingQuery) {
QueryResolver.setChildMetadata(insert.getQueryExpression(), command);
QueryResolver.resolveCommand(insert.getQueryExpression(), metadata.getMetadata(), false);
}
Set<GroupSymbol> groups = new HashSet<GroupSymbol>();
groups.add(insert.getGroup());
// resolve any functions in the values
List values = insert.getValues();
if (usingQuery) {
values = insert.getQueryExpression().getProjectedSymbols();
}
if (insert.getVariables().isEmpty()) {
if (insert.getGroup().isResolved()) {
List<ElementSymbol> variables = ResolverUtil.resolveElementsInGroup(insert.getGroup(), metadata);
for (Iterator<ElementSymbol> i = variables.iterator(); i.hasNext(); ) {
insert.addVariable(i.next().clone());
}
} else {
for (int i = 0; i < values.size(); i++) {
if (usingQuery) {
Expression ses = (Expression) values.get(i);
ElementSymbol es = new ElementSymbol(Symbol.getShortName(ses));
es.setType(ses.getType());
insert.addVariable(es);
} else {
// $NON-NLS-1$
insert.addVariable(new ElementSymbol("expr" + i));
}
}
}
} else if (insert.getGroup().isResolved()) {
resolveVariables(metadata, insert, groups);
}
resolveTypes(insert, metadata, values, usingQuery);
if (usingQuery && insert.getQueryExpression() instanceof SetQuery) {
// now that the first branch is set, we need to make sure that all branches conform
QueryResolver.resolveCommand(insert.getQueryExpression(), metadata.getMetadata(), false);
resolveTypes(insert, metadata, values, usingQuery);
}
if (!insert.getGroup().isResolved()) {
// define the implicit temp group
ResolverUtil.resolveImplicitTempGroup(metadata, insert.getGroup(), insert.getVariables());
resolveVariables(metadata, insert, groups);
// ensure that the types match
resolveTypes(insert, metadata, values, usingQuery);
}
}
use of org.teiid.query.sql.symbol.ElementSymbol in project teiid by teiid.
the class InsertResolver method getAutoIncrementKey.
public static List<ElementSymbol> getAutoIncrementKey(Object metadataId, List<ElementSymbol> columns, QueryMetadataInterface metadata) throws QueryMetadataException, TeiidComponentException {
Object pk = metadata.getPrimaryKey(metadataId);
if (pk == null) {
return null;
}
List<?> cols = metadata.getElementIDsInKey(pk);
List<ElementSymbol> key = null;
for (ElementSymbol es : columns) {
if (cols.contains(es.getMetadataID())) {
if (!metadata.elementSupports(es.getMetadataID(), SupportConstants.Element.AUTO_INCREMENT) && !metadata.elementSupports(es.getMetadataID(), SupportConstants.Element.NULL)) {
return null;
}
if (key == null) {
key = new ArrayList<ElementSymbol>();
}
key.add(es);
}
}
return key;
}
use of org.teiid.query.sql.symbol.ElementSymbol in project teiid by teiid.
the class TempTableResolver method addAdditionalMetadata.
public static void addAdditionalMetadata(Create create, TempMetadataID tempTable) {
if (!create.getPrimaryKey().isEmpty()) {
ArrayList<TempMetadataID> primaryKey = new ArrayList<TempMetadataID>(create.getPrimaryKey().size());
for (ElementSymbol symbol : create.getPrimaryKey()) {
Object mid = symbol.getMetadataID();
if (mid instanceof TempMetadataID) {
primaryKey.add((TempMetadataID) mid);
} else if (mid instanceof Column) {
// TODO: this breaks our normal metadata usage
primaryKey.add(tempTable.getElements().get(((Column) mid).getPosition() - 1));
}
}
tempTable.setPrimaryKey(primaryKey);
}
for (int i = 0; i < create.getColumns().size(); i++) {
Column column = create.getColumns().get(i);
TempMetadataID tid = tempTable.getElements().get(i);
if (column.isAutoIncremented()) {
tid.setAutoIncrement(true);
}
if (column.getNullType() == NullType.No_Nulls) {
tid.setNotNull(true);
}
}
}
use of org.teiid.query.sql.symbol.ElementSymbol in project teiid by teiid.
the class UpdateProcedureResolver method collectDeclareVariable.
private void collectDeclareVariable(DeclareStatement obj, GroupSymbol variables, TempMetadataAdapter metadata, GroupContext externalGroups) throws QueryResolverException, TeiidComponentException {
ElementSymbol variable = obj.getVariable();
String typeName = obj.getVariableType();
GroupSymbol gs = variable.getGroupSymbol();
if (gs == null) {
String outputName = variable.getShortName();
variable.setGroupSymbol(new GroupSymbol(ProcedureReservedWords.VARIABLES));
variable.setOutputName(outputName);
} else {
if (gs.getSchema() != null || !gs.getShortName().equalsIgnoreCase(ProcedureReservedWords.VARIABLES)) {
// $NON-NLS-1$
handleUnresolvableDeclaration(variable, QueryPlugin.Util.getString("ERR.015.010.0031", new Object[] { ProcedureReservedWords.VARIABLES, variable }));
}
}
boolean exists = false;
try {
ResolverVisitor.resolveLanguageObject(variable, null, externalGroups, metadata);
exists = true;
} catch (QueryResolverException e) {
// ignore, not already defined
}
if (exists) {
// $NON-NLS-1$
handleUnresolvableDeclaration(variable, QueryPlugin.Util.getString("ERR.015.010.0032", variable.getOutputName()));
}
boolean exception = typeName.equalsIgnoreCase(SQLConstants.NonReserved.EXCEPTION);
variable.setType(exception ? DataTypeManager.DefaultDataClasses.OBJECT : DataTypeManager.getDataTypeClass(typeName));
variable.setGroupSymbol(variables);
TempMetadataID id = new TempMetadataID(variable.getName(), exception ? Exception.class : variable.getType());
id.setUpdatable(true);
variable.setMetadataID(id);
// TODO: this will cause the variables group to loose it's cache of resolved symbols
metadata.getMetadataStore().addElementToTempGroup(ProcedureReservedWords.VARIABLES, variable.clone());
}
Aggregations