use of net.sf.jsqlparser.schema.Table in project dbeaver by dbeaver.
the class SQLSemanticProcessor method getConstraintTable.
/**
* Extract alias (or source table name) for specified constraint from SQL select.
* Searches in FROM and JOIN
*/
@Nullable
public static Table getConstraintTable(PlainSelect select, DBDAttributeConstraint constraint) {
String constrTable;
DBSAttributeBase ca = constraint.getAttribute();
if (ca instanceof DBDAttributeBinding) {
constrTable = ((DBDAttributeBinding) ca).getMetaAttribute().getEntityName();
} else if (ca instanceof DBSEntityAttribute) {
constrTable = ((DBSEntityAttribute) ca).getParentObject().getName();
} else {
return null;
}
if (constrTable == null) {
return null;
}
FromItem fromItem = select.getFromItem();
Table table = findTableInFrom(fromItem, constrTable);
if (table == null) {
// Maybe it is a join
if (!CommonUtils.isEmpty(select.getJoins())) {
for (Join join : select.getJoins()) {
table = findTableInFrom(join.getRightItem(), constrTable);
if (table != null) {
break;
}
}
}
}
return table;
}
use of net.sf.jsqlparser.schema.Table in project dbeaver by serge-rider.
the class SQLSemanticProcessor method getConstraintExpression.
private static Expression getConstraintExpression(PlainSelect select, DBDAttributeConstraint co) throws JSQLParserException {
Expression orderExpr;
String attrName = co.getAttribute().getName();
if (attrName.isEmpty()) {
orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
} else if (CommonUtils.isJavaIdentifier(attrName)) {
// Use column table only if there are multiple source tables (joins)
Table orderTable = CommonUtils.isEmpty(select.getJoins()) ? null : getConstraintTable(select, co);
orderExpr = new Column(orderTable, attrName);
} else {
// TODO: set tableAlias for all column references in expression
orderExpr = CCJSqlParserUtil.parseExpression(attrName);
//orderExpr = new CustomExpression(attrName);
//orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
}
return orderExpr;
}
use of net.sf.jsqlparser.schema.Table in project dbeaver by dbeaver.
the class SQLSemanticProcessor method getConstraintExpression.
private static Expression getConstraintExpression(DBPDataSource dataSource, PlainSelect select, DBDAttributeConstraint co) throws JSQLParserException {
Expression orderExpr;
String attrName = DBUtils.getQuotedIdentifier(dataSource, co.getAttribute().getName());
if (attrName.isEmpty()) {
orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
} else if (CommonUtils.isJavaIdentifier(attrName)) {
// Use column table only if there are multiple source tables (joins)
Table orderTable = CommonUtils.isEmpty(select.getJoins()) ? null : getConstraintTable(select, co);
orderExpr = new Column(orderTable, attrName);
} else {
// TODO: set tableAlias for all column references in expression
orderExpr = CCJSqlParserUtil.parseExpression(attrName);
// orderExpr = new CustomExpression(attrName);
// orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
}
return orderExpr;
}
use of net.sf.jsqlparser.schema.Table in project JSqlParser by JSQLParser.
the class CreateViewDeParserTest method testUseExtrnalExpressionDeparser.
/**
* Test of deParse method, of class CreateViewDeParser.
*/
@Test
public void testUseExtrnalExpressionDeparser() throws JSQLParserException {
StringBuilder b = new StringBuilder();
SelectDeParser selectDeParser = new SelectDeParser();
selectDeParser.setBuffer(b);
ExpressionDeParser expressionDeParser = new ExpressionDeParser(selectDeParser, b) {
@Override
public void visit(Column tableColumn) {
final Table table = tableColumn.getTable();
String tableName = null;
if (table != null) {
if (table.getAlias() != null) {
tableName = table.getAlias().getName();
} else {
tableName = table.getFullyQualifiedName();
}
}
if (tableName != null && !tableName.isEmpty()) {
getBuffer().append("\"").append(tableName).append("\"").append(".");
}
getBuffer().append("\"").append(tableColumn.getColumnName()).append("\"");
}
};
selectDeParser.setExpressionVisitor(expressionDeParser);
CreateViewDeParser instance = new CreateViewDeParser(b, selectDeParser);
CreateView vc = (CreateView) CCJSqlParserUtil.parse("CREATE VIEW test AS SELECT a, b FROM mytable");
instance.deParse(vc);
assertEquals("CREATE VIEW test AS SELECT a, b FROM mytable", vc.toString());
assertEquals("CREATE VIEW test AS SELECT \"a\", \"b\" FROM mytable", instance.getBuffer().toString());
}
use of net.sf.jsqlparser.schema.Table in project JSqlParser by JSQLParser.
the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingReplaceWithoutItemsList.
@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingReplaceWithoutItemsList() {
Replace replace = new Replace();
Table table = new Table();
List<Column> columns = new ArrayList<Column>();
List<Expression> expressions = new ArrayList<Expression>();
Column column1 = new Column();
Column column2 = new Column();
Expression expression1 = mock(Expression.class);
Expression expression2 = mock(Expression.class);
replace.setTable(table);
replace.setColumns(columns);
replace.setExpressions(expressions);
columns.add(column1);
columns.add(column2);
expressions.add(expression1);
expressions.add(expression2);
statementDeParser.visit(replace);
then(expression1).should().accept(expressionDeParser);
then(expression2).should().accept(expressionDeParser);
}
Aggregations