Search in sources :

Example 11 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType 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 12 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType 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 13 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType 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 14 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project Mycat-Server by MyCATApache.

the class DruidCreateTableParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement) stmt;
    if (createStmt.getQuery() != null) {
        String msg = "create table from other table not supported :" + stmt;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String tableName = StringUtil.removeBackquote(createStmt.getTableSource().toString().toUpperCase());
    if (schema.getTables().containsKey(tableName)) {
        TableConfig tableConfig = schema.getTables().get(tableName);
        AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
        if (algorithm instanceof SlotFunction) {
            SQLColumnDefinition column = new SQLColumnDefinition();
            column.setDataType(new SQLCharacterDataType("int"));
            column.setName(new SQLIdentifierExpr("_slot"));
            column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
            ((SQLCreateTableStatement) stmt).getTableElementList().add(column);
            String sql = createStmt.toString();
            rrs.setStatement(sql);
            ctx.setSql(sql);
        }
    }
    ctx.addTable(tableName);
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLNonTransientException(java.sql.SQLNonTransientException) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) TableConfig(io.mycat.config.model.TableConfig) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SlotFunction(io.mycat.route.function.SlotFunction)

Example 15 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project Mycat-Server by MyCATApache.

the class RouterUtil method changeCreateTable.

private static String changeCreateTable(SchemaConfig schema, String tableName, String sql) {
    if (schema.getTables().containsKey(tableName)) {
        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement insertStatement = parser.parseStatement();
        if (insertStatement instanceof MySqlCreateTableStatement) {
            TableConfig tableConfig = schema.getTables().get(tableName);
            AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
            if (algorithm instanceof SlotFunction) {
                SQLColumnDefinition column = new SQLColumnDefinition();
                column.setDataType(new SQLCharacterDataType("int"));
                column.setName(new SQLIdentifierExpr("_slot"));
                column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
                ((SQLCreateTableStatement) insertStatement).getTableElementList().add(column);
                return insertStatement.toString();
            }
        }
    }
    return sql;
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) TableConfig(io.mycat.config.model.TableConfig) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SlotFunction(io.mycat.route.function.SlotFunction)

Aggregations

SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)15 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)7 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)7 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)7 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)5 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4 TableConfig (io.mycat.config.model.TableConfig)4 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)4 SlotFunction (io.mycat.route.function.SlotFunction)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 CastType (com.actiontech.dble.plan.common.CastType)1 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)1