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

📄 stringmanager.java

📁 一个简单的数据库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }catch(Exception ex)        {            throw new OperationInfo("Select syntax error!The attribute(s) specified are not valid!");        }    }    public static boolean isConditionExist(String cond)    {        if(cond.indexOf("=")!=-1||cond.indexOf("<>")!=-1||cond.indexOf("<")!=-1)            return true;        else if(cond.indexOf(">")!=-1||cond.indexOf(">")!=-1||cond.indexOf("<=")!=-1)            return true;        else if(cond.indexOf(">=")!=-1||cond.indexOf("between")!=-1||cond.indexOf("and")!=-1)            return true;        return false;    }    public static ArrayList<Tuple> operaSelect(String instr,StringBuilder builder,ArrayList<Integer>p) throws Exception    {        if(!verifySemicolon(instr))            throw new OperationInfo("Select syntax error!Semicolon not satisfied!");        instr=instr.substring(0,instr.indexOf(";"));        int fromIndex;        if((fromIndex=instr.indexOf("from"))==-1)            throw new OperationInfo("Select syntax error:!No 'from' in this instruction!");        String att=instr.substring(6,fromIndex).trim();        if(att.length()==0)            throw new OperationInfo("Select syntax error!:There must be some attributes names or '*'between 'select' and 'from'.");        //////////////////////////////////////////////////////////        boolean selectAll=false;        int[] pos;        String[] atts = null;        ArrayList<String> attribs=getSelectAttribs(att);        if(attribs==null)            throw new OperationInfo("Select syntax error!");        else if(attribs.get(0).equals("*"))            selectAll=true;        else        {            atts=new String[attribs.size()];            for(int i=0;i<attribs.size();i++)            {                atts[i]=attribs.get(i);            }        }        ///////////////////////////////////////////////////////////        int whereIndex=instr.indexOf("where");        boolean isCondExist=isConditionExist(instr);        if(isCondExist&&whereIndex==-1)            throw new OperationInfo("Select syntax error!No 'where' in this instruction!");        if(!isCondExist&&whereIndex!=-1)            throw new OperationInfo("Select syntax error!Conditions must be given.");        String conditions=instr.substring(instr.indexOf("where")+5).trim();                ///////////////////////////////////////////////////////////        String tbName;        if(whereIndex==-1)            tbName=instr.substring(fromIndex+4).trim();        else            tbName=instr.substring(fromIndex+4,whereIndex).trim();        if(!verifyName(tbName))            throw new OperationInfo("Select syntax error!The table name:["+tbName+"] specified is not valid!");        TableData tb=CatalogManager.getTableByName(tbName);                if(tb==null)            throw new OperationInfo("Select error!The table:["+tbName+"] doesn't exist!");        if(selectAll)        {            atts=tb.getAttribNames();            for(int i=0;i<atts.length;i++)                p.add(i);        }        else        {            String[] namesTemp=tb.getAttribNames();            pos=new int[atts.length];            for(int i=0,k=0;i<atts.length;i++)            {                int j;                for(j=0;j<namesTemp.length;j++)                {                    if(namesTemp[j].equals(atts[i]))                    {                        pos[k++]=j;                        break;                    }                }                if(j==namesTemp.length)                    throw new OperationInfo("Select error!The attribte:["+atts[i]+"] specified doesn't exist!");            }            for(int i=0;i<pos.length;i++)                p.add(pos[i]);            /*StringBuilder sb=new StringBuilder();            for(int i=0;i<p.size();i++)                sb.append(p.get(i)+"\n");            JOptionPane.showMessageDialog(null, sb.toString());             */        }        builder.append(atts[0]);        for(int i=1;i<atts.length;i++)            builder.append(","+atts[i]);               //无条件        if(whereIndex==-1)        {            return APIManager.getAllTuplesForTable(tb);         }        else    //有条件        {            Condition[] conds=getConditions(tb,conditions);            return APIManager.getTuplesWithCondition(tb, conds);        }    }    public static void operaDelete(String instr) throws Exception    {        //这个instr不包含";"        if(!verifyBracket(instr))            throw new OperationInfo("Delete syntax error!Brackets are not balanced!");        int fromIndex=instr.indexOf("from");        int whereIndex=instr.indexOf("where");                if(fromIndex==-1)            throw new OperationInfo("Delete syntax error!No 'from' in this instruction!");        String temp=instr.substring(6,fromIndex).trim();        if(temp.length()!=0)            throw new OperationInfo("Delete syntax error!");        boolean isCondExist=isConditionExist(instr);        if(isCondExist&&whereIndex==-1)            throw new OperationInfo("Delete syntax error!No 'where' in this instruction!");        if(!isCondExist&&whereIndex!=-1)            throw new OperationInfo("Delete syntax error!The conditions must be given!");                String tbName;        if(whereIndex==-1)            tbName=instr.substring(fromIndex+4).trim();        else            tbName=instr.substring(fromIndex+4,whereIndex).trim();        if(!verifyName(tbName))            throw new OperationInfo("Delete syntax error!The table name specified:["+tbName+"]is not valid!");        TableData tb=CatalogManager.getTableByName(tbName);        if(tb==null)            throw new OperationInfo("Select error!The table:["+tbName+"] doesn't exist!");                if(whereIndex!=-1)        {            String conditions=instr.substring(instr.indexOf("where")+5).trim();            Condition[] conds=getConditions(tb,conditions);            APIManager.deleteTuplesWithCondition(tb,conds);        }        else        {            int delteNumber=APIManager.deleteAllTupleFromTable(tbName);            if(delteNumber==0)                throw new OperationInfo("The tuple(s) you want to delete doesn't exist!");            else                throw new Success("Delete successed!["+delteNumber+"]tuples have been successfully deleted!");              }    }    public static Condition[] getConditions(TableData tb,String cond) throws Exception    {        String[] attribNames=tb.getAttribNames();        ArrayList<Condition> conditions=new ArrayList<Condition>();        String[] conds=cond.split("and");        int index;        String name=null;        String op=null;        String value=null;                int len=conds.length;        for(int i=0;i<len;i++)        {            name=null;            op=null;            value=null;            String temp=conds[i];            Condition c=new Condition();                        if(temp.indexOf("<=")!=-1)            {                index=temp.indexOf("<=");                name=temp.substring(0,index).trim();                op="<=";                value=temp.substring(index+2).trim();            }            else if(temp.indexOf(">=")!=-1)            {                index=temp.indexOf(">=");                name=temp.substring(0,index).trim();                op=">=";                value=temp.substring(index+2).trim();            }                        else if(temp.indexOf("<>")!=-1)            {                index=temp.indexOf("<>");                name=temp.substring(0,index).trim();                op="<>";                value=temp.substring(index+2).trim();            }            else if(temp.indexOf("<")!=-1)            {                index=temp.indexOf("<");                name=temp.substring(0,index).trim();                op="<";                value=temp.substring(index+1).trim();            }            else if(temp.indexOf(">")!=-1)            {                index=temp.indexOf(">");                name=temp.substring(0,index).trim();                op=">";                value=temp.substring(index+1).trim();            }            else if(temp.indexOf("=")!=-1)            {                index=temp.indexOf("=");                name=temp.substring(0,index).trim();                op="=";                value=temp.substring(index+1).trim();            }            else                 throw new OperationInfo("Select syntax error!The condition is not valid!");            if(name==null||op==null||value==null)                throw new OperationInfo("Select syntax error!The condition is not valid!");            int j;            for(j=0;j<attribNames.length;j++)            {                if(attribNames[j].equals(name))                    break;            }            if(j==attribNames.length)                throw new OperationInfo("Select error!The attribute name specified:["+name+"]doesn't exsit!");             c.attribute=name;            c.operand=op;            c.compValue=value;            conditions.add(c);        }        Condition[] cc=new Condition[conditions.size()];        for(int i=0;i<conditions.size();i++)        {            cc[i]=conditions.get(i);        }        return cc;    }}

⌨️ 快捷键说明

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