use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class MySqlOutputVisitor method visit.
public boolean visit(MySqlCreateTableStatement x) {
print0(ucase ? "CREATE " : "create ");
for (SQLCommentHint hint : x.getHints()) {
hint.accept(this);
print(' ');
}
if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) {
print0(ucase ? "TEMPORARY TABLE " : "temporary table ");
} else {
print0(ucase ? "TABLE " : "table ");
}
if (x.isIfNotExiists()) {
print0(ucase ? "IF NOT EXISTS " : "if not exists ");
}
printTableSourceExpr(x.getName());
if (x.getLike() != null) {
print0(ucase ? " LIKE " : " like ");
x.getLike().accept(this);
}
int size = x.getTableElementList().size();
if (size > 0) {
print0(" (");
incrementIndent();
println();
for (int i = 0; i < size; ++i) {
if (i != 0) {
print0(", ");
println();
}
x.getTableElementList().get(i).accept(this);
}
decrementIndent();
println();
print(')');
}
for (Map.Entry<String, SQLObject> option : x.getTableOptions().entrySet()) {
String key = option.getKey();
print(' ');
print0(ucase ? key : key.toLowerCase());
if ("TABLESPACE".equals(key)) {
print(' ');
option.getValue().accept(this);
continue;
} else if ("UNION".equals(key)) {
print0(" = (");
option.getValue().accept(this);
print(')');
continue;
}
print0(" = ");
option.getValue().accept(this);
}
if (x.getPartitioning() != null) {
println();
x.getPartitioning().accept(this);
}
if (x.getTableGroup() != null) {
println();
print0(ucase ? "TABLEGROUP " : "tablegroup ");
x.getTableGroup().accept(this);
}
if (x.getSelect() != null) {
incrementIndent();
println();
x.getSelect().accept(this);
decrementIndent();
}
for (SQLCommentHint hint : x.getOptionHints()) {
print(' ');
hint.accept(this);
}
return false;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class OracleSchemaStatVisitor method visit.
@Override
public boolean visit(ConditionalInsertClauseItem x) {
SQLObject parent = x.getParent();
if (parent instanceof ConditionalInsertClause) {
parent = parent.getParent();
}
if (parent instanceof OracleMultiInsertStatement) {
SQLSelect subQuery = ((OracleMultiInsertStatement) parent).getSubQuery();
if (subQuery != null) {
String table = (String) subQuery.getAttribute("_table_");
setCurrentTable(x, table);
}
}
x.getWhen().accept(this);
x.getThen().accept(this);
restoreCurrentTable(x);
return false;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class ExportParameterVisitorUtils method exportParameter.
public static SQLExpr exportParameter(final List<Object> parameters, final SQLExpr param) {
Object value = null;
boolean replace = false;
if (param instanceof SQLCharExpr) {
value = ((SQLCharExpr) param).getText();
replace = true;
}
if (param instanceof SQLBooleanExpr) {
value = ((SQLBooleanExpr) param).getValue();
replace = true;
}
if (param instanceof SQLNumericLiteralExpr) {
value = ((SQLNumericLiteralExpr) param).getNumber();
replace = true;
}
if (replace) {
SQLObject parent = param.getParent();
if (parent != null) {
List<SQLObject> mergedList = (List<SQLObject>) parent.getAttribute(ParameterizedOutputVisitorUtils.ATTR_MERGED);
if (mergedList != null) {
List<Object> mergedListParams = new ArrayList<Object>(mergedList.size() + 1);
for (int i = 0; i < mergedList.size(); ++i) {
SQLObject item = mergedList.get(i);
if (item instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpItem = (SQLBinaryOpExpr) item;
exportParameter(mergedListParams, binaryOpItem.getRight());
}
}
if (mergedListParams.size() > 0) {
mergedListParams.add(0, value);
value = mergedListParams;
}
}
}
parameters.add(value);
return new SQLVariantRefExpr("?");
}
return param;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class ParameterizedOutputVisitorUtils method merge.
public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
SQLExpr left = x.getLeft();
SQLExpr right = x.getRight();
SQLObject parent = x.getParent();
if (left instanceof SQLLiteralExpr && right instanceof SQLLiteralExpr) {
if (//
x.getOperator() == SQLBinaryOperator.Equality || x.getOperator() == SQLBinaryOperator.NotEqual) {
if ((left instanceof SQLIntegerExpr) && (right instanceof SQLIntegerExpr)) {
if (((SQLIntegerExpr) left).getNumber().intValue() < 100) {
left.putAttribute(ATTR_PARAMS_SKIP, true);
}
if (((SQLIntegerExpr) right).getNumber().intValue() < 100) {
right.putAttribute(ATTR_PARAMS_SKIP, true);
}
} else {
left.putAttribute(ATTR_PARAMS_SKIP, true);
right.putAttribute(ATTR_PARAMS_SKIP, true);
}
}
return x;
}
for (; ; ) {
if (x.getRight() instanceof SQLBinaryOpExpr) {
if (x.getLeft() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr leftBinaryExpr = (SQLBinaryOpExpr) x.getLeft();
if (leftBinaryExpr.getRight().equals(x.getRight())) {
x = leftBinaryExpr;
v.incrementReplaceCunt();
continue;
}
}
SQLExpr mergedRight = merge(v, (SQLBinaryOpExpr) x.getRight());
if (mergedRight != x.getRight()) {
x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), mergedRight);
v.incrementReplaceCunt();
}
x.setParent(parent);
}
break;
}
if (x.getLeft() instanceof SQLBinaryOpExpr) {
SQLExpr mergedLeft = merge(v, (SQLBinaryOpExpr) x.getLeft());
if (mergedLeft != x.getLeft()) {
SQLBinaryOpExpr tmp = new SQLBinaryOpExpr(mergedLeft, x.getOperator(), x.getRight());
tmp.setParent(parent);
x = tmp;
v.incrementReplaceCunt();
}
}
// ID = ? OR ID = ? => ID = ?
if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
if ((x.getLeft() instanceof SQLBinaryOpExpr) && (x.getRight() instanceof SQLBinaryOpExpr)) {
SQLBinaryOpExpr leftBinary = (SQLBinaryOpExpr) x.getLeft();
SQLBinaryOpExpr rightBinary = (SQLBinaryOpExpr) x.getRight();
if (mergeEqual(leftBinary, rightBinary)) {
v.incrementReplaceCunt();
leftBinary.setParent(x.getParent());
putMergedArribute(leftBinary, rightBinary);
return leftBinary;
}
if (//
isLiteralExpr(leftBinary.getLeft()) && leftBinary.getOperator() == SQLBinaryOperator.BooleanOr) {
if (mergeEqual(leftBinary.getRight(), x.getRight())) {
v.incrementReplaceCunt();
putMergedArribute(leftBinary, rightBinary);
return leftBinary;
}
}
}
}
return x;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class SchemaStatVisitor method visit.
public boolean visit(SQLColumnDefinition x) {
String tableName = null;
{
SQLObject parent = x.getParent();
if (parent instanceof SQLCreateTableStatement) {
tableName = ((SQLCreateTableStatement) parent).getName().toString();
}
}
if (tableName == null) {
return true;
}
String columnName = x.getName().toString();
Column column = addColumn(tableName, columnName);
if (x.getDataType() != null) {
column.setDataType(x.getDataType().getName());
}
for (SQLColumnConstraint item : x.getConstraints()) {
if (item instanceof SQLPrimaryKey) {
column.setPrimaryKey(true);
} else if (item instanceof SQLUnique) {
column.setUnique(true);
}
}
return false;
}
Aggregations