* Fix compilation and improve sqlite examples, patch from Luiz Americo

git-svn-id: trunk@6791 -
This commit is contained in:
joost 2007-03-11 20:00:43 +00:00
parent 2dc4a8d1b6
commit 9054139d26
5 changed files with 273 additions and 179 deletions

View File

@ -1,60 +1,90 @@
program browseds;
{$Mode ObjFpc}
{$H+}
{$define DEBUGHEAP}
uses
//To test the sqlite3 version replace sqliteds by sqlite3ds
// and TSqliteDataset by TSqlite3Dataset
uses
{$ifdef DEBUGHEAP}
Heaptrc,
{$endif}
{$ifdef Linux}
cmem,
{$endif}
crt,sysutils,sqliteds,db;
crt,
sqliteds,
sysutils,db,inifiles;
var
dsTest:TSQliteDataset;
I:Integer;
const
SQLITEDS_TESTS_INI_FILE = 'sqlitedstests.ini';
DEFAULT_TABLENAME = 'tabletest';
DEFAULT_FILENAME = 'test.db';
var
dsTest:TSqliteDataset;
ini: TIniFile;
i:Integer;
procedure DumpFieldData (F : TField);
begin
With F Do
begin
Writeln ('Field : ',FieldName);
Writeln ('Data type : ',FieldTypeNames[DataType]);
Writeln ('As String : ',AsString);
Case Datatype of
ftSmallint, ftInteger, ftWord : Writeln ('As Longint : ',AsLongint);
ftBoolean : Writeln ('As Boolean : ',AsBoolean);
ftFloat : Writeln ('As Float : ',AsFloat);
ftDate, ftTime, ftDateTime : Writeln ('As DateTime : ',AsDateTime);
with F do
begin
Write (FieldName:10,FieldTypeNames[DataType]:12);
if DataType <> ftMemo then
Write(AsString:30)
else
Write('(memo)':30);
case Datatype of
ftSmallint, ftInteger, ftWord, ftAutoInc : Writeln (AsLongint:28);
ftBoolean : Writeln (AsBoolean:28);
ftFloat : Writeln (AsFloat:28);
ftDate, ftTime, ftDateTime : Writeln (AsDateTime:28);
ftLargeInt: WriteLn(AsLargeInt:28);
ftMemo: WriteLn('(memo)':28);
ftString: WriteLn(AsString:28);
ftCurrency: WriteLn(AsCurrency:28);
else
WriteLn;
end;
end;
writeln('------------------');
//Readkey;
end;
end;
begin
{$ifdef DEBUGHEAP}
SetHeapTraceOutput('heaplog.txt');
SetHeapTraceOutput(ExtractFileName(ParamStr(0))+'.heap.log');
{$endif}
dsTest:=TsqliteDataset.Create(nil);
dsTest:=TSqliteDataset.Create(nil);
with dsTest do
Begin
FileName:='New.db';
TableName:='NewTable';
Sql:= 'SELECT _ROWID_,* FROM NewTable';
begin
//Load Database properties from a inifile
ini:=TIniFile.Create(SQLITEDS_TESTS_INI_FILE);
FileName:=ini.ReadString('testinfo','filename',DEFAULT_FILENAME);
TableName:=ini.ReadString('testinfo','tablename',DEFAULT_TABLENAME);
ini.Destroy;
//Calling Open with an empty SQL, is the same of setting SQL to 'SELECT * from [TableName]';
Open;
WriteLn('RecordCount: ',RecordCount);
First;
//Browse all records
while not Eof do
begin
writeln(':::: Press a key to see data from record ',RecNo,' ::::');
Readkey;
For I:=0 to FieldCount-1 do
ClrScr;
Writeln('Record ',RecNo,'/',RecordCount);
Writeln('--------------------------------------------------------------------------------');
Writeln ('Field Name':10,'Data Type':12,'As String':30, 'As Native Type':28);
Writeln('--------------------------------------------------------------------------------');
for i:=0 to FieldCount - 1 do
DumpFieldData(Fields[I]);
Next;
WriteLn;
if not Eof then
WriteLn(':::: Press a key to see the next record ::::')
else
WriteLn(':::: Press a key to finish the program ::::');
Readkey;
end;
Close;
Destroy;
end;
Exit;
end.

View File

@ -1,6 +1,12 @@
program concurrencyds;
{$Mode ObjFpc}
{$H+}
{$define DEBUGHEAP}
//To test the sqlite3 version replace sqliteds by sqlite3ds
// and TSqliteDataset by TSqlite3Dataset
uses
{$ifdef DEBUGHEAP}
Heaptrc,
@ -8,38 +14,52 @@ uses
{$ifdef Linux}
cmem,
{$endif}
crt,sysutils,SqliteDS;
sysutils,sqliteds, inifiles;
const
SQLITEDS_TESTS_INI_FILE = 'sqlitedstests.ini';
DEFAULT_TABLENAME = 'tabletest';
DEFAULT_FILENAME = 'test.db';
FieldNames: array [0..10] of String =
(
'Integer',
'String',
'Boolean',
'Float',
'Word',
'Date',
'DateTime',
'Time',
'LargeInt',
'AutoInc',
'Currency'
);
var
dsOne,dsTwo:TSQliteDataset;
dsArray: array [0..10] of TSqliteDataset;
ini:TIniFile;
i: Integer;
begin
{$ifdef DEBUGHEAP}
SetHeapTraceOutput('heaplog.txt');
SetHeapTraceOutput(ExtractFileName(ParamStr(0))+'.heap.log');
{$endif}
dsOne:=TsqliteDataset.Create(nil);
dsTwo:=TsqliteDataset.Create(nil);
dsOne.FileName:='New.db';
dsTwo.FileName:='New.db';
dsOne.TableName:='NewTable';
dsTwo.TableName:='NewTable';
dsOne.Sql:= 'SELECT Code FROM NewTable';
dsTwo.Sql:= 'SELECT Name FROM NewTable';
dsOne.Open;
dsTwo.Open;
writeln('Sqlite Return after opening dsTwo: ',dsTwo.SqliteReturnString);
dsOne.First;
dsTwo.First;
WriteLn('Code: ',dsOne.FieldByName('Code').AsInteger);
WriteLn('Name: ',dsTwo.FieldByName('Name').AsString);
dsOne.Next;
dsTwo.Next;
WriteLn('Code: ',dsOne.FieldByName('Code').AsInteger);
WriteLn('Name: ',dsTwo.FieldByName('Name').AsString);
dsOne.Close;
dsTwo.Close;
dsOne.Destroy;
dsTwo.Destroy;
Readkey;
exit;
ini:=TIniFile.Create(SQLITEDS_TESTS_INI_FILE);
for i:= 0 to 10 do
begin
dsArray[i] := TSqliteDataset.Create(nil);
with dsArray[i] do
begin
FileName:=ini.ReadString('testinfo','filename',DEFAULT_FILENAME);
TableName:=ini.ReadString('testinfo','tablename',DEFAULT_TABLENAME);
//Each dataset will retrieve only one field of the same table
Sql:='Select '+FieldNames[i]+ ' from '+ TableName;
Open;
WriteLn('Value of Field ',FieldNames[i],' : ',FieldByName(FieldNames[i]).AsString);
end;
end;
ini.Destroy;
for i:= 0 to 10 do
dsArray[i].Destroy;
end.

View File

@ -1,44 +1,65 @@
program createds;
{$Mode ObjFpc}
{$H+}
{$define DEBUGHEAP}
uses
//To test the sqlite3 version replace sqliteds by sqlite3ds
// and TSqliteDataset by TSqlite3Dataset
uses
{$ifdef DEBUGHEAP}
Heaptrc,
{$endif}
{$ifdef Linux}
cmem,
{$endif}
crt,sysutils,db,SqliteDS;
sqliteds,
sysutils,db,inifiles;
var
dsTest:TSQliteDataset;
const
SQLITEDS_TESTS_INI_FILE = 'sqlitedstests.ini';
DEFAULT_TABLENAME = 'tabletest';
DEFAULT_FILENAME = 'test.db';
var
dsTest:TSqliteDataset;
ini: TIniFile;
begin
begin
{$ifdef DEBUGHEAP}
SetHeapTraceOutput('heaplog.txt');
SetHeapTraceOutput(ExtractFileName(ParamStr(0))+'.heap.log');
{$endif}
dsTest:=TsqliteDataset.Create(nil);
dsTest:=TSqliteDataset.Create(nil);
with dsTest do
Begin
FileName:='New.db';
begin
//Load Database properties from a inifile
ini:=TIniFile.Create(SQLITEDS_TESTS_INI_FILE);
FileName:=ini.ReadString('testinfo','filename',DEFAULT_FILENAME);
TableName:=ini.ReadString('testinfo','tablename',DEFAULT_TABLENAME);
ini.Destroy;
//Ensure the file does not exist yet
if FileExists(FileName) then
DeleteFile(FileName);
TableName:='NewTable';
//Create a table with all available field types
with FieldDefs do
begin
Clear;
Add('Code',ftInteger,0,False);
Add('Name',ftString,0,False);
Add('Bool',ftBoolean,0,False);
Add('Float',ftFloat,0,False);
Add('Word',ftWord,0,False);
Add('DateTime',ftDateTime,0,False);
Add('Date',ftDate,0,False);
Add('Time',ftTime,0,False);
end;
Add('Integer',ftInteger);
Add('String',ftString);
Add('Boolean',ftBoolean);
Add('Float',ftFloat);
Add('Word',ftWord);
Add('DateTime',ftDateTime);
Add('Date',ftDate);
Add('Time',ftTime);
Add('AutoInc',ftAutoInc);
Add('Memo',ftMemo);
Add('LargeInt',ftLargeint);
Add('Currency',ftCurrency);
end;
CreateTable;
writeln('SqliteReturnString after CreateTable: ',SqliteReturnString);
writeln('ReturnString after CreateTable: ',ReturnString);
Destroy;
end;
exit;
end.

View File

@ -1,69 +1,98 @@
program fillds;
{$Mode ObjFpc}
{$H+}
{$define DEBUGHEAP}
uses
//To test the sqlite3 version replace sqliteds by sqlite3ds
// and TSqliteDataset by TSqlite3Dataset
uses
{$ifdef DEBUGHEAP}
Heaptrc,
{$endif}
{$ifdef Linux}
cmem,
{$endif}
crt,sysutils,SqliteDS;
sqliteds,
sysutils,db,IniFiles;
var
dsTest:TSQliteDataset;
const
SQLITEDS_TESTS_INI_FILE = 'sqlitedstests.ini';
DEFAULT_TABLENAME = 'tabletest';
DEFAULT_FILENAME = 'test.db';
MEMOTEST_FILENAME = 'createds.pas';
begin
var
dsTest:TSqliteDataset;
ini: TIniFile;
begin
{$ifdef DEBUGHEAP}
SetHeapTraceOutput('heaplog.txt');
SetHeapTraceOutput(ExtractFileName(ParamStr(0))+'.heap.log');
{$endif}
dsTest:=TsqliteDataset.Create(nil);
dsTest:=TSqliteDataset.Create(nil);
with dsTest do
Begin
FileName:='New.db';
TableName:='NewTable';
Sql:= 'SELECT _ROWID_,* FROM NewTable';
begin
//Load Database properties from a inifile
ini:=TIniFile.Create(SQLITEDS_TESTS_INI_FILE);
FileName:=ini.ReadString('testinfo','filename',DEFAULT_FILENAME);
TableName:=ini.ReadString('testinfo','tablename',DEFAULT_TABLENAME);
ini.Destroy;
//Calling Open with an empty SQL, is the same of setting SQL to 'SELECT * from [TableName]';
Open;
//Add some dummy values
Append;
FieldByName('Code').AsInteger:=100;
FieldByName('Name').AsString:='Luiz';
FieldByName('Bool').AsBoolean:= True;
FieldByName('Integer').AsInteger:=100;
FieldByName('String').AsString:='Luiz';
FieldByName('Boolean').AsBoolean:= False;
FieldByName('Float').AsFloat:=2;
FieldByName('Word').AsInteger:=2763;
FieldByName('DateTime').AsDateTime:=Now;
FieldByName('Time').AsDateTime:=Time;
FieldByName('Date').AsDateTime:=Date;
FieldByName('Memo').AsString:='Here is a long text (Not so long in fact :-))';
FieldByName('Currency').AsFloat:=1.23;
FieldByName('LargeInt').AsLargeInt:=2163871263187263;
Post;
Append;
FieldByName('Code').AsInteger:=101;
FieldByName('Name').AsString:='Américo';
FieldByName('Bool').AsBoolean:= True;
FieldByName('Integer').AsInteger:=101;
FieldByName('String').AsString:='Américo';
FieldByName('Boolean').AsBoolean:= False;
FieldByName('Float').AsFloat:=1.1;
FieldByName('DateTime').AsDateTime:=Now;
FieldByName('Time').AsDateTime:=Time;
FieldByName('Date').AsDateTime:=Date;
FieldByName('LargeInt').AsLargeInt:=-9223372036854775808;
//a real long text :-).
if FileExists(MEMOTEST_FILENAME) then
TMemoField(FieldByName('Memo')).LoadFromFile(MEMOTEST_FILENAME);
Post;
Append;
FieldByName('Code').AsInteger:=102;
FieldByName('Name').AsString:='Ana';
FieldByName('Bool').AsBoolean:= False;
FieldByName('Integer').AsInteger:=102;
FieldByName('String').AsString:='Ana';
FieldByName('Boolean').AsBoolean:= False;
FieldByName('Float').AsFloat:=5.0E-324;
FieldByName('DateTime').AsDateTime:=Now;
FieldByName('Time').AsDateTime:=Time;
FieldByName('Date').AsDateTime:=Date;
FieldByName('LargeInt').AsLargeInt:=9223372036854775807;
Post;
Append;
FieldByName('Code').AsInteger:=103;
FieldByName('Name').AsString:='Luiza';
FieldByName('Bool').AsBoolean:= False;
FieldByName('Integer').AsInteger:=103;
FieldByName('String').AsString:='Luiza';
FieldByName('Boolean').AsBoolean:= True;
FieldByName('Float').AsFloat:=1.7E308;
FieldByName('DateTime').AsDateTime:=Now;
FieldByName('Time').AsDateTime:=Time;
FieldByName('Date').AsDateTime:=Date;
FieldByName('Currency').AsFloat:=20.08;
Post;
//Save the added data to database
ApplyUpdates;
writeln('Last sqlite return: ',SqliteReturnString);
Close;
writeln('ReturnString after ApplyUpdates: ',ReturnString);
//Is not necessary to call Close. Destroy will call it.
//Close;
Destroy;
end;
exit;
end.

View File

@ -1,6 +1,12 @@
program testds;
{$Mode ObjFpc}
{$H+}
{$define DEBUGHEAP}
//To test the sqlite3 version replace sqliteds by sqlite3ds
// and TSqliteDataset by TSqlite3Dataset
uses
{$ifdef DEBUGHEAP}
Heaptrc,
@ -8,103 +14,91 @@ uses
{$ifdef Linux}
cmem,
{$endif}
crt,sysutils,db,SqliteDS;
crt,sysutils,db,sqliteds,IniFiles;
const
SQLITEDS_TESTS_INI_FILE = 'sqlitedstests.ini';
DEFAULT_TABLENAME = 'tabletest';
DEFAULT_FILENAME = 'test.db';
var
dsTest:TSQliteDataset;
I:Integer;
Procedure DumpField(F : Tfield);
begin
With F do
begin
Writeln ('FieldName : ',FieldName);
Writeln ('FieldNo : ',FieldNo);
Writeln ('Index : ',Index);
Writeln ('DataSize : ',DataSize);
Writeln ('Size : ',Size);
Writeln ('DataType : ',FieldTypeNames[DataType]);
Writeln ('Class : ',ClassName);
Writeln ('Required : ',required);
Writeln ('ReadOnly : ',ReadOnly);
Writeln ('Visible : ',Visible);
end;
writeln('-------- Press a key to continue ----------');
readkey;
end;
dsTest:TSqliteDataset;
ini: TIniFile;
begin
{$ifdef DEBUGHEAP}
SetHeapTraceOutput('heaplog.txt');
SetHeapTraceOutput(ExtractFileName(ParamStr(0))+'.heap.log');
{$endif}
dsTest:=TsqliteDataset.Create(nil);
dsTest:=TSqliteDataset.Create(nil);
with dsTest do
Begin
FileName:='New.db';
TableName:='NewTable';
SQL:='SELECT _ROWID_,* from NewTable';
begin
//Load Database properties from a inifile
ini:=TIniFile.Create(SQLITEDS_TESTS_INI_FILE);
FileName:=ini.ReadString('testinfo','filename',DEFAULT_FILENAME);
TableName:=ini.ReadString('testinfo','tablename',DEFAULT_TABLENAME);
ini.Destroy;
//Calling Open with an empty SQL, is the same of setting SQL to 'SELECT * from [TableName]';
Open;
//writeln('SqliteReturnString after Open: ',SqliteReturnString);
//readkey;
Writeln ('Fields count : ',FieldCount);
WriteLn('============DumpFields ============');
For I:=0 to FieldCount-1 do
DumpField(Fields[i]);
writeln('Push a key to test -Edit-');
readkey;
clrscr;
WriteLn('Old Code:',FieldbyName('Code').AsInteger);
WriteLn('Old Name:',FieldbyName('Name').AsString);
FieldbyName('Code').AsInteger:=12345;
FieldbyName('Name').AsString:='Record Edited in TestDs.pas';
WriteLn('New Code:',FieldbyName('Code').AsInteger);
WriteLn('New Name:',FieldbyName('Name').AsString);
writeln('Push a key to test -Append-');
readkey;
clrscr;
Append;
FieldbyName('Code').AsInteger:=22222;
FieldbyName('Name').AsString:='This will be deleted';
Readkey;
ClrScr;
WriteLn('Old Integer:',FieldbyName('Integer').AsInteger);
WriteLn('Old String:',FieldbyName('String').AsString);
Edit;
FieldbyName('Integer').AsInteger:=12345;
FieldbyName('String').AsString:='Record Edited in TestDs.pas';
Post;
WriteLn('First Record Appended - Code:',FieldbyName('Code').AsInteger);
WriteLn('First Record Appended - Name:',FieldbyName('Name').AsString);
WriteLn('New Integer:',FieldbyName('Integer').AsInteger);
WriteLn('New String:',FieldbyName('String').AsString);
WriteLn('Push a key to test -Append-');
ReadKey;
ClrScr;
Append;
FieldbyName('Code').AsInteger:=3333;
FieldbyName('Name').AsString:='This will stay';
FieldbyName('Integer').AsInteger:=22222;
FieldbyName('String').AsString:='This will be deleted';
Post;
WriteLn('Second Record Appended - Code:',FieldbyName('Code').AsInteger);
WriteLn('Second Record Appended - Name:',FieldbyName('Name').AsString);
writeln('Push a key to test -Delete-');
readkey;
clrscr;
WriteLn('First Record Appended - Integer:',FieldbyName('Integer').AsInteger);
WriteLn('First Record Appended - String:',FieldbyName('String').AsString);
Append;
FieldbyName('Integer').AsInteger:=3333;
FieldbyName('String').AsString:='This will stay';
Post;
WriteLn('Second Record Appended - Integer:',FieldbyName('Integer').AsInteger);
WriteLn('Second Record Appended - String:',FieldbyName('String').AsString);
WriteLn('Push a key to test -Delete-');
ReadKey;
ClrScr;
Prior;
WriteLn('Current record:');
Writeln('RowId:',Fields[0].AsInteger);
WriteLn('Code: ',FieldbyName('Code').AsInteger);
WriteLn('Name: ',FieldbyName('Name').AsString);
if FieldbyName('Code').AsInteger = 22222 then
WriteLn('Integer: ',FieldbyName('Integer').AsInteger);
WriteLn('String: ',FieldbyName('String').AsString);
if FieldbyName('Integer').AsInteger = 22222 then
begin
Writeln('This record should be deleted');
Delete;
Delete;
end;
WriteLn('After Delete:');
Writeln('RowId:',Fields[0].AsInteger);
WriteLn('Code: ',FieldbyName('Code').AsInteger);
WriteLn('Name: ',FieldbyName('Name').AsString);
WriteLn('Integer: ',FieldbyName('Integer').AsInteger);
WriteLn('String: ',FieldbyName('String').AsString);
WriteLn('Try to find record with code = 22222');
First;
While Not Eof do
while not Eof do
begin
if FieldbyName('Code').AsInteger = 22222 then
Writeln('Record Found: It Should Not Occur')
if FieldByName('Integer').AsInteger = 22222 then
Writeln('Record Found Manually: It''s a bug')
else
Writeln('Record NOT Found: It''s OK');
Writeln('Record NOT Found Manually: It''s OK');
Next;
end;
readkey;
if Locate('Integer',22222,[]) then
WriteLn('Record Found Using Locate: It''s a bug')
else
WriteLn('Record Not Found Using Locate: It''s OK');
ApplyUpdates;
writeln('SqliteReturnString after ApplyUpdates: ',SqliteReturnString);
Close;
writeln('ReturnString after ApplyUpdates: ',ReturnString);
Destroy;
end;
end.