+ Patch from Luiz Américo to fix several issues

This commit is contained in:
michael 2005-02-18 22:34:38 +00:00
parent 24a0d113f9
commit a21420401f

View File

@ -72,7 +72,6 @@ type
FOrphanItems: TList;
procedure BuildLinkedList;
procedure DisposeLinkedList;
procedure SetSql (AValue:String);
protected
function AllocRecordBuffer: PChar; override;
procedure FreeRecordBuffer(var Buffer: PChar); override;
@ -127,7 +126,7 @@ type
property FileName: String read FFileName write FFileName;
property IndexFieldName: String read FIndexFieldName write FIndexFieldName;
property SaveOnClose: Boolean read FSaveOnClose write FSaveOnClose;
property SQL: String read FSql write SetSql;
property SQL: String read FSql write FSql;
property TableName: String read FTableName write FTableName;
//property Active;
property FieldDefs;
@ -180,7 +179,6 @@ var
Counter:Integer;
AType:TFieldType;
ColumnStr:String;
TempAttributes:TFieldAttributes;
begin
// Sqlite is typeless (allows any type in any field)
// regardless of what is in Create Table, but returns
@ -190,7 +188,7 @@ begin
For Counter:= 0 to Columns - 1 do
begin
ColumnStr:= UpCase(StrPas(ColumnNames[Counter + Columns]));
If (ColumnStr = 'INTEGER') or (ColumnStr = 'INT') then
If (ColumnStr = 'INTEGER') then
begin
AType:= ftInteger;
FieldSize:=SizeOf(Integer);
@ -220,6 +218,7 @@ begin
FieldSize:=SizeOf(TDateTime);
end else if (ColumnStr = 'AUTOINC') then
begin
//Todo: remove this check. do it in open
if TSqliteDataset(TheDataset).Tablename = '' then
DatabaseError('Sqliteds - AutoInc fields requires Tablename to be set');
AType:= ftAutoInc;
@ -229,19 +228,9 @@ begin
end else
begin
AType:= ftString;
FieldSize:=0;
end;
with TDataset(TheDataset).FieldDefs do
begin
Add(StrPas(ColumnNames[Counter]), AType, FieldSize, False);
If Items[Counter].Name = '_ROWID_' then
begin
TempAttributes:=Items[Counter].Attributes;
System.Include(TempAttributes,faReadonly);
Items[Counter].Attributes:=TempAttributes;
end;
FieldSize:=10; //??
end;
TDataset(TheDataset).FieldDefs.Add(StrPas(ColumnNames[Counter]), AType, FieldSize, False);
end;
result:=-1;
end;
@ -249,8 +238,12 @@ end;
// TSqliteDataset override methods
function TSqliteDataset.AllocRecordBuffer: PChar;
var
APointer:Pointer;
begin
Result := AllocMem(FBufferSize);
APointer := AllocMem(FBufferSize);
PDataRecord(APointer^):=FBeginItem;
Result:=APointer;
end;
procedure TSqliteDataset.BuildLinkedList;
@ -278,6 +271,7 @@ begin
New(FBeginItem);
FBeginItem^.Next:=nil;
FBeginItem^.Previous:=nil;
FBeginItem^.BookMarkFlag:=bfBOF;
TempItem:=FBeginItem;
FRecordCount:=0;
FSqliteReturnId:=sqlite_step(vm,@FRowCount,@ColumnValues,@ColumnNames);
@ -394,13 +388,19 @@ function TSqliteDataset.GetFieldData(Field: TField; Buffer: Pointer): Boolean;
var
ValError:Word;
FieldRow:PChar;
//FieldIndex:Integer;
begin
if FRecordCount = 0 then // avoid exception in empty datasets
if FRecordCount = 0 then // avoid exception in empty datasets -Todo: see if still applys
begin
Result:=False;
Exit;
end;
FieldRow:=PPDataRecord(ActiveBuffer)^^.Row[Field.Index];
//Small hack to allow reopening datasets with TDbEdit
//while not fix it in LCL (It seems that TDataLink doesnt update Field property
//after Closing and reopening datasets)
//FieldRow:=PPDataRecord(ActiveBuffer)^^.Row[Field.Index];
//FieldIndex:=Field.FieldNo - 1;
FieldRow:=PPDataRecord(ActiveBuffer)^^.Row[Field.FieldNo - 1];
Result := FieldRow <> nil;
if Result and (Buffer <> nil) then //supports GetIsNull
begin
@ -521,6 +521,7 @@ procedure TSqliteDataset.InternalClose;
begin
if FSaveOnClose then
ApplyUpdates;
//BindFields(False);
if DefaultFields then
DestroyFields;
if FDataAllocated then
@ -541,6 +542,8 @@ procedure TSqliteDataset.InternalDelete;
var
TempItem:PDataRecord;
begin
If FRecordCount = 0 then
Exit;
Dec(FRecordCount);
TempItem:=PPDataRecord(ActiveBuffer)^;
// Remove from changed list
@ -627,7 +630,7 @@ begin
BindFields(True);
// Get indexfieldno if available
if FIndexFieldName <> '' then
FIndexFieldNo:=FieldByName(FIndexFieldName).Index
FIndexFieldNo:=FieldByName(FIndexFieldName).FieldNo - 1
else
FIndexFieldNo:=FAutoIncFieldNo;
@ -684,35 +687,33 @@ var
TempStr:String;
ActiveItem:PDataRecord;
begin
if (FRecordCount = 0) and (State <> dsInsert) then //avoid exception in win32 + lcl + TDbEdit
Exit;
ActiveItem:=PPDataRecord(ActiveBuffer)^;
if (ActiveItem <> FCacheItem) and (FUpdatedItems.IndexOf(ActiveItem) = -1) and (FAddedItems.IndexOf(ActiveItem) = -1) then
FUpdatedItems.Add(ActiveItem);
if Buffer = nil then
ActiveItem^.Row[Field.Index]:=nil
ActiveItem^.Row[Pred(Field.FieldNo)]:=nil
else
begin
StrDispose(ActiveItem^.Row[Pred(Field.FieldNo)]);
case Field.Datatype of
ftString:
begin
StrDispose(ActiveItem^.Row[Field.Index]);
ActiveItem^.Row[Field.Index]:=StrNew(PChar(Buffer));
ActiveItem^.Row[Pred(Field.FieldNo)]:=StrNew(PChar(Buffer));
end;
ftInteger,ftBoolean,ftWord:
begin
StrDispose(ActiveItem^.Row[Field.Index]);
Str(LongInt(Buffer^),TempStr);
ActiveItem^.Row[Field.Index]:=StrAlloc(Length(TempStr)+1);
StrPCopy(ActiveItem^.Row[Field.Index],TempStr);
ActiveItem^.Row[Pred(Field.FieldNo)]:=StrAlloc(Length(TempStr)+1);
StrPCopy(ActiveItem^.Row[Pred(Field.FieldNo)],TempStr);
end;
ftFloat,ftDateTime,ftDate,ftTime:
begin
StrDispose(ActiveItem^.Row[Field.Index]);
Str(Double(Buffer^),TempStr);
ActiveItem^.Row[Field.Index]:=StrAlloc(Length(TempStr)+1);
StrPCopy(ActiveItem^.Row[Field.Index],TempStr);
end;
ActiveItem^.Row[Pred(Field.FieldNo)]:=StrAlloc(Length(TempStr)+1);
StrPCopy(ActiveItem^.Row[Pred(Field.FieldNo)],TempStr);
end;
end;// case
end;//if
end;
procedure TSqliteDataset.SetRecNo(Value: Integer);
@ -720,7 +721,7 @@ var
Counter:Integer;
TempItem:PDataRecord;
begin
if Value >= FRecordCount then
if (Value >= FRecordCount) or (Value < 0) then
DatabaseError('SqliteDs - Record Number Out Of Range');
TempItem:=FBeginItem;
for Counter := 0 to Value do
@ -730,12 +731,6 @@ end;
// Specific functions
procedure TSqliteDataset.SetSql(AValue:String);
begin
FSql:=AValue;
// Todo: Retrieve Tablename from SQL ??
end;
function TSqliteDataset.ExecSQL(ASql:String):Integer;
begin
Result:=0;
@ -772,7 +767,7 @@ begin
For CounterItems:= 0 to FUpdatedItems.Count - 1 do
begin
SqlTemp:=SqlTemp+'UPDATE '+FTableName+' SET ';
for CounterFields:= 1 to Fields.Count - 1 do
for CounterFields:= 0 to Fields.Count - 1 do
begin
if PDataRecord(FUpdatedItems[CounterItems])^.Row[CounterFields] <> nil then
begin
@ -793,14 +788,14 @@ begin
For CounterItems:= 0 to FAddedItems.Count - 1 do
begin
SqlTemp:=SqlTemp+'INSERT INTO '+FTableName+ ' ( ';
for CounterFields:= 1 to Fields.Count - 1 do
for CounterFields:= 0 to Fields.Count - 1 do
begin
SqlTemp:=SqlTemp + Fields[CounterFields].FieldName;
if CounterFields <> Fields.Count - 1 then
SqlTemp:=SqlTemp+' , ';
end;
SqlTemp:=SqlTemp+') VALUES ( ';
for CounterFields:= 1 to Fields.Count - 1 do
for CounterFields:= 0 to Fields.Count - 1 do
begin
if PDataRecord(FAddedItems[CounterItems])^.Row[CounterFields] <> nil then
begin