📄 table.h
字号:
fieldBlock.readString(readField.name,246,10+base);
if(!strcmp(readField.name,newKeyName))
{
fieldBlock.write(1,1,9+base);
fieldBlock.write((int)0,1,4+base);
fieldBlock.save();
flag=1;
}
}
return flag;
}
void Table::deleteRecord(int i)
{
int a1,a2,b1,b2,j;
Block indexBlock,recBlock;
FIELD v;
v.name = "Valid";
findField(v);
indexBlock.load(v.indexBlock);
a1=(v.length*i)/BLOCK_SIZE;
a2=(v.length*i)%BLOCK_SIZE;
b1=a1/(BLOCK_SIZE/4-1);
b2=a1%(BLOCK_SIZE/4-1);
for(j=0;j<b1;j++)
indexBlock.load(indexBlock.read(4,BLOCK_SIZE-4));
recBlock.load(indexBlock.read(4,b2*4));
recBlock.write((int)0,1,a2);
recBlock.save();
}
void Table::listRecord(FIELD conditionField,SYMBOL op,FIELDREC expect)
{
int i,j;
int base;
FIELD * fields = new FIELD[realField];
Block fieldBlock;
for(j=0, i=0;i<realField;i++) //read out all fields
{
do{
if(j%8==0)
fieldBlock.load(tableIndex->read(4,j/8*4+14));
base = (j%8)*256;
j++;
}while(!fieldBlock.read(1,0+base));
fields[i].name = new char[247];
fields[i].valid =1;
fields[i].length = fieldBlock.read(2,1+base);
fields[i].type=(SYMBOL) fieldBlock.read(1,3+base);
fields[i].nl= fieldBlock.read(1,4+base);
fields[i].indexBlock= fieldBlock.read(4,5+base);
fields[i].key = fieldBlock.read(1,9+base);
fieldBlock.readString(fields[i].name,246,10+base);
if(i>0)
cout<<setw(fields[i].length+4)<<fields[i].name;
}
cout<<endl;
for(i=1;i<realField;i++)
{
cout<<setfill('-')<<setw(fields[i].length+4)<<" ";
}
cout<<setfill(' ')<<endl;
FIELDREC fr,frc;
for(j=0;j<(long)totalRec;j++)
{
fr=getOneFieldRecord(fields[0],j);
if(atoi(fr.value)) //if valid cout
{
if(conditionField.valid)
{
frc=getOneFieldRecord(conditionField,j);
if(!testCondition(frc,expect,op))
{
continue;
}
}
for(i=1;i<realField;i++)
{
fr=getOneFieldRecord(fields[i],j);
if(fr.value[0])
cout<<setw(fields[i].length+4)<<fr.value;
else
cout<<setw(fields[i].length+4)<<"NULL";
}
cout<<endl;
}
}
}
void Table::listRecord(FIELD conditionField,SYMBOL op,FIELDREC expect,FIELDNODE * head)
{
FIELDNODE * p;
FIELD v;
v.name = "Valid";
findField(v);
p=head;
while(p->next)
{
p=p->next;
cout<<setw(p->f.length+4)<<p->f.name;
}
cout<<endl;
p=head;
while(p->next)
{
p=p->next;
cout<<setfill('-')<<setw(p->f.length+4)<<" ";
}
cout<<setfill(' ')<<endl;
int index = 0,j;
FIELDREC fr,frc;
for(j=0;j<(long)totalRec;j++)
{
fr=getOneFieldRecord(v,index);
if(atoi(fr.value)) //if valid cout
{
if(conditionField.valid)
{
frc=getOneFieldRecord(conditionField,index);
if(!testCondition(frc,expect,op))
{
index++;
continue;
}
}
p=head;
while(p->next)
{
p=p->next;
fr=getOneFieldRecord(p->f,index);
if(fr.value[0])
cout<<setw(p->f.length+4)<<fr.value;
else
cout<<setw(p->f.length+4)<<"NULL";
}
cout<<endl;
index++;
}
}
}
int Table::deleteField(FIELD k)
{
int i,j;
Block fieldBlock;
int base;
char * name = new char [247];
for(j=0,i=0;i<realField;i++)
{
do{
if(j%8==0)
fieldBlock.load(tableIndex->read(4,j/8*4+14));
base = (j%8)*256;
j++;
}while(!fieldBlock.read(1,0+base));
name[0]=0;
if(fieldBlock.read(1,0+base))
{
fieldBlock.readString(name,246,10+base);
if(strcmp(name,k.name)==0)
{
fieldBlock.write(1,4,0+base);
fieldBlock.save();
realField--;
tableIndex->write(realField,2,2);
tableIndex->save();
return 1;
}
}
}
return 0; //if no such field return 0
}
void Table::deleteRecord(FIELD conditionField,SYMBOL op,FIELDREC expect)
{
FIELDREC test;
for(int i=0;i<(long)totalRec;i++)
{
test = getOneFieldRecord(conditionField,i);
if(testCondition(test,expect,op))
deleteRecord(i);
}
}
int Table::update(FIELD setField,FIELDREC setValue,FIELD conditionField,SYMBOL op,FIELDREC expect)
{
int typeFlag = 0,i,j;
int a1,a2,b1,b2;
unsigned char * ucWord =new unsigned char[1024];
FIELD v;
v.name = "Valid";
findField(v);
if(setValue.type ==NUM)//type check
{
switch(setField.type)
{
case BYTE:
if(atoi(setValue.value)>255||atoi(setValue.value)<0)
typeFlag=1;
break;
case INT:
if(atoi(setValue.value)<-32768||atoi(setValue.value)>32767)
typeFlag=1;
break;
//case LONG:
}
setValue.type = setField.type;
}
else
{
if(setValue.type!=setField.type)
typeFlag=1;
}
if(typeFlag)
{
cout<<"Field "<<setField.name<<" type dismatch!"<<endl;
return 1;
}
if(setField.key) //if primary key,value must be unique
{
FIELDREC fr1,fr2;
for(j=0;j<(long)totalRec;j++)
{
fr2=getOneFieldRecord(v,j);
if(atoi(fr2.value))
{
fr1=getOneFieldRecord(setField,j);
if(!strcmp(fr1.value,setValue.value))
return 2;
}
}
}
FIELDREC fr,frc;
Block indexBlock,recBlock;
for(j=0;j<(long)totalRec;j++)
{
fr=getOneFieldRecord(v,j);
if(atoi(fr.value)) //if valid check condition
{
if(conditionField.valid)
{
frc=getOneFieldRecord(conditionField,j);
if(!testCondition(frc,expect,op))
{
continue;
}
}
//change value
indexBlock.load(setField.indexBlock);
a1=(setField.length*j)/BLOCK_SIZE;
a2=(setField.length*j)%BLOCK_SIZE;
b1=a1/(BLOCK_SIZE/4-1);
b2=a1%(BLOCK_SIZE/4-1);
for(i=0;i<b1;i++)
indexBlock.load(indexBlock.read(4,BLOCK_SIZE-4));
recBlock.load(indexBlock.read(4,b2*4));
getUcWord(ucWord,setField.length,setValue);
for(i=0;i<setField.length;i++)
{
if(a2==BLOCK_SIZE) //last block is full
{
recBlock.save();
b2++;
if(b2==BLOCK_SIZE/4-1)
{
indexBlock.load(indexBlock.read(4,BLOCK_SIZE-4));
b2=0;
}
recBlock.load(indexBlock.read(4,b2*4));
a2=0;
}
recBlock.write(ucWord[i],1,a2);
a2++;
}
recBlock.save();
}
}
return 0;
}
int testCondition(FIELDREC test,FIELDREC expect,SYMBOL op)
{
if(expect.type==NUM)
{
int a,b;
a=atoi(test.value);
b=atoi(expect.value);
switch(op)
{
case EQL:
if(a==b)
return 1;
else
return 0;
break;
case NEQ:
if(a!=b)
return 1;
else
return 0;
break;
case LSS:
if(a<b)
return 1;
else
return 0;
break;
case LEQ:
if(a<=b)
return 1;
else
return 0;
break;
case GTR:
if(a>b)
return 1;
else
return 0;
break;
case GEQ:
if(a>=b)
return 1;
else
return 0;
break;
default:
return 0;
}
}
else
{//expect.type==CHAR
int a=strcmp(test.value,expect.value);
switch(op)
{
case EQL:
if(a==0)
return 1;
else
return 0;
break;
case NEQ:
if(a!=0)
return 1;
else
return 0;
break;
case LSS:
if(a<0)
return 1;
else
return 0;
break;
case LEQ:
if(a<=0)
return 1;
else
return 0;
break;
case GTR:
if(a>0)
return 1;
else
return 0;
break;
case GEQ:
if(a>=0)
return 1;
else
return 0;
break;
default:
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -