sqldbtoolsunit.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. unit SQLDBToolsUnit;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, toolsunit,
  6. db,
  7. sqldb, ibconnection, mysql40conn, mysql41conn, mysql50conn, mysql51conn, pqconnection,odbcconn,oracleconnection,sqlite3conn;
  8. type TSQLDBTypes = (mysql40,mysql41,mysql50,mysql51,postgresql,interbase,odbc,oracle,sqlite3);
  9. const MySQLdbTypes = [mysql40,mysql41,mysql50,mysql51];
  10. DBTypesNames : Array [TSQLDBTypes] of String[19] =
  11. ('MYSQL40','MYSQL41','MYSQL50','MYSQL51','POSTGRESQL','INTERBASE','ODBC','ORACLE','SQLITE3');
  12. FieldtypeDefinitionsConst : Array [TFieldType] of String[20] =
  13. (
  14. '',
  15. 'VARCHAR(10)',
  16. 'SMALLINT',
  17. 'INTEGER',
  18. '',
  19. 'BOOLEAN',
  20. 'FLOAT',
  21. '',
  22. 'DECIMAL(18,4)',
  23. 'DATE',
  24. 'TIME',
  25. 'TIMESTAMP',
  26. '',
  27. '',
  28. '',
  29. 'BLOB',
  30. 'BLOB',
  31. 'BLOB',
  32. '',
  33. '',
  34. '',
  35. '',
  36. '',
  37. 'CHAR(10)',
  38. '',
  39. 'BIGINT',
  40. '',
  41. '',
  42. '',
  43. '',
  44. '',
  45. '',
  46. '',
  47. '',
  48. '',
  49. '',
  50. 'TIMESTAMP',
  51. 'NUMERIC(18,6)',
  52. '',
  53. ''
  54. );
  55. type
  56. { TSQLDBConnector }
  57. TSQLDBConnector = class(TDBConnector)
  58. private
  59. FConnection : TSQLConnection;
  60. FTransaction : TSQLTransaction;
  61. FQuery : TSQLQuery;
  62. FUniDirectional: boolean;
  63. procedure CreateFConnection;
  64. procedure CreateFTransaction;
  65. Function CreateQuery : TSQLQuery;
  66. protected
  67. procedure SetTestUniDirectional(const AValue: boolean); override;
  68. function GetTestUniDirectional: boolean; override;
  69. procedure CreateNDatasets; override;
  70. procedure CreateFieldDataset; override;
  71. procedure DropNDatasets; override;
  72. procedure DropFieldDataset; override;
  73. Function InternalGetNDataset(n : integer) : TDataset; override;
  74. Function InternalGetFieldDataset : TDataSet; override;
  75. procedure TryDropIfExist(ATableName : String);
  76. public
  77. destructor Destroy; override;
  78. constructor Create; override;
  79. property Connection : TSQLConnection read FConnection;
  80. property Transaction : TSQLTransaction read FTransaction;
  81. property Query : TSQLQuery read FQuery;
  82. end;
  83. var SQLDbType : TSQLDBTypes;
  84. FieldtypeDefinitions : Array [TFieldType] of String[20];
  85. implementation
  86. { TSQLDBConnector }
  87. procedure TSQLDBConnector.CreateFConnection;
  88. var i : TSQLDBTypes;
  89. t : integer;
  90. begin
  91. for i := low(DBTypesNames) to high(DBTypesNames) do
  92. if UpperCase(dbconnectorparams) = DBTypesNames[i] then sqldbtype := i;
  93. FieldtypeDefinitions := FieldtypeDefinitionsConst;
  94. if SQLDbType = MYSQL40 then Fconnection := tMySQL40Connection.Create(nil);
  95. if SQLDbType = MYSQL41 then Fconnection := tMySQL41Connection.Create(nil);
  96. if SQLDbType = MYSQL50 then Fconnection := tMySQL50Connection.Create(nil);
  97. if SQLDbType = MYSQL51 then Fconnection := tMySQL51Connection.Create(nil);
  98. if SQLDbType in [mysql40,mysql41] then
  99. begin
  100. // Mysql versions prior to 5.0.3 removes the trailing spaces on varchar
  101. // fields on insertion. So to test properly, we have to do the same
  102. for t := 0 to testValuesCount-1 do
  103. testStringValues[t] := TrimRight(testStringValues[t]);
  104. end;
  105. if SQLDbType in MySQLdbTypes then
  106. begin
  107. //MySQL recognizes BOOLEAN, but as synonym for TINYINT, not true sql boolean datatype
  108. FieldtypeDefinitions[ftBoolean] := '';
  109. // Use 'DATETIME' for datetime-fields instead of timestamp, because
  110. // mysql's timestamps are only valid in the range 1970-2038.
  111. // Downside is that fields defined as 'TIMESTAMP' aren't tested
  112. FieldtypeDefinitions[ftDateTime] := 'DATETIME';
  113. FieldtypeDefinitions[ftMemo] := 'TEXT';
  114. end;
  115. if SQLDbType = sqlite3 then
  116. begin
  117. Fconnection := TSQLite3Connection.Create(nil);
  118. FieldtypeDefinitions[ftCurrency] := 'CURRENCY';
  119. FieldtypeDefinitions[ftMemo] := 'CLOB'; //or TEXT SQLite supports both, but CLOB is sql standard (TEXT not)
  120. FieldtypeDefinitions[ftFixedChar] := '';
  121. end;
  122. if SQLDbType = POSTGRESQL then
  123. begin
  124. Fconnection := tPQConnection.Create(nil);
  125. FieldtypeDefinitions[ftCurrency] := 'MONEY';
  126. FieldtypeDefinitions[ftBlob] := 'TEXT';
  127. FieldtypeDefinitions[ftMemo] := 'TEXT';
  128. FieldtypeDefinitions[ftGraphic] := '';
  129. end;
  130. if SQLDbType = INTERBASE then
  131. begin
  132. Fconnection := tIBConnection.Create(nil);
  133. FieldtypeDefinitions[ftBoolean] := '';
  134. FieldtypeDefinitions[ftMemo] := 'BLOB SUB_TYPE TEXT';
  135. end;
  136. if SQLDbType = ODBC then Fconnection := tODBCConnection.Create(nil);
  137. if SQLDbType = ORACLE then Fconnection := TOracleConnection.Create(nil);
  138. if SQLDbType in [mysql40,mysql41,mysql50,mysql51,odbc,interbase] then
  139. begin
  140. // Some DB's do not support milliseconds in time-fields.
  141. // Firebird support miliseconds, see BUG 17199 (when resolved, then interbase can be excluded)
  142. for t := 0 to testValuesCount-1 do
  143. begin
  144. testTimeValues[t] := copy(testTimeValues[t],1,8)+'.000';
  145. testValues[ftTime,t] := copy(testTimeValues[t],1,8)+'.000';
  146. end;
  147. end;
  148. if SQLDbType in [postgresql,interbase] then
  149. begin
  150. // Some db's do not support times > 24:00:00
  151. testTimeValues[3]:='13:25:15.000';
  152. testValues[ftTime,3]:='13:25:15.000';
  153. if SQLDbType = interbase then
  154. begin
  155. // Firebird does not support time = 24:00:00
  156. testTimeValues[2]:='23:00:00.000';
  157. testValues[ftTime,2]:='23:00:00.000';
  158. end;
  159. end;
  160. if SQLDbType in [sqlite3] then
  161. testValues[ftCurrency]:=testValues[ftBCD]; //decimal separator for currencies must be decimal point
  162. if not assigned(Fconnection) then writeln('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
  163. with Fconnection do
  164. begin
  165. DatabaseName := dbname;
  166. UserName := dbuser;
  167. Password := dbpassword;
  168. HostName := dbhostname;
  169. if length(dbQuoteChars)>1 then
  170. begin
  171. FieldNameQuoteChars[0] := dbQuoteChars[1];
  172. FieldNameQuoteChars[1] := dbQuoteChars[2];
  173. end;
  174. open;
  175. end;
  176. end;
  177. procedure TSQLDBConnector.CreateFTransaction;
  178. begin
  179. Ftransaction := tsqltransaction.create(nil);
  180. with Ftransaction do
  181. database := Fconnection;
  182. end;
  183. Function TSQLDBConnector.CreateQuery : TSQLQuery;
  184. begin
  185. Result := TSQLQuery.create(nil);
  186. with Result do
  187. begin
  188. database := Fconnection;
  189. transaction := Ftransaction;
  190. end;
  191. end;
  192. procedure TSQLDBConnector.SetTestUniDirectional(const AValue: boolean);
  193. begin
  194. FUniDirectional:=avalue;
  195. FQuery.UniDirectional:=AValue;
  196. end;
  197. function TSQLDBConnector.GetTestUniDirectional: boolean;
  198. begin
  199. result := FUniDirectional;
  200. end;
  201. procedure TSQLDBConnector.CreateNDatasets;
  202. var CountID : Integer;
  203. begin
  204. try
  205. Ftransaction.StartTransaction;
  206. TryDropIfExist('FPDEV');
  207. Fconnection.ExecuteDirect('create table FPDEV ( ' +
  208. ' ID INT NOT NULL, ' +
  209. ' NAME VARCHAR(50), ' +
  210. ' PRIMARY KEY (ID) ' +
  211. ') ');
  212. FTransaction.CommitRetaining;
  213. for countID := 1 to MaxDataSet do
  214. Fconnection.ExecuteDirect('insert into FPDEV (ID,NAME)' +
  215. 'values ('+inttostr(countID)+',''TestName'+inttostr(countID)+''')');
  216. Ftransaction.Commit;
  217. except
  218. if Ftransaction.Active then Ftransaction.Rollback
  219. end;
  220. end;
  221. procedure TSQLDBConnector.CreateFieldDataset;
  222. var CountID : Integer;
  223. FType : TFieldType;
  224. Sql,sql1: String;
  225. begin
  226. try
  227. Ftransaction.StartTransaction;
  228. TryDropIfExist('FPDEV_FIELD');
  229. Sql := 'create table FPDEV_FIELD (ID INT NOT NULL,';
  230. for FType := low(TFieldType)to high(TFieldType) do
  231. if FieldtypeDefinitions[FType]<>'' then
  232. sql := sql + 'F' + Fieldtypenames[FType] + ' ' +FieldtypeDefinitions[FType]+ ',';
  233. Sql := Sql + 'PRIMARY KEY (ID))';
  234. FConnection.ExecuteDirect(Sql);
  235. FTransaction.CommitRetaining;
  236. for countID := 0 to testValuesCount-1 do
  237. begin
  238. Sql := 'insert into FPDEV_FIELD (ID';
  239. Sql1 := 'values ('+IntToStr(countID);
  240. for FType := low(TFieldType)to high(TFieldType) do
  241. if FieldtypeDefinitions[FType]<>'' then
  242. begin
  243. sql := sql + ',F' + Fieldtypenames[FType];
  244. if testValues[FType,CountID] <> '' then
  245. sql1 := sql1 + ',' + QuotedStr(testValues[FType,CountID])
  246. else
  247. sql1 := sql1 + ',NULL';
  248. end;
  249. Sql := sql + ')';
  250. Sql1 := sql1+ ')';
  251. Fconnection.ExecuteDirect(sql + ' ' + sql1);
  252. end;
  253. Ftransaction.Commit;
  254. except
  255. if Ftransaction.Active then Ftransaction.Rollback
  256. end;
  257. end;
  258. procedure TSQLDBConnector.DropNDatasets;
  259. begin
  260. if assigned(FTransaction) then
  261. begin
  262. try
  263. if Ftransaction.Active then Ftransaction.Rollback;
  264. Ftransaction.StartTransaction;
  265. Fconnection.ExecuteDirect('DROP TABLE FPDEV');
  266. Ftransaction.Commit;
  267. Except
  268. if Ftransaction.Active then Ftransaction.Rollback
  269. end;
  270. end;
  271. end;
  272. procedure TSQLDBConnector.DropFieldDataset;
  273. begin
  274. if assigned(FTransaction) then
  275. begin
  276. try
  277. if Ftransaction.Active then Ftransaction.Rollback;
  278. Ftransaction.StartTransaction;
  279. Fconnection.ExecuteDirect('DROP TABLE FPDEV_FIELD');
  280. Ftransaction.Commit;
  281. Except
  282. if Ftransaction.Active then Ftransaction.Rollback
  283. end;
  284. end;
  285. end;
  286. function TSQLDBConnector.InternalGetNDataset(n: integer): TDataset;
  287. begin
  288. Result := CreateQuery;
  289. with (Result as TSQLQuery) do
  290. begin
  291. sql.clear;
  292. sql.add('SELECT * FROM FPDEV WHERE ID < '+inttostr(n+1));
  293. UniDirectional:=TestUniDirectional;
  294. end;
  295. end;
  296. function TSQLDBConnector.InternalGetFieldDataset: TDataSet;
  297. begin
  298. Result := CreateQuery;
  299. with (Result as TSQLQuery) do
  300. begin
  301. sql.clear;
  302. sql.add('SELECT * FROM FPDEV_FIELD');
  303. tsqlquery(Result).UniDirectional:=TestUniDirectional;
  304. end;
  305. end;
  306. procedure TSQLDBConnector.TryDropIfExist(ATableName: String);
  307. begin
  308. // This makes live soo much easier, since it avoids the exception if the table already
  309. // exists. And while this exeption is in a try..except statement, the debugger
  310. // always shows the exception. Which is pretty annoying
  311. // It only works with Firebird 2, though.
  312. try
  313. if SQLDbType = INTERBASE then
  314. begin
  315. FConnection.ExecuteDirect('execute block as begin if (exists (select 1 from rdb$relations where rdb$relation_name=''' + ATableName + ''')) '+
  316. 'then execute statement ''drop table ' + ATAbleName + ';'';end');
  317. FTransaction.CommitRetaining;
  318. end;
  319. except
  320. FTransaction.RollbackRetaining;
  321. end;
  322. end;
  323. destructor TSQLDBConnector.Destroy;
  324. begin
  325. if assigned(FTransaction) then
  326. begin
  327. try
  328. if Ftransaction.Active then Ftransaction.Rollback;
  329. Ftransaction.StartTransaction;
  330. Fconnection.ExecuteDirect('DROP TABLE FPDEV2');
  331. Ftransaction.Commit;
  332. Except
  333. if Ftransaction.Active then Ftransaction.Rollback
  334. end; // try
  335. end;
  336. inherited Destroy;
  337. FreeAndNil(FQuery);
  338. FreeAndNil(FTransaction);
  339. FreeAndNil(FConnection);
  340. end;
  341. constructor TSQLDBConnector.Create;
  342. begin
  343. FConnection := nil;
  344. CreateFConnection;
  345. CreateFTransaction;
  346. FQuery := CreateQuery;
  347. FConnection.Transaction := FTransaction;
  348. Inherited;
  349. end;
  350. initialization
  351. RegisterClass(TSQLDBConnector);
  352. end.