📄 jvbdeqbe.pas
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvDBQBE.PAS, released on 2002-07-04.
The Initial Developers of the Original Code are: Fedor Koshevnikov, Igor Pavluk and Serge Korolev
Copyright (c) 1997, 1998 Fedor Koshevnikov, Igor Pavluk and Serge Korolev
Copyright (c) 2001,2002 SGB Software
All Rights Reserved.
Additional credits and thanks goto AO ROSNO and
Master-Bank for there additions to this unit
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvBDEQBE.pas,v 1.12 2005/02/17 10:19:59 marquardt Exp $
unit JvBDEQBE;
{$I jvcl.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
Classes, DB, DBTables, Bde;
const
DefQBEStartParam = '#';
type
TCheckType = (ctNone, ctCheck, ctCheckPlus, ctCheckDesc, ctCheckGroup);
TJvQBEQuery = class(TDBDataSet)
private
FStmtHandle: HDBIStmt;
FQBE: TStringList;
FPrepared: Boolean;
FParams: TParams;
FStartParam: Char;
FAuxiliaryTables: Boolean;
FText: string;
FRowsAffected: Integer;
FConstrained: Boolean;
FLocal: Boolean;
FRequestLive: Boolean;
FBlankAsZero: Boolean;
FParamCheck: Boolean;
function CreateCursor(GenHandle: Boolean): HDBICur;
procedure ReplaceParams(QBEText: TStrings);
procedure CreateParams(List: TParams; const Value: PChar);
procedure FreeStatement;
function GetQBE: TStrings;
function GetQueryCursor(GenHandle: Boolean): HDBICur;
procedure GetStatementHandle(QBEText: PChar);
procedure PrepareQBE(Value: PChar);
procedure QueryChanged(Sender: TObject);
procedure SetQBE(Value: TStrings);
procedure SetParams(Value: TParams);
procedure SetPrepared(Value: Boolean);
procedure SetPrepare(Value: Boolean);
procedure SetStartParam(Value: Char);
procedure ReadParamData(Reader: TReader);
procedure WriteParamData(Writer: TWriter);
function GetRowsAffected: Integer;
protected
{ IProviderSupport }
procedure PSExecute; override;
function PSGetParams: TParams; override;
procedure PSSetCommandText(const CommandText: string); override;
procedure PSSetParams(AParams: TParams); override;
function CreateHandle: HDBICur; override;
procedure Disconnect; override;
function GetParamsCount: Word;
procedure DefineProperties(Filer: TFiler); override;
function SetDBFlag(Flag: Integer; Value: Boolean): Boolean; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetQBEText: PChar;
procedure ExecQBE;
function ParamByName(const Value: string): TParam;
procedure Prepare;
procedure RefreshQuery;
procedure UnPrepare;
property Local: Boolean read FLocal;
property ParamCount: Word read GetParamsCount;
property Prepared: Boolean read FPrepared write SetPrepare;
property StmtHandle: HDBIStmt read FStmtHandle;
property Text: string read FText;
property RowsAffected: Integer read GetRowsAffected;
published
property AutoRefresh;
property AuxiliaryTables: Boolean read FAuxiliaryTables write FAuxiliaryTables default True;
property ParamCheck: Boolean read FParamCheck write FParamCheck default True;
property StartParam: Char read FStartParam write SetStartParam default DefQBEStartParam;
{ Ensure StartParam is declared before QBE }
property QBE: TStrings read GetQBE write SetQBE;
{ Ensure QBE is declared before Params }
property BlankAsZero: Boolean read FBlankAsZero write FBlankAsZero default False;
property Params: TParams read FParams write SetParams stored False;
property RequestLive: Boolean read FRequestLive write FRequestLive default False;
property UpdateMode;
property UpdateObject;
property Constrained: Boolean read FConstrained write FConstrained default False;
property Constraints stored ConstraintsStored;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvBDEQBE.pas,v $';
Revision: '$Revision: 1.12 $';
Date: '$Date: 2005/02/17 10:19:59 $';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
SysUtils, DBConsts, BDEConst,
JvDBUtils;
constructor TJvQBEQuery.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FQBE := TStringList.Create;
FQBE.OnChange := QueryChanged;
FParams := TParams.Create(Self);
FStartParam := DefQBEStartParam;
FParamCheck := True;
FAuxiliaryTables := True;
FRowsAffected := -1;
FRequestLive := False;
end;
destructor TJvQBEQuery.Destroy;
begin
Destroying;
Disconnect;
FQBE.Free;
FParams.Free;
inherited Destroy;
end;
procedure TJvQBEQuery.Disconnect;
begin
Close;
UnPrepare;
end;
procedure TJvQBEQuery.RefreshQuery;
var
Bookmark: TBookmark;
begin
DisableControls;
Bookmark := GetBookmark;
try
Close;
Open;
try
GotoBookmark(Bookmark);
except
{ ignore exceptions }
end;
finally
FreeBookmark(Bookmark);
EnableControls;
end;
end;
procedure TJvQBEQuery.SetPrepare(Value: Boolean);
begin
if Value then
Prepare
else
UnPrepare;
end;
procedure TJvQBEQuery.Prepare;
begin
SetDBFlag(dbfPrepared, True);
SetPrepared(True);
end;
procedure TJvQBEQuery.UnPrepare;
begin
SetPrepared(False);
SetDBFlag(dbfPrepared, False);
end;
procedure TJvQBEQuery.SetStartParam(Value: Char);
begin
if Value <> FStartParam then
begin
FStartParam := Value;
QueryChanged(nil);
end;
end;
function TJvQBEQuery.GetQBE: TStrings;
begin
Result := FQBE;
end;
procedure TJvQBEQuery.SetQBE(Value: TStrings);
begin
if FQBE.Text <> Value.Text then
begin
Disconnect;
FQBE.OnChange := nil;
FQBE.Assign(Value);
FQBE.OnChange := QueryChanged;
QueryChanged(nil);
end;
end;
procedure TJvQBEQuery.QueryChanged(Sender: TObject);
var
List: TParams;
begin
if not (csReading in ComponentState) then
begin
Disconnect;
FText := QBE.Text;
if ParamCheck or (csDesigning in ComponentState) then
begin
List := TParams.Create(Self);
try
CreateParams(List, PChar(Text));
List.AssignValues(FParams);
FParams.Clear;
FParams.Assign(List);
finally
List.Free;
end;
end;
DataEvent(dePropertyChange, 0);
end
else
begin
FText := QBE.Text;
FParams.Clear;
CreateParams(FParams, PChar(Text));
end;
end;
procedure TJvQBEQuery.SetParams(Value: TParams);
begin
FParams.AssignValues(Value);
end;
procedure TJvQBEQuery.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('ParamData', ReadParamData, WriteParamData, True);
end;
procedure TJvQBEQuery.ReadParamData(Reader: TReader);
begin
Reader.ReadValue;
Reader.ReadCollection(FParams);
end;
procedure TJvQBEQuery.WriteParamData(Writer: TWriter);
begin
Writer.WriteCollection(Params);
end;
function TJvQBEQuery.GetParamsCount: Word;
begin
Result := FParams.Count;
end;
procedure TJvQBEQuery.ReplaceParams(QBEText: TStrings);
var
I: Integer;
function ReplaceString(const S: string): string;
var
I, J, P, LiteralChars: Integer;
Param: TParam;
Temp: string;
Found: Boolean;
begin
Result := S;
for I := Params.Count - 1 downto 0 do
begin
Param := Params[I];
if Param.DataType = ftUnknown then
Continue; { ignore undefined params }
repeat
P := Pos(StartParam + Param.Name, Result);
Found := (P > 0) and ((Length(Result) = P + Length(Param.Name)) or
NameDelimiter(Result[P + Length(Param.Name) + 1]));
if Found then
begin
LiteralChars := 0;
for J := 1 to P - 1 do
if IsLiteral(Result[J]) then
Inc(LiteralChars);
Found := LiteralChars mod 2 = 0;
if Found then
begin
Temp := Param.Text;
if Temp = '' then
begin
if (Param.DataType = ftString) and not Param.IsNull then
Temp := '""'
else
Temp := 'BLANK'; { special QBE operator }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -