mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-26 08:49:17 +02:00
* Added some comments
* Tests are more generic, ie use BLOB for firebird, TEXT for postgresql etc * Added support for SQLite3 git-svn-id: trunk@8599 -
This commit is contained in:
parent
c88406c9e5
commit
10930a5165
@ -7,13 +7,58 @@ interface
|
|||||||
uses
|
uses
|
||||||
Classes, SysUtils, toolsunit,
|
Classes, SysUtils, toolsunit,
|
||||||
db,
|
db,
|
||||||
sqldb, ibconnection, mysql40conn, mysql41conn, mysql50conn, pqconnection,odbcconn,oracleconnection;
|
sqldb, ibconnection, mysql40conn, mysql41conn, mysql50conn, pqconnection,odbcconn,oracleconnection,sqlite3conn;
|
||||||
|
|
||||||
type TSQLDBTypes = (mysql40,mysql41,mysql50,postgresql,interbase,odbc,oracle);
|
type TSQLDBTypes = (mysql40,mysql41,mysql50,postgresql,interbase,odbc,oracle,sqlite3);
|
||||||
|
|
||||||
const MySQLdbTypes = [mysql40,mysql41,mysql50];
|
const MySQLdbTypes = [mysql40,mysql41,mysql50];
|
||||||
DBTypesNames : Array [TSQLDBTypes] of String[19] =
|
DBTypesNames : Array [TSQLDBTypes] of String[19] =
|
||||||
('MYSQL40','MYSQL41','MYSQL50','POSTGRESQL','INTERBASE','ODBC','ORACLE');
|
('MYSQL40','MYSQL41','MYSQL50','POSTGRESQL','INTERBASE','ODBC','ORACLE','SQLITE3');
|
||||||
|
|
||||||
|
FieldtypeDefinitionsConst : Array [TFieldType] of String[15] =
|
||||||
|
(
|
||||||
|
'',
|
||||||
|
'VARCHAR(10)',
|
||||||
|
'SMALLINT',
|
||||||
|
'INTEGER',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'FLOAT',
|
||||||
|
'DECIMAL(18,4)',
|
||||||
|
'',
|
||||||
|
'DATE',
|
||||||
|
'TIMESTAMP',
|
||||||
|
'TIMESTAMP',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'BLOB',
|
||||||
|
'BLOB',
|
||||||
|
'BLOB',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'CHAR(10)',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'TIMESTAMP',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
''
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
{ TSQLDBConnector }
|
{ TSQLDBConnector }
|
||||||
@ -41,7 +86,8 @@ type
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
var SQLDbType : TSQLDBTypes;
|
var SQLDbType : TSQLDBTypes;
|
||||||
|
FieldtypeDefinitions : Array [TFieldType] of String[15];
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
{ TSQLDBConnector }
|
{ TSQLDBConnector }
|
||||||
@ -51,11 +97,25 @@ var i : TSQLDBTypes;
|
|||||||
begin
|
begin
|
||||||
for i := low(DBTypesNames) to high(DBTypesNames) do
|
for i := low(DBTypesNames) to high(DBTypesNames) do
|
||||||
if UpperCase(dbconnectorparams) = DBTypesNames[i] then sqldbtype := i;
|
if UpperCase(dbconnectorparams) = DBTypesNames[i] then sqldbtype := i;
|
||||||
|
|
||||||
|
FieldtypeDefinitions := FieldtypeDefinitionsConst;
|
||||||
|
|
||||||
if SQLDbType = MYSQL40 then Fconnection := tMySQL40Connection.Create(nil);
|
if SQLDbType = MYSQL40 then Fconnection := tMySQL40Connection.Create(nil);
|
||||||
if SQLDbType = MYSQL41 then Fconnection := tMySQL41Connection.Create(nil);
|
if SQLDbType = MYSQL41 then Fconnection := tMySQL41Connection.Create(nil);
|
||||||
if SQLDbType = MYSQL50 then Fconnection := tMySQL50Connection.Create(nil);
|
if SQLDbType = MYSQL50 then Fconnection := tMySQL50Connection.Create(nil);
|
||||||
if SQLDbType = POSTGRESQL then Fconnection := tpqConnection.Create(nil);
|
if SQLDbType = sqlite3 then
|
||||||
|
begin
|
||||||
|
Fconnection := TSQLite3Connection.Create(nil);
|
||||||
|
FieldtypeDefinitions[ftCurrency] := '';
|
||||||
|
FieldtypeDefinitions[ftFixedChar] := '';
|
||||||
|
end;
|
||||||
|
if SQLDbType = POSTGRESQL then
|
||||||
|
begin
|
||||||
|
Fconnection := tpqConnection.Create(nil);
|
||||||
|
FieldtypeDefinitions[ftBlob] := 'TEXT';
|
||||||
|
FieldtypeDefinitions[ftMemo] := 'TEXT';
|
||||||
|
FieldtypeDefinitions[ftGraphic] := '';
|
||||||
|
end;
|
||||||
if SQLDbType = INTERBASE then Fconnection := tIBConnection.Create(nil);
|
if SQLDbType = INTERBASE then Fconnection := tIBConnection.Create(nil);
|
||||||
if SQLDbType = ODBC then Fconnection := tODBCConnection.Create(nil);
|
if SQLDbType = ODBC then Fconnection := tODBCConnection.Create(nil);
|
||||||
if SQLDbType = ORACLE then Fconnection := TOracleConnection.Create(nil);
|
if SQLDbType = ORACLE then Fconnection := TOracleConnection.Create(nil);
|
||||||
@ -116,23 +176,41 @@ end;
|
|||||||
|
|
||||||
procedure TSQLDBConnector.CreateFieldDataset;
|
procedure TSQLDBConnector.CreateFieldDataset;
|
||||||
var CountID : Integer;
|
var CountID : Integer;
|
||||||
|
FType : TFieldType;
|
||||||
|
Sql,sql1: String;
|
||||||
begin
|
begin
|
||||||
try
|
try
|
||||||
Ftransaction.StartTransaction;
|
Ftransaction.StartTransaction;
|
||||||
Fconnection.ExecuteDirect('create table FPDEV_FIELD ( ' +
|
|
||||||
' ID INT NOT NULL, ' +
|
Sql := 'create table FPDEV_FIELD (ID INT NOT NULL,';
|
||||||
' FSTRING VARCHAR(10), ' +
|
for FType := low(TFieldType)to high(TFieldType) do
|
||||||
' FINTEGER INT, ' +
|
if FieldtypeDefinitions[FType]<>'' then
|
||||||
' FDATE DATE, ' +
|
sql := sql + 'F' + Fieldtypenames[FType] + ' ' +FieldtypeDefinitions[FType]+ ',';
|
||||||
' FDATETIME TIMESTAMP, ' +
|
Sql := Sql + 'PRIMARY KEY (ID))';
|
||||||
' PRIMARY KEY (ID) ' +
|
|
||||||
') ');
|
FConnection.ExecuteDirect(Sql);
|
||||||
|
|
||||||
FTransaction.CommitRetaining;
|
FTransaction.CommitRetaining;
|
||||||
|
|
||||||
for countID := 0 to testValuesCount-1 do
|
for countID := 0 to testValuesCount-1 do
|
||||||
Fconnection.ExecuteDirect('insert into FPDEV_FIELD (ID,FSTRING,FINTEGER,FDATE,FDATETIME)' +
|
begin
|
||||||
'values ('+inttostr(countID)+','''+testStringValues[CountID]+''','''+inttostr(testIntValues[CountID])+''','''+testDateValues[CountID]+''','''+testDateValues[CountID]+''')');
|
|
||||||
|
Sql := 'insert into FPDEV_FIELD (ID';
|
||||||
|
Sql1 := 'values ('+IntToStr(countID);
|
||||||
|
for FType := low(TFieldType)to high(TFieldType) do
|
||||||
|
if FieldtypeDefinitions[FType]<>'' then
|
||||||
|
begin
|
||||||
|
sql := sql + ',F' + Fieldtypenames[FType];
|
||||||
|
if testValues[FType,CountID] <> '' then
|
||||||
|
sql1 := sql1 + ',''' + testValues[FType,CountID] + ''''
|
||||||
|
else
|
||||||
|
sql1 := sql1 + ',NULL';
|
||||||
|
end;
|
||||||
|
Sql := sql + ')';
|
||||||
|
Sql1 := sql1+ ')';
|
||||||
|
|
||||||
|
Fconnection.ExecuteDirect(sql + ' ' + sql1);
|
||||||
|
end;
|
||||||
|
|
||||||
Ftransaction.Commit;
|
Ftransaction.Commit;
|
||||||
except
|
except
|
||||||
|
@ -396,8 +396,7 @@ procedure TTestFieldTypes.TestChangeBlob;
|
|||||||
var s : string;
|
var s : string;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (ID int,FT blob)');
|
TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (ID int,FT '+FieldtypeDefinitions[ftblob]+')');
|
||||||
// TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (ID int,FT text)');
|
|
||||||
TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For interbase
|
TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For interbase
|
||||||
|
|
||||||
TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (ID,FT) values (1,''Test deze blob'')');
|
TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (ID,FT) values (1,''Test deze blob'')');
|
||||||
@ -438,8 +437,7 @@ end;
|
|||||||
|
|
||||||
procedure TTestFieldTypes.TestBlobGetText;
|
procedure TTestFieldTypes.TestBlobGetText;
|
||||||
begin
|
begin
|
||||||
CreateTableWithFieldType(ftBlob,'BLOB');
|
CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
|
||||||
// CreateTableWithFieldType(ftBlob,'TEXT');
|
|
||||||
TestFieldDeclaration(ftBlob,0);
|
TestFieldDeclaration(ftBlob,0);
|
||||||
|
|
||||||
TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
|
TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
|
||||||
@ -468,7 +466,7 @@ var
|
|||||||
ASQL : TSQLQuery;
|
ASQL : TSQLQuery;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
CreateTableWithFieldType(ftBlob,'BLOB');
|
CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
|
||||||
// CreateTableWithFieldType(ftBlob,'TEXT');
|
// CreateTableWithFieldType(ftBlob,'TEXT');
|
||||||
TestFieldDeclaration(ftBlob,0);
|
TestFieldDeclaration(ftBlob,0);
|
||||||
|
|
||||||
@ -496,8 +494,7 @@ var
|
|||||||
i : byte;
|
i : byte;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
CreateTableWithFieldType(ftBlob,'BLOB');
|
CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
|
||||||
// CreateTableWithFieldType(ftBlob,'TEXT');
|
|
||||||
TestFieldDeclaration(ftBlob,0);
|
TestFieldDeclaration(ftBlob,0);
|
||||||
|
|
||||||
TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
|
TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
|
||||||
@ -555,7 +552,7 @@ var
|
|||||||
i, corrTestValueCount : byte;
|
i, corrTestValueCount : byte;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
CreateTableWithFieldType(ftDateTime,'TIMESTAMP');
|
CreateTableWithFieldType(ftDateTime,FieldtypeDefinitions[ftDateTime]);
|
||||||
TestFieldDeclaration(ftDateTime,8);
|
TestFieldDeclaration(ftDateTime,8);
|
||||||
|
|
||||||
if SQLDbType=mysql40 then corrTestValueCount := testValuesCount-21
|
if SQLDbType=mysql40 then corrTestValueCount := testValuesCount-21
|
||||||
@ -742,6 +739,7 @@ begin
|
|||||||
|
|
||||||
with TSQLDBConnector(DBConnector).Query do
|
with TSQLDBConnector(DBConnector).Query do
|
||||||
begin
|
begin
|
||||||
|
PacketRecords := -1;
|
||||||
sql.clear;
|
sql.clear;
|
||||||
sql.append('insert into FPDEV2 (ID,FIELD1) values (:id,:field1)');
|
sql.append('insert into FPDEV2 (ID,FIELD1) values (:id,:field1)');
|
||||||
|
|
||||||
@ -911,8 +909,7 @@ var
|
|||||||
ASQL : TSQLQuery;
|
ASQL : TSQLQuery;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
CreateTableWithFieldType(ftBlob,'BLOB');
|
CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
|
||||||
// CreateTableWithFieldType(ftBlob,'TEXT');
|
|
||||||
TestFieldDeclaration(ftBlob,0);
|
TestFieldDeclaration(ftBlob,0);
|
||||||
|
|
||||||
ASQL := DBConnector.GetNDataset(True,1) as tsqlquery;
|
ASQL := DBConnector.GetNDataset(True,1) as tsqlquery;
|
||||||
@ -987,6 +984,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TTestFieldTypes.TestParametersAndDates;
|
procedure TTestFieldTypes.TestParametersAndDates;
|
||||||
|
// See bug 7205
|
||||||
begin
|
begin
|
||||||
with TSQLDBConnector(DBConnector).Query do
|
with TSQLDBConnector(DBConnector).Query do
|
||||||
begin
|
begin
|
||||||
|
@ -147,10 +147,12 @@ var dbtype,
|
|||||||
dbname,
|
dbname,
|
||||||
dbuser,
|
dbuser,
|
||||||
dbhostname,
|
dbhostname,
|
||||||
dbpassword : string;
|
dbpassword : string;
|
||||||
DataEvents : string;
|
DataEvents : string;
|
||||||
DBConnector : TDBConnector;
|
DBConnector : TDBConnector;
|
||||||
|
testValues : Array [TFieldType,0..testvaluescount -1] of string;
|
||||||
|
|
||||||
|
|
||||||
procedure InitialiseDBConnector;
|
procedure InitialiseDBConnector;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
@ -217,7 +219,17 @@ end;
|
|||||||
|
|
||||||
procedure InitialiseDBConnector;
|
procedure InitialiseDBConnector;
|
||||||
var DBConnectorClass : TPersistentClass;
|
var DBConnectorClass : TPersistentClass;
|
||||||
|
i : integer;
|
||||||
begin
|
begin
|
||||||
|
testValues[ftString] := testStringValues;
|
||||||
|
testValues[ftDate] := testDateValues;
|
||||||
|
for i := 0 to testValuesCount-1 do
|
||||||
|
begin
|
||||||
|
testValues[ftFloat,i] := FloatToStr(testFloatValues[i]);
|
||||||
|
testValues[ftSmallint,i] := IntToStr(testSmallIntValues[i]);
|
||||||
|
testValues[ftInteger,i] := IntToStr(testIntValues[i]);
|
||||||
|
end;
|
||||||
|
|
||||||
if dbconnectorname = '' then raise Exception.Create('There is no db-connector specified');
|
if dbconnectorname = '' then raise Exception.Create('There is no db-connector specified');
|
||||||
DBConnectorClass := GetClass('T'+dbconnectorname+'DBConnector');
|
DBConnectorClass := GetClass('T'+dbconnectorname+'DBConnector');
|
||||||
if assigned(DBConnectorClass) then
|
if assigned(DBConnectorClass) then
|
||||||
|
Loading…
Reference in New Issue
Block a user