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

📄 myldbvariant.pas

📁 一个本地database引擎,支持中文T_Sql查询,兼容DELPHI标准数据库控件
💻 PAS
📖 第 1 页 / 共 5 页
字号:
  {$IFDEF D6H}
      bftSignedInt64:
        Result := AsInt64;
  {$ENDIF}
      bftUnsignedInt8:
        Result := AsByte;
      bftUnsignedInt16:
        Result := AsWord;
      bftUnsignedInt32:
        Result := AsCardinal;

      bftSingle:
        Result := AsSingle;
      bftDouble:
        Result := AsDouble;
      bftExtended:
        Result := AsExtended;

      bftDate:
        Result := AsTDate;
      bftTime:
        Result := AsTTime;
      bftDateTime:
        Result := AsTDateTime;

      bftLogical:
        Result := AsLogical;

      bftCurrency:
        Result := AsCurrency;

      bftBlob:
        Result := AsString;

      else
        // Check for null
        if (IsNull) then
          Result := Null
        else
        raise EMYLDBException.Create(30114, ErrorGUnsupportedVariantType, [BftToStr(FDataType)]);
    end;//case
end;//GetDataAsVariant


//------------------------------------------------------------------------------
// Cast
//------------------------------------------------------------------------------
procedure TMYLDBVariant.Cast(NewDataType: TMYLDBBaseFieldType);
var p: Pointer;
    NewSize: Integer;
begin
  if (FDataType = NewDataType) then
    Exit;

  if ( IsNull ) then
    begin
      Clear(NewDataType);
    end
  else
    begin
      if (FDataType = bftClob) and (NewDataType = bftVarChar) then
        begin
          Inc(FDataSize);
          MemoryManager.ReallocMem(FPData, FDataSize, True);
          FDataType := NewDataType;
        end
       else
        begin
          p := CastToNewBuffer(FPData, FDataSize, FDataType, NewDataType, NewSize);
          Clear(NewDataType);
          FIsNull := False;
          FDataType := NewDataType;
          FPData := p;
          FDataSize := NewSize;
        end;
    end;
end;//Cast


//------------------------------------------------------------------------------
// return true if DataType is numeric
//------------------------------------------------------------------------------
function TMYLDBVariant.IsNumericDataType: Boolean;
begin
  Result := IsNumericFieldType(FDataType);
end;//IsNumericDataType


//------------------------------------------------------------------------------
// return true if DataType is String
//------------------------------------------------------------------------------
function TMYLDBVariant.IsStringDataType: Boolean;
begin
  Result := IsStringFieldType(FDataType);
end;//IsStringDataType


//------------------------------------------------------------------------------
// return true if DataType is WideString
//------------------------------------------------------------------------------
function TMYLDBVariant.IsWideStringDataType: Boolean;
begin
  Result := IsWideStringFieldType(FDataType);
end;//IsStringDataType


//------------------------------------------------------------------------------
// return true if DataType is BLOB
//------------------------------------------------------------------------------
function TMYLDBVariant.IsBlobDataType: Boolean;
begin
  Result := IsBLOBFieldType(FDataType);
end;//IsBlobDataType


//------------------------------------------------------------------------------
// return true if DataType is Time, Date, DateTime
//------------------------------------------------------------------------------
function TMYLDBVariant.IsDateTimeDataType: Boolean;
begin
  Result := IsDateTimeFieldType(FDataType);
end;//IsDateTimeFieldType


//------------------------------------------------------------------------------
// return Length of String or -1 (if not IsStringType)
//------------------------------------------------------------------------------
function TMYLDBVariant.StrLen: Integer;
begin
  if (IsStringDataType and (not IsNull)) then
    if (IsWideStringDataType) then
      Result := ((FDataSize) div 2) - 1
    else
      Result := FDataSize - 1
  else
    Result := -1;
end;


//------------------------------------------------------------------------------
// value := -value ( if not number then raise)
//------------------------------------------------------------------------------
procedure TMYLDBVariant.InvertValue;
begin
  if not IsNull then
    case FDataType of
      bftSignedInt8:      AsShortint := -AsShortint;
      bftSignedInt16:     AsSmallint := -AsSmallint;
      bftSignedInt32:     AsInteger := -AsInteger;
      bftSignedInt64:     AsInt64 := -AsInt64;

      bftUnsignedInt8:    AsShortint := -AsByte;
      bftUnsignedInt16:   AsSmallint := -AsWord;
      bftUnsignedInt32:   AsInteger := -AsCardinal;

      bftSingle:          AsSingle := -AsSingle;
      bftDouble:          AsDouble := -AsDouble;
      bftExtended:        AsExtended := -AsExtended;

      bftDate:            AsMYLDBDate := -AsMYLDBDate;

      bftLogical:         AsLogical := not AsLogical;
      bftCurrency:        AsCurrency := -AsCurrency;
      else
        raise EMYLDBException.Create(30123, ErrorGCannotInvertValue, [BftToStr(FDataType)]);
    end;
end;//InvertValue


{$IFOPT Q+}
 {$DEFINE RESET_OVERFLOW}
 {$Q-}
{$ENDIF}
//------------------------------------------------------------------------------
// data := data + value
//------------------------------------------------------------------------------
procedure TMYLDBVariant.Add(Value: TMYLDBVariant);
var
  dt: TMYLDBDateTime;
begin
  if Value.IsNull then
   begin
    if not IsStringDataType then SetNull(FDataType)
   end
  else
    if not IsNull then
      begin
        Cast(GetCommonDataType(FDataType, Value.DataType));
        case FDataType of
          bftChar,
          bftVarchar:         AsString := AsString + Value.AsString;
          bftWideChar,
          bftWideVarchar:     AsWideString := AsWideString + Value.AsWideString;

          bftSignedInt8:      AsShortint := AsShortint + Value.AsShortint;
          bftSignedInt16:     AsSmallint := AsSmallint + Value.AsSmallint;
          bftSignedInt32:     AsInteger := AsInteger + Value.AsInteger;
          bftSignedInt64:     AsInt64 := AsInt64 + Value.AsInt64;

          bftUnsignedInt8:    AsByte := AsByte + Value.AsByte;
          bftUnsignedInt16:   AsWord := AsWord + Value.AsWord;
          bftUnsignedInt32:   AsCardinal := AsCardinal + Value.AsCardinal;

          bftSingle:          AsSingle := AsSingle + Value.AsSingle;
          bftDouble:          AsDouble := AsDouble + Value.AsDouble;
          bftExtended:        AsExtended := AsExtended + Value.AsExtended;

          bftDate:            AsMYLDBDate := AsMYLDBDate + Value.AsMYLDBDate;
          bftTime:            AsMYLDBTime := AsMYLDBTime + Value.AsMYLDBTime;
          bftDateTime:        begin
                                dt := AsMYLDBDateTime;
                                dt.Date := dt.Date + Value.AsMYLDBDateTime.Date;
                                dt.Time := dt.Time + Value.AsMYLDBDateTime.Time;
                                if (dt.Time >= MILSECS_IN_DAY) then
                                 begin
                                  dt.Date := dt.Date + dt.Time div MILSECS_IN_DAY;
                                  dt.Time := dt.Time mod MILSECS_IN_DAY;
                                 end;
                                AsMYLDBDateTime := dt;
                              end;

          bftLogical:         AsLogical := AsLogical or Value.AsLogical;
          bftCurrency:        AsCurrency := AsCurrency + Value.AsCurrency;
          else
            raise EMYLDBException.Create(30124, ErrorGNotNumericDataType, ['+']);
        end;
      end
    else//If IsNull
      if not IsStringDataType then
        SetNull(FDataType)
      else
        Assign(Value);

end;//Add


//------------------------------------------------------------------------------
// data := data - value
//------------------------------------------------------------------------------
procedure TMYLDBVariant.Sub(Value: TMYLDBVariant);
var
  dt: TMYLDBDateTime;
begin
  if Value.IsNull then
    SetNull(FDataType)
  else
    if not IsNull then
      begin
        Cast(GetCommonDataType(FDataType, Value.DataType));
        case FDataType of
          bftSignedInt8:      AsShortint := AsShortint - Value.AsShortint;
          bftSignedInt16:     AsSmallint := AsSmallint - Value.AsSmallint;
          bftSignedInt32:     AsInteger := AsInteger - Value.AsInteger;
          bftSignedInt64:     AsInt64 := AsInt64 - Value.AsInt64;

          bftUnsignedInt8:    AsByte := AsByte - Value.AsByte;
          bftUnsignedInt16:   AsWord := AsWord - Value.AsWord;
          bftUnsignedInt32:   AsCardinal := AsCardinal - Value.AsCardinal;

          bftSingle:          AsSingle := AsSingle - Value.AsSingle;
          bftDouble:          AsDouble := AsDouble - Value.AsDouble;
          bftExtended:        AsExtended := AsExtended - Value.AsExtended;

          bftDate:            AsMYLDBDate := AsMYLDBDate - Value.AsMYLDBDate;
          bftTime:            AsMYLDBTime := AsMYLDBTime - Value.AsMYLDBTime;
          bftDateTime:        begin
                                dt := AsMYLDBDateTime;
                                dt.Date := dt.Date - Value.AsMYLDBDateTime.Date;
                                dt.Time := dt.Time - Value.AsMYLDBDateTime.Time;
                                if (dt.Time >= MILSECS_IN_DAY) then
                                 begin
                                  dt.Date := dt.Date + dt.Time div MILSECS_IN_DAY;
                                  dt.Time := dt.Time mod MILSECS_IN_DAY;
                                 end;
                                AsMYLDBDateTime := dt;
                              end;

          bftLogical:         AsLogical := AsLogical or Value.AsLogical;
          bftCurrency:        AsCurrency := AsCurrency - Value.AsCurrency;

          else
            raise EMYLDBException.Create(30125, ErrorGNotNumericDataType, ['-']);
        end;
      end;
end;//Sub


//------------------------------------------------------------------------------
// data := data * value
//------------------------------------------------------------------------------
procedure TMYLDBVariant.Mul(Value: TMYLDBVariant);
var
  dt: TMYLDBDateTime;
begin
  if Value.IsNull then
    SetNull(FDataType)
  else
    if not IsNull then
      begin
        Cast(GetCommonDataType(FDataType, Value.DataType));
        case FDataType of
          bftSignedInt8:      AsShortint := AsShortint * Value.AsShortint;
          bftSignedInt16:     AsSmallint := AsSmallint * Value.AsSmallint;
          bftSignedInt32:     AsInteger := AsInteger * Value.AsInteger;
          bftSignedInt64:     AsInt64 := AsInt64 * Value.AsInt64;

          bftUnsignedInt8:    AsByte := AsByte * Value.AsByte;
          bftUnsignedInt16:   AsWord := AsWord * Value.AsWord;
          bftUnsignedInt32:   AsCardinal := AsCardinal * Value.AsCardinal;

          bftSingle:          AsSingle := AsSingle * Value.AsSingle;
          bftDouble:          AsDouble := AsDouble * Value.AsDouble;
          bftExtended:        AsExtended := AsExtended * Value.AsExtended;

          bftDate:            AsMYLDBDate := AsMYLDBDate * Value.AsMYLDBDate;
          bftTime:            AsMYLDBTime := AsMYLDBTime * Value.AsMYLDBTime;
          bftDateTime:        begin
                                dt := AsMYLDBDateTime;
                                dt.Date := dt.Date * Value.AsMYLDBDateTime.Date;
                                dt.Time := dt.Time * Value.AsMYLDBDateTime.Time;
                                if (dt.Time >= MILSECS_IN_DAY) then
                                 begin
                                  dt.Date := dt.Date + dt.Time div MILSECS_IN_DAY;
                                  dt.Time := dt.Time mod MILSECS_IN_DAY;
                                 end;
                                AsMYLDBDateTime := dt;
                              end;

          bftLogical:         AsLogical := AsLogical or Value.AsLogical;
          bftCurrency:        AsCurrency := AsCurrency * Value.AsCurrency;
          else
            raise EMYLDBException.Create(30126, ErrorGNotNumericDataType, ['*']);
        end;
      end;
end;//Mul


//------------------------------------------------------------------------------
// data := data / value
//------------------------------------------------------------------------------
procedure TMYLDBVariant.Division(Value: TMYLDBVariant);
var
  dt: TMYLDBDateTime;
begin
  if Value.IsNull then
    SetNull(FDataType)
  else
    if not IsNull then
      begin
        try
          Cast(GetCommonDataType(FDataType, Value.DataType));
          case FDataType of
            bftSignedInt8:      AsShortint := AsShortint div Value.AsShortint;
            bftSignedInt16:     AsSmallint := AsSmallint div Value.AsSmallint;
            bftSignedInt32:     AsInteger := AsInteger div Value.AsInteger;
            bftSignedInt64:     AsInt64 := AsInt64 div Value.AsInt64;

            bftUnsignedInt8:    AsByte := AsByte div Value.AsByte;
            bftUnsignedInt16:   AsWord := AsWord div Value.AsWord;
            bftUnsignedInt32:   AsCardinal := AsCardinal div Value.AsCardinal;

⌨️ 快捷键说明

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