unit DataMain;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
DBClient, GMServer_TLB, StdVcl, DBXpress, FMTBcd, DB, SqlExpr, variants,
ComCtrls, Provider, ADODB, Inifiles, Dialogs ;
type
TGMService = class(TRemoteDataModule, IGMService)
SCCLIENT: TSQLConnection;
ACACT: TADOConnection;
ADOTB_Acts: TADOTable;
DSP_TActs: TDataSetProvider;
SQY_Temp: TSQLQuery;
SQLSP_UserInfo: TSQLStoredProc;
DSP_UserInfo: TDataSetProvider;
SDS_GetDatas: TSQLDataSet;
SQY_DoCmd: TSQLQuery;
CDS_GetDatas: TClientDataSet;
DSP_GetDatas: TDataSetProvider;
CDS_UserInfo: TClientDataSet;
SDS_MultiGet: TSQLDataSet;
DSP_MultiGet: TDataSetProvider;
SDS_1: TSQLDataSet;
DSP_test: TDataSetProvider;
procedure RemoteDataModuleCreate(Sender: TObject);
procedure RemoteDataModuleDestroy(Sender: TObject);
function DSP_GetDatasDataRequest(Sender: TObject;
Input: OleVariant): OleVariant;
function DSP_MultiGetDataRequest(Sender: TObject;
Input: OleVariant): OleVariant;
private
{ Private declarations }
protected
class procedure UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
override;
function GetActConn:Boolean;
procedure ConnToAct(const aActName: WideString;
out oError: OleVariant);
safecall;
procedure LogUser(const aUserName, aUserPass: WideString;
out aLogined: OleVariant);
safecall;
procedure GetServerTime(out oTime: OleVariant);
safecall;
procedure AddToList(const aUser, aName, aDept, aLogTime, aHost,
aIP: WideString);
safecall;
procedure DelFromList(const aUser, aIP: WideString);
safecall;
procedure CheckRecord(const aSqlStr: WideString;
out oRecNo,
oError: OleVariant);
safecall;
proceduredo
UserCmd(const aSqlStr: WideString;
aPrepared: OleVariant;
out oSuced, oError: OleVariant);
safecall;
procedure GetCmdDatas(const aSqlStr: WideString;
var vDatas: OleVariant;
out oError: OleVariant);
safecall;
procedure GetUserInfo(const aUserCode: WideString;
out vDatas,
oError: OleVariant);
safecall;
procedure MultiCDSGet(aGetType: Shortint;
const aProviderName: WideString;
abegin
, aGetCount: OleVariant;
out vDatas, oError: OleVariant);
safecall;
public
{ Public declarations }
end;
implementation
uses crypt, ServiceMain, StdCtrls, StrUtils;
{$R *.DFM}
class procedure TGMService.UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
begin
if Register then
begin
inherited UpdateRegistry(Register, ClassID, ProgID);
EnableSocketTransport(ClassID);
EnableWebTransport(ClassID);
end else
begin
DisableSocketTransport(ClassID);
DisableWebTransport(ClassID);
inherited UpdateRegistry(Register, ClassID, ProgID);
end;
end;
procedure TGMService.RemoteDataModuleCreate(Sender: TObject);
begin
if not GetActConn then
exit;
frmServiceMain.UpdateLogs(1);
end;
function TGMService.GetActConn: Boolean;
var
myConnStr : string ;
myInifile : Tinifile ;
begin
myInifile := Tinifile.Create(ExtractFilePath(Paramstr(0))+'CSCACT.gml');
try
ACACT.Connected := false ;
myConnStr := myInifile.ReadString('myconnstr','connstr','');
myConnStr := GMDecrypt(myConnStr,135,3);//反加密5次取得正确字符串
ACACT.ConnectionString := myConnStr;
ACACT.Connected := true ;
myInifile.Free;
Result := True;
except
on e:Exceptiondo
begin
Result := False;
myInifile.Free;
ShowMessage(e.Message);
end;
end;
end;
procedure TGMService.ConnToAct(const aActName: WideString;
out oError: OleVariant);
var
LoadStrings,TempStrings:TStrings;
ConnStream:TMemoryStream;
begin
oError := '';
LoadStrings := TStringList.Create;
TempStrings := TStringList.Create;
if SCCLIENT.Connected then
SCCLIENT.Connected:= false ;
SCCLIENT.Params.Clear;
if not(ADOTB_Acts.Active) then
ADOTB_Acts.Open;
ADOTB_Acts.Filter:= 'ActCode='''+aActName+'''';
ADOTB_Acts.Filtered := True;
if ADOTB_Acts.RecordCount>0 then
begin
ConnStream := TMemoryStream.Create;
Tblobfield(ADOTB_Acts.FieldByName('ConnStr')).SaveToStream(ConnStream);
ConnStream.Seek(0,soFrombegin
ning);
LoadStrings.LoadFromStream(ConnStream);
ConnStream.Free;
end;
if DecryptStrings(LoadStrings,TempStrings,135) then
SCCLIENT.Params.Assign(TempStrings) else
exit;
if SCCLIENT.Params.Count>2 then
begin
try
SCCLIENT.Connected := true ;
except
on e:Exceptiondo
oError:=e.Message;
end;
end;
TempStrings.Free;
LoadStrings.Free;
end;
procedure TGMService.LogUser(const aUserName, aUserPass: WideString;
out aLogined: OleVariant);
var
MySql:String;
begin
aLogined := False;
MySql :='select UserCode from A_Users where UserCode='''+aUserName+''' and UserPass='''+aUserPass+'''';
MySql := MySql + ' and Expired=0';
SQY_Temp.Close;
SQY_Temp.SQL.Clear;
SQY_Temp.SQL.Add(MySql);
SQY_Temp.Open;
if SQY_Temp.RecordCount>0 then
aLogined := True else
aLogined := False;
end;
procedure TGMService.GetServerTime(out oTime: OleVariant);
begin
SQY_Temp.Close;
SQY_Temp.SQL.Clear;
SQY_Temp.SQL.Add('select aTime = getDate()');
SQY_Temp.Open;
oTime := SQY_Temp.Fields[0].AsString;
SQY_Temp.Close;
end;
procedure TGMService.AddToList(const aUser, aName, aDept, aLogTime, aHost,
aIP: WideString);
var
aLstItem: TListItem;
begin
with frmServiceMaindo
begin
aLstItem := LstView_Users.Items.Add;
aLstItem.Caption := aUser;
aLstItem.SubItems.Add(aName);
aLstItem.SubItems.Add(aDept);
aLstItem.SubItems.Add(aLogTime);
aLstItem.SubItems.Add(aHost);
aLstItem.SubItems.Add(aIP);
end;
end;
procedure TGMService.DelFromList(const aUser, aIP: WideString);
var
i: integer;
begin
with frmServiceMaindo
begin
for i := 0 to LstView_Users.Items.Count - 1do
begin
if (Trim(LstView_Users.Items.Item
.Caption) = Trim(aUser)) and
(Trim(LstView_Users.Items.Item.SubItems[4]) = Trim(aIP))
then
begin
LstView_Users.Items.Delete(i);
end;
end;
end;
end;
procedure TGMService.CheckRecord(const aSqlStr: WideString;
out oRecNo,
oError: OleVariant);
begin
try
SQY_Temp.Close;
SQY_Temp.SQL.Clear;
SQY_Temp.SQL.Add(aSqlStr);
SQY_Temp.Open;
if Not(SQY_Temp.IsEmpty) then
oRecNo := SQY_Temp.RecordCount
else
oRecNo := 0;
except
on e:Exceptiondo
begin
oRecNo := 0;
oError := e.Message;
end;
end;
end;
procedure TGMService.DoUserCmd(const aSqlStr: WideString;
aPrepared: OleVariant;
out oSuced, oError: OleVariant);
begin
oSuced := False ;
if not(SCCLIENT.Connected) then
SCCLIENT.Connected:=True;
try
with SQY_DoCmddo
begin
Close;
SQL.Clear ;
SQL.Add(aSqlStr);
ExecSQL(aPrepared);
Close;
end;
oSuced := True ;
except
on e:Exceptiondo
oError := e.Message;
end;
end;
procedure TGMService.GetCmdDatas(const aSqlStr: WideString;
var vDatas: OleVariant;
out oError: OleVariant);
begin
try
try
CDS_GetDatas.Close;
CDS_GetDatas.CommandText := aSqlStr ;
CDS_GetDatas.Open;
vDatas := CDS_GetDatas.Data;
finally
CDS_GetDatas.Active := False;
end;
except
on e:Exceptiondo
oError := e.Message;
end;
end;
procedure TGMService.GetUserInfo(const aUserCode: WideString;
out vDatas,
oError: OleVariant);
begin
try
try
CDS_UserInfo.Close;
CDS_UserInfo.FetchParams ;
CDS_UserInfo.Params.ParamByName('@UserCode').AsString := aUserCode ;
CDS_UserInfpen ;
vDatas := CDS_UserInfo.Data;
finally
CDS_UserInfo.Active := False;
end;
except
on e:Exceptiondo
oError := e.Message;
end;
end;
procedure TGMService.RemoteDataModuleDestroy(Sender: TObject);
begin
frmServiceMain.UpdateLogs(-1);
end;
function TGMService.DSP_GetDatasDataRequest(Sender: TObject;
Input: OleVariant): OleVariant;
var
RecsOut:Integer;
begin
if VarIsArray(Input) then
begin
with Sender as TDataSetProviderdo
begin
if Input[0]=0 then
begin
with TSQLDataSet(DSP_GetDatas.DataSet)do
begin
Close;
if Input[1]<>'' then
CommandText := Input[1] ;
Open;
end;
Result := TSQLDataSet(DSP_GetDatas.DataSet).RecordCount;
end else
begin
TSQLDataSet(DataSet).First;
TSQLDataSet(DataSet).MoveBy(Input[0]-1);
Result := GetRecords(Input[1],RecsOut,MetaDataOption);
end;
end;
end;
end;
function TGMService.DSP_MultiGetDataRequest(Sender: TObject;
Input: OleVariant): OleVariant;
var
RecsOut:Integer;
begin
if VarIsArray(Input) then
begin
with Sender as TDataSetProviderdo
begin
if Input[0]=0 then
begin
with TSQLDataSet(DataSet)do
begin
Close;
if Input[1]<>'' then
CommandText := Input[1] ;
Open;
end;
Result := TSQLDataSet(DataSet).RecordCount;
end
else
begin
TSQLDataSet(DataSet).First;
TSQLDataSet(DataSet).MoveBy(Input[0]-1);
Result := GetRecords(Input[1],RecsOut,MetaDataOption);
end;
end;
end;
end;
procedure TGMService.MultiCDSGet(aGetType: Shortint;
const aProviderName: WideString;
abegin
, aGetCount: OleVariant;
out vDatas, oError: OleVariant);
var
RecsOut:Integer;
begin
if (aProviderName = '') or (FindComponent(aProviderName)=nil) then
//找不到provider(包括Multiget)
begin
oError:='Provider not found!';
exit;
end;
with TDataSetProvider(FindComponent(aProviderName))do
begin
if not Assigned(DataSet) then
begin
oError := ' Provider has not been assigned an available dataset!';
exit;
end;
if aGetType=0 then
//客户端Dofirst,取RecordCount
begin
with TSQLDataSet(DataSet)do
begin
if not Active then
Open;
vDatas := RecordCount;
end;
end else
//分批量取数据
begin
if not DataSet.Active then
DataSet.Open;
TSQLDataSet(DataSet).First;
TSQLDataSet(DataSet).MoveBy(abegin
-1);
vDatas := GetRecords(aGetCount,RecsOut,MetaDataOption);
end;
end;
end;
initialization
TComponentFactory.Create(ComServer, TGMService,
Class_GMService, ciMultiInstance, tmApartment);
end.