# sql-encapsulation **Repository Path**: isjinhao/sql-encapsulation ## Basic Information - **Project Name**: sql-encapsulation - **Description**: 封装对sql的操作 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-04-16 - **Last Updated**: 2021-08-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 全部功能 ### 语法解析 将SQL解析为语法树,并能检测SQL的基本信息,比如获取SQL操作的表及操作了表的哪些字段。 ### 脚本生成 脚本生成器。通过读取excel文件、insert脚本并按SQL模板生成sql脚本。(未开发,预计2021年8月份开始) ### 集合运算 将SQL作用于集合来处理数据。(未开发,预计2021年10月份开始) ### 索引检测 MySQL常用的索引问题检测。(未开发,预计2022年1月份开始) ## 面向用户 Java后端开发工程师,在Java后端应用中处理SQL。 ## 设计原则 - 尽可能的支持MySQL的语法,对其他数据库产品的语法无支持计划。 - 函数支持自定义 - 同一功能同一性能的语法不进行支持,保证后续拓展功能的简单。 - 不允许所有代码堆叠在一个类中,能抽出来的都要抽出来,这是递归下降+观察模式这种语法解析方式很容易出现的问题。目前Parser的抽象实现为AbstratParser,CurdStatementVisitor的抽象实现有InsertVisitor、UpdateVisitor、SelectVisitor、DeleteVisitor、StatementAstMetaInfoVisitor等。 ## feature详细 ### feature计划 - 增加对查询字段为true、false、number、string的支持 √ - 增加InsertParser对if not exists语法的支持 × - 增加InsertParser对insert select语法的支持 √ - 处理join的bug,所有的join类型:("left" | "right" | "full" | "cross")? ("inner" | "outer")? ("join" | ",") √ - 自定义函数 √ - 支持多CurdParser实例 √ - 异常信息标准化,提示都是final String,且具有errorCode √ - 处理IS √ - 增加日期参数语法 √ - 实现日期转换的六个函数 √ - FunctionHandler的returnType改为自定义的枚举 √ - timeUnit语法 & extract函数 √ - 函数计算的错误提示精确化 - CASE WHEN - Evaluator由CurdStatement创建 ### 不支持的feature #### DDL语句 只支持curd语句,不支持create、alter、drop、grant等语句。 #### 关键字的大写 举例: `SELECT * FROM T;`。 在业务上改写为: `select * from T`。 即所有关键字都需要小写。 #### 函数只能处理单一行的字段数据 举例: `select rank() over(partition by deptid order by salary) my_rank, deptid, USERID, salary from tsaler;` 在业务上可以先用SQL取出来所有数据,再通过应用程序处理得到最后的结果。 #### 字段中使用select语句 举例: `select a, (select b from t2 where t2.unique = t1.n) as b from t1 where t1.id = 10;`。 在业务上可以改写为: `select a, b from t1, t2 where t1.id = 10 and t1.n = t2.unique;` #### using(a, b)语法 举例: `select c, d from t1, t2 using(a, b) where id > 10000;`。 在业务上可以改写为: `select c, d from t1, t2 on t1.a = t2.a and t1.b = t2.b where id > 10000;`。 #### select into语法 举例: `select app_name, country into Websites from apps where app_type = '割韭菜';`。 在业务上可以改写为: `insert into Websites (name, country) select app_name, country from apps where app_type = '割韭菜';`。 #### insert into select的 select 不支持grouping 举例: `insert into STUDENT(SNO, SNAME, SSEX, SBIRTHDAY, CLASS) (select 300, '张三', '男', '2000-01-01', '95032')`。 在业务上可以改写为: ` insert into STUDENT(SNO, SNAME, SSEX, SBIRTHDAY, CLASS) select 300, '张三', '男', '2000-01-01', '95032';` ## 函数 ### 日期 - now():当前日期(MySQL支持) - date_format(date, format):日期转字符串(MySQL支持) - unix_timestamp(date):日期转时间戳(MySQL支持) - str_to_date(dateStr, format):字符串转日期(MySQL支持) - from_unixtime(timestamp):时间戳转日期(MySQL支持) - date_add(date, interval):日期加(MySQL支持) - date_sub(date, interval):日期减(MySQL支持) - timestampdiff(type, start_date, end_date)(MySQL支持) - date(date):取日期(MySQL支持) - extract(timeUnit):取对应单位的日期的数值(MySQL支持) ### logic - decode(value, condition1, result1, ..., defaultResult):case when语法 - if(booleanCondition, trueResult, falseResult):if函数(MySQL支持) ### 字符串 - replace(str, oldSeg, newSeg):(MySQL支持) - substring(str, start, length):(MySQL支持) - concat(str1, str2, ...):(MySQL支持) ## 测试通过的案例 ### 语法解析 ### 脚本生成 ### 集合运算