sqldbtoolsunit.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. public
  73. destructor Destroy; override;
  74. constructor Create; override;
  75. property Connection : TSQLConnection read FConnection;
  76. property Transaction : TSQLTransaction read FTransaction;
  77. property Query : TSQLQuery read FQuery;
  78. end;
  79. var SQLDbType : TSQLDBTypes;
  80. FieldtypeDefinitions : Array [TFieldType] of String[15];
  81. implementation
  82. { TSQLDBConnector }
  83. procedure TSQLDBConnector.CreateFConnection;
  84. var i : TSQLDBTypes;
  85. t : integer;
  86. begin
  87. for i := low(DBTypesNames) to high(DBTypesNames) do
  88. if UpperCase(dbconnectorparams) = DBTypesNames[i] then sqldbtype := i;
  89. FieldtypeDefinitions := FieldtypeDefinitionsConst;
  90. if SQLDbType = MYSQL40 then Fconnection := tMySQL40Connection.Create(nil);
  91. if SQLDbType = MYSQL41 then Fconnection := tMySQL41Connection.Create(nil);
  92. if SQLDbType in [mysql40,mysql41] then
  93. begin
  94. // Mysql versions prior to 5.0.3 removes the trailing spaces on varchar
  95. // fields on insertion. So to test properly, we have to do the same
  96. for t := 0 to testValuesCount-1 do
  97. testStringValues[t] := TrimRight(testStringValues[t]);
  98. end;
  99. if SQLDbType = MYSQL50 then Fconnection := tMySQL50Connection.Create(nil);
  100. if SQLDbType in MySQLdbTypes then
  101. FieldtypeDefinitions[ftLargeint] := 'BIGINT';
  102. if SQLDbType = sqlite3 then
  103. begin
  104. Fconnection := TSQLite3Connection.Create(nil);
  105. FieldtypeDefinitions[ftCurrency] := 'CURRENCY';
  106. FieldtypeDefinitions[ftFixedChar] := '';
  107. end;
  108. if SQLDbType = POSTGRESQL then
  109. begin
  110. Fconnection := tpqConnection.Create(nil);
  111. FieldtypeDefinitions[ftBlob] := 'TEXT';
  112. FieldtypeDefinitions[ftMemo] := 'TEXT';
  113. FieldtypeDefinitions[ftGraphic] := '';
  114. FieldtypeDefinitions[ftCurrency] := 'MONEY';
  115. end;
  116. if SQLDbType = INTERBASE then
  117. begin
  118. Fconnection := tIBConnection.Create(nil);
  119. FieldtypeDefinitions[ftLargeint] := 'BIGINT';
  120. end;
  121. if SQLDbType = ODBC then Fconnection := tODBCConnection.Create(nil);
  122. if SQLDbType = ORACLE then Fconnection := TOracleConnection.Create(nil);
  123. if not assigned(Fconnection) then writeln('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
  124. with Fconnection do
  125. begin
  126. DatabaseName := dbname;
  127. UserName := dbuser;
  128. Password := dbpassword;
  129. HostName := dbhostname;
  130. open;
  131. end;
  132. end;
  133. procedure TSQLDBConnector.CreateFTransaction;
  134. begin
  135. Ftransaction := tsqltransaction.create(nil);
  136. with Ftransaction do
  137. database := Fconnection;
  138. end;
  139. Function TSQLDBConnector.CreateQuery : TSQLQuery;
  140. begin
  141. Result := TSQLQuery.create(nil);
  142. with Result do
  143. begin
  144. database := Fconnection;
  145. transaction := Ftransaction;
  146. end;
  147. end;
  148. procedure TSQLDBConnector.CreateNDatasets;
  149. var CountID : Integer;
  150. begin
  151. try
  152. Ftransaction.StartTransaction;
  153. Fconnection.ExecuteDirect('create table FPDEV ( ' +
  154. ' ID INT NOT NULL, ' +
  155. ' NAME VARCHAR(50), ' +
  156. ' PRIMARY KEY (ID) ' +
  157. ') ');
  158. FTransaction.CommitRetaining;
  159. for countID := 1 to MaxDataSet do
  160. Fconnection.ExecuteDirect('insert into FPDEV (ID,NAME)' +
  161. 'values ('+inttostr(countID)+',''TestName'+inttostr(countID)+''')');
  162. Ftransaction.Commit;
  163. except
  164. if Ftransaction.Active then Ftransaction.Rollback
  165. end;
  166. end;
  167. procedure TSQLDBConnector.CreateFieldDataset;
  168. var CountID : Integer;
  169. FType : TFieldType;
  170. Sql,sql1: String;
  171. begin
  172. try
  173. Ftransaction.StartTransaction;
  174. Sql := 'create table FPDEV_FIELD (ID INT NOT NULL,';
  175. for FType := low(TFieldType)to high(TFieldType) do
  176. if FieldtypeDefinitions[FType]<>'' then
  177. sql := sql + 'F' + Fieldtypenames[FType] + ' ' +FieldtypeDefinitions[FType]+ ',';
  178. Sql := Sql + 'PRIMARY KEY (ID))';
  179. FConnection.ExecuteDirect(Sql);
  180. FTransaction.CommitRetaining;
  181. for countID := 0 to testValuesCount-1 do
  182. begin
  183. Sql := 'insert into FPDEV_FIELD (ID';
  184. Sql1 := 'values ('+IntToStr(countID);
  185. for FType := low(TFieldType)to high(TFieldType) do
  186. if FieldtypeDefinitions[FType]<>'' then
  187. begin
  188. sql := sql + ',F' + Fieldtypenames[FType];
  189. if testValues[FType,CountID] <> '' then
  190. sql1 := sql1 + ',''' + StringReplace(testValues[FType,CountID],'''','''''',[rfReplaceAll]) + ''''
  191. else
  192. sql1 := sql1 + ',NULL';
  193. end;
  194. Sql := sql + ')';
  195. Sql1 := sql1+ ')';
  196. Fconnection.ExecuteDirect(sql + ' ' + sql1);
  197. end;
  198. Ftransaction.Commit;
  199. except
  200. if Ftransaction.Active then Ftransaction.Rollback
  201. end;
  202. end;
  203. procedure TSQLDBConnector.DropNDatasets;
  204. begin
  205. try
  206. if Ftransaction.Active then Ftransaction.Rollback;
  207. Ftransaction.StartTransaction;
  208. Fconnection.ExecuteDirect('DROP TABLE FPDEV');
  209. Ftransaction.Commit;
  210. Except
  211. if Ftransaction.Active then Ftransaction.Rollback
  212. end;
  213. end;
  214. procedure TSQLDBConnector.DropFieldDataset;
  215. begin
  216. try
  217. if Ftransaction.Active then Ftransaction.Rollback;
  218. Ftransaction.StartTransaction;
  219. Fconnection.ExecuteDirect('DROP TABLE FPDEV_FIELD');
  220. Ftransaction.Commit;
  221. Except
  222. if Ftransaction.Active then Ftransaction.Rollback
  223. end;
  224. end;
  225. function TSQLDBConnector.InternalGetNDataset(n: integer): TDataset;
  226. begin
  227. Result := CreateQuery;
  228. with (Result as TSQLQuery) do
  229. begin
  230. sql.clear;
  231. sql.add('SELECT * FROM FPDEV WHERE ID < '+inttostr(n+1));
  232. end;
  233. end;
  234. function TSQLDBConnector.InternalGetFieldDataset: TDataSet;
  235. begin
  236. Result := CreateQuery;
  237. with (Result as TSQLQuery) do
  238. begin
  239. sql.clear;
  240. sql.add('SELECT * FROM FPDEV_FIELD');
  241. end;
  242. end;
  243. destructor TSQLDBConnector.Destroy;
  244. begin
  245. try
  246. if Ftransaction.Active then Ftransaction.Rollback;
  247. Ftransaction.StartTransaction;
  248. Fconnection.ExecuteDirect('DROP TABLE FPDEV2');
  249. Ftransaction.Commit;
  250. Except
  251. if Ftransaction.Active then Ftransaction.Rollback
  252. end;
  253. inherited Destroy;
  254. FreeAndNil(FQuery);
  255. FreeAndNil(FTransaction);
  256. FreeAndNil(FConnection);
  257. end;
  258. constructor TSQLDBConnector.Create;
  259. begin
  260. FConnection := nil;
  261. CreateFConnection;
  262. CreateFTransaction;
  263. FQuery := CreateQuery;
  264. FConnection.Transaction := FTransaction;
  265. Inherited;
  266. end;
  267. initialization
  268. RegisterClass(TSQLDBConnector);
  269. end.