|
delphi+FPC一个纯key-value二进制存储
适用于DELPHI和FPC。
众所周知,JSON就是典型的key-value存储。
{
"name":"咏南" ,
"url":"www.咏南中间件.com"
}
笔者弄了一个DELPHIkey-value二进制存储。
可用于数据序列/还原、缓存数据,方法参数传递。。。
TSerialize = class
private
fValue: TBytes;
fKey: rawbytestring;
fList: tlist;
private
procedure setInt(const keyName: rawbytestring; const Value: integer);
procedure setStr(const keyName: rawbytestring; const Value: rawbytestring);
procedure setVariant(const keyName: rawbytestring; const Value: variant);
procedure setBytes(const keyName: rawbytestring; const Value: TBytes);
procedure setDateTime(const keyName: rawbytestring; const Value: TDateTime);
procedure setBool(const keyName: rawbytestring; const Value: boolean);
procedure setSingle(const keyName: rawbytestring; const Value: single);
procedure setDouble(const keyName: rawbytestring; const Value: double);
procedure setByte(const keyName: rawbytestring; const Value: byte);
procedure setInt64(const keyName: rawbytestring; const Value: int64);
procedure setCurrency(const keyName: rawbytestring; const Value: Currency);
procedure setStream(const keyName: rawbytestring; const Value: tstream);
procedure setWord(const keyName: rawbytestring; const Value: word);
procedure setExtended(const keyName: rawbytestring; const Value: Extended);
procedure setLongWord(const keyName: rawbytestring; const Value: LongWord);
procedure setShortint(const keyName: rawbytestring; const Value: Shortint);
procedure setSmallint(const keyName: rawbytestring; const Value: Smallint);
procedure setBCD(const keyName: rawbytestring; const Value: tbcd);
private
function getInt(const keyName: rawbytestring): integer;
function getStr(const keyName: rawbytestring): rawbytestring;
function getVariant(const keyName: rawbytestring): variant;
function getBytes(const keyName: rawbytestring): TBytes;
function getDateTime(const keyName: rawbytestring): TDateTime;
function getBool(const keyName: rawbytestring): boolean;
function getSingle(const keyName: rawbytestring): single;
function getDouble(const keyName: rawbytestring): double;
function getByte(const keyName: rawbytestring): byte;
function getInt64(const keyName: rawbytestring): int64;
function getCurrency(const keyName: rawbytestring): Currency;
function getStream(const keyName: rawbytestring): tstream;
function getWord(const keyName: rawbytestring): word;
function getExtended(const keyName: rawbytestring): Extended;
function getLongWord(const keyName: rawbytestring): LongWord;
function getShortint(const keyName: rawbytestring): Shortint;
function getSmallint(const keyName: rawbytestring): Smallint;
function getBCD(const keyName: rawbytestring): tbcd;
private
function getCount: integer;
public
constructor Create;
destructor Destroy; override;
public
function key(const keyName: rawbytestring): TSerialize;
procedure Clear;
function Delete(const keyName: rawbytestring): boolean;
public
procedure marshal(stream: TStream);
function marshal2: TBytes;
function marshal3: RawByteString;
function marshal5: OleVariant;
public
procedure unMarshal(stream: TStream); overload;
procedure unMarshal(bytes: TBytes); overload;
procedure unMarshal(raw: RawByteString); overload;
procedure unMarshal(ole: OleVariant); overload;
public
property asInt[const keyName: rawbytestring]: integer read getInt write setInt;
property asStr[const keyName: rawbytestring]: rawbytestring read getStr write setStr;
property AsVariant[const keyName: rawbytestring]: variant read getVariant write setVariant;
property asBytes[const keyName: rawbytestring]: TBytes read getBytes write setBytes;
property AsDateTime[const keyName: rawbytestring]: TDateTime read getDateTime write setDateTime;
property asBool[const keyName: rawbytestring]: boolean read getBool write setBool;
property asSingle[const keyName: rawbytestring]: single read getSingle write setSingle;
property asDouble[const keyName: rawbytestring]: double read getDouble write setDouble;
property asByte[const keyName: rawbytestring]: byte read getByte write setByte;
property asInt64[const keyName: rawbytestring]: int64 read getInt64 write setInt64;
property asCurrency[const keyName: rawbytestring]: Currency read getCurrency write setCurrency;
property asStream[const keyName: rawbytestring]: tstream read getStream write setStream;
property asWord[const keyName: rawbytestring]: word read getword write setword;
property asExtended[const keyName: rawbytestring]: Extended read getExtended write setExtended;
property asLongWord[const keyName: rawbytestring]: LongWord read getLongWord write setLongWord;
property asShortint[const keyName: rawbytestring]: Shortint read getShortint write setShortint;
property asSmallint[const keyName: rawbytestring]: Smallint read getSmallint write setSmallint;
property asBCD[const keyName: rawbytestring]: tbcd read getBCD write setBCD;
property asFloat[const keyName: rawbytestring]: double read getDouble write setDouble;
property asCardinal[const keyName: rawbytestring]: LongWord read getLongWord write setLongWord;
public
property Count: integer read getCount;
end;
例子:
procedure TFuncBin.snowflakeID(req, res: TSerialize);
var
sf: tsnowflakeID;
begin
sf := tsnowflakeID.Create;
try
try
sf.DatacenterId := req.asByte['datacenterid'];
sf.WorkerID := req.asByte['workerid'];
res.asInt['status'] := 200;
res.asStr['message'] := 'success';
res.asInt64['snowflakeid'] := sf.NextId;
except
on E: Exception do
begin
res.asInt['status'] := 500;
res.asStr['message'] := 'fail';
res.asStr['exception'] := 'bin.func.snowflakeID()' + e.Message;
WriteLog('bin.func.snowflakeID()' + e.Message);
end;
end;
finally
sf.Free;
end;
end;
procedure TFuncBin.select(req, res: TSerialize);
var
db: tdb;
pool: tdbpool;
i: Integer;
begin
try
try
pool := GetDBPool(req.asStr['dbid']);
db := pool.Lock;
db.dsp.DataSet := db.qry;
for i := 1 to req.asByte['cnt'] do
begin
db.qry.Close;
db.qry.SQL.Clear;
db.qry.SQL.Text := req.asStr['sql' + i.tostring];
res.AsVariant['ds' + i.ToString] := db.dsp.Data;
end;
res.asInt['status'] := 200;
res.asStr['message'] := 'success';
except
on E: Exception do
begin
res.asInt['status'] := 500;
res.asStr['message'] := 'fail';
res.asStr['exception'] := 'bin.func.select()' + e.Message
end;
end;
finally
pool.Unlock(db);
end;
end;
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/17088663.html
来源:https://www.cnblogs.com/hnxxcxg/p/17088663.html |