sqldbtoolsunit.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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] := 'BYTEA';
  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 datetime and 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. if length(testValues[ftDateTime,t]) > 19 then
  147. testValues[ftDateTime,t] := copy(testValues[ftDateTime,t],1,19)+'.000';
  148. end;
  149. end;
  150. if SQLDbType in [postgresql,interbase] then
  151. begin
  152. // Some db's do not support times > 24:00:00
  153. testTimeValues[3]:='13:25:15.000';
  154. testValues[ftTime,3]:='13:25:15.000';
  155. if SQLDbType = interbase then
  156. begin
  157. // Firebird does not support time = 24:00:00
  158. testTimeValues[2]:='23:00:00.000';
  159. testValues[ftTime,2]:='23:00:00.000';
  160. end;
  161. end;
  162. if SQLDbType in [sqlite3] then
  163. testValues[ftCurrency]:=testValues[ftBCD]; //decimal separator for currencies must be decimal point
  164. if not assigned(Fconnection) then writeln('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
  165. with Fconnection do
  166. begin
  167. DatabaseName := dbname;
  168. UserName := dbuser;
  169. Password := dbpassword;
  170. HostName := dbhostname;
  171. if length(dbQuoteChars)>1 then
  172. begin
  173. FieldNameQuoteChars[0] := dbQuoteChars[1];
  174. FieldNameQuoteChars[1] := dbQuoteChars[2];
  175. end;
  176. open;
  177. end;
  178. end;
  179. procedure TSQLDBConnector.CreateFTransaction;
  180. begin
  181. Ftransaction := tsqltransaction.create(nil);
  182. with Ftransaction do
  183. database := Fconnection;
  184. end;
  185. Function TSQLDBConnector.CreateQuery : TSQLQuery;
  186. begin
  187. Result := TSQLQuery.create(nil);
  188. with Result do
  189. begin
  190. database := Fconnection;
  191. transaction := Ftransaction;
  192. end;
  193. end;
  194. procedure TSQLDBConnector.SetTestUniDirectional(const AValue: boolean);
  195. begin
  196. FUniDirectional:=avalue;
  197. FQuery.UniDirectional:=AValue;
  198. end;
  199. function TSQLDBConnector.GetTestUniDirectional: boolean;
  200. begin
  201. result := FUniDirectional;
  202. end;
  203. procedure TSQLDBConnector.CreateNDatasets;
  204. var CountID : Integer;
  205. begin
  206. try
  207. Ftransaction.StartTransaction;
  208. TryDropIfExist('FPDEV');
  209. Fconnection.ExecuteDirect('create table FPDEV ( ' +
  210. ' ID INT NOT NULL, ' +
  211. ' NAME VARCHAR(50), ' +
  212. ' PRIMARY KEY (ID) ' +
  213. ') ');
  214. FTransaction.CommitRetaining;
  215. for countID := 1 to MaxDataSet do
  216. Fconnection.ExecuteDirect('insert into FPDEV (ID,NAME)' +
  217. 'values ('+inttostr(countID)+',''TestName'+inttostr(countID)+''')');
  218. Ftransaction.Commit;
  219. except
  220. if Ftransaction.Active then Ftransaction.Rollback
  221. end;
  222. end;
  223. procedure TSQLDBConnector.CreateFieldDataset;
  224. var CountID : Integer;
  225. FType : TFieldType;
  226. Sql,sql1: String;
  227. begin
  228. try
  229. Ftransaction.StartTransaction;
  230. TryDropIfExist('FPDEV_FIELD');
  231. Sql := 'create table FPDEV_FIELD (ID INT NOT NULL,';
  232. for FType := low(TFieldType)to high(TFieldType) do
  233. if FieldtypeDefinitions[FType]<>'' then
  234. sql := sql + 'F' + Fieldtypenames[FType] + ' ' +FieldtypeDefinitions[FType]+ ',';
  235. Sql := Sql + 'PRIMARY KEY (ID))';
  236. FConnection.ExecuteDirect(Sql);
  237. FTransaction.CommitRetaining;
  238. for countID := 0 to testValuesCount-1 do
  239. begin
  240. Sql := 'insert into FPDEV_FIELD (ID';
  241. Sql1 := 'values ('+IntToStr(countID);
  242. for FType := low(TFieldType)to high(TFieldType) do
  243. if FieldtypeDefinitions[FType]<>'' then
  244. begin
  245. sql := sql + ',F' + Fieldtypenames[FType];
  246. if testValues[FType,CountID] <> '' then
  247. sql1 := sql1 + ',' + QuotedStr(testValues[FType,CountID])
  248. else
  249. sql1 := sql1 + ',NULL';
  250. end;
  251. Sql := sql + ')';
  252. Sql1 := sql1+ ')';
  253. Fconnection.ExecuteDirect(sql + ' ' + sql1);
  254. end;
  255. Ftransaction.Commit;
  256. except
  257. if Ftransaction.Active then Ftransaction.Rollback
  258. end;
  259. end;
  260. procedure TSQLDBConnector.DropNDatasets;
  261. begin
  262. if assigned(FTransaction) then
  263. begin
  264. try
  265. if Ftransaction.Active then Ftransaction.Rollback;
  266. Ftransaction.StartTransaction;
  267. Fconnection.ExecuteDirect('DROP TABLE FPDEV');
  268. Ftransaction.Commit;
  269. Except
  270. if Ftransaction.Active then Ftransaction.Rollback
  271. end;
  272. end;
  273. end;
  274. procedure TSQLDBConnector.DropFieldDataset;
  275. begin
  276. if assigned(FTransaction) then
  277. begin
  278. try
  279. if Ftransaction.Active then Ftransaction.Rollback;
  280. Ftransaction.StartTransaction;
  281. Fconnection.ExecuteDirect('DROP TABLE FPDEV_FIELD');
  282. Ftransaction.Commit;
  283. Except
  284. if Ftransaction.Active then Ftransaction.Rollback
  285. end;
  286. end;
  287. end;
  288. function TSQLDBConnector.InternalGetNDataset(n: integer): TDataset;
  289. begin
  290. Result := CreateQuery;
  291. with (Result as TSQLQuery) do
  292. begin
  293. sql.clear;
  294. sql.add('SELECT * FROM FPDEV WHERE ID < '+inttostr(n+1));
  295. UniDirectional:=TestUniDirectional;
  296. end;
  297. end;
  298. function TSQLDBConnector.InternalGetFieldDataset: TDataSet;
  299. begin
  300. Result := CreateQuery;
  301. with (Result as TSQLQuery) do
  302. begin
  303. sql.clear;
  304. sql.add('SELECT * FROM FPDEV_FIELD');
  305. tsqlquery(Result).UniDirectional:=TestUniDirectional;
  306. end;
  307. end;
  308. procedure TSQLDBConnector.TryDropIfExist(ATableName: String);
  309. begin
  310. // This makes live soo much easier, since it avoids the exception if the table already
  311. // exists. And while this exeption is in a try..except statement, the debugger
  312. // always shows the exception. Which is pretty annoying
  313. // It only works with Firebird 2, though.
  314. try
  315. if SQLDbType = INTERBASE then
  316. begin
  317. FConnection.ExecuteDirect('execute block as begin if (exists (select 1 from rdb$relations where rdb$relation_name=''' + ATableName + ''')) '+
  318. 'then execute statement ''drop table ' + ATAbleName + ';'';end');
  319. FTransaction.CommitRetaining;
  320. end;
  321. except
  322. FTransaction.RollbackRetaining;
  323. end;
  324. end;
  325. destructor TSQLDBConnector.Destroy;
  326. begin
  327. if assigned(FTransaction) then
  328. begin
  329. try
  330. if Ftransaction.Active then Ftransaction.Rollback;
  331. Ftransaction.StartTransaction;
  332. Fconnection.ExecuteDirect('DROP TABLE FPDEV2');
  333. Ftransaction.Commit;
  334. Except
  335. if Ftransaction.Active then Ftransaction.Rollback
  336. end; // try
  337. end;
  338. inherited Destroy;
  339. FreeAndNil(FQuery);
  340. FreeAndNil(FTransaction);
  341. FreeAndNil(FConnection);
  342. end;
  343. constructor TSQLDBConnector.Create;
  344. begin
  345. FConnection := nil;
  346. CreateFConnection;
  347. CreateFTransaction;
  348. FQuery := CreateQuery;
  349. FConnection.Transaction := FTransaction;
  350. Inherited;
  351. end;
  352. initialization
  353. RegisterClass(TSQLDBConnector);
  354. end.