TClientDataSet的 fastscript封装

// 陈新光 2017-2-10
// TClientDataSet's fastscript

unit fs_ClientDataSet;

interface

{$i fs.inc}

uses
SysUtils, Classes, fs_iinterpreter, fs_itools, fs_ievents,
DB, fs_iclassesrtti, Variants, DBClient
{$IFDEF Delphi16}
, System.Types, Controls
{$ENDIF}
;

type
TCDSRTTI = class(TClientDataSet);

TCDSNotifyEvent = class(TfsCustomEvent)
public
procedure DoEvent(Dataset: TClientDataSet);
function GetMethod: Pointer; override;
end;

TCDSErrorEvent = class(TfsCustomEvent)
public
procedure DoEvent(DataSet: TDataSet; E: EDatabaseError;var Action: TDataAction);
function GetMethod: Pointer; override;
end;

TCDSFilterRecordEvent = class(TfsCustomEvent)
public
procedure DoEvent(DataSet: TClientDataSet; var Accept: Boolean);
function GetMethod: Pointer; override;
end;

TCDSFieldGetTextEvent = class(TfsCustomEvent)
public
procedure DoEvent(Sender: TField; var Text: String; DisplayText: Boolean);
function GetMethod: Pointer; override;
end;

type
TCDSFunctions = class(TfsRTTIModule)
private
function CallMethod(Instance: TObject; ClassType: TClass;
const MethodName: String; Caller: TfsMethodHelper): Variant;
function GetProp(Instance: TObject; ClassType: TClass;
const PropName: String): Variant;
procedure SetProp(Instance: TObject; ClassType: TClass;
const PropName: String; Value: Variant);
public
constructor Create(AScript: TfsScript); override;
end;

implementation

type
TByteSet = set of 0..7;
PByteSet = ^TByteSet;

procedure TCDSNotifyEvent.DoEvent(Dataset: TClientDataSet);
begin
CallHandler([Dataset]);
end;

function TCDSNotifyEvent.GetMethod: Pointer;
begin
Result := @TCDSNotifyEvent.DoEvent;
end;

procedure TCDSErrorEvent.DoEvent(DataSet: TDataSet; E: EDatabaseError;var Action: TDataAction);
begin
CallHandler([Dataset,@E,@Action]);
Action := Handler.Params[2].Value;
end;

function TCDSErrorEvent.GetMethod: Pointer;
begin
Result := @TCDSErrorEvent.DoEvent;
end;

procedure TCDSFilterRecordEvent.DoEvent(DataSet: Tclientdataset; var Accept: Boolean);
begin
CallHandler([DataSet, Accept]);
Accept := Handler.Params[1].Value;
end;

function TCDSFilterRecordEvent.GetMethod: Pointer;
begin
Result := @TCDSFilterRecordEvent.DoEvent;
end;

procedure TCDSFieldGetTextEvent.DoEvent(Sender: TField; var Text: String; DisplayText: Boolean);
begin
CallHandler([Sender, Text, DisplayText]);
Text := Handler.Params[1].Value;
end;

function TCDSFieldGetTextEvent.GetMethod: Pointer;
begin
Result := @TCDSFieldGetTextEvent.DoEvent;
end;

constructor TCDSFunctions.Create(AScript: TfsScript);
begin
inherited Create(AScript);
with AScript do
begin
with AddClass(TClientDataSet, 'TDataSet') do
begin
AddMethod('procedure Open', CallMethod);
AddMethod('procedure Close', CallMethod);
AddMethod('procedure First', CallMethod);
AddMethod('procedure Last', CallMethod);
AddMethod('procedure Next', CallMethod);
AddMethod('procedure Prior', CallMethod);
AddMethod('procedure Cancel', CallMethod);
AddMethod('procedure Delete', CallMethod);
AddMethod('procedure Post', CallMethod);
AddMethod('procedure Append', CallMethod);
AddMethod('procedure Insert', CallMethod);
AddMethod('procedure Edit', CallMethod);
AddConstructor('constructor Create(AOwner: TComponent)',CallMethod);

AddMethod('function FieldByName(const FieldName: string): TField', CallMethod);
AddMethod('procedure GetFieldNames(List: TStrings)', CallMethod);
AddMethod('function FindFirst: Boolean', CallMethod);
AddMethod('function FindLast: Boolean', CallMethod);
AddMethod('function FindNext: Boolean', CallMethod);
AddMethod('function FindPrior: Boolean', CallMethod);
AddMethod('procedure FreeBookmark(Bookmark: TBookmark)', CallMethod);
AddMethod('function GetBookmark: TBookmark', CallMethod);
AddMethod('procedure GotoBookmark(Bookmark: TBookmark)', CallMethod);
AddMethod('function Locate(const KeyFields: string; const KeyValues: Variant;' +
'Options: TLocateOptions): Boolean', CallMethod);
AddMethod('function IsEmpty: Boolean', CallMethod);
AddMethod('procedure EnableControls', CallMethod);
AddMethod('procedure DisableControls', CallMethod);
AddMethod('procedure AddIndex(const Name, Fields: string;Options: TIndexOptions)',CallMethod);

AddProperty('Bof', 'Boolean', GetProp, nil);
AddProperty('Eof', 'Boolean', GetProp, nil);
AddProperty('FieldCount', 'Integer', GetProp, nil);
AddProperty('FieldDefs', 'TFieldDefs', GetProp, nil);
AddProperty('Fields', 'TFields', GetProp, nil);
AddProperty('Filter', 'string', GetProp, SetProp);
AddProperty('Filtered', 'Boolean', GetProp, SetProp);
AddProperty('FilterOptions', 'TFilterOptions', GetProp, SetProp);
AddProperty('Active', 'Boolean', GetProp, SetProp);
AddProperty('Data','OleVariant',GetProp,SetProp);
AddProperty('Params','TParams',GetProp,NIL);
AddProperty('IndexDefs','TIndexDefs',GetProp,nil);
AddProperty('FilterCode','string',GetProp,SetProp);
AddProperty('FilterLineListText','string',GetProp,SetProp);
AddProperty('FilterLineSQL','string',GetProp,SetProp);
AddProperty('FbMustFilter','Boolean',GetProp,SetProp);
AddProperty('FbPost','Boolean',GetProp,SetProp);
AddProperty('FbMultTable','Boolean',GetProp,SetProp);
AddProperty('RecordCount','Integer',GetProp,nil);
AddProperty('QFDataSetOpenSQL','string',GetProp,SetProp);

AddEvent('BeforeOpen', TCDSNotifyEvent);
AddEvent('AfterOpen', TCDSNotifyEvent);
AddEvent('BeforeClose', TCDSNotifyEvent);
AddEvent('AfterClose', TCDSNotifyEvent);
AddEvent('BeforeInsert', TCDSNotifyEvent);
AddEvent('AfterInsert', TCDSNotifyEvent);
AddEvent('BeforeEdit', TCDSNotifyEvent);
AddEvent('AfterEdit', TCDSNotifyEvent);
AddEvent('BeforePost', TCDSNotifyEvent);
AddEvent('AfterPost', TCDSNotifyEvent);
AddEvent('BeforeCancel', TCDSNotifyEvent);
AddEvent('AfterCancel', TCDSNotifyEvent);
AddEvent('BeforeDelete', TCDSNotifyEvent);
AddEvent('AfterDelete', TCDSNotifyEvent);
AddEvent('BeforeScroll', TCDSNotifyEvent);
AddEvent('AfterScroll', TCDSNotifyEvent);
AddEvent('OnCalcFields', TCDSNotifyEvent);
AddEvent('OnFilterRecord', TCDSFilterRecordEvent);
AddEvent('OnNewRecord', TCDSNotifyEvent);
AddEvent('OnPostError', TCDSErrorEvent);
end;
end;
end;

function TCDSFunctions.CallMethod(Instance: TObject; ClassType: TClass;
const MethodName: String; Caller: TfsMethodHelper): Variant;
var
_TDataSet: TClientDataSet;
_TIndexDefs:TIndexDefs;

function IntToLocateOptions(i: Integer): TLocateOptions;
begin
Result := [];
if (i and 1) <> 0 then
Result := Result + [loCaseInsensitive];
if (i and 2) <> 0 then
Result := Result + [loPartialKey];
end;

function IntToIndexOptions(i: Integer): TIndexOptions;
begin
Result := [];
if (i = 1) then
Result := Result + [ixPrimary];
if (i = 2) then
Result := Result + [ixUnique];
if (i = 3) then
Result := Result + [ixDescending];
if (i = 4) then
Result := Result + [ixCaseInsensitive];
if (i = 5) then
Result := Result + [ixExpression];
if (i = 6) then
Result := Result + [ixNonMaintained];
end;
procedure IndexDefsAdd(QName, QFields: string;QArgs:Variant);
var ar:TIndexOptions;
i,n:Integer;
begin
n := VarArrayHighBound(QArgs, 1) + 1;
for i := 0 to n - 1 do
begin
ar :=ar+ IntToIndexOptions(QArgs[i]);
end;
_TIndexDefs.Add(QName,QFields,ar);
end;

procedure BsAddIndex(QName, QFields: string;QArgs:Variant);
var ar:TIndexOptions;
i,n:Integer;
begin
n := VarArrayHighBound(QArgs, 1) + 1;
for i := 0 to n - 1 do
begin
ar :=ar+ IntToIndexOptions(QArgs[i]);
end;
_TDataSet.AddIndex(QName,QFields,ar);
end;

begin
Result := 0;
if ClassType = TClientDataSet then
begin
_TDataSet := TClientDataSet(Instance);
if MethodName = 'OPEN' then
_TDataSet.Open
else if MethodName = 'CLOSE' then
_TDataSet.Close
else if MethodName = 'FIRST' then
_TDataSet.First
else if MethodName = 'LAST' then
_TDataSet.Last
else if MethodName = 'NEXT' then
_TDataSet.Next
else if MethodName = 'PRIOR' then
_TDataSet.Prior
else if MethodName = 'CANCEL' then
_TDataSet.Cancel
else if MethodName = 'DELETE' then
_TDataSet.Delete
else if MethodName = 'POST' then
_TDataSet.Post
else if MethodName = 'APPEND' then
_TDataSet.Append
else if MethodName = 'INSERT' then
_TDataSet.Insert
else if MethodName = 'EDIT' then
_TDataSet.Edit
else if MethodName = 'FIELDBYNAME' then
Result := frxInteger(_TDataSet.FieldByName(Caller.Params[0]))
else if MethodName = 'GETFIELDNAMES' then
_TDataSet.GetFieldNames(TStrings(frxInteger(Caller.Params[0])))
else if MethodName = 'FINDFIRST' then
Result := _TDataSet.FindFirst
else if MethodName = 'FINDLAST' then
Result := _TDataSet.FindLast
else if MethodName = 'FINDNEXT' then
Result := _TDataSet.FindNext
else if MethodName = 'FINDPRIOR' then
Result := _TDataSet.FindPrior
else if MethodName = 'FREEBOOKMARK' then
_TDataSet.FreeBookmark(TBookMark(frxInteger(Caller.Params[0])))
{$IFNDEF WIN64}
else if MethodName = 'GETBOOKMARK' then
Result := frxInteger(_TDataSet.GetBookmark)
{$ENDIF}
else if MethodName = 'GOTOBOOKMARK' then
_TDataSet.GotoBookmark(TBookMark(frxInteger(Caller.Params[0])))
else if MethodName = 'LOCATE' then
Result := _TDataSet.Locate(Caller.Params[0], Caller.Params[1], IntToLocateOptions(Caller.Params[2]))
else if MethodName = 'ISEMPTY' then
Result := _TDataSet.IsEmpty
else if MethodName = 'ENABLECONTROLS' then
_TDataSet.EnableControls
else if MethodName = 'DISABLECONTROLS' then
_TDataSet.DisableControls
else if MethodName='CREATE' then
Result := frxInteger(TComponent(Instance).Create(TComponent(frxInteger(Caller.Params[0]))))
else if MethodName='ADDINDEX' then
BsAddIndex(Caller.Params[0], Caller.Params[1],Caller.Params[2])
end
else
if ClassType = TIndexDefs then
begin
_TIndexDefs := TIndexDefs(Instance);
if MethodName='ADD' then
IndexDefsAdd(Caller.Params[0],Caller.Params[1],Caller.Params[2])
end;
end;

function TCDSFunctions.GetProp(Instance: TObject; ClassType: TClass;
const PropName: String): Variant;
var
_TDataSet: TClientDataSet;

function FilterOptionsToInt(f: TFilterOptions): Integer;
begin
Result := 0;
if foCaseInsensitive in f then
Result := Result or 1;
if foNoPartialCompare in f then
Result := Result or 2;
end;

begin
Result := 0;
if ClassType = TClientDataSet then
begin
_TDataSet := TClientDataSet(Instance);
if PropName = 'BOF' then
Result := _TDataSet.Bof
else if PropName = 'EOF' then
Result := _TDataSet.Eof
else if PropName = 'FIELDCOUNT' then
Result := _TDataSet.FieldCount
else if PropName = 'FIELDDEFS' then
Result := frxInteger(_TDataSet.FieldDefs)
else if PropName = 'FIELDS' then
Result := frxInteger(_TDataSet.Fields)
else if PropName = 'FILTER' then
Result := _TDataSet.Filter
else if PropName = 'FILTERED' then
Result := _TDataSet.Filtered
else if PropName = 'FILTEROPTIONS' then
Result := FilterOptionsToInt(_TDataSet.FilterOptions)
else if PropName = 'ACTIVE' then
Result := _TDataSet.Active
else if PropName = 'DATA' then
Result := _TDataSet.Data
else if PropName = 'PARAMS' then
Result := frxInteger(_TDataSet.Params)
else if PropName = 'INDEXDEFS' then
Result := frxInteger(_TDataSet.IndexDefs)
else if PropName = 'RECORDCOUNT' then
Result := _TDataSet.RecordCount;
end
end;

procedure TCDSFunctions.SetProp(Instance: TObject; ClassType: TClass;
const PropName: String; Value: Variant);
var
_TDataSet: TClientDataSet;

function IntToFilterOptions(i: Integer): TFilterOptions;
begin
Result := [];
if (i and 1) <> 0 then
Result := Result + [foCaseInsensitive];
if (i and 2) <> 0 then
Result := Result + [foNoPartialCompare];
end;

begin
if ClassType = TClientDataSet then
begin
_TDataSet := TClientDataSet(Instance);
if PropName = 'FILTER' then
_TDataSet.Filter := Value
else if PropName = 'FILTERED' then
_TDataSet.Filtered := Value
else if PropName = 'FILTEROPTIONS' then
_TDataSet.FilterOptions := IntToFilterOptions(Value)
else if PropName = 'ACTIVE' then
_TDataSet.Active := Value
ELSE if PropName = 'DATA' then
_TDataSet.Data := Value;
end
end;

initialization
fsRTTIModules.Add(TCDSFunctions);
finalization
fsRTTIModules.Remove(TCDSFunctions);

end.

TClientDataSet的 fastscript封装的更多相关文章

  1. TdxBarButton的FASTSCRIPT封装

    TdxBarButton的FASTSCRIPT封装 // cxg 2017-2-13 unit fs_dev; interface{$i fs.inc}uses fs_iinterpreter, fs ...

  2. unigui控件的FASTSCRIPT封装

    unigui控件的FASTSCRIPT封装 unit fs_uniControl; interface{$i fs.inc}uses fs_iinterpreter, fs_itools, fs_ie ...

  3. delphi开发学习四:TClientDataSet与TDataSetProvider控件使用实例

    1.TClientDataSet控件 通过TClientDataSet控件可以建立瘦客户端的应用程序,且数据执行效率较高,但它不能和数据库自动连接,程序中必须制定它如何获取数据.一般情况下,TClie ...

  4. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  6. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  7. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  8. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

  9. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

随机推荐

  1. Chromium Embedded Framework

    关于CEF 近期由于工作需要开始研究了Google的Chromium Embedded Framework(CEF),这是一个基于Google Chromium开源代码的项目,使用CEF可以很方便的在 ...

  2. 启动activity的标准的action常量及对应的字符串

  3. 细嚼慢咽 Mongoose 5

    此文已由作者黄锴授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 由于Mongoose一直没有中文文档,加上现在市面上充斥着太多"快速上手",很多中文 ...

  4. Spring core resourc层结构体系及JDK与Spring对classpath中资源的获取方式及结果对比

    1. Spring core resourc层结构体系 1.1. Resource相关结构体系 1.2. ResourceLoader相关体系 2. JDK与Spring对classpath中资源的获 ...

  5. python中json.dump() 和 json.dumps() 有那些区别?

    JSON字符串用json.dumps, json.loads JSON文件名用json.dump, json.load 以下内容摘自:<Python Cookbook> json 模块提供 ...

  6. mysql使用日常备忘

    批量插入数据时,如果主键是有业务意义的,并非自自增张,那么有可能在插入的数据中有与已存在的键值重复的,可以用如下方式来插入: INSERT IGNORE 当要插入一个数据时,插入的字段值中主键字段或唯 ...

  7. 九度oj 题目1361:翻转单词顺序

    题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“stu ...

  8. iOS学习笔记44-Swift(四)枚举和结构体

    一.Swift的枚举 枚举是一系相关联的值定义的一个公共的组类型,同时能够让你在编程的时候在类型安全的情况下去使用这些值.Swift中的枚举比OC中的枚举强大得多, 因为Swift中的枚举是一等类型, ...

  9. 刷题总结——树有几多愁(51nod1673 虚树+状压dp+贪心)

    题目: lyk有一棵树,它想给这棵树重标号. 重标号后,这棵树的所有叶子节点的值为它到根的路径上的编号最小的点的编号. 这棵树的烦恼值为所有叶子节点的值的乘积. lyk想让这棵树的烦恼值最大,你只需输 ...

  10. 我最喜欢的XML(三种方式)

    我最喜欢的方式 下面的三个 XML 文档包含完全相同的信息: 第一个例子中使用了 date 属性: <note date="08/08/2008"> <to> ...