sqldbtoolsunit.pas 9.2 KB

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