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

📄 catalog.java

📁 用JAVA实现的miniSQL
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    			reason = new String("不存在此属性");
    			return false;
    		}
    		catalog.seek(current+513);
    		length = catalog.read();
    		if(length==0){
    			catalog.close();
    			reason = new String("该属性上的没有索引");
    			return false;
    		}
    		catalog.seek(current+513);
    		catalog.write(0);
    		catalog.close();
    	}catch(Exception e){ return false;}
    	return true;
    }
    boolean seletetest(Interpreter cmd){
        long current = existFile(cmd.table);
        if(current<0){
        	reason = new String("无此表");
            return false;
        }
        if(cmd.attnumber==0&&cmd.opnumber==0)
            return getinformation(cmd, cmd.table);
        boolean[] s;
        boolean[] w;
        s= new boolean[cmd.attnumber];
        w = new boolean[cmd.opnumber];
        try{
        catalog = new RandomAccessFile(new File("catalog.log"),"rws");
        long total = current+catalog.readInt();
        int length = 0;
        byte[] name;
        current = current + 528;
        while(current<total){
        	catalog.seek(current);
        	length = catalog.read();
        	name = new byte[length*2];
            catalog.read(name);
            catalog.seek(current+511);
            String n = new String(name,"UTF16");
            int flag = catalog.read();
            flag = flag/4;        
        	if(cmd.attnumber!=0){
        		for(int i=0;i<cmd.attnumber;i++){
        			if(cmd.attribute[i].equalsIgnoreCase(n))
        			    s[i]=true;
        		}
        	}
        	if(cmd.opnumber!=0){
        		for(int i=0;i<cmd.opnumber;i++){	
        			if(cmd.opatt[i].equalsIgnoreCase(n)){
        				if(flag==3&&cmd.ischar[i])
        					w[i]=true;
        				else if((flag==1||flag==2)&&!cmd.ischar[i])
        					w[i]=true;
        			}
        		}
        	}
        	current=current+1024;
        	
        }
        catalog.close();
        }catch(Exception e){return false;}
        boolean ww=true;
        boolean ss=true;
        for(int i=0;i<cmd.opnumber;i++)
        	ww=ww&&w[i];
        for(int i=0;i<cmd.attnumber;i++)
            ss=ss&&s[i];
        reason = new String("类型不对");
        if(ww&&ss){
        	return getinformation(cmd, cmd.table);
        }
        return false;
    }
    boolean deletetest(Interpreter cmd){
    	long current = existFile(cmd.table);
        if(current<0){
        	reason = new String("无此表");
            return false;
        }
        if(cmd.opnumber==0)
            return getinformation(cmd, cmd.table);
        boolean[] w;
        w = new boolean[cmd.opnumber];
        try{
        catalog = new RandomAccessFile(new File("catalog.log"),"rws");
        long total = current+catalog.readInt();
        int length = 0;
        byte[] name;
        current = current + 528;
        while(current<total){
        	catalog.seek(current);
        	length = catalog.read();
        	name = new byte[length*2];
            catalog.read(name);
            catalog.seek(current+511);
            String n = new String(name,"UTF16");
            int flag = catalog.read();
            flag = flag/4;
        	if(cmd.opnumber!=0){
        		for(int i=0;i<cmd.opnumber;i++){	
        			if(cmd.opatt[i].equalsIgnoreCase(n)){
        				if(flag==3&&cmd.ischar[i])
        					w[i]=true;
        				else if((flag==1||flag==2)&&!cmd.ischar[i])
        					w[i]=true;
        			}
        		}
        	}
        	current=current+1024;
        	
        }
        catalog.close();
        }catch(Exception e){return false;}
        boolean ww=true;
        for(int i=0;i<cmd.opnumber;i++)
        	ww=ww&&w[i];
        reason = new String("类型不对");
        if(ww){
        	return getinformation(cmd, cmd.table);
        }
        return false;
    }
    boolean inserttest(Interpreter cmd){
    	long current = existFile(cmd.table);
        if(current<0){
        	reason = new String("无此表");
            return false;
        }
        int attno;
        boolean[] s;
        try{
            catalog = new RandomAccessFile(new File("catalog.log"),"rws");
            catalog.seek(current+515);
            attno=catalog.read();
            if(attno!=cmd.opnumber){
            	reason = new String("插入属性个数不对");
            	return false;
            }
            if(attno==0){
            	reason = new String("插入属性个数不能为零");
            	return false;
            }
            s= new boolean[attno];
            current = current + 1039;
            int type=0;
            for(int i=0;i<attno;i++){
            	catalog.seek(current);
                type = catalog.read();
                type = type/4;
                if(type==3&&cmd.ischar[i]&&cmd.op2[i].length()<=catalog.read())
                 	s[i]=true;
                else if((type==1||type==2)&&!cmd.ischar[i])
                	s[i]=true;
                current = current +1024;
            }
            boolean ss=true;
            for(int i=0;i<attno;i++)
            	ss=ss&&s[i];   
            catalog.close();
            if(ss){
            	return getinformation(cmd, cmd.table);
            }
            return false;
        }catch(Exception e){return false;}
    }
    boolean addrecord(String table){
    	long current = existFile(table);
        if(current<0){
        	reason = new String("无此表");
            return false;
        }
        long attno;
        try{
            catalog = new RandomAccessFile(new File("catalog.log"),"rws");
            catalog.seek(current+516);
            attno=catalog.readLong();
            attno++;
            catalog.seek(current+516);
            catalog.writeLong(attno);
            catalog.close();
        }catch(Exception e){return false;}
        return true;
    }
    long gettablerecord(String table){
    	long current = existFile(table);
        if(current<0){
        	reason = new String("无此表");
            return 0;
        }
        long attno;
        try{
            catalog = new RandomAccessFile(new File("catalog.log"),"rws");
            catalog.seek(current+516);
            attno=catalog.readLong();
            catalog.seek(current+516);
            catalog.writeLong(0);
            catalog.close();
        }catch(Exception e){return 0;}
        return attno;
    }
    boolean subrecord(String table, int less){
    	long current = existFile(table);
        if(current<0){
        	reason = new String("无此表");
            return false;
        }
        long attno;
        try{
            catalog = new RandomAccessFile(new File("catalog.log"),"rws");
            catalog.seek(current+516);
            attno=catalog.readLong();
            attno= attno - less;
            catalog.seek(current+516);
            catalog.writeLong(attno);
            catalog.close();
        }catch(Exception e){return false;}
        return true;
    }
    int toType(int type,boolean unique, boolean primary){
    	int a=type*4;
    	if(primary)
    		unique = true;
    	if(unique)
    		a=a+2;
    	if(primary)
    		a=a+1;
        return a;
    }
    boolean test(Interpreter cmd){
    	switch(cmd.mean){
    		case 0:{
    			return false;
    		}
    		case 1:{
    			if(existFile(cmd.table)>=0){
    				reason = new String("表已存在");
    				return false;
    			}
    			if(createTable(cmd))
    				return true;
    			else{
    				reason = new String("写入catalog失败");
    				return false;
    			}
    		}
    		case 2:{
    			if(deleteTable(cmd, existFile(cmd.table)))
    				return true;
    			else
    				return false;
    		}
    		case 3:{
    			if(createIndex(cmd, cmd.table, cmd.uniqueatt, cmd.indexname)){
    				getinformation(cmd, cmd.table);
    				return true;
    			}
    				
    			else
    				return false;
    		}
    		case 4:{
    			if(deleteIndex(cmd.indexname))
    				return true;
    			else
    				return false;
    		}
    		case 5:{
    			if(seletetest(cmd))
    			    return true;
    			else
    				return false;
    		}
    		case 6:{
    			if(inserttest(cmd))
    				return true;
    			else
    				return false;
    		}
    		case 7:{
    			if(deletetest(cmd))
    				return true;
    			else
    				return false;
    		}
    		case 8:{
    			return true;
    		}
    		case 9:{
    			return true;
    		}
    		default:
    			return false;
    	}
    }
}

⌨️ 快捷键说明

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