Exemple #1
0
Condition_list* Condition_list::copy( Action_data_base* data_P ) const
    {
    Condition_list* ret = new Condition_list( comment(), data_P );
    for( Iterator it( *this );
         it;
         ++it )
        ret->append( it.current()->copy( ret ));
    return ret;
    }
Condition_list* Condition_list::copy() const
    {
    kDebug() << count();

    Condition_list* ret = new Condition_list(comment());
    for( ConstIterator it = begin();
            it != end();
            ++it )
        {
        ret->append((*it)->copy());
        }
    return ret;
    }
bool Select_tuple(vector<int> offsetlist, Table table, Condition_list list)
{		
    Block * Target;			
    int compi;
    select_num = 0;
    float compf;
    string comps;
    char *target = new char[1];
    int comi;
    int comf;
    int i,j;
    int vi=0;
    int ip;
    string value;
    int dp;			
    int offset=0;	
    Condition_list::iterator li;
    int Blocknum = 0;
    char* t;		
    string tuplevalue;
    string file_name = table.table_name + "_table.rec";
    select_values.clear();
    select_offsets.clear();
    if(offsetlist.empty()==1)
    {
        Blocknum = Block_num(file_name);			
    }
    if(Blocknum != 0 || offsetlist.empty() != 1)
    {
        while(1)
        {
            if(offsetlist.empty()!=1)
            {
                offset = offsetlist[vi];
            }
            if(offset>=Block_num(file_name))
                break;
            Target = mybuffer.GetBlock(file_name,offset,0);
            for(dp=0;dp<BlockSize-table.length()-1;dp+=(table.length()+1))
            {
                memcpy(target,Target->record+dp+table.length(),1);
                if(*target == '1')
                {
                    if(list.empty()!=1)
                    {
                        for(li=list.begin();li!=list.end();li++)
                        {
                            for(i=0;i<table.attr_count;i++)
                            {
                                if(table.attrs[i].attr_name == li->attr_name)
                                    break;
                            }
                            ip = 0;
                            for(j=0;j<i;j++)
                            {
                                ip+=table.attrs[j].attr_len;
                            }
                            if(table.attrs[i].attr_type==CHAR)
                            {
                                t = new char[table.attrs[i].attr_len+1];
                                memcpy(t,Target->record+dp+ip,table.attrs[i].attr_len);
                                t[table.attrs[i].attr_len]='\0';
                                comps = t;
                                delete t;
                                if(li->op_type == "<>")
                                {
                                    if(li->cmp_value == comps)
                                        break;
                                }
                                else if(li->op_type == "=")
                                {
                                    if(li->cmp_value != comps)
                                        break;
                                }
                                else if(li->op_type == ">")
                                {
                                    if(li->cmp_value >= comps)
                                        break;
                                }
                                else if(li->op_type == ">=")
                                {
                                    if(li->cmp_value > comps)
                                        break;
                                }
                                else if(li->op_type == "<")
                                {
                                    if(li->cmp_value <= comps)
                                        break;
                                }
                                else if(li->op_type == "<=")
                                {
                                    if(li->cmp_value < comps)
                                        break;
                                }	
                            }
                            else if(table.attrs[i].attr_type==INT)
                            {
                                memcpy(&compi,Target->record+dp+ip,4);
                                comi = atoi(li->cmp_value.c_str());
                                if(li->op_type == "<>")
                                {
                                    if(comi == compi)
                                        break;
                                }
                                else if(li->op_type == "=")
                                {
                                    if(comi != compi)
                                        break;
                                }
                                else if(li->op_type == ">")
                                {
                                    if(comi>= compi)
                                        break;
                                }
                                else if(li->op_type == ">=")
                                {
                                    if(comi > compi)
                                        break;
                                }
                                else if(li->op_type == "<")
                                {
                                    if(comi <= compi)
                                        break;
                                }
                                else if(li->op_type == "<=")
                                {
                                    if(comi < compi)
                                        break;
                                }
                            }
                            else if(table.attrs[i].attr_type==FLOAT)
                            {
                                memcpy(&compf,Target->record+dp+ip,4);
                                comf = atof(li->cmp_value.c_str());
                                if(li->op_type == "<>")
                                {
                                    if(comf == compf)
                                        break;
                                }
                                else if(li->op_type == "=")
                                {
                                    if(comf != compf)
                                        break;
                                }
                                else if(li->op_type == ">")
                                {
                                    if(comf >= compf)
                                        break;
                                }
                                else if(li->op_type == ">=")
                                {
                                    if(comf > compf)
                                        break;
                                }
                                else if(li->op_type == "<")
                                {
                                    if(comf <= compf)
                                        break;
                                }
                                else if(li->op_type == "<=")
                                {
                                    if(comf < compf)
                                        break;
                                }	
                            }
                        }
                    }
                    if(li == list.end()||list.empty())
                    {
                        select_num++;
                        tuplevalue = "";
                        for(int i=0;i<table.attr_count;i++)
                        {
                            ip = 0;
                            for(j=0;j<i;j++)
                            {
                                ip+=table.attrs[j].attr_len;
                            }
                            value = "";
                            if(table.attrs[i].attr_type==CHAR)
                            {
                                t = new char[table.attrs[i].attr_len+1];
                                memcpy(t,Target->record+dp+ip,table.attrs[i].attr_len);
                                t[table.attrs[i].attr_len]='\0';
                                value = t;
                                tuplevalue+=value+'\t';
                                delete t;
                            }
                            if(table.attrs[i].attr_type==INT)
                            {
                                t = new char[100];
                                memcpy(&compi,Target->record+dp+ip,4);									
                                sprintf(t,"%d",compi);
                                value = t;
                                tuplevalue+=value+'\t';
                                delete t;
                            }
                            if(table.attrs[i].attr_type==FLOAT)
                            {
                                t = new char[100];
                                memcpy(&compf,Target->record+dp+ip,4);
                                sprintf(t,"%f",compf);
                                value = t;
                                tuplevalue+=value+'\t';
                                delete t;
                            }
                        }
                        select_values.push_back(tuplevalue);
                        select_offsets.push_back(offset);						
                    }
                }				
            }
            if(offsetlist.empty()==1)
            {
                offset++;
                if(offset==Blocknum)
                    break;
            }
            if(offsetlist.empty()!=1)
            { 
                vi++; 
                if(vi == offsetlist.size())
                    break;
            } 
        }		
    }
    delete target;
    return true;
}