Search in sources :

Example 91 with SQLDataType

use of com.alibaba.druid.sql.ast.SQLDataType in project druid by alibaba.

the class Oracle2PG_DataTypeTest_number_int method test_oracle2pg_decimal.

public void test_oracle2pg_decimal() throws Exception {
    String sql = "decimal(19,1)";
    SQLDataType dataType = SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).parseDataType();
    SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
    assertEquals("DECIMAL(19, 1)", pgDataType.toString());
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType)

Example 92 with SQLDataType

use of com.alibaba.druid.sql.ast.SQLDataType in project druid by alibaba.

the class Oracle2PG_DataTypeTest_number_int method test_oracle2pg_int_6.

public void test_oracle2pg_int_6() throws Exception {
    String sql = "number(6, 0)";
    SQLDataType dataType = SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).parseDataType();
    SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
    assertEquals("INT", pgDataType.toString());
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType)

Example 93 with SQLDataType

use of com.alibaba.druid.sql.ast.SQLDataType 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 94 with SQLDataType

use of com.alibaba.druid.sql.ast.SQLDataType 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 95 with SQLDataType

use of com.alibaba.druid.sql.ast.SQLDataType 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)

Aggregations

SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)97 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)8 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)7 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)4 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)3 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntervalExpr (com.alibaba.druid.sql.ast.expr.SQLIntervalExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)1 SQLAlterTableAlterColumn (com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn)1