Search in sources :

Example 16 with SQLAllColumnExpr

use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.

the class SQLTransformUtils method transformOracleToMySql.

public static SQLDataType transformOracleToMySql(SQLDataType x) {
    final String name = x.getName();
    final long nameHash = x.nameHashCode64();
    if (name == null) {
        return x;
    }
    List<SQLExpr> argumentns = x.getArguments();
    SQLDataType dataType;
    if (nameHash == FnvHash.Constants.UROWID) {
        int len = 4000;
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLIntegerExpr) {
                len = ((SQLIntegerExpr) arg0).getNumber().intValue();
            }
        }
        dataType = new SQLDataTypeImpl("varchar", len);
    } else if (nameHash == FnvHash.Constants.ROWID) {
        dataType = new SQLDataTypeImpl("char", 10);
    } else if (nameHash == FnvHash.Constants.BOOLEAN) {
        dataType = new SQLDataTypeImpl("tinyint");
    } else if (nameHash == FnvHash.Constants.INTEGER) {
        dataType = new SQLDataTypeImpl("int");
    } else if (nameHash == FnvHash.Constants.FLOAT || nameHash == FnvHash.Constants.BINARY_FLOAT) {
        dataType = new SQLDataTypeImpl("float");
    } else if (nameHash == FnvHash.Constants.REAL || nameHash == FnvHash.Constants.BINARY_DOUBLE || nameHash == FnvHash.Constants.DOUBLE_PRECISION) {
        dataType = new SQLDataTypeImpl("double");
    } else if (nameHash == FnvHash.Constants.NUMBER) {
        if (argumentns.size() == 0) {
            dataType = new SQLDataTypeImpl("decimal", 38);
        } else {
            SQLExpr arg0 = argumentns.get(0);
            int precision, scale = 0;
            if (arg0 instanceof SQLAllColumnExpr) {
                precision = 9;
            } else {
                precision = ((SQLIntegerExpr) arg0).getNumber().intValue();
            }
            if (argumentns.size() > 1) {
                scale = ((SQLIntegerExpr) argumentns.get(1)).getNumber().intValue();
            }
            if (scale > precision) {
                if (arg0 instanceof SQLAllColumnExpr) {
                    precision = 19;
                    if (scale > precision) {
                        precision = scale;
                    }
                } else {
                    precision = scale;
                }
            }
            if (scale == 0) {
                if (precision < 3) {
                    dataType = new SQLDataTypeImpl("tinyint");
                } else if (precision < 5) {
                    dataType = new SQLDataTypeImpl("smallint");
                } else if (precision < 9) {
                    dataType = new SQLDataTypeImpl("int");
                } else if (precision <= 20) {
                    dataType = new SQLDataTypeImpl("bigint");
                } else {
                    dataType = new SQLDataTypeImpl("decimal", precision);
                }
            } else {
                dataType = new SQLDataTypeImpl("decimal", precision, scale);
            }
        }
    } else if (nameHash == FnvHash.Constants.DEC || nameHash == FnvHash.Constants.DECIMAL) {
        dataType = x.clone();
        dataType.setName("decimal");
        int precision = 0;
        if (argumentns.size() > 0) {
            precision = ((SQLIntegerExpr) argumentns.get(0)).getNumber().intValue();
        }
        int scale = 0;
        if (argumentns.size() > 1) {
            scale = ((SQLIntegerExpr) argumentns.get(1)).getNumber().intValue();
            if (precision < scale) {
                ((SQLIntegerExpr) dataType.getArguments().get(1)).setNumber(precision);
            }
        }
    // if (precision == 38 && scale == 0 && x.getParent() instanceof SQLCastExpr) {
    // dataType.getArguments().clear();
    // dataType.setName("int");
    // }
    // ///////////////////////////////
    } else if (nameHash == FnvHash.Constants.RAW) {
        int len;
        if (argumentns.size() == 0) {
            len = -1;
        } else if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
        } else {
            throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
        }
        if (len == -1) {
            dataType = new SQLDataTypeImpl("binary");
        } else if (len <= 255) {
            dataType = new SQLDataTypeImpl("binary", len);
        } else {
            dataType = new SQLDataTypeImpl("varbinary", len);
        }
    } else if (nameHash == FnvHash.Constants.CHAR || nameHash == FnvHash.Constants.CHARACTER) {
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            int len;
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            if (len <= 255) {
                dataType = new SQLCharacterDataType("char", len);
            } else {
                dataType = new SQLCharacterDataType("varchar", len);
            }
        } else if (argumentns.size() == 0) {
            dataType = new SQLCharacterDataType("char");
        } else {
            throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
        }
    } else if (nameHash == FnvHash.Constants.NCHAR) {
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            int len;
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            if (len <= 255) {
                dataType = new SQLCharacterDataType("nchar", len);
            } else {
                dataType = new SQLCharacterDataType("nvarchar", len);
            }
        } else if (argumentns.size() == 0) {
            dataType = new SQLCharacterDataType("nchar");
        } else {
            throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
        }
    } else if (nameHash == FnvHash.Constants.VARCHAR2) {
        if (argumentns.size() > 0) {
            int len;
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            dataType = new SQLCharacterDataType("varchar", len);
        } else {
            dataType = new SQLCharacterDataType("varchar");
        }
    } else if (nameHash == FnvHash.Constants.NVARCHAR2) {
        if (argumentns.size() > 0) {
            int len;
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            dataType = new SQLCharacterDataType("nvarchar", len);
        } else {
            dataType = new SQLCharacterDataType("nvarchar");
        }
    } else if (nameHash == FnvHash.Constants.BFILE) {
        dataType = new SQLCharacterDataType("varchar", 255);
    } else if (nameHash == FnvHash.Constants.DATE || nameHash == FnvHash.Constants.TIMESTAMP) {
        int len = -1;
        if (argumentns.size() > 0) {
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
        }
        if (len >= 0) {
            if (len > 6) {
                len = 6;
            }
            dataType = new SQLDataTypeImpl("datetime", len);
        } else {
            dataType = new SQLDataTypeImpl("datetime");
        }
    } else if (nameHash == FnvHash.Constants.BLOB || nameHash == FnvHash.Constants.LONG_RAW) {
        argumentns.clear();
        dataType = new SQLDataTypeImpl("LONGBLOB");
    } else if (nameHash == FnvHash.Constants.CLOB || nameHash == FnvHash.Constants.NCLOB || nameHash == FnvHash.Constants.LONG || nameHash == FnvHash.Constants.XMLTYPE) {
        argumentns.clear();
        dataType = new SQLCharacterDataType("LONGTEXT");
    } else {
        dataType = x;
    }
    if (dataType != x) {
        dataType.setParent(x.getParent());
    }
    return dataType;
}
Also used : SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 17 with SQLAllColumnExpr

use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.

the class SQLTransformUtils method transformOracleToPostgresql.

public static SQLDataType transformOracleToPostgresql(SQLDataType x) {
    final String name = x.getName();
    final long nameHash = x.nameHashCode64();
    if (name == null) {
        return x;
    }
    List<SQLExpr> argumentns = x.getArguments();
    SQLDataType dataType;
    if (nameHash == FnvHash.Constants.UROWID) {
        int len = 4000;
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLIntegerExpr) {
                len = ((SQLIntegerExpr) arg0).getNumber().intValue();
            }
        }
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.VARCHAR, len);
    } else if (nameHash == FnvHash.Constants.ROWID) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.CHAR, 10);
    } else if (nameHash == FnvHash.Constants.BOOLEAN || nameHash == FnvHash.Constants.SMALLINT) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.SMALLINT);
    } else if (nameHash == FnvHash.Constants.INTEGER || nameHash == FnvHash.Constants.INT) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.DECIMAL, 38);
    } else if (nameHash == FnvHash.Constants.BINARY_FLOAT) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.REAL);
    } else if (nameHash == FnvHash.Constants.BINARY_DOUBLE || nameHash == FnvHash.Constants.FLOAT || nameHash == FnvHash.Constants.DOUBLE || nameHash == FnvHash.Constants.REAL || nameHash == FnvHash.Constants.DOUBLE_PRECISION) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.DOUBLE_PRECISION);
    } else if (nameHash == FnvHash.Constants.NUMBER) {
        if (argumentns.size() == 0) {
            dataType = new SQLDataTypeImpl(SQLDataType.Constants.DECIMAL, 38);
        } else {
            SQLExpr arg0 = argumentns.get(0);
            int precision, scale = 0;
            if (arg0 instanceof SQLAllColumnExpr) {
                precision = 19;
                scale = -1;
            } else {
                precision = ((SQLIntegerExpr) arg0).getNumber().intValue();
            }
            if (argumentns.size() > 1) {
                scale = ((SQLIntegerExpr) argumentns.get(1)).getNumber().intValue();
            }
            if (scale > precision) {
                if (arg0 instanceof SQLAllColumnExpr) {
                    precision = 19;
                    if (scale > precision) {
                        precision = scale;
                    }
                } else {
                    precision = scale;
                }
            }
            if (scale == 0) {
                if (precision < 5) {
                    dataType = new SQLDataTypeImpl(SQLDataType.Constants.SMALLINT);
                } else if (precision < 9) {
                    dataType = new SQLDataTypeImpl(SQLDataType.Constants.INT);
                } else if (precision <= 20) {
                    dataType = new SQLDataTypeImpl(SQLDataType.Constants.BIGINT);
                } else {
                    dataType = new SQLDataTypeImpl(SQLDataType.Constants.DECIMAL, precision);
                }
            } else if (scale == -1) {
                dataType = new SQLDataTypeImpl(SQLDataType.Constants.DOUBLE_PRECISION);
            } else {
                dataType = new SQLDataTypeImpl(SQLDataType.Constants.DECIMAL, precision, scale);
            }
        }
    } else if (nameHash == FnvHash.Constants.DEC || nameHash == FnvHash.Constants.DECIMAL) {
        dataType = x.clone();
        dataType.setName(SQLDataType.Constants.DECIMAL);
        int precision = 0;
        if (argumentns.size() > 0) {
            precision = ((SQLIntegerExpr) argumentns.get(0)).getNumber().intValue();
        }
        int scale = 0;
        if (argumentns.size() > 1) {
            scale = ((SQLIntegerExpr) argumentns.get(1)).getNumber().intValue();
            if (precision < scale) {
                ((SQLIntegerExpr) dataType.getArguments().get(1)).setNumber(precision);
            }
        }
    } else if (nameHash == FnvHash.Constants.CHARACTER) {
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            int len;
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            dataType = new SQLCharacterDataType(SQLDataType.Constants.CHAR, len);
        } else if (argumentns.size() == 0) {
            dataType = new SQLCharacterDataType(SQLDataType.Constants.CHAR);
        } else {
            throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
        }
    } else if (nameHash == FnvHash.Constants.CHAR) {
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            int len;
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            if (len <= 2000) {
                dataType = x;
                dataType.setName(SQLDataType.Constants.CHAR);
            } else {
                dataType = new SQLCharacterDataType(SQLDataType.Constants.TEXT);
            }
        } else if (argumentns.size() == 0) {
            dataType = new SQLCharacterDataType(SQLDataType.Constants.CHAR);
        } else {
            throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
        }
    } else if (nameHash == FnvHash.Constants.NCHAR) {
        // no changed
        dataType = x;
        dataType.setName(SQLDataType.Constants.NCHAR);
    } else if (nameHash == FnvHash.Constants.VARCHAR || nameHash == FnvHash.Constants.VARCHAR2) {
        if (argumentns.size() > 0) {
            int len;
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else if (arg0 instanceof SQLVariantRefExpr) {
                len = 2000;
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            if (len <= 4000) {
                dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR, len);
            } else {
                dataType = new SQLCharacterDataType(SQLDataType.Constants.TEXT);
            }
        } else {
            dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR);
        }
    } else if (nameHash == FnvHash.Constants.NVARCHAR || nameHash == FnvHash.Constants.NVARCHAR2 || nameHash == FnvHash.Constants.NCHAR_VARYING) {
        if (argumentns.size() > 0) {
            int len;
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR, len);
        } else {
            dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR);
        }
    } else if (nameHash == FnvHash.Constants.BFILE) {
        dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR, 255);
    } else if (nameHash == FnvHash.Constants.DATE) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.TIMESTAMP, 0);
    } else if (nameHash == FnvHash.Constants.TIMESTAMP) {
        x.setName(SQLDataType.Constants.TIMESTAMP);
        if (x.isWithLocalTimeZone()) {
            x.setWithLocalTimeZone(false);
            x.setWithTimeZone(null);
        }
        dataType = x;
    } else if (nameHash == FnvHash.Constants.DATETIME) {
        int len = -1;
        if (argumentns.size() > 0) {
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
        }
        if (len > 0) {
            dataType = new SQLDataTypeImpl(SQLDataType.Constants.TIMESTAMP, len);
        } else {
            dataType = new SQLDataTypeImpl(SQLDataType.Constants.TIMESTAMP);
        }
    } else if (nameHash == FnvHash.Constants.BLOB || nameHash == FnvHash.Constants.LONG_RAW || nameHash == FnvHash.Constants.RAW) {
        argumentns.clear();
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.BYTEA);
    } else if (nameHash == FnvHash.Constants.CLOB || nameHash == FnvHash.Constants.NCLOB || nameHash == FnvHash.Constants.LONG) {
        argumentns.clear();
        dataType = new SQLCharacterDataType(SQLDataType.Constants.TEXT);
    } else if (nameHash == FnvHash.Constants.XMLTYPE) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.XML);
    } else {
        dataType = x;
    }
    if (dataType != x) {
        dataType.setParent(x.getParent());
    }
    return dataType;
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl)

Example 18 with SQLAllColumnExpr

use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.

the class SQLTransformUtils method transformOracleToPPAS.

public static SQLDataType transformOracleToPPAS(SQLDataType x) {
    final String name = x.getName();
    final long nameHash = x.nameHashCode64();
    if (name == null) {
        return x;
    }
    List<SQLExpr> argumentns = x.getArguments();
    SQLDataType dataType;
    if (nameHash == FnvHash.Constants.UROWID) {
        int len = 4000;
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLIntegerExpr) {
                len = ((SQLIntegerExpr) arg0).getNumber().intValue();
            }
        }
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.VARCHAR, len);
    } else if (nameHash == FnvHash.Constants.ROWID) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.CHAR, 10);
    } else if (nameHash == FnvHash.Constants.BOOLEAN || nameHash == FnvHash.Constants.SMALLINT) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.SMALLINT);
    } else if (nameHash == FnvHash.Constants.INTEGER || nameHash == FnvHash.Constants.INT) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.DECIMAL, 38);
    } else if (nameHash == FnvHash.Constants.BINARY_FLOAT) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.REAL);
    } else if (nameHash == FnvHash.Constants.BINARY_DOUBLE || nameHash == FnvHash.Constants.FLOAT || nameHash == FnvHash.Constants.DOUBLE || nameHash == FnvHash.Constants.REAL || nameHash == FnvHash.Constants.DOUBLE_PRECISION) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.DOUBLE_PRECISION);
    } else if (nameHash == FnvHash.Constants.NUMBER) {
        dataType = x.clone();
        if (argumentns.size() > 0) {
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLAllColumnExpr) {
                SQLIntegerExpr precisionExpr = new SQLIntegerExpr(38);
                dataType.getArguments().set(0, precisionExpr);
            }
        }
    } else if (nameHash == FnvHash.Constants.DEC || nameHash == FnvHash.Constants.DECIMAL) {
        dataType = x.clone();
        dataType.setName(SQLDataType.Constants.DECIMAL);
        int precision = 0;
        if (argumentns.size() > 0) {
            precision = ((SQLIntegerExpr) argumentns.get(0)).getNumber().intValue();
        }
        int scale = 0;
        if (argumentns.size() > 1) {
            scale = ((SQLIntegerExpr) argumentns.get(1)).getNumber().intValue();
            if (precision < scale) {
                ((SQLIntegerExpr) dataType.getArguments().get(1)).setNumber(precision);
            }
        }
    } else if (nameHash == FnvHash.Constants.CHARACTER) {
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            int len;
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            dataType = new SQLCharacterDataType(SQLDataType.Constants.CHAR, len);
        } else if (argumentns.size() == 0) {
            dataType = new SQLCharacterDataType(SQLDataType.Constants.CHAR);
        } else {
            throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
        }
    } else if (nameHash == FnvHash.Constants.CHAR) {
        if (argumentns.size() == 1) {
            SQLExpr arg0 = argumentns.get(0);
            int len;
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            if (len <= 2000) {
                dataType = x;
                dataType.setName(SQLDataType.Constants.CHAR);
            } else {
                dataType = new SQLCharacterDataType(SQLDataType.Constants.TEXT);
            }
        } else if (argumentns.size() == 0) {
            dataType = new SQLCharacterDataType(SQLDataType.Constants.CHAR);
        } else {
            throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
        }
    } else if (nameHash == FnvHash.Constants.NCHAR) {
        // no changed
        dataType = x;
        dataType.setName(SQLDataType.Constants.NCHAR);
    } else if (nameHash == FnvHash.Constants.VARCHAR || nameHash == FnvHash.Constants.VARCHAR2) {
        if (argumentns.size() > 0) {
            int len;
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else if (arg0 instanceof SQLVariantRefExpr) {
                len = 2000;
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            if (len <= 4000) {
                dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR, len);
            } else {
                dataType = new SQLCharacterDataType(SQLDataType.Constants.TEXT);
            }
        } else {
            dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR);
        }
    } else if (nameHash == FnvHash.Constants.NVARCHAR || nameHash == FnvHash.Constants.NVARCHAR2 || nameHash == FnvHash.Constants.NCHAR_VARYING) {
        if (argumentns.size() > 0) {
            int len;
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
            dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR, len);
        } else {
            dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR);
        }
    } else if (nameHash == FnvHash.Constants.BFILE) {
        dataType = new SQLCharacterDataType(SQLDataType.Constants.VARCHAR, 255);
    } else if (nameHash == FnvHash.Constants.DATE) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.TIMESTAMP, 0);
    } else if (nameHash == FnvHash.Constants.TIMESTAMP) {
        x.setName(SQLDataType.Constants.TIMESTAMP);
        if (x.isWithLocalTimeZone()) {
            x.setWithLocalTimeZone(false);
            x.setWithTimeZone(null);
        }
        dataType = x;
    } else if (nameHash == FnvHash.Constants.DATETIME) {
        int len = -1;
        if (argumentns.size() > 0) {
            SQLExpr arg0 = argumentns.get(0);
            if (arg0 instanceof SQLNumericLiteralExpr) {
                len = ((SQLNumericLiteralExpr) arg0).getNumber().intValue();
            } else {
                throw new UnsupportedOperationException(SQLUtils.toOracleString(x));
            }
        }
        if (len > 0) {
            dataType = new SQLDataTypeImpl(SQLDataType.Constants.TIMESTAMP, len);
        } else {
            dataType = new SQLDataTypeImpl(SQLDataType.Constants.TIMESTAMP);
        }
    } else if (nameHash == FnvHash.Constants.BLOB || nameHash == FnvHash.Constants.LONG_RAW || nameHash == FnvHash.Constants.RAW) {
        argumentns.clear();
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.BYTEA);
    } else if (nameHash == FnvHash.Constants.CLOB || nameHash == FnvHash.Constants.NCLOB || nameHash == FnvHash.Constants.LONG) {
        argumentns.clear();
        dataType = new SQLCharacterDataType(SQLDataType.Constants.TEXT);
    } else if (nameHash == FnvHash.Constants.XMLTYPE) {
        dataType = new SQLDataTypeImpl(SQLDataType.Constants.XML);
    } else {
        dataType = x;
    }
    if (dataType != x) {
        dataType.setParent(x.getParent());
    }
    return dataType;
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl)

Example 19 with SQLAllColumnExpr

use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.

the class OdpsStatementParser method parseInsert.

public SQLStatement parseInsert() {
    if (lexer.token() == Token.FROM) {
        lexer.nextToken();
        HiveMultiInsertStatement stmt = new HiveMultiInsertStatement();
        if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.VARIANT) {
            Lexer.SavePoint mark = lexer.mark();
            SQLExpr tableName = this.exprParser.name();
            if (lexer.token() == Token.LPAREN) {
                lexer.reset(mark);
                tableName = this.exprParser.primary();
            }
            SQLTableSource from = new SQLExprTableSource(tableName);
            if (lexer.token() == Token.IDENTIFIER) {
                String alias = alias();
                from.setAlias(alias);
            }
            SQLSelectParser selectParser = createSQLSelectParser();
            from = selectParser.parseTableSourceRest(from);
            if (lexer.token() == Token.WHERE) {
                lexer.nextToken();
                SQLExpr where = this.exprParser.expr();
                SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock();
                queryBlock.addSelectItem(new SQLAllColumnExpr());
                queryBlock.setFrom(from);
                queryBlock.setWhere(where);
                if (lexer.token() == Token.GROUP) {
                    selectParser.parseGroupBy(queryBlock);
                }
                stmt.setFrom(new SQLSubqueryTableSource(queryBlock));
            } else {
                stmt.setFrom(from);
            }
        } else {
            SQLCommentHint hint = null;
            if (lexer.token() == Token.HINT) {
                hint = this.exprParser.parseHint();
            }
            accept(Token.LPAREN);
            boolean paren2 = lexer.token() == Token.LPAREN;
            SQLSelectParser selectParser = createSQLSelectParser();
            SQLSelect select = selectParser.select();
            SQLTableSource from = null;
            if (paren2 && lexer.token() != Token.RPAREN) {
                String subQueryAs = null;
                if (lexer.token() == Token.AS) {
                    lexer.nextToken();
                    subQueryAs = tableAlias(true);
                } else {
                    subQueryAs = tableAlias(false);
                }
                SQLSubqueryTableSource subQuery = new SQLSubqueryTableSource(select, subQueryAs);
                from = selectParser.parseTableSourceRest(subQuery);
            }
            accept(Token.RPAREN);
            String alias;
            if (lexer.token() == Token.INSERT) {
                alias = null;
            } else if (lexer.token() == Token.SELECT) {
                // skip
                alias = null;
            } else {
                if (lexer.token() == Token.AS) {
                    lexer.nextToken();
                }
                alias = lexer.stringVal();
                accept(Token.IDENTIFIER);
            }
            if (from == null) {
                from = new SQLSubqueryTableSource(select, alias);
            } else {
                if (alias != null) {
                    from.setAlias(alias);
                }
            }
            SQLTableSource tableSource = selectParser.parseTableSourceRest(from);
            if (hint != null) {
                if (tableSource instanceof SQLJoinTableSource) {
                    ((SQLJoinTableSource) tableSource).setHint(hint);
                }
            }
            stmt.setFrom(tableSource);
        }
        if (lexer.token() == Token.SELECT) {
            SQLSelectParser selectParser = createSQLSelectParser();
            SQLSelect query = selectParser.select();
            HiveInsert insert = new HiveInsert();
            insert.setQuery(query);
            stmt.addItem(insert);
            return stmt;
        }
        for (; ; ) {
            HiveInsert insert = parseHiveInsert();
            stmt.addItem(insert);
            if (lexer.token() != Token.INSERT) {
                break;
            }
        }
        return stmt;
    }
    return parseHiveInsertStmt();
}
Also used : HiveInsert(com.alibaba.druid.sql.dialect.hive.ast.HiveInsert) HiveMultiInsertStatement(com.alibaba.druid.sql.dialect.hive.ast.HiveMultiInsertStatement) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)

Example 20 with SQLAllColumnExpr

use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.

the class SchemaRepository method acceptCreateTable.

SchemaObject acceptCreateTable(SQLCreateTableStatement x) {
    SQLCreateTableStatement x1 = x.clone();
    String schemaName = x1.getSchema();
    Schema schema = findSchema(schemaName, true);
    SQLSelect select = x1.getSelect();
    if (select != null) {
        select.accept(createResolveVisitor(SchemaResolveVisitor.Option.ResolveAllColumn));
        SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
        this.resolve(queryBlock);
        if (queryBlock != null) {
            List<SQLSelectItem> selectList = queryBlock.getSelectList();
            for (SQLSelectItem selectItem : selectList) {
                SQLExpr selectItemExpr = selectItem.getExpr();
                if (selectItemExpr instanceof SQLAllColumnExpr || (selectItemExpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) selectItemExpr).getName().equals("*"))) {
                    continue;
                }
                SQLColumnDefinition column = null;
                if (selectItemExpr instanceof SQLName) {
                    final SQLColumnDefinition resolvedColumn = ((SQLName) selectItemExpr).getResolvedColumn();
                    if (resolvedColumn != null) {
                        column = new SQLColumnDefinition();
                        column.setDataType(selectItem.computeDataType());
                        if (DbType.mysql == dbType) {
                            if (resolvedColumn.getDefaultExpr() != null) {
                                column.setDefaultExpr(resolvedColumn.getDefaultExpr().clone());
                            }
                            if (resolvedColumn.getConstraints().size() > 0) {
                                for (SQLColumnConstraint constraint : resolvedColumn.getConstraints()) {
                                    column.addConstraint(constraint.clone());
                                }
                            }
                            if (resolvedColumn.getComment() != null) {
                                column.setComment(resolvedColumn.getComment());
                            }
                        }
                    }
                }
                if (column == null) {
                    column = new SQLColumnDefinition();
                    column.setDataType(selectItem.computeDataType());
                }
                String name = selectItem.computeAlias();
                column.setName(name);
                column.setDbType(dbType);
                x1.addColumn(column);
            }
            if (x1.getTableElementList().size() > 0) {
                x1.setSelect(null);
            }
        }
    }
    SQLExprTableSource like = x1.getLike();
    if (like != null) {
        SchemaObject tableObject = null;
        SQLName name = like.getName();
        if (name != null) {
            tableObject = findTable(name);
        }
        SQLCreateTableStatement tableStmt = null;
        if (tableObject != null) {
            SQLStatement stmt = tableObject.getStatement();
            if (stmt instanceof SQLCreateTableStatement) {
                tableStmt = (SQLCreateTableStatement) stmt;
            }
        }
        if (tableStmt != null) {
            SQLName tableName = x1.getName();
            tableStmt.cloneTo(x1);
            x1.setName(tableName);
            x1.setLike((SQLExprTableSource) null);
        }
    }
    x1.setSchema(null);
    String name = x1.computeName();
    SchemaObject table = schema.findTableOrView(name);
    if (table != null) {
        if (x1.isIfNotExists()) {
            return table;
        }
        LOG.info("replaced table '" + name + "'");
    }
    table = new SchemaObject(schema, name, SchemaObjectType.Table, x1);
    schema.objects.put(table.nameHashCode64(), table);
    return table;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)

Aggregations

SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)21 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)13 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)10 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)9 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)8 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)5 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)4 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)4 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)3 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)3