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

📄 stringmanager.java

📁 一个简单的数据库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                {                    attribNames.add(name);                    attribTypes.add(assist[0]);                    if(assist[1].equals("unique"))                        features.add(1);                    else                        features.add(0);                }                   }        }     }    public static boolean verifyQuotation(String instr) throws Exception    {        instr=instr.trim();        if(instr.startsWith("'")&&instr.endsWith("'"))            return true;        if(instr.startsWith("\"")&&instr.endsWith("\""))            return true;        return false;    }    public static String extractValue(String val,String type) throws Exception    {        val=val.trim();        if(type.equals("int"))        {            try            {                return String.valueOf(Integer.parseInt(val));            }catch(Exception ex)            {                throw new OperationInfo("Insert value type error!:["+val+"]"+"is not a valid int!");            }        }else if(type.equals("float"))        {            try            {                return String.valueOf(Float.parseFloat(val));            }catch(Exception ex)            {                throw new OperationInfo("Insert value type error!:["+val+"]"+"is not a valid float!");            }        }else        {            try            {                if(verifyQuotation(val))                {                    String value=val.substring(1,val.length()-1);                    if(value.length()>Integer.parseInt(type.substring(4)))                        throw new OperationInfo("Insert value type error!:["+val+"]"+"char variable out of bound!");                    return value;                }                else                      throw new OperationInfo("Insert value type error!:["+val+"]"+"Quotation not balanced!");            }catch(Exception ex)            {               throw ex;            }           }         }    public static String[] getValues(String[] vals,TableData table) throws Exception    {        //最后一个不是value而是这个tuple所在的table名字        int len=vals.length;        String[] types=table.getAttribTypes();        String[] values=new String[len+1];        for(int i=0;i<len;i++)        {            values[i]=extractValue(vals[i].trim(),types[i]);        }        values[len]=table.getName();        return values;    }    public static String[] operCreateIndex(String instr) throws Exception    {        //这个instr是从这个要新建的index的name开始的        //params依次存放index name,table name,attribute name        String[] params=new String[3];        String tbName,indName,attribName;        if(!verifyBracket(instr))            throw new OperationInfo("Creating index error!:[create index "+instr+"]");        if(instr.indexOf("on")==-1)            throw new OperationInfo("Creating index error!:+[create index "+instr+"]");        indName=instr.substring(0,instr.indexOf("on")).trim();        if(!verifyName(indName))            throw new OperationInfo("Creating index error!The index name:["+indName+"] is not valid!");        instr=instr.substring(instr.indexOf("on")+2).trim();        if(instr.indexOf("(")==-1||instr.indexOf(")")==-1||instr.indexOf("(")>=instr.indexOf(")"))            throw new OperationInfo("Creating index error!The form of the braket:"+instr+" is not valid!");        tbName=instr.substring(0,instr.indexOf("(")).trim();        if(!verifyName(tbName))            throw new OperationInfo("Creating index error!The table name:["+tbName+"] is not valid!");        if(CatalogManager.getTableByName(tbName)==null)            throw new OperationInfo("Creating index error!The table :["+tbName+"] doesn't exist!");        attribName=instr.substring(instr.indexOf("(")+1,instr.indexOf(")"));        if(!verifyName(attribName))            throw new OperationInfo("Creating index error!The attibute name:["+attribName+"] is not valid!");        if(!verifyAttribName(CatalogManager.getTableByName(tbName),attribName))            throw new OperationInfo("Creating index error!The attribte name:["+attribName+"]doesn't exist!");        TableData tb=CatalogManager.getTableByName(tbName);        ArrayList<IndexTag> indices=tb.getIndices();        for(int i=0;i<indices.size();i++)        {            IndexTag tag=indices.get(i);            if(tag.getAttribName().equals(attribName))            {                if(tag.getIndexName().equals(tag.getAttribName()+"_auto_index"))                {                    Index index=FileManager.readInIndex(tag.getFullName());                    String fullN=index.getFullName();                    index.setIndexName(indName);                    tag.setIndexName(tb, indName);                    FileManager.writeOutIndex(index);                    //FileManager.deleteFile(fullN);                    APIManager.deleteOnCasecade(fullN);                    if(!tag.isInFile())                    {                        ArrayList<Index> is=IndexManager.getIndices();                        int len=is.size();                        for(int j=0;j<len;j++)                        {                            Index ind=is.get(j);                            if(ind.getTBName().equals(tbName)&&                                    ind.getAttribName().equals(tag.getAttribName()))                                    ind.setIndexName(indName);                        }                    }                }                else                    throw new OperationInfo("Define another index on the same attribute!:["+attribName+"]");            }        }        params[0]=indName;        params[1]=tbName;        params[2]=attribName;        return params;    }    public static String[] operInsertTuple(String instr) throws Exception    {        if(!verifySemicolon(instr))            throw new OperationInfo("Insert syntax error!Semicolon not valid!:["+"insert"+instr+"]\n");        if(!verifyBracket(instr))            throw new OperationInfo("Insert syntax error!Bracket not balanced!:["+"insert"+instr+"]\n");        instr=instr.substring(6).trim();        if(instr.indexOf("into")==-1)            throw new OperationInfo("Insert syntax error!:["+"insert "+instr+"]\n");        instr=instr.substring(4).trim();        if(instr.indexOf("values")==-1)            throw new OperationInfo("Insert syntax error!:["+"insert "+instr+"]\n");        String tbName=instr.substring(0,instr.indexOf("values")).trim();        if(!verifyName(tbName))            throw new OperationInfo("Insert syntax error!Table name error!:["+tbName+"]\n");        if(!verifyTableName(tbName))            throw new OperationInfo("Insert syntax error!:Table :["+tbName+"]doesn't exist!\n");                    try        {            instr=instr.substring(instr.indexOf("(")+1,instr.indexOf(")")).trim();        }catch(Exception ex)        {            throw new OperationInfo("Insert syntax error!Bracket not valid!:["+"insert"+instr+"]\n");        }        TableData table=CatalogManager.getTableByName(tbName);        String[] tempValues=instr.split(",");        if(tempValues.length!=table.getAttributes().size())            throw new OperationInfo("Insert syntax error!The size of the attributes doesn't match!");        String[] values=null;        values=getValues(tempValues,table);        return values;    }/*    public static String opearaDropIndex(String instr) throws Exception    {        if(!verifySemicolon(instr))            throw new OperationInfo("Drop index syntax error!Semicolon not valid!:["+instr+"]");        if(!verifyBracket(instr))            throw new OperationInfo("Drop index syntax error!Bracket not balanced!:["+instr+"]");        String indName=instr.substring(instr.indexOf("index")+5).trim();        if(!verifyName(indName))            throw new OperationInfo("Drop index name is not valid!:["+indName+"]");        //ArrayLis<IndexTag> indTags=Catalog    } */     public static void operaDropIndex(String instr) throws Exception    {        if(!verifySemicolon(instr))            throw new OperationInfo("Drop index syntax error!:The instruction doesn't ends with ;");        if(!verifyBracket(instr))            throw new OperationInfo("Drop index syntax error!The instruction's brackets are not balanced!");        String temp=instr.substring(instr.indexOf("drop")+4,instr.indexOf("index"));        if(temp.trim().length()!=0)            throw new OperationInfo("Drop index syntax error!There can't be anything but white space between 'drop' and 'index'!");        String indName;        if(instr.indexOf("from")==-1)            throw new OperationInfo("Drop index syntax error!There is no 'from'!in["+instr+"]");        int fromIndex=instr.indexOf("from");        indName=instr.substring(instr.indexOf("index")+5,fromIndex).trim();        String tbName;        if(instr.substring(instr.indexOf(";")+1).trim().length()!=0)            throw new OperationInfo("Drop index syntax error!There can't be anything but white space after ';'!");        tbName=instr.substring(fromIndex+4,instr.indexOf(";")).trim();        if(!verifyName(tbName))            throw new OperationInfo("Drop index syntax error!The table name:["+tbName+"]specified is not valid!");        if(!verifyTableName(tbName))            throw new OperationInfo("Drop index error!The table :["+tbName+"]doesn't exit!");        TableData tb=CatalogManager.getTableByName(tbName);               APIManager.dropIndex(indName, tb);    }    public static String operaDropTable(String instr) throws Exception    {        //这个instr是从最开头开始的        String tbName;                if(!verifySemicolon(instr))            throw new OperationInfo("Drop table syntax error!The brackets are not balanced!:["+instr+"]");        String temp=instr.substring(instr.indexOf("drop")+4,instr.indexOf("table")).trim();        if(temp.length()!=0)            throw new OperationInfo("Draop tabe syntax error!There can't be anything but white space between 'drop' and 'table'!");        tbName=instr.substring(instr.indexOf("table")+5,instr.indexOf(";")).trim();        if(!verifyName(tbName))            throw new OperationInfo("Drop table syntax error!The table name:["+tbName+"] is not valid");        if(!verifyTableName(tbName))            throw new OperationInfo("Drop table error!The table :["+tbName+"] doesn't exist!");        return tbName;    }    public static ArrayList<String> getSelectAttribs(String att) throws Exception    {        try        {            ArrayList<String> attribs=new ArrayList<String>();            if(att.equals("*"))                attribs.add("*");            else            {                String[] temp=att.split(",");                for(int i=0;i<temp.length;i++)                    temp[i]=temp[i].trim();                for(int i=0;i<temp.length;i++)                {                    if(!verifyName(temp[i]))                        throw new Exception();                    attribs.add(temp[i]);                }            }            return attribs;

⌨️ 快捷键说明

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