📄 myldbvariant.pas
字号:
//------------------------------------------------------------------------------
// SetNull
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetNull(DataType: TMYLDBBaseFieldType);
begin
FIsNull := True;
end;//SetNull
//------------------------------------------------------------------------------
// Free Buffer
//------------------------------------------------------------------------------
procedure TMYLDBVariant.Clear(DataType: TMYLDBBaseFieldType);
begin
if ((FPData <> nil) and (not FIsDataLinked)) then
MemoryManager.FreeAndNillMem(FPData);
FPData := nil;
FDataType := DataType;
FDataSize := 0;
FIsDataLinked := false;
FIsNull := True;
end;//Clear
//------------------------------------------------------------------------------
// Set Data Type
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetDataType(DataType: TMYLDBBaseFieldType);
begin
if not IsNull then
try
Cast(DataType);
except
Clear(DataType);
end
else
if (FPData <> nil) then
Clear(DataType)
else
FDataType := DataType;
end;//SetDataType
//------------------------------------------------------------------------------
// CastResultToType
//------------------------------------------------------------------------------
procedure TMYLDBVariant.CastResultToType(NewDataType: TMYLDBBaseFieldType; out Result);
var
p: Pointer;
TmpSize: Integer;
begin
p := CastToNewBuffer(FPData, FDataSize, FDataType, NewDataType, TmpSize);
case NewDataType of
bftChar,
bftVarChar:
begin
SetLength(String(Result), TmpSize-1);
if (PChar(Result) <> nil) then
Move(PChar(p)^, PChar(Result)^, TmpSize);
end;
bftWideChar, bftWideVarChar:
begin
SetLength(WideString(Result), (TmpSize) div 2 - 1);
if (PWideChar(Result) <> nil) then
Move(PWideChar(p)^, PWideChar(Result)^, TmpSize);
end;
else
Move(p^, Result, TmpSize);
end;
MemoryManager.FreeAndNillMem(p);
end;//CastResultToType
//------------------------------------------------------------------------------
// Cast value to empty FPData
//------------------------------------------------------------------------------
procedure TMYLDBVariant.CastAndSetData(const Value; ValueType: TMYLDBBaseFieldType);
var p: Pointer;
begin
if ((FPData <> nil) and (not FIsDataLinked)) then
MemoryManager.FreeAndNillMem(FPData);
if (ValueType in [bftChar, bftVarChar, bftWideChar, bftWideVarchar]) then
p := Pointer(Value)
else
p := @Value;
FPData := CastToNewBuffer(p, -1, ValueType, FDataType, FDataSize);
end;//CastAndSetData
//------------------------------------------------------------------------------
// raise EMYLDBException if FPData = nil
//------------------------------------------------------------------------------
procedure TMYLDBVariant.CheckDataForNull;
begin
if IsNull then
raise EMYLDBException.Create(30016, ErrorGValueIsNull);
end;//CheckDataForNull
//------------------------------------------------------------------------------
// Assign data from source (or make link to Source.Data)
//------------------------------------------------------------------------------
procedure TMYLDBVariant.Assign(Source: TMYLDBVariant; CopyDataFlag: boolean = true);
begin
Clear;
FDataType := Source.FDataType;
FDataSize := Source.FDataSize;
FIsDataLinked := not CopyDataFlag;
FIsNull := Source.FIsNull;
if (not Source.IsNull) then
if (CopyDataFlag) then
begin
FPData := MemoryManager.GetMem(FDataSize);
Move(Source.FPData^, FPData^, FDataSize);
end
else
FPData := Source.FPData;
end;//Assign
//------------------------------------------------------------------------------
// Set Data (or make link to Data)
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetData(Buffer: Pointer; Size: Integer;
DataType: TMYLDBBaseFieldType; CopyDataFlag: boolean);
begin
if (CopyDataFlag) then
begin
if ((FPData <> nil) and
(not FIsDataLinked) and
(Size = FDataSize)) then
// Using Old Buffer (FPData)
else
begin
Clear;
FPData := MemoryManager.GetMem(Size);
end
end
else
begin
Clear;
FPData := Buffer;
end;
FDataType := DataType;
FDataSize := Size;
FIsNull := False;
FIsDataLinked := not CopyDataFlag;
if (CopyDataFlag) then
Move(Buffer^, FPData^, FDataSize);
end;//SetData
//------------------------------------------------------------------------------
// Copy Data to Address
//------------------------------------------------------------------------------
function TMYLDBVariant.CopyDataToAddress(Buffer: Pointer; MaxSize: Integer): boolean;
var
L: Integer;
begin
if (IsNull) then
begin
Result := False;
end
else
begin
L := DataSize;
if (MaxSize <> -1) then
if (L > MaxSize) then
raise EMYLDBException.Create(20056, ErrorABufferSizeExceeded);
if (IsStringDataType and (L<=1)) then
Result := False
else
begin
Move(FPData^, Buffer^, L);
Result := True;
end;
end;
end;//CopyDataToAddress
//------------------------------------------------------------------------------
// return (FPData = nil)
//------------------------------------------------------------------------------
function TMYLDBVariant.GetIsNull: Boolean;
begin
Result := FIsNull or (FPData = nil);
end;//GetIsNull
//------------------------------------------------------------------------------
// Set typed Data to Variant
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetDataValue(const value; ValueType: TMYLDBBaseFieldType);
begin
if ( (FDataType = ValueType) and
(FPData <> nil) and
(not IsStringDataType) and // Assign if simple type (Integer, ...)
(not FIsDataLinked) ) then
begin
Move(Value, FPData^, FDataSize)
end
else
begin
if (FPData <> nil) then
Clear(FDataType);
if FDataType = bftUnknown then
FDataType := ValueType;
// Set NULL if value=''
if ((IsStringFieldType(ValueType) and (PChar(value) = nil)) or
(IsWideStringFieldType(ValueType) and (Length(WideString(PWideChar(value)))=0)) or
(IsStringFieldType(ValueType) and (not IsWideStringFieldType(ValueType)) and (SysUtils.StrLen(PChar(value))=0))) then
begin
FIsDataLinked := False;
Exit;
end;
if FDataType = ValueType then
AllocMemAndSetData(FDataType, Value, FPData, FDataSize)
else
CastAndSetData(Value, ValueType);
end;
FIsNull := False;
FIsDataLinked := False;
end;//SetDataValue
//------------------------------------------------------------------------------
// Set typed Data from Variant
//------------------------------------------------------------------------------
procedure TMYLDBVariant.GetDataValue(out Value; ValueType: TMYLDBBaseFieldType);
begin
if (not IsStringFieldType(ValueType)) then
CheckDataForNull;
if (IsStringFieldType(ValueType) and IsNull) then
begin
case ValueType of
bftChar,
bftVarchar:
SetLength(String(Value), 0);
bftWideChar,
bftWideVarchar:
SetLength(WideString(Value), 0)
end;
end
else
if FDataType = ValueType then
begin
case ValueType of
bftChar,
bftVarchar:
begin
SetLength(String(Value), SysUtils.StrLen(FPData));
Move(PChar(FPData)^, PChar(Value)^, SysUtils.StrLen(FPData));
end;
bftBlob:
begin
SetLength(String(Value), FDataSize-1);
Move(PChar(FPData)^, PChar(Value)^, FDataSize-1);
end;
bftWideChar,
bftWideVarchar:
begin
SetLength(WideString(Value), Length(WideString(PWideChar(FPData))));
Move(PWideChar(FPData)^, PWideChar(Value)^, Length(WideString(PWideChar(FPData)))*sizeof(WideChar));
end;
else
CopyDataToAddress(@Value);
end;
end
else
CastResultToType(ValueType, Value);
end;//GetDataValue
//------------------------------------------------------------------------------
// Set Data for Int8 (Shortint)
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetDataAsSignedInt8(Value: Shortint);
begin
SetDataValue(Value, bftSignedInt8);
end;//SetDataAsSignedInt8
//------------------------------------------------------------------------------
// Get Data for Int8 (Shortint)
//------------------------------------------------------------------------------
function TMYLDBVariant.GetDataAsSignedInt8: Shortint;
begin
GetDataValue(Result, bftSignedInt8);
end;//GetDataAsSignedInt8
//------------------------------------------------------------------------------
// Set Data for Signed Int16 (Smallint)
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetDataAsSignedInt16(Value: Smallint);
begin
SetDataValue(Value, bftSignedInt16);
end;//SetDataAsSignedInt16
//------------------------------------------------------------------------------
// Get Data for Signed Int16 (Smallint)
//------------------------------------------------------------------------------
function TMYLDBVariant.GetDataAsSignedInt16: Smallint;
begin
GetDataValue(Result, bftSignedInt16);
end;
//------------------------------------------------------------------------------
// Set Data for Signed Int32 (Integer)
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetDataAsSignedInt32(Value: Integer);
begin
SetDataValue(Value, bftSignedInt32);
end;//SetDataAsSignedInt32
//------------------------------------------------------------------------------
// Get Data for Signed Int32 (Integer)
//------------------------------------------------------------------------------
function TMYLDBVariant.GetDataAsSignedInt32: Integer;
begin
GetDataValue(Result, bftSignedInt32);
end;//GetDataAsSignedInt32
//------------------------------------------------------------------------------
// Set Data for Signed Int64 (Int64)
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetDataAsSignedInt64(Value: Int64);
begin
SetDataValue(Value, bftSignedInt64);
end;//SetDataAsSignedInt64
//------------------------------------------------------------------------------
// Get Data for Signed Int64 (Int64)
//------------------------------------------------------------------------------
function TMYLDBVariant.GetDataAsSignedInt64: Int64;
begin
GetDataValue(Result, bftSignedInt64);
end;//GetDataAsSignedInt64
//------------------------------------------------------------------------------
// Set Data for Unsigned Int8 (Byte)
//------------------------------------------------------------------------------
procedure TMYLDBVariant.SetDataAsUnsignedInt8(Value: Byte);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -