├── README.md
├── dist
├── bin
│ ├── sql.bat
│ └── sql.sh
├── doc
│ └── imgs
│ │ ├── 1.png
│ │ ├── 10.png
│ │ ├── 12.png
│ │ ├── 13.png
│ │ ├── 14.png
│ │ ├── 2.png
│ │ ├── 3.gif
│ │ ├── 4.png
│ │ ├── 5.png
│ │ ├── 6.png
│ │ ├── 7.png
│ │ ├── 8.png
│ │ └── 9.png
├── lib
│ └── sql.jar
├── script
│ ├── format.class
│ └── format.js
├── sql.zip
├── table
│ └── table.fs
└── test
│ ├── log.json
│ ├── log.txt
│ └── readme.txt
├── format.class
├── format.js
├── log.json
├── log.txt
├── pom.xml
├── sql.log
├── sql.out
├── src
├── main
│ ├── java
│ │ ├── com
│ │ │ ├── core
│ │ │ │ ├── Config.java
│ │ │ │ ├── Engine.java
│ │ │ │ ├── FileFilter.java
│ │ │ │ ├── FileSQL.java
│ │ │ │ ├── FileUtil.java
│ │ │ │ ├── Parser.java
│ │ │ │ ├── Printer.java
│ │ │ │ ├── SQLFunc.java
│ │ │ │ ├── SQLMain.java
│ │ │ │ ├── TabMgr.java
│ │ │ │ └── format
│ │ │ │ │ ├── ClazzFormater.java
│ │ │ │ │ ├── FormatFactory.java
│ │ │ │ │ ├── Formater.java
│ │ │ │ │ ├── JsonFormater.java
│ │ │ │ │ ├── RgxFormater.java
│ │ │ │ │ ├── ScriptFormater.java
│ │ │ │ │ ├── SplitFormater.java
│ │ │ │ │ └── TableFormater.java
│ │ │ ├── log
│ │ │ │ └── Log.java
│ │ │ └── shell
│ │ │ │ └── Console.java
│ │ └── sql.java
│ └── resources
│ │ ├── sql.bat
│ │ └── sql.sh
└── test
│ ├── java
│ └── format.java
│ └── resources
│ └── format.js
└── table.fs
/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/README.md
--------------------------------------------------------------------------------
/dist/bin/sql.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 |
3 | set "CURRENT_DIR=%cd%"
4 | if not "%SQL_HOME%" == "" goto filesql
5 |
6 | set "SQL_HOME=%CURRENT_DIR%"
7 | if exist "%SQL_HOME%\bin\sql.bat" goto filesql
8 |
9 | cd ..
10 | set "SQL_HOME=%cd%"
11 | cd "%CURRENT_DIR%"
12 |
13 |
14 | :filesql
15 |
16 | java -jar "%SQL_HOME%/lib/sql.jar" com.shell.Console -Dlog=%SQL_HOME%/log -Dscript=%SQL_HOME%/script -Dtable=%SQL_HOME%/table -Dworkspace=%CURRENT_DIR%
--------------------------------------------------------------------------------
/dist/bin/sql.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | stty erase ^H
3 |
4 | if [ "$SQL_HOME" = "" ] ;then
5 | CURRENT_DIR=`pwd`
6 | if [ -f $SQL_HOME/bin/sql.sh ]; then
7 | SQL_HOME=$CURRENT_DIR
8 | else
9 | cd ..
10 | CURRENT_DIR=`pwd`
11 | SQL_HOME=$CURRENT_DIR
12 | fi
13 | export SQL_HOME=$SQL_HOME
14 | fi
15 |
16 | java -jar "$SQL_HOME/lib/sql.jar" com.shell.Console -Dlog=$SQL_HOME/log -Dscript=$SQL_HOME/script -Dtable=$SQL_HOME/table -Dworkspace=$CURRENT_DIR
17 |
--------------------------------------------------------------------------------
/dist/doc/imgs/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/1.png
--------------------------------------------------------------------------------
/dist/doc/imgs/10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/10.png
--------------------------------------------------------------------------------
/dist/doc/imgs/12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/12.png
--------------------------------------------------------------------------------
/dist/doc/imgs/13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/13.png
--------------------------------------------------------------------------------
/dist/doc/imgs/14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/14.png
--------------------------------------------------------------------------------
/dist/doc/imgs/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/2.png
--------------------------------------------------------------------------------
/dist/doc/imgs/3.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/3.gif
--------------------------------------------------------------------------------
/dist/doc/imgs/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/4.png
--------------------------------------------------------------------------------
/dist/doc/imgs/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/5.png
--------------------------------------------------------------------------------
/dist/doc/imgs/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/6.png
--------------------------------------------------------------------------------
/dist/doc/imgs/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/7.png
--------------------------------------------------------------------------------
/dist/doc/imgs/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/8.png
--------------------------------------------------------------------------------
/dist/doc/imgs/9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/doc/imgs/9.png
--------------------------------------------------------------------------------
/dist/lib/sql.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/lib/sql.jar
--------------------------------------------------------------------------------
/dist/script/format.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/script/format.class
--------------------------------------------------------------------------------
/dist/script/format.js:
--------------------------------------------------------------------------------
1 | function format(row,field,fields){
2 | var fields=fields.split(',');
3 | for(var i in fields){
4 | if(fields[i]==field){
5 | return row.split('|')[i];
6 | }
7 | }
8 | return null;
9 | };
--------------------------------------------------------------------------------
/dist/sql.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/dist/sql.zip
--------------------------------------------------------------------------------
/dist/table/table.fs:
--------------------------------------------------------------------------------
1 | log.txt id,name,ip,segment,num |
2 | log.json id,name,ip,segment,num json
3 |
--------------------------------------------------------------------------------
/dist/test/log.json:
--------------------------------------------------------------------------------
1 | {id:6,name:'taobao',ip:'192.168.1.1',segment:{service:'order',code:1},num:666}
2 | {id:7,name:'ctrip',ip:'192.168.2.1',segment:{service:'query',code:1},num:777}
3 | {id:8,name:'tmall',ip:'192.168.1.1',segment:{service:'delete',code:1},num:888}
4 | {id:9,name:'qunar',ip:'192.168.3.1',segment:{service:'update',code:1},num:999}
5 | {id:10,name:'jdong',ip:'192.168.4.1',segment:{service:'search',code:1},num:100}
--------------------------------------------------------------------------------
/dist/test/log.txt:
--------------------------------------------------------------------------------
1 | 1|taobao|192.168.0.1|{service:'order',code:1}|111
2 | 2|ctrip|192.168.0.2|{service:'query',code:2}|222
3 | 3|tmall|192.168.0.3|{service:'delete',code:3}|333
4 | 4|qunar|192.168.0.4|{service:'update',code:4}|444
5 | 5|jdong|192.168.0.5|{service:'search',code:5}|555
--------------------------------------------------------------------------------
/dist/test/readme.txt:
--------------------------------------------------------------------------------
1 | +--+------+-----------+-------------------------+---+
2 | |id|name |ip |segment |num|
3 | +--+------+-----------+-------------------------+---+
4 | |1 |taobao|192.168.0.1|{service:'order',code:1} |111|
5 | |2 |ctrip |192.168.0.2|{service:'query',code:2} |222|
6 | |3 |tmall |192.168.0.3|{service:'delete',code:3}|333|
7 | |4 |qunar |192.168.0.4|{service:'update',code:4}|444|
8 | |5 |jdong |192.168.0.5|{service:'search',code:5}|555|
9 | +--+------+-----------+-------------------------+---+
10 |
--------------------------------------------------------------------------------
/format.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/format.class
--------------------------------------------------------------------------------
/format.js:
--------------------------------------------------------------------------------
1 | function format(row,field,fields){
2 | var fields=fields.split(',');
3 | for(var i in fields){
4 | if(fields[i]==field){
5 | return row.split('|')[i];
6 | }
7 | }
8 | return null;
9 | };
--------------------------------------------------------------------------------
/log.json:
--------------------------------------------------------------------------------
1 | {id:6,name:'taobao',ip:'192.168.1.1',segment:{service:'order',code:1},num:666}
2 | {id:7,name:'ctrip',ip:'192.168.2.1',segment:{service:'query',code:1},num:777}
3 | {id:8,name:'tmall',ip:'192.168.1.1',segment:{service:'delete',code:1},num:888}
4 | {id:9,name:'qunar',ip:'192.168.3.1',segment:{service:'update',code:1},num:999}
5 | {id:10,name:'jdong',ip:'192.168.4.1',segment:{service:'search',code:1},num:100}
--------------------------------------------------------------------------------
/log.txt:
--------------------------------------------------------------------------------
1 | 1|taobao|192.168.0.1|{service:'order',code:1}|111
2 | 2|ctrip|192.168.0.2|{service:'query',code:2}|222
3 | 3|tmall|192.168.0.3|{service:'delete',code:3}|333
4 | 4|qunar|192.168.0.4|{service:'update',code:4}|444
5 | 5|jdong|192.168.0.5|{service:'search',code:5}|555
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | file-sql
4 | file-sql
5 | 0.0.1-SNAPSHOT
6 |
7 |
8 | com.jayway.jsonpath
9 | json-path
10 | 2.2.0
11 |
12 |
13 | org.slf4j
14 | slf4j-simple
15 | 1.7.22
16 |
17 |
18 |
19 |
20 |
21 | maven-compiler-plugin
22 | 3.5.1
23 |
24 | 1.8
25 | 1.8
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/sql.log:
--------------------------------------------------------------------------------
1 | 2017-02-24 10:36:02 [main] INFO com.log.Log - execute error:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
2 | at java.util.LinkedList.checkElementIndex(Unknown Source)
3 | at java.util.LinkedList.get(Unknown Source)
4 | at com.core.Printer.printbuff(Printer.java:154)
5 | at com.core.Printer.close(Printer.java:92)
6 | at com.core.FileSQL.print(FileSQL.java:131)
7 | at com.core.FileSQL.group(FileSQL.java:110)
8 | at com.core.FileSQL.run(FileSQL.java:68)
9 | at com.core.SQLMain.execute(SQLMain.java:13)
10 | at com.shell.Console.run(Console.java:70)
11 | at com.shell.Console.execute(Console.java:54)
12 | at com.shell.Console.main(Console.java:138)
13 |
14 |
--------------------------------------------------------------------------------
/sql.out:
--------------------------------------------------------------------------------
1 | 2017-02-24 14:15:47 [main] INFO com.log.Log - execute error:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
2 | at java.util.LinkedList.checkElementIndex(Unknown Source)
3 | at java.util.LinkedList.get(Unknown Source)
4 | at com.core.Printer.printbuff(Printer.java:154)
5 | at com.core.Printer.close(Printer.java:92)
6 | at com.core.FileSQL.print(FileSQL.java:128)
7 | at com.core.FileSQL.filter(FileSQL.java:89)
8 | at com.core.FileSQL.run(FileSQL.java:70)
9 | at com.core.SQLMain.execute(SQLMain.java:13)
10 | at com.shell.Console.run(Console.java:70)
11 | at com.shell.Console.execute(Console.java:54)
12 | at com.shell.Console.main(Console.java:138)
13 |
14 | 2017-02-24 14:17:34 [main] INFO com.log.Log - execute error:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
15 | at java.util.LinkedList.checkElementIndex(Unknown Source)
16 | at java.util.LinkedList.get(Unknown Source)
17 | at com.core.Printer.printbuff(Printer.java:154)
18 | at com.core.Printer.close(Printer.java:92)
19 | at com.core.FileSQL.print(FileSQL.java:128)
20 | at com.core.FileSQL.filter(FileSQL.java:89)
21 | at com.core.FileSQL.run(FileSQL.java:70)
22 | at com.core.SQLMain.execute(SQLMain.java:13)
23 | at com.shell.Console.run(Console.java:70)
24 | at com.shell.Console.history(Console.java:88)
25 | at com.shell.Console.run(Console.java:63)
26 | at com.shell.Console.execute(Console.java:54)
27 | at com.shell.Console.main(Console.java:138)
28 |
29 | 2017-02-24 14:17:55 [main] INFO com.log.Log - execute error:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
30 | at java.util.LinkedList.checkElementIndex(Unknown Source)
31 | at java.util.LinkedList.get(Unknown Source)
32 | at com.core.Printer.printbuff(Printer.java:154)
33 | at com.core.Printer.close(Printer.java:92)
34 | at com.core.FileSQL.print(FileSQL.java:128)
35 | at com.core.FileSQL.filter(FileSQL.java:89)
36 | at com.core.FileSQL.run(FileSQL.java:70)
37 | at com.core.SQLMain.execute(SQLMain.java:13)
38 | at com.shell.Console.run(Console.java:70)
39 | at com.shell.Console.history(Console.java:88)
40 | at com.shell.Console.run(Console.java:63)
41 | at com.shell.Console.execute(Console.java:54)
42 | at com.shell.Console.main(Console.java:138)
43 |
44 | 2017-02-24 14:18:10 [main] INFO com.log.Log - execute error:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
45 | at java.util.LinkedList.checkElementIndex(Unknown Source)
46 | at java.util.LinkedList.get(Unknown Source)
47 | at com.core.Printer.printbuff(Printer.java:154)
48 | at com.core.Printer.close(Printer.java:92)
49 | at com.core.FileSQL.print(FileSQL.java:128)
50 | at com.core.FileSQL.filter(FileSQL.java:89)
51 | at com.core.FileSQL.run(FileSQL.java:70)
52 | at com.core.SQLMain.execute(SQLMain.java:13)
53 | at com.shell.Console.run(Console.java:70)
54 | at com.shell.Console.history(Console.java:88)
55 | at com.shell.Console.run(Console.java:63)
56 | at com.shell.Console.execute(Console.java:54)
57 | at com.shell.Console.main(Console.java:138)
58 |
59 |
--------------------------------------------------------------------------------
/src/main/java/com/core/Config.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/Config.java
--------------------------------------------------------------------------------
/src/main/java/com/core/Engine.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/Engine.java
--------------------------------------------------------------------------------
/src/main/java/com/core/FileFilter.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/FileFilter.java
--------------------------------------------------------------------------------
/src/main/java/com/core/FileSQL.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/FileSQL.java
--------------------------------------------------------------------------------
/src/main/java/com/core/FileUtil.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/FileUtil.java
--------------------------------------------------------------------------------
/src/main/java/com/core/Parser.java:
--------------------------------------------------------------------------------
1 | package com.core;
2 |
3 | import java.util.LinkedHashMap;
4 | import java.util.Map;
5 | import java.util.function.Function;
6 | import java.util.regex.Matcher;
7 | import java.util.regex.Pattern;
8 |
9 | /**
10 | * 解析器
11 | * @author 魏国兴
12 | *
13 | */
14 | public class Parser {
15 |
16 | // 语法
17 | public static final String DMLWORDS="select|from|where|group by|order by|limit|into";
18 | public static final String DDLWORDS="create table|fmt|drop table|update table|show tables|desc";
19 | public static final String OPERTIONS=">=|<=|!=|=|>|<|like|regex";
20 | public static final String AGGREG_FUNCS="sum|avg|max|min|count|distinct";
21 | public static final String FIELD_FUNCS="json_path|math|replace";
22 | public static final String OTHER="desc|asc|as";
23 |
24 | public static Map Parse(String sql){
25 | Map map=new LinkedHashMap<>();
26 | sql=format(sql);
27 | if(sql.matches("^("+DMLWORDS+").*")){
28 | map=DML(sql);
29 | }else if(sql.matches("^("+DDLWORDS+").*")){
30 | map=DDL(sql);
31 | }
32 | return map;
33 | }
34 | // 格式化
35 | private static String format(String sql){
36 | sql=sql.replaceAll("^\\s+|\\s+$|\\n+|;$","")
37 | .replaceAll("\\s+"," ")
38 | .replaceAll("\\s*,\\s*",",");
39 | sql=replace(sql,"\\s*("+DMLWORDS+"|"+DDLWORDS+"|"+OPERTIONS+"|"+AGGREG_FUNCS+"|"+FIELD_FUNCS+"|"+OTHER+")\\s*",
40 | match->{
41 | return match.group(0).toLowerCase();
42 | });
43 | return sql;
44 | }
45 | // 解析
46 | public static Map DML(String sql) {
47 |
48 | Map map=new LinkedHashMap<>();
49 | // 格式化
50 | sql=sql.replaceAll("\\s*("+OPERTIONS+")\\s*","$1");
51 | // 保存
52 | map.put("sql",sql);
53 |
54 |
55 | // 转换distinct
56 | sql=distinct(sql);
57 | // 转换sql
58 | sql=translate(sql).replaceAll("\\s*("+DMLWORDS+")\\s+","#$0#").substring(1).concat("#");
59 |
60 | // 保存
61 | replace(sql,"("+DMLWORDS+")\\s+#(.*?)#",match->{
62 | String func=match.group(1);
63 | String param=match.group(2);
64 | map.put(func,param);
65 | return null;
66 | });
67 | // 聚合函数
68 | String aggreg_func="";
69 | for(String field : map.get("select").split(",")){
70 | if(field.matches("^("+AGGREG_FUNCS+").*")) {
71 | aggreg_func+=field+",";
72 | }
73 | }
74 | if(!aggreg_func.equals("")){
75 | map.put("aggreg_func",aggreg_func.replaceAll(",$",""));
76 | }
77 |
78 | return map;
79 | }
80 | // 别名
81 | public static Map Alias(String select){
82 | Map alias=new LinkedHashMap<>();
83 | for(String field:select.split(",")){
84 | replace(field,"(.*?)\\s+as\\s+(\\w+)",m->{
85 | String key=m.group(1);
86 | String value=m.group(2);
87 | alias.put(value,key);
88 | return null;
89 | });
90 | };
91 | return alias;
92 | }
93 | // 函数参数
94 | public static String[] Func(String exp){
95 | String regx="(^[a-zA-Z_][a-zA-Z0-9_]*)\\((.*?)\\)";
96 | Pattern p = Pattern.compile(regx);
97 | Matcher m = p.matcher(exp);
98 | String func=null;
99 | String param=null;
100 | while (m.find()) {
101 | func=m.group(1);
102 | param=m.group(2);
103 | }
104 | return new String[]{func,param};
105 | }
106 |
107 | // 替换掉括弧之中的逗号
108 | private static String translate(String str){
109 | int index=0;
110 | int start=-1;
111 | for(int i=0;i{
132 | if(!sqlbuff.toString().contains("group")){
133 | sqlbuff.append(" group by "+match.group(2));
134 | }
135 | return null;
136 | });
137 | sql=sqlbuff.toString().replace("distinct","").replaceAll("\\s+"," ");
138 | return sql;
139 | }
140 | private static String replace(String str,String rgx,Function func){
141 | Pattern p=Pattern.compile(rgx);
142 | Matcher m=p.matcher(str);
143 | while(m.find()){
144 | String replace=func.apply(m);
145 | if(replace!=null&&!replace.equals("")){
146 | str=str.replace(m.group(0),replace);
147 | }
148 | }
149 | return str;
150 | }
151 |
152 | // DDL语句解析
153 | public static Map DDL(String sql){
154 | Map map=new LinkedHashMap<>();
155 | if(sql.startsWith("show tables")){
156 | map.put(sql,"show tables");
157 | return map;
158 | }
159 | sql=sql.replaceAll("\\s*("+DDLWORDS+")\\s+","#$0#").substring(1).concat("#");
160 | replace(sql,"("+DDLWORDS+")\\s+#(.*?)#",match->{
161 | String func=match.group(1);
162 | String param=match.group(2);
163 | map.put(func,param);
164 | return null;
165 | });
166 |
167 | return map;
168 | }
169 |
170 | public static void main(String args[]){
171 | String sql=" select name ,sum(num),distinct user ,json_path(sum(json,json),$.office) as office from {log1,log2}.txt,log3.json where office = taobao and name = haoe group by user order by num limit 0 , 10";
172 | Parse(sql).forEach((key,value)->{
173 | System.out.printf("%s==>%s\n",key,value);
174 | });
175 | Alias(Parse(sql).get("select")).forEach((key,value)->{
176 | System.out.printf("%s==>%s\n",key,value);
177 | });
178 |
179 | String create="create table log3.tb (id,user,ip) fmt function format(row,field,fields){ for(var i in fields){ if(fields[i]==field){ return row.split('|')[i];};};return null;};";
180 | Parse(create).forEach((key,value)->{
181 | System.out.printf("%s==>%s\n",key,value);
182 | });
183 | String drop="drop table *.txt";
184 | Parse(drop).forEach((key,value)->{
185 | System.out.printf("%s==>%s\n",key,value);
186 | });
187 | String desc="desc *.txt";
188 | Parse(desc).forEach((key,value)->{
189 | System.out.printf("%s==>%s\n",key,value);
190 | });
191 | String show="show tables";
192 | Parse(show).forEach((key,value)->{
193 | System.out.printf("%s==>%s\n",key,value);
194 | });
195 | }
196 | }
197 |
198 |
--------------------------------------------------------------------------------
/src/main/java/com/core/Printer.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/Printer.java
--------------------------------------------------------------------------------
/src/main/java/com/core/SQLFunc.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/SQLFunc.java
--------------------------------------------------------------------------------
/src/main/java/com/core/SQLMain.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/SQLMain.java
--------------------------------------------------------------------------------
/src/main/java/com/core/TabMgr.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/TabMgr.java
--------------------------------------------------------------------------------
/src/main/java/com/core/format/ClazzFormater.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/format/ClazzFormater.java
--------------------------------------------------------------------------------
/src/main/java/com/core/format/FormatFactory.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/format/FormatFactory.java
--------------------------------------------------------------------------------
/src/main/java/com/core/format/Formater.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/format/Formater.java
--------------------------------------------------------------------------------
/src/main/java/com/core/format/JsonFormater.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/format/JsonFormater.java
--------------------------------------------------------------------------------
/src/main/java/com/core/format/RgxFormater.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/format/RgxFormater.java
--------------------------------------------------------------------------------
/src/main/java/com/core/format/ScriptFormater.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/format/ScriptFormater.java
--------------------------------------------------------------------------------
/src/main/java/com/core/format/SplitFormater.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/core/format/SplitFormater.java
--------------------------------------------------------------------------------
/src/main/java/com/core/format/TableFormater.java:
--------------------------------------------------------------------------------
1 | package com.core.format;
2 |
3 | public class TableFormater extends Formater {
4 |
5 | public TableFormater(String fields, String express) {
6 | super(fields,express);
7 | }
8 | @Override
9 | public String format(String row, String field) {
10 | row=row.replaceAll("^[|,]|[|,]$","");
11 | String[] rows=row.split(token(this.express));
12 | int index=index(field);
13 | if(index>-1){
14 | return rows[index].replaceAll("^\\s+|\\s+$","");
15 | }
16 | return null;
17 | }
18 | private String token(String express){
19 | if(express.equals("|")){
20 | return "\\"+express;
21 | }
22 | if(express.equals("s")){
23 | return "\\"+express+"+";
24 | }
25 | return express;
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/log/Log.java:
--------------------------------------------------------------------------------
1 | package com.log;
2 |
3 | import java.io.PrintWriter;
4 | import java.io.StringWriter;
5 | import java.nio.file.Paths;
6 |
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 |
10 | import com.core.Config;
11 |
12 | public class Log {
13 | static Logger log=null;
14 | public static void log(String format,Object arg){
15 | if(Config.getLogDir()!=null){
16 | System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY,"DEBUG");
17 | System.setProperty(org.slf4j.impl.SimpleLogger.SHOW_DATE_TIME_KEY,"true");
18 | System.setProperty(org.slf4j.impl.SimpleLogger.DATE_TIME_FORMAT_KEY,"yyy-MM-dd HH:mm:ss");
19 | System.setProperty(org.slf4j.impl.SimpleLogger.LOG_FILE_KEY,Paths.get(Config.getLogDir(),"sql.out").toString());
20 | if(log==null){
21 | log=LoggerFactory.getLogger(Log.class);
22 | }
23 | if(arg instanceof Exception){
24 | log.info(format+trace((Exception)arg));
25 | }else{
26 | log.info(format,arg);
27 | }
28 | }
29 |
30 | }
31 | public static String trace(Exception e) {
32 | StringWriter stwriter = new StringWriter();
33 | PrintWriter writer = new PrintWriter(stwriter);
34 | e.printStackTrace(writer);
35 | StringBuffer buffer=stwriter.getBuffer();
36 | return buffer.toString();
37 | }
38 | public static void main(String args[]){
39 | Log.log("test","tes");
40 | Log.log("test","tes");
41 | Log.log("test","tes");
42 | try{
43 | int z=0;
44 | System.out.println(1/z);
45 | }catch(Exception e){
46 | Log.log("eror",e);
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/com/shell/Console.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mircode/sql-file/03f9653905c1806ef9b318d28e90dc9f66c531ca/src/main/java/com/shell/Console.java
--------------------------------------------------------------------------------
/src/main/java/sql.java:
--------------------------------------------------------------------------------
1 | import com.shell.Console;
2 |
3 |
4 | public class sql {
5 | public static void main(String args[]){
6 | Console.main(args);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/resources/sql.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 |
3 | set "CURRENT_DIR=%cd%"
4 | if not "%SQL_HOME%" == "" goto filesql
5 |
6 | set "SQL_HOME=%CURRENT_DIR%"
7 | if exist "%SQL_HOME%\bin\sql.bat" goto filesql
8 |
9 | cd ..
10 | set "SQL_HOME=%cd%"
11 | cd "%CURRENT_DIR%"
12 |
13 |
14 | :filesql
15 |
16 | java -jar "%SQL_HOME%/lib/sql.jar" sql "-Dlog=%SQL_HOME%/log" "-Dscript=%SQL_HOME%/script" "-Dtable=%SQL_HOME%/table" "-Dworkspace=%CURRENT_DIR%"
--------------------------------------------------------------------------------
/src/main/resources/sql.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | stty erase ^H
3 |
4 | if [ "$SQL_HOME" = "" ] ;then
5 | CURRENT_DIR=`pwd`
6 | if [ -f $SQL_HOME/bin/sql.sh ]; then
7 | SQL_HOME=$CURRENT_DIR
8 | else
9 | cd ..
10 | CURRENT_DIR=`pwd`
11 | SQL_HOME=$CURRENT_DIR
12 | fi
13 | export SQL_HOME=$SQL_HOME
14 | fi
15 |
16 | java -jar "%SQL_HOME%/lib/sql.jar" sql "-Dlog=%SQL_HOME%/log" "-Dscript=%SQL_HOME%/script" "-Dtable=%SQL_HOME%/table" "-Dworkspace=%CURRENT_DIR%"
17 |
--------------------------------------------------------------------------------
/src/test/java/format.java:
--------------------------------------------------------------------------------
1 | public class format {
2 | public String format(String row,String field,String fs){
3 | String[] rows=row.split("\\|");
4 | String[] fields=fs.split(",");
5 | for(int i=0,len=fields.length;i