Exemplo n.º 1
0
 bool ObObj::operator==(const ObObj &that_obj) const
 {
   bool result = false;
   int err = OB_SUCCESS;
   if ((get_type() == ObNullType) && (that_obj.get_type() == ObNullType))
   {
     result = true;
   }
   else if ((get_type() == ObNullType) || (that_obj.get_type() == ObNullType))
   {
     result = false;
   }
   else if (!can_compare(that_obj))
   {
     result = false;
     TBSYS_LOG(ERROR, "logic error, cann't compare two obj with different type [this.type:%d,"
       "that.type:%d]", get_type(), that_obj.get_type());
   }
   else
   {
     _ObjValue this_value;
     _ObjValue that_value;
     int64_t this_timestamp = 0;
     int64_t that_timestamp = 0;
     ObObjType type = get_type();
     switch (type)
     {
     case ObIntType:
       err = get_int(this_value.int_val);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_int(that_value.int_val);
         if (OB_SUCCESS == err)
         {
           result = this_value.int_val == that_value.int_val;
         }
       }
       break;
     case ObVarcharType:
       err = get_varchar(this_value.varchar_val);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_varchar(that_value.varchar_val);
         if (OB_SUCCESS == err)
         {
           result = this_value.varchar_val == that_value.varchar_val;
         }
       }
       break;
     case ObFloatType:
       err = get_float(this_value.float_val);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_float(that_value.float_val);
         if (OB_SUCCESS == err)
         {
           result = fabsf(this_value.float_val - that_value.float_val) < FLOAT_EPSINON;
         }
       }
       break;
     case ObDoubleType:
       err = get_double(this_value.double_val);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_double(that_value.double_val);
         if (OB_SUCCESS == err)
         {
           result = fabs(this_value.double_val - that_value.double_val) < DOUBLE_EPSINON;
         }
       }
       break;
     case ObDateTimeType:
     case ObPreciseDateTimeType:
     case ObCreateTimeType:
     case ObModifyTimeType:
       err = get_timestamp(this_timestamp);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_timestamp(that_timestamp);
         if (OB_SUCCESS == err)
         {
           result = this_timestamp == that_timestamp;
         }
       }
       break;
       /*case ObSeqType:
         /// @todo (wushi [email protected]) sequence
         break;*/
     default:
       result = OB_ERR_UNEXPECTED;
       TBSYS_LOG(ERROR,"unexpected obj type [obj.type:%d]", type);
       break;
     }
   }
   return result;
 }
Exemplo n.º 2
0
int ObObj::compare_same_type(const ObObj &other) const
{
  int cmp = 0;
  switch(get_type())
  {
    case ObIntType:
      if (this->value_.int_val < other.value_.int_val)
      {
        cmp = -1;
      }
      else if (this->value_.int_val == other.value_.int_val)
      {
        cmp = 0;
      }
      else
      {
        cmp = 1;
      }
      break;
    case ObDecimalType:
      {
        ObNumber n1, n2;
        get_decimal(n1);
        other.get_decimal(n2);
        cmp = n1.compare(n2);
        break;
      }
    case ObVarcharType:
      {
        ObString varchar1, varchar2;
        this->get_varchar(varchar1);
        other.get_varchar(varchar2);
        cmp = varchar1.compare(varchar2);
        break;
      }
    case ObFloatType:
      {
        bool float_eq = fabsf(value_.float_val - other.value_.float_val) < FLOAT_EPSINON;
        if (float_eq)
        {
          cmp = 0;
        }
        else if (this->value_.float_val < other.value_.float_val)
        {
          cmp = -1;
        }
        else
        {
          cmp = 1;
        }
        break;
      }
    case ObDoubleType:
      {
        bool double_eq = fabs(value_.double_val - other.value_.double_val) < DOUBLE_EPSINON;
        if (double_eq)
        {
          cmp = 0;
        }
        else if (this->value_.double_val < other.value_.double_val)
        {
          cmp = -1;
        }
        else
        {
          cmp = 1;
        }
        break;
      }
    case ObDateTimeType:
    case ObPreciseDateTimeType:
    case ObCreateTimeType:
    case ObModifyTimeType:
      {
        int64_t ts1 = 0;
        int64_t ts2 = 0;
        get_timestamp(ts1);
        other.get_timestamp(ts2);
        if (ts1 < ts2)
        {
          cmp = -1;
        }
        else if (ts1 == ts2)
        {
          cmp = 0;
        }
        else
        {
          cmp = 1;
        }
        break;
      }
    case ObBoolType:
      cmp = this->value_.bool_val - other.value_.bool_val;
      break;
    default:
      TBSYS_LOG(ERROR, "invalid type=%d", get_type());
      break;
  }
  return cmp;
}