use of org.teiid.core.TeiidException in project teiid by teiid.
the class TeiidSQLException method create.
public static TeiidSQLException create(Throwable exception, String message) {
message = getMessage(exception, message);
Throwable origException = exception;
if (exception instanceof TeiidSQLException && message.equals(exception.getMessage())) {
return (TeiidSQLException) exception;
}
if (exception instanceof SQLException) {
return new TeiidSQLException((SQLException) exception, message, true);
}
String sqlState = null;
int errorCode = 0;
SQLException se = ExceptionUtil.getExceptionOfType(exception, SQLException.class);
if (se != null && se.getSQLState() != null) {
sqlState = se.getSQLState();
errorCode = se.getErrorCode();
}
TeiidException te = ExceptionUtil.getExceptionOfType(exception, TeiidException.class);
String code = null;
if (te != null && te.getCode() != null) {
code = te.getCode();
if (errorCode == 0) {
String intPart = code;
if (code.startsWith("TEIID")) {
// $NON-NLS-1$
intPart = code.substring(5);
}
try {
errorCode = Integer.valueOf(intPart);
} catch (NumberFormatException e) {
}
}
}
if (sqlState == null) {
exception = findRootException(exception);
sqlState = determineSQLState(exception, sqlState);
}
if (sqlState == null) {
sqlState = SQLStates.DEFAULT;
}
TeiidSQLException tse = new TeiidSQLException(origException, message, sqlState, errorCode);
tse.teiidCode = code;
return tse;
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class WarningUtil method createWarning.
/**
* Used to wrap warnings/exceptions into SQLWarning.
* The chain of warnings is translated into a chain of SQLWarnings.
* @param reason String object which is the description of the warning.
* @param ex Throwable object which needs to be wrapped.
*/
static SQLWarning createWarning(Throwable ex) {
String sourceName = null;
String modelName = null;
if (ex instanceof SourceWarning) {
SourceWarning exception = (SourceWarning) ex;
if (exception.isPartialResultsError()) {
// $NON-NLS-1$
PartialResultsWarning warning = new PartialResultsWarning(JDBCPlugin.Util.getString("WarningUtil.Failures_occurred"));
warning.addConnectorFailure(exception.getConnectorBindingName(), TeiidSQLException.create(exception));
return warning;
}
ex = exception.getCause();
sourceName = exception.getConnectorBindingName();
modelName = exception.getModelName();
}
String code = null;
if (ex instanceof TeiidException) {
code = ((TeiidException) ex).getCode();
}
return new TeiidSQLWarning(ex.getMessage(), code, ex, sourceName, modelName);
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class RuleCollapseSource method rewriteGroupByAsView.
public static Query rewriteGroupByAsView(Query query, QueryMetadataInterface metadata, boolean addViewForOrderBy) {
if (query.getGroupBy() == null) {
return query;
}
Select select = query.getSelect();
GroupBy groupBy = query.getGroupBy();
if (!addViewForOrderBy) {
query.setGroupBy(null);
}
Criteria having = query.getHaving();
query.setHaving(null);
OrderBy orderBy = query.getOrderBy();
query.setOrderBy(null);
Limit limit = query.getLimit();
query.setLimit(null);
Set<Expression> newSelectColumns = new LinkedHashSet<Expression>();
for (final Iterator<Expression> iterator = groupBy.getSymbols().iterator(); iterator.hasNext(); ) {
newSelectColumns.add(iterator.next());
}
Set<AggregateSymbol> aggs = new HashSet<AggregateSymbol>();
aggs.addAll(AggregateSymbolCollectorVisitor.getAggregates(select, true));
if (having != null) {
aggs.addAll(AggregateSymbolCollectorVisitor.getAggregates(having, true));
}
for (AggregateSymbol aggregateSymbol : aggs) {
for (Expression expr : aggregateSymbol.getArgs()) {
newSelectColumns.add(SymbolMap.getExpression(expr));
}
}
Select innerSelect = new Select();
for (Expression expr : newSelectColumns) {
innerSelect.addSymbol(expr);
}
query.setSelect(innerSelect);
Query outerQuery = null;
try {
// $NON-NLS-1$
outerQuery = QueryRewriter.createInlineViewQuery(new GroupSymbol("X"), query, metadata, query.getSelect().getProjectedSymbols());
} catch (TeiidException err) {
throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30257, err);
}
Iterator<Expression> iter = outerQuery.getSelect().getProjectedSymbols().iterator();
HashMap<Expression, Expression> expressionMap = new HashMap<Expression, Expression>();
for (Expression symbol : query.getSelect().getProjectedSymbols()) {
// need to unwrap on both sides as the select expression could be aliased
// TODO: could add an option to createInlineViewQuery to disable alias creation
expressionMap.put(SymbolMap.getExpression(symbol), SymbolMap.getExpression(iter.next()));
}
if (!addViewForOrderBy) {
ExpressionMappingVisitor.mapExpressions(groupBy, expressionMap);
outerQuery.setGroupBy(groupBy);
}
ExpressionMappingVisitor.mapExpressions(having, expressionMap, true);
outerQuery.setHaving(having);
ExpressionMappingVisitor.mapExpressions(orderBy, expressionMap, true);
outerQuery.setOrderBy(orderBy);
outerQuery.setLimit(limit);
ExpressionMappingVisitor.mapExpressions(select, expressionMap, true);
outerQuery.setSelect(select);
outerQuery.setOption(query.getOption());
query = outerQuery;
return query;
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class RowBasedSecurityHelper method resolveCondition.
static Criteria resolveCondition(QueryMetadataInterface metadata, final GroupSymbol group, String fullName, Map.Entry<String, DataPolicy> entry, PermissionMetaData pmd, String filterString) throws QueryMetadataException {
Criteria filter = (Criteria) pmd.getResolvedCondition();
if (filter == null) {
try {
filter = QueryParser.getQueryParser().parseCriteria(filterString);
GroupSymbol gs = group;
if (group.getDefinition() != null) {
gs = new GroupSymbol(fullName);
gs.setMetadataID(group.getMetadataID());
}
Collection<GroupSymbol> groups = Arrays.asList(gs);
for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(filter)) {
container.getCommand().pushNewResolvingContext(groups);
QueryResolver.resolveCommand(container.getCommand(), metadata, false);
}
ResolverVisitor.resolveLanguageObject(filter, groups, metadata);
ValidatorReport report = Validator.validate(filter, metadata, new ValidationVisitor());
if (report.hasItems()) {
ValidatorFailure firstFailure = report.getItems().iterator().next();
// $NON-NLS-1$
throw new QueryMetadataException(QueryPlugin.Event.TEIID31129, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31129, entry.getKey(), fullName) + " " + firstFailure);
}
pmd.setResolvedCondition(filter.clone());
} catch (QueryMetadataException e) {
throw e;
} catch (TeiidException e) {
throw new QueryMetadataException(QueryPlugin.Event.TEIID31129, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31129, entry.getKey(), fullName));
}
} else {
filter = (Criteria) filter.clone();
}
return filter;
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class ColumnMaskingHelper method maskColumn.
private static Expression maskColumn(ElementSymbol col, GroupSymbol unaliased, QueryMetadataInterface metadata, ExpressionMappingVisitor emv, Map<String, DataPolicy> policies, CommandContext cc) throws TeiidComponentException, TeiidProcessingException {
Object metadataID = col.getMetadataID();
String fullName = metadata.getFullName(metadataID);
final GroupSymbol group = col.getGroupSymbol();
String elementType = metadata.getElementRuntimeTypeName(col.getMetadataID());
Class<?> expectedType = DataTypeManager.getDataTypeClass(elementType);
List<WhenThen> cases = null;
Collection<GroupSymbol> groups = Arrays.asList(unaliased);
for (Map.Entry<String, DataPolicy> entry : policies.entrySet()) {
DataPolicyMetadata dpm = (DataPolicyMetadata) entry.getValue();
PermissionMetaData pmd = dpm.getPermissionMap().get(fullName);
if (pmd == null) {
continue;
}
String maskString = pmd.getMask();
if (maskString == null) {
continue;
}
Criteria condition = null;
if (pmd.getCondition() != null) {
condition = RowBasedSecurityHelper.resolveCondition(metadata, group, metadata.getFullName(group.getMetadataID()), entry, pmd, pmd.getCondition());
} else {
condition = QueryRewriter.TRUE_CRITERIA;
}
Expression mask = (Expression) pmd.getResolvedMask();
if (mask == null) {
try {
mask = QueryParser.getQueryParser().parseExpression(pmd.getMask());
for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(mask)) {
container.getCommand().pushNewResolvingContext(groups);
QueryResolver.resolveCommand(container.getCommand(), metadata, false);
}
ResolverVisitor.resolveLanguageObject(mask, groups, metadata);
ValidatorReport report = Validator.validate(mask, metadata, new ValidationVisitor());
if (report.hasItems()) {
ValidatorFailure firstFailure = report.getItems().iterator().next();
// $NON-NLS-1$
throw new QueryMetadataException(QueryPlugin.Event.TEIID31139, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31139, dpm.getName(), fullName) + " " + firstFailure);
}
if (mask.getType() != expectedType) {
mask = ResolverUtil.convertExpression(mask, elementType, metadata);
}
pmd.setResolvedMask(mask.clone());
if (!dpm.isAnyAuthenticated()) {
// we treat this as user deterministic since the data roles won't change. this may change if the logic becomes dynamic
// TODO: this condition may not even be used
cc.setDeterminismLevel(Determinism.USER_DETERMINISTIC);
}
} catch (QueryMetadataException e) {
throw e;
} catch (TeiidException e) {
throw new QueryMetadataException(QueryPlugin.Event.TEIID31129, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31129, dpm.getName(), fullName));
}
} else {
mask = (Expression) mask.clone();
}
if (group.getDefinition() != null) {
PreOrPostOrderNavigator.doVisit(mask, emv, PreOrPostOrderNavigator.PRE_ORDER, true);
}
if (cases == null) {
cases = new ArrayList<ColumnMaskingHelper.WhenThen>();
}
cases.add(new WhenThen(pmd.getOrder(), condition, mask));
}
if (cases == null) {
return col;
}
Collections.sort(cases);
List<Criteria> whens = new ArrayList<Criteria>();
List<Expression> thens = new ArrayList<Expression>();
for (WhenThen whenThen : cases) {
whens.add(whenThen.when);
thens.add(whenThen.then);
}
SearchedCaseExpression sce = new SearchedCaseExpression(whens, thens);
sce.setElseExpression(col);
sce.setType(expectedType);
Expression mask = QueryRewriter.rewriteExpression(sce, cc, metadata, true);
return mask;
}
Aggregations