⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interpreter.java

📁 一个简单的数据库
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package interpreterManager;import apiManager.*;import database.OperationInfo;import database.*;import fileManager.*;import java.io.*;import stringManager.*;import java.lang.Exception;import java.util.*;import javax.swing.JOptionPane;import bufferedManager.*;import catalogManager.*;/** * * @author outlaw */public class Interpreter {    public Interpreter(){    }    public static void createDataBase(String instr) throws Exception{        try        {            String[] pathAndName=null;            pathAndName=StringManager.operCreateDB(instr.substring( instr.indexOf("database")+8).trim());            ArrayList<String> dbNames=CatalogManager.getAllDBName();            for(int i=0;i<dbNames.size();i++)            {                if(dbNames.get(i).equals(pathAndName[1]))                    throw new OperationInfo("Creating database error!:Creating a database with the same name:["+pathAndName[1]+"]");            }            APIManager.createDataBase(pathAndName[0], pathAndName[1]);            }catch(Exception ex)        {            throw ex;        }    }    public static void createTable(String instr) throws Exception{        ArrayList<String> attribNames=new ArrayList<String>();        ArrayList<String> attribTypes=new ArrayList<String>();        ArrayList<Integer>  features=new ArrayList<Integer>();        StringBuilder tbName=new StringBuilder();        StringManager.operCreateTB(instr.substring(instr.indexOf("table")+5).trim(),tbName,attribNames, attribTypes, features);        if(attribNames.size()<1||attribNames.size()>32)            throw new OperationInfo("Too few or too many attributes defined in the table :["+tbName+"]");        TableData tb=CatalogManager.getTableByName(tbName.toString());        if(tb!=null)            throw new OperationInfo("Creating table["+tbName+"] error!You have a table with the same name!");        APIManager.createTable(tbName.toString(),attribNames,attribTypes,features);    }    public static void insertTuple(String instr) throws Exception    {        //values的最后一个是它所在的table name        //这个函数还可以改进,从而不需建立一个新的String的数组tempValues,减小开销。        String[] tempValues=null,values=null;        tempValues=StringManager.operInsertTuple(instr);        values=new String[tempValues.length-1];        int len=values.length;        for(int i=0;i<len;i++)        {            values[i]=tempValues[i];        }        APIManager.insertTuple(values,tempValues[len]);    }    public static void execFile(String fullName) throws Exception    {        StringBuilder sb=new StringBuilder();        String data=null;        String[] instrs=null;        FileInputStream fis=new FileInputStream(fullName);        BufferedReader br=new BufferedReader(new InputStreamReader(fis));        while((data=br.readLine())!=null)        {            sb.append(data);        }        instrs=sb.toString().split("create");        for(int i=1;i<instrs.length;i++)        {            createTable("create "+instrs[i]);        }    }    public static void quit() throws Exception    {        APIManager.quit();    }    public static void select(String instr) throws Exception    {        StringBuilder builder2=new StringBuilder();        ArrayList<Integer> pos=new ArrayList<Integer>();        ArrayList<Tuple> tuples=StringManager.operaSelect(instr,builder2,pos);        if(tuples==null||tuples.size()==0)            throw new Success("There are no tuples found!");        String[] temp=builder2.toString().split(",");                StringBuilder builder=new StringBuilder();        for(int i=0;i<temp.length;i++)        {            String t=temp[i].trim();            int len=t.length();            builder.append(t);            for(int j=0;j<15-len;j++)                builder.append(" ");        }        builder.append("\n");        int size=tuples.size();        for(int i=0;i<size;i++)        {            String[] values=tuples.get(i).getValues();            /*for(int j=0;j<values.length;j++)            {                String value=values[j];                int len=value.length();                builder.append(value);                for(int k=0;k<15-len;k++)                    builder.append(" ");            }            */            for(int j=0;j<pos.size();j++)            {                String value=values[pos.get(j)];                int len=value.length();                builder.append(value);                for(int k=0;k<15-len;k++)                    builder.append(" ");            }            builder.append("\n");        }        throw new Success(builder.toString());    }    public static void createIndex(String instr) throws Exception    {        String[] params;        params=StringManager.operCreateIndex(instr.substring(instr.indexOf("index")+5).trim());        APIManager.createIndex(params,true);    }    public static void dropTable(String instr) throws Exception    {        String tbName;        tbName=StringManager.operaDropTable(instr);        APIManager.dropTable(tbName);    }    public static void dropIndex(String instr) throws Exception    {        String indName;        StringManager.operaDropIndex(instr);    }    public static void deleteTuple(String instr) throws Exception    {        StringManager.operaDelete(instr.substring(0,instr.indexOf(";")).trim());    }    /*public static ArrayList<String>  engine(String instr) throws Exception    {        String[] keyWord={"create","insert","select","delete","drop"};        int[] len={6,6,6,6,4};        instr=instr.trim();        int id;        String command=null;        ArrayList<String> commands=new ArrayList<String>();        int smallIndex;        while(instr.length()!=0)        {            instr=instr.trim();            int i;            smallIndex=10000;            for(i=0;i<keyWord.length;i++)            {                String keyword=keyWord[i];                int index=instr.indexOf(keyword,1);                if(index!=-1&&index!=0&&index<smallIndex)                {                    smallIndex=index;                }            }            if(smallIndex==10000)            {                commands.add(instr);                break;            }            else            {                command=instr.substring(0,smallIndex);                commands.add(command);                instr=instr.substring(smallIndex);            }        }        return commands;    }*/        public static ArrayList<String>  engine(String instr) throws Exception    {        instr=instr.trim();        ArrayList<String> commands=new ArrayList<String>();        if(instr.indexOf(";")==-1)        {            commands.add(instr);            return commands;        }        String[] temp=instr.split(";");        boolean isLeft=false;        String t=instr.substring(instr.lastIndexOf(";")+1);        if(t.trim().length()!=0)            isLeft=true;        if(isLeft)        {            for(int i=0;i<temp.length-2;i++)            {                temp[i]+=";";                commands.add(temp[i]);            }            commands.add(temp[temp.length-2]+";"+temp[temp.length-1]);        }        else        {            for(int i=0;i<temp.length;i++)            {                temp[i]+=";";                commands.add(temp[i]);            }        }                   return commands;    }    public static void dispatchInstr(String instr) throws Exception    {        instr=instr.trim();        String instr_case_no_sense=instr;        instr=instr.toLowerCase();                if(!StringManager.verifySemicolon(instr))            throw new OperationInfo("SQL syntax error!\nSemicolon not satisfied!" +                    "\n");        if(!StringManager.verifyBracket(instr))            throw new OperationInfo("The bracket of this command:["+instr+"]is not balanced!");                if(instr.indexOf("create")!=-1&&instr.substring(0,6).equals("create"))        {            String TblOrInd;            try            {                TblOrInd=instr.substring(6).trim().substring(0,5);            }catch(Exception ex)            {                throw new OperationInfo("Syntax error!");            }            if(instr.substring(6).trim().substring(0,8).equals("database"))            {                try                {                    createDataBase(instr);                }catch(Exception ex)                {                    throw ex;                }            }else if(TblOrInd.equals("table"))            {                createTable(instr);            }else if(TblOrInd.equals("index"))            {                createIndex(instr);            }else                 throw new OperationInfo("Create instruction sytax error!");        }        else if(instr.indexOf("drop")!=-1&&instr.substring(0,4).equals("drop"))        {            String TblOrInd;            try            {                TblOrInd=instr.substring(4).trim().substring(0,5);            }catch(Exception ex)            {                throw new OperationInfo("Syntax error!");            }            if(TblOrInd.equals("table"))            {                dropTable(instr);            }            else if(TblOrInd.equals("index"))            {                dropIndex(instr);            }            else                 throw new OperationInfo("Drop instruction syntax error!");        }        else if(instr.indexOf("select")!=-1&&instr.substring(0,6).equals("select"))        {             select(instr_case_no_sense);        }        else if(instr.indexOf("insert")!=-1&&instr.substring(0,6).equals("insert"))        {            insertTuple(instr_case_no_sense);        }        else if(instr.indexOf("delete")!=-1&&instr.substring(0,6).equals("delete"))        {            if(instr.substring(6).indexOf("from")!=-1)                deleteTuple(instr_case_no_sense);            else                throw new OperationInfo("Delete syntax error!No 'from' in this instruction");        }        else if(instr.indexOf("execfile")!=-1)        {            execFile(instr.substring(instr.indexOf("execfile")+8,instr.indexOf(";")).trim());        }        else if(instr.indexOf("quit")!=-1&&instr.substring(0,instr.indexOf(";")).trim().equals("quit"))        {            quit();        }        else            throw new OperationInfo("Instruction syntax error!");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -