|
@@ -76,6 +76,7 @@ type
|
|
|
function GetHandle : pointer; virtual; abstract;
|
|
|
|
|
|
Function AllocateCursorHandle : TSQLCursor; virtual; abstract;
|
|
|
+ Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); virtual; abstract;
|
|
|
Function AllocateTransactionHandle : TSQLHandle; virtual; abstract;
|
|
|
|
|
|
procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); virtual; abstract;
|
|
@@ -163,6 +164,7 @@ type
|
|
|
FFromPart : String;
|
|
|
FWhereStartPos : integer;
|
|
|
FWhereStopPos : integer;
|
|
|
+ FParseSQL : boolean;
|
|
|
// FSchemaInfo : TSchemaInfo;
|
|
|
|
|
|
procedure CloseStatement;
|
|
@@ -172,12 +174,14 @@ type
|
|
|
function GetStatementType : TStatementType;
|
|
|
procedure SetIndexDefs(AValue : TIndexDefs);
|
|
|
procedure SetReadOnly(AValue : Boolean);
|
|
|
+ procedure SetParseSQL(AValue : Boolean);
|
|
|
procedure SetUsePrimaryKeyAsKey(AValue : Boolean);
|
|
|
procedure SetUpdateMode(AValue : TUpdateMode);
|
|
|
procedure OnChangeSQL(Sender : TObject);
|
|
|
|
|
|
procedure Execute;
|
|
|
- Procedure ParseSQL(var SQL : string);
|
|
|
+ Procedure SQLParser(var SQL : string);
|
|
|
+ Function AddFilter(SQLstr : string) : string;
|
|
|
protected
|
|
|
// abstract & virtual methods of TBufDataset
|
|
|
function Fetch : boolean; override;
|
|
@@ -194,10 +198,8 @@ type
|
|
|
function GetCanModify: Boolean; override;
|
|
|
function ApplyRecUpdate(UpdateKind : TUpdateKind) : boolean; override;
|
|
|
Function IsPrepared : Boolean; virtual;
|
|
|
- function GetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean): Boolean; overload; override;
|
|
|
procedure SetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean); overload; override;
|
|
|
procedure SetFiltered(Value: Boolean); override;
|
|
|
- procedure SetFilterText(const Value: string); override;
|
|
|
public
|
|
|
procedure Prepare; virtual;
|
|
|
procedure UnPrepare; virtual;
|
|
@@ -246,6 +248,7 @@ type
|
|
|
property UpdateMode : TUpdateMode read FUpdateMode write SetUpdateMode;
|
|
|
property UsePrimaryKeyAsKey : boolean read FUsePrimaryKeyAsKey write SetUsePrimaryKeyAsKey;
|
|
|
property StatementType : TStatementType read GetStatementType;
|
|
|
+ property ParseSQL : Boolean read FParseSQL write SetParseSQL;
|
|
|
// property SchemaInfo : TSchemaInfo read FSchemaInfo default stNoSchema;
|
|
|
end;
|
|
|
|
|
@@ -526,25 +529,22 @@ begin
|
|
|
Result := Assigned(FCursor) and FCursor.FPrepared;
|
|
|
end;
|
|
|
|
|
|
-function TSQLQuery.GetFieldData(Field: TField; Buffer: Pointer;
|
|
|
- NativeFormat: Boolean): Boolean;
|
|
|
-begin
|
|
|
- Result:=GetFieldData(Field, Buffer);
|
|
|
-end;
|
|
|
-
|
|
|
procedure TSQLQuery.SetFieldData(Field: TField; Buffer: Pointer;
|
|
|
NativeFormat: Boolean);
|
|
|
begin
|
|
|
SetFieldData(Field, Buffer);
|
|
|
end;
|
|
|
|
|
|
-procedure TSQLQuery.SetFilterText(const Value: string);
|
|
|
+Function TSQLQuery.AddFilter(SQLstr : string) : string;
|
|
|
|
|
|
begin
|
|
|
- if Filtered then
|
|
|
- begin
|
|
|
- end;
|
|
|
- Inherited SetFilterText(Value);
|
|
|
+ if FWhereStartPos = 0 then
|
|
|
+ SQLstr := SQLstr + ' where (' + Filter + ')'
|
|
|
+ else if FWhereStopPos > 0 then
|
|
|
+ system.insert(' and ('+Filter+') ',SQLstr,FWhereStopPos+1)
|
|
|
+ else
|
|
|
+ system.insert(' where ('+Filter+') ',SQLstr,FWhereStartPos);
|
|
|
+ Result := SQLstr;
|
|
|
end;
|
|
|
|
|
|
procedure TSQLQuery.SetFiltered(Value: Boolean);
|
|
@@ -552,6 +552,7 @@ procedure TSQLQuery.SetFiltered(Value: Boolean);
|
|
|
var S : String;
|
|
|
|
|
|
begin
|
|
|
+ if Value and not FParseSQL then DatabaseErrorFmt(SNoParseSQL,['Filtering ']);
|
|
|
if (Filtered <> Value) and Active then
|
|
|
begin
|
|
|
CloseStatement;
|
|
@@ -560,25 +561,15 @@ begin
|
|
|
|
|
|
s := FSQLBuf;
|
|
|
|
|
|
- if Value then
|
|
|
- begin
|
|
|
- if FWhereStartPos = 0 then
|
|
|
- s := s + ' where (' + Filter + ')'
|
|
|
- else if FWhereStopPos > 0 then
|
|
|
- system.insert(' and ('+Filter+') ',S,FWhereStopPos+1)
|
|
|
- else
|
|
|
- system.insert(' where ('+Filter+') ',S,FWhereStartPos);
|
|
|
- end;
|
|
|
-
|
|
|
+ if Value then s := AddFilter(s);
|
|
|
+
|
|
|
(Database as tsqlconnection).PrepareStatement(Fcursor,(transaction as tsqltransaction),S,FParams);
|
|
|
|
|
|
-
|
|
|
Execute;
|
|
|
inherited InternalOpen;
|
|
|
First;
|
|
|
-
|
|
|
- inherited SetFiltered(Value);
|
|
|
end;
|
|
|
+ inherited setfiltered(Value);
|
|
|
end;
|
|
|
|
|
|
procedure TSQLQuery.Prepare;
|
|
@@ -604,9 +595,12 @@ begin
|
|
|
|
|
|
FSQLBuf := TrimRight(FSQL.Text);
|
|
|
|
|
|
- ParseSQL(FSQLBuf);
|
|
|
+ SQLParser(FSQLBuf);
|
|
|
|
|
|
- Db.PrepareStatement(Fcursor,sqltr,FSQLBuf,FParams);
|
|
|
+ if filtered then
|
|
|
+ Db.PrepareStatement(Fcursor,sqltr,AddFilter(FSQLBuf),FParams)
|
|
|
+ else
|
|
|
+ Db.PrepareStatement(Fcursor,sqltr,FSQLBuf,FParams);
|
|
|
|
|
|
if (FCursor.FStatementType = stSelect) and not ReadOnly then
|
|
|
InitUpdates(FSQLBuf);
|
|
@@ -617,8 +611,11 @@ procedure TSQLQuery.UnPrepare;
|
|
|
|
|
|
begin
|
|
|
CheckInactive;
|
|
|
- if IsPrepared then (Database as tsqlconnection).UnPrepareStatement(FCursor);
|
|
|
- FreeAndNil(FCursor);
|
|
|
+ if IsPrepared then with Database as TSQLConnection do
|
|
|
+ begin
|
|
|
+ UnPrepareStatement(FCursor);
|
|
|
+ DeAllocateCursorHandle(FCursor);
|
|
|
+ end;
|
|
|
end;
|
|
|
|
|
|
procedure TSQLQuery.FreeFldBuffers;
|
|
@@ -682,7 +679,7 @@ begin
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
-procedure TSQLQuery.ParseSQL(var SQL : string);
|
|
|
+procedure TSQLQuery.SQLParser(var SQL : string);
|
|
|
|
|
|
type TParsePart = (ppStart,ppSelect,ppWhere,ppFrom,ppOrder,ppComment,ppBogus);
|
|
|
|
|
@@ -721,6 +718,7 @@ begin
|
|
|
FCursor.FStatementType := (Database as tsqlconnection).StrToStatementType(s);
|
|
|
if FCursor.FStatementType = stSelect then ParsePart := ppSelect
|
|
|
else break;
|
|
|
+ if not FParseSQL then break;
|
|
|
PStatementPart := CurrentP;
|
|
|
end;
|
|
|
ppSelect : begin
|
|
@@ -858,6 +856,7 @@ begin
|
|
|
FSQL.OnChange := @OnChangeSQL;
|
|
|
FIndexDefs := TIndexDefs.Create(Self);
|
|
|
FReadOnly := false;
|
|
|
+ FParseSQL := True;
|
|
|
// Delphi has upWhereAll as default, but since strings and oldvalue's don't work yet
|
|
|
// (variants) set it to upWhereKeyOnly
|
|
|
FUpdateMode := upWhereKeyOnly;
|
|
@@ -877,12 +876,27 @@ end;
|
|
|
procedure TSQLQuery.SetReadOnly(AValue : Boolean);
|
|
|
|
|
|
begin
|
|
|
- if not Active then FReadOnly := AValue
|
|
|
- else
|
|
|
+ CheckInactive;
|
|
|
+ if not AValue then
|
|
|
begin
|
|
|
- // Just temporary, this should be possible in the future
|
|
|
- DatabaseError(SActiveDataset);
|
|
|
- end;
|
|
|
+ if FParseSQL then FReadOnly := False
|
|
|
+ else DatabaseErrorFmt(SNoParseSQL,['Updating ']);
|
|
|
+ end
|
|
|
+ else FReadOnly := True;
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TSQLQuery.SetParseSQL(AValue : Boolean);
|
|
|
+
|
|
|
+begin
|
|
|
+ CheckInactive;
|
|
|
+ if not AValue then
|
|
|
+ begin
|
|
|
+ FReadOnly := True;
|
|
|
+ Filtered := False;
|
|
|
+ FParseSQL := False;
|
|
|
+ end
|
|
|
+ else
|
|
|
+ FParseSQL := True;
|
|
|
end;
|
|
|
|
|
|
procedure TSQLQuery.SetUsePrimaryKeyAsKey(AValue : Boolean);
|