druid DDL解析关于获取字段长度精度以及是否为空的函数

biswetbf  于 2022-10-22  发布在  Druid
关注(0)|答案(2)|浏览(334)

DDL解析关于获取字段长度精度以及是否为空的函数怎么获取呢?

String ddl = "CREATE TABLE tableP (\n" + " rate double(10,2) DEFAULT NULL,\n" + " id int(10) NOT NULL AUTO_INCREMENT,\n" + " province varchar(255) DEFAULT NULL,\n" + " PRIMARY KEY ( id )\n" + ") ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;"; MySqlStatementParser parser = new MySqlStatementParser(ddl); List<SQLStatement> list3 = parser.parseStatementList(); for (SQLStatement statement : list3) { if (statement instanceof MySqlCreateTableStatement) { System.out.println("表注解:" + ((MySqlCreateTableStatement) statement).getComment()); System.out.println("表名:" + ((MySqlCreateTableStatement) statement).getTableSource().getName().getSimpleName()); List<SQLTableElement> elms = ((MySqlCreateTableStatement) statement).getTableElementList(); for (SQLTableElement elm : elms) { if (elm instanceof SQLColumnDefinition) { SQLColumnDefinition elm1 = (SQLColumnDefinition) elm; System.out.println("字段名:" + elm1.getName()); System.out.println("字段类型:" + elm1.getDataType()); System.out.println("中文名:" + elm1.getComment()); SQLIntegerExpr sqlIntegerExpr = (SQLIntegerExpr) elm1.getDataType().getArguments().get(0); System.out.println("长度:" + sqlIntegerExpr.getNumber()); } } } }

如上面示例所对应的
1.字段长度精度获取
2.字段是否为空获取

bogh5gae

bogh5gae1#

String ddl = "CREATE TABLE `tableP` (\n" +
            "  `rate` double(10,2) DEFAULT NULL,\n" +
            "  `id` int(10) NOT NULL AUTO_INCREMENT,\n" +
            "  `province` varchar(255) DEFAULT NULL,\n" +
            "  PRIMARY KEY (`id`)\n" +
            ") ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;";
    MySqlStatementParser parser = new MySqlStatementParser(ddl);
    List<SQLStatement> list3 = parser.parseStatementList();
    for (SQLStatement statement : list3) {
        if (statement instanceof MySqlCreateTableStatement) {
            System.out.println("表注解:" + ((MySqlCreateTableStatement) statement).getComment());
            System.out.println("表名:" + ((MySqlCreateTableStatement) statement).getTableSource().getName().getSimpleName());
            List<SQLTableElement> elms = ((MySqlCreateTableStatement) statement).getTableElementList();
            for (SQLTableElement elm : elms) {
                if (elm instanceof SQLColumnDefinition) {
                    SQLColumnDefinition elm1 = (SQLColumnDefinition) elm;
                    System.out.println("字段名:" + elm1.getName());
                    System.out.println("字段类型:" + elm1.getDataType());
                    System.out.println("中文名:" + elm1.getComment());
                    SQLIntegerExpr sqlIntegerExpr = (SQLIntegerExpr) elm1.getDataType().getArguments().get(0);
                    System.out.println("长度:" + sqlIntegerExpr.getNumber());
                }
            }
        }
    }
gupuwyp2

gupuwyp22#

你好有个问题。我想动态的去追加表。比如一开始是 select * from a,b 加c表 select * from a,b,c 我发现SQLJoinTableSource都只有进行替换的功能。没有追加的操作。或者最多把right的SelectTableReference的改为SQLJointTableSource。但这样也很复杂

相关问题