function TpFIBDataSet.LockRecord(RaiseErr:boolean = True):TLockStatus;
var
ExceptMessage:string;
Retry:boolean;
tf:TField;
i:integer;
UnknownErrorMsg, LockTxt:string;
TableName:string;
Where:string;
begin
if (EmptyStrings(QUpdate.SQL) or
(AutoUpdateOptions.AutoReWriteSqls and (AutoUpdateOptions.UpdateOnlyModifiedFields)))
then
with AutoUpdateOptions do
if AutoReWriteSqls then
begin
LockTxt:=UpdateTableName;
if KeyFieldList.Count>0 then
begin
Where:=KeyFieldList[0]+'=?'+KeyFieldList[0];
for i:=1 to KeyFieldList.Count-1 do
Where:=Where+' and '+KeyFieldList[i]+'=?'+KeyFieldList[i];
end
else
raise Exception.Create(SFIBErrorUnableLock)
end
else
raise Exception.Create(SFIBErrorUnableLock)
else
begin
LockTxt:=GetModifyTable(QUpdate.ReadySQLText(False),True);
Where:=QUpdate.WhereClause[1]
end;
if (LockTxt<>'') then
begin
TableName:=ExtractWord(1,LockTxt,CharsAfterClause);
tf:= GetFieldForTable(TableName);
if tf<>nil then
begin
LockTxt:='UPDATE '+LockTxt+' SET '
+FormatIdentifier(Database.SQLDialect, GetRelationFieldName(tf))+'= ?'
+FormatIdentifier(Database.SQLDialect, tf.FieldName)+' WHERE '
+Where;
end
else
LockTxt:= ''
end;
if LockTxt<>'' then
begin
if (vLockQry.SQL.Count=0) or (vLockQry.SQL.Text<>LockTxt+#13#10) then
vLockQry.SQL.Text:= LockTxt
end
else
vLockQry.SQL.Assign(QUpdate.SQL);
if vLockQry.Database<>Database then
vLockQry.Database:= Database;
if vLockQry.Transaction<>UpdateTransaction then
vLockQry.Transaction:= UpdateTransaction;
if not CachedUpdates and not (State in [dsEdit, dsInsert]) then
SaveOldBuffer(GetActiveBuf);
SetQueryParams(vLockQry, GetActiveBuf);
if CachedUpdates then
for i:= 0 to Pred(vLockQry.Params.Count) do
begin
tf:= FindField(vLockQry.Params[i].Name);
if (tf<>nil) and not tf.IsBlob then
if tf.isNull then
vLockQry.Params[i].IsNull:= True
else
if tf is TDateTimeField then
vLockQry.Params[i].AsDateTime:= tf.OldValue
else
vLockQry.Params[i].Value:= tf.OldValue
end;
Result:= lsUnknownError;
Retry:= True;
while Retry do
begin
try
if (not UpdateTransaction.InTransaction) and (poStartTransaction in Options)
then
UpdateTransaction.StartTransaction;
vLockQry.ExecQuery;
if vLockQry.RowsAffected = 1 then
Result:= lsSuccess
else
if vLockQry.RowsAffected = 0 then
Result:= lsNotExist
else
Result:= lsMultiply;
except
on E:EFIBError do
case E.SQLCode of
sqlcode_deadlock:
Result:= lsDeadLock;
sqlcode_901:
if E.IBErrorCode= isc_lock_conflict then
Result:= lsDeadLock
else
begin
UnknownErrorMsg:= E.Message;
Result:= lsUnknownError
end;
else
UnknownErrorMsg:= E.Message;
Result:= lsUnknownError
end;
end;
if (Result<>lsSuccess) and RaiseErr then
begin
case Result of
lsDeadLock:ExceptMessage:= SEdDeadLockMess;
lsNotExist:ExceptMessage:= SEdNotExistRecord;
lsMultiply:ExceptMessage:= SEdMultiplyRecord;
lsUnknownError:ExceptMessage:= UnknownErrorMsg;
end;
ExceptMessage:= SEdErrorPrefix+ExceptMessage;
Retry:= RaiseLockError(Result, ExceptMessage) = daRetry
end
else
Retry:= False
end;
end;
Bir mum, yanındaki mumları tutuşturmakla,
ışığında hiç bir şey kaybetmez. Mevlana
OS win.10, IDE Delphi 10.3, RDBMS Firebird and MSSQL, BROWSER Chrome