📄 dbcompile.java
字号:
import java.io.*;
import java.util.Iterator;
import java.util.Vector;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JTextField;
public class DBcompile {
public static void main(String []args){
Frame f=new Frame("TEST"); //生成窗口
f.setSize(500,430); //窗口大小
f.setLocation(500, 200); //窗口位置
f.addWindowListener(new WindowAdapter(){ //窗口关闭按钮
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//f.setResizable(false);
JButton btn=new JButton("Perform"); //添加按钮
btn.setSize(50, 100); //按钮大小
TextArea t=new TextArea(); //添加输入框
t.setSize(400, 100); //输入框大小
TextArea t1=new TextArea(); //添加输出框
t1.setSize(400, 200);
t1.setEditable(false);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.add(t);
f.add(btn);
f.add(t1);
SC sc=new SC(t,t1); //StateChange类包含了状态转移的所有操作
btn.addActionListener(sc);
f.setVisible(true);
}
}
class SC implements ActionListener{
public static TableManager tm = new TableManager();
char des;
int pos=-1;
Vector table=new Vector();
Vector tablename;
Vector column;
Vector type;
Vector bound;
Vector value;
Vector refer;
Vector conj;
Vector standard;
Vector value1;
Vector function;
Vector othername;
int []nextStatus=new int[7]; //初始状态集合
String action,by;
File a=new File("rule1.txt"); //状态规则文件
File b=new File("source.txt"); //sql语句源文件
File c=new File("des.txt"); //目标操作过程文件
File d=new File("key.txt");
InputStreamReader in;
BufferedReader in1;
BufferedWriter out;
String sign;
String next;
String result;
boolean end,order;
String op[],ref[],key[],punc[],r[],datatype[],func[],conn[];
String rules[][];
TextArea t;
TextArea t1;
String outscreen;
public SC(TextArea t,TextArea t1) {
this.t=t;
this.t1=t1;// TODO Auto-generated constructor stub
}
public void actionPerformed(ActionEvent e)
{
outscreen="";by="";
pos=-1;
nextStatus[0]=0;nextStatus[1]=1;nextStatus[2]=2;nextStatus[3]=136;nextStatus[4]=141;nextStatus[5]=-1;nextStatus[6]=-1;
action="";
end=false;order=true;
sign=" ";
next="";
result="";
t1.setText(" ");
execute(); //execute()方法是SC类的开始方法
}
void execute(){
String rule="",sql="",source;
int s;
try {
//////////////////////////////////////////
//将关键字key,类型type,关系符ref,操作符op,标点符号punc取出
in1=new BufferedReader(new FileReader(d));
key=in1.readLine().split(" ");key[0]=key[0].substring(key[0].indexOf(":")+1);
datatype=in1.readLine().split(" ");datatype[0]=datatype[0].substring(datatype[0].indexOf(":")+1);
ref=in1.readLine().split(" ");ref[0]=ref[0].substring(ref[0].indexOf(":")+1);
op=in1.readLine().split(" ");op[0]=op[0].substring(op[0].indexOf(":")+1);
punc=in1.readLine().split(" ");punc[0]=punc[0].substring(punc[0].indexOf(":")+1);
func=in1.readLine().split(" ");func[0]=func[0].substring(func[0].indexOf(":")+1);
in1.close();
//////////////////////////////////////////
//将状态转换表规则取出,r以行为单位
in=new InputStreamReader(new FileInputStream(a));
while((s=in.read())!=-1){
rule+=(char)s;
}
r=rule.split("\n");
for(int i=0;i<r.length;i++)
r[i]=r[i].substring(0,r[i].length()-1);
//////////////////////////////////////////
//rules存储表中每个单位
rules=new String[r.length][5];
for(int i=0;i<rules.length;i++)
{
rules[i]=r[i].split(" ");
}
//////////////////////////////////////////
//将sql语句取出
//System.out.println(t.getSelectedText());
sql=t.getSelectedText();
if(sql==""||sql.length()<=1) sql=t.getText();
sql=sql.substring(0,sql.indexOf(";")+1);
sql=sql.toLowerCase();
if(sql==null || sql.equals("")) {
outscreen+="please enter the sql\n";
return;
}
else sql="$ "+sql;
//////////////////////////////////////////
//将sql语句规范化
String tmp[] = null;
for(int i=0;i<sql.length();i++) //加空格
{
for(int k=0;k<3;k++)
{
switch(k){
case 0:tmp=op;break;
case 1:tmp=ref;break;
case 2:tmp=punc;
}
for(int j=0;j<tmp.length;j++)
{
if(tmp[j].equals(sql.charAt(i)+""))
if((sql.charAt(i)=='<'||sql.charAt(i)=='>'||sql.charAt(i)=='!') && sql.charAt(i+1)=='=')
{
sql = sql.substring(0, i) + " " + sql.charAt(i)
+ sql.charAt(i + 1) + " "
+ sql.substring(i + 2);
i += 3;
break;
}else {
sql=sql.substring(0, i)+" "+sql.charAt(i)
+" "+sql.substring(i+1);
i+=2;
break;
}
}
}
}
sql=sql.replace("\n"," ");
sql=sql.replace("\t"," ");
sql=sql.replace("\r"," ");
while(sql.contains(" ")) //去空格
sql=sql.replace(" "," ");
//////////////////////////////////////////
System.out.println(sql);
process(sql);
//////////////////////////////////////////
//写文件
out=new BufferedWriter(new FileWriter(c));
out.write(sql);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void process(String sql)
{
int p,q;
String nS[];
String s="",s1="";
String sqls[]=sql.split(" ");
for(int i=0 ;i<sqls.length;i++) //sql语句预处理
{
s+=recognize(sqls[i])+" ";
}
s1=s;
while(s1.contains("varchar ( number )"))
{
p=sql.indexOf("varchar");
q=sql.substring(p).indexOf(")");
sql=sql.substring(0,p)+"string"+sql.substring(p+q+1);
s1=s1.substring(s1.indexOf("varchar ( number )")+7);
}
s=s.replace("varchar ( number )", "datatype");
sqls=sql.split(" ");
System.out.println(sql);
System.out.println(s);
String sql1[]=s.split(" ");
int j=0;
for(int i=0;i<nextStatus.length;i++)
{
if(nextStatus[i]==-1) break;
if(rules[nextStatus[i]][1].equals(sql1[j]) && rules[nextStatus[i]][2].equals(sql1[j+1]))
{
System.out.println(r[nextStatus[i]]);
s=rules[nextStatus[i]][3];
if(!action(s,sqls[j]))
break;
nS=rules[nextStatus[i]][4].split(",");
for(int k=0;k<nS.length;k++)
nextStatus[k]=Integer.parseInt(nS[k]);
for(int k=nS.length;k<nextStatus.length;k++)
nextStatus[k]=-1;
j++;
i=-1;
continue;
}
}
if(nextStatus[0]==-1)
{
outscreen+="success!\n";
switch(des){
case 't':actionTable();
tablename.removeAllElements();
column.removeAllElements();
type.removeAllElements();
bound.removeAllElements();
break;
case 'i':actionInsert();
tablename.removeAllElements();
column.removeAllElements();
value.removeAllElements();
break;
case 'u':actionUpdate();
conj.removeAllElements();
refer.removeAllElements();
column.removeAllElements();
tablename.removeAllElements();
value.removeAllElements();
value1.removeAllElements();
standard.removeAllElements();
break;
case 'd':actionDelete();
conj.removeAllElements();
refer.removeAllElements();
tablename.removeAllElements();
value.removeAllElements();
standard.removeAllElements();
break;
case 's':actionSelect();
othername.removeAllElements();
function.removeAllElements();
column.removeAllElements();
conj.removeAllElements();
refer.removeAllElements();
tablename.removeAllElements();
value.removeAllElements();
standard.removeAllElements();
break;
}
t1.setText(outscreen);
}
else {
outscreen+="wrong!\n";
t1.setText(outscreen);
}
}
private void actionSelect() {
String tableName=(String)tablename.get(0);
TableStructure ts = tm.getTable(tableName);
if(ts==null){
outscreen+="Can't find the table!\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -