美文网首页
Oracle数据注释解析成查询语句

Oracle数据注释解析成查询语句

作者: power杏迪 | 来源:发表于2023-08-30 16:21 被阅读0次

场景

在实际的开发过程中我们可能会遇到,将一个表的字段转换成查询sql,例如:

select name as "姓名",age as "年龄" from acount

或者说根据sql将数据导出到excel中,这时候excel表头的注释不能是字段名称,要应用成相关字段的注释。如果字段少手写也是可以的,麻烦点就在与字段一多就很累。

解决问题

使用工具类OracleCommentParse就可以帮你解决这个头痛的问题。

public class OracleCommentParse {


    /**
     * 示例:
     * comment on column table.name IS '姓名';
     * comment on column table.age IS '年龄';
     * 解析后的sql:
     * select name as "姓名",age as "年龄" from table
     */
    public static void main(String[] args) {

        String fileName = "文件地址\\文件名称.txt";

        String table = "表名";

        LinkedHashMap<String, String> map = new LinkedHashMap<>();

        try {
            Scanner sc = new Scanner(new FileReader(fileName));
            //分隔符
            sc.useDelimiter(";");
            //按分隔符读取字符串
            while (sc.hasNext()) {
                String str = sc.next().toUpperCase();

                //判断表名字出现的位置
                int tableIndex = str.indexOf(table);

                //判断is最后出现的位置
                int isIndex = str.lastIndexOf("IS");
                //获取字段
                String column = str.substring(tableIndex + table.length() + 1, isIndex - 1);

                //获取字段注释
                String columnComment = str.substring(isIndex + 4, str.length() - 1);

                map.put(column, columnComment);

            }


            //拼接成查询sql
            StringBuilder sb = new StringBuilder();
            sb.append("select ");
            for (Map.Entry<String, String> entry : map.entrySet()) {

                sb.append(entry.getKey()).append(" as ").append("\"" + entry.getValue() + "\"").append(",");
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append(" from ").append(table);

            System.out.println(sb.toString());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}

示例:

提供一份txt文件,格式如下:

 comment on column table.name IS '姓名';
 comment on column table.age IS '年龄';

第一步 读取文件内容

工具类中 以 ; 为结尾读取第一行数据,
这时候读取的结果:comment on column table.name IS '姓名';

第二步 java的字符截取

// 获取表名出现的位置
int tableIndex = str.indexOf(table);

//判断is最后出现的位置  因为 IS是oracle注释语法的关键词
int isIndex = str.lastIndexOf("IS");

//获取字段 
String column = str.substring(tableIndex + table.length() + 1, isIndex - 1);

//获取字段注释
String columnComment = str.substring(isIndex + 4, str.length() - 1);

结果

select name as "姓名",age as "年龄" from table

工具类源码地址

地址:https://github.com/POWERzhangdi/jet_fighter/blob/main/src/main/java/com/jet/fighter/dbcomment/OracleCommentParse.java

相关文章

  • Oracle数据库学习整理(一)

    Oracle 基础知识 查询数据 本节将学习如何从Oracle数据库中查询数据。 ●Select语句-演示如何查询...

  • Oracle基础(第一节)

    关于注释: 1、--:单行注释;2、/* */:多行注释; 关于查询语句 DQL: 数据操作语句 DML:...

  • Oracle 基础知识

    查询数据 本节将学习如何从Oracle数据库中查询数据。 ●Select语句-演示如何查询单个表中的数据。 排序数...

  • Oracle 常用sql语句

    其他 1.Oracle 查询表字段和注释 可以用来拼接sql语句,或者拼接生成 java 实体类 2.Oracle...

  • MySQL数据库查询语句

    ** select查询语句**select *from 数据表名;注释:查询数据表里所有数据select name...

  • GreenDao

    前言:数据库:MySQL、Oracle、Sqlite 一. 复习SQL语句(结构化查询语言) 1.SQL语句分类 ...

  • SQL - Select / Create

    ORACLE 12c 中 SQL Server Select 语句从数据库中调出数据,是一种查询语句 1:必须要有...

  • GreenDao

    数据库:MySQL、Oracle、Sqlite 一. 复习SQL语句(结构化查询语言) 1.SQL语句分类 DDL...

  • SQL语言③--SQL是如何执行的

    1、Oracle中SQL语句是怎么执行的,什么是硬解析,什么是软解析? Oracle中SQL语句的执行过程...

  • SQL之WHERE语句

    SQL语句是数据库查询语句,可以应用在各种数据库操作软件中,比如Mysql,Oracle,因此SQL语句学一套就基...

网友评论

      本文标题:Oracle数据注释解析成查询语句

      本文链接:https://www.haomeiwen.com/subject/ihkamdtx.html