testsqlfieldtypes.pas 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. unit TestSQLFieldTypes;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, testregistry,
  6. db;
  7. type
  8. TParamProc = procedure(AParam:TParam; i : integer);
  9. TFieldProc = procedure(AField:TField; i : integer);
  10. { TTestFieldTypes }
  11. TTestFieldTypes= class(TTestCase)
  12. private
  13. procedure CreateTableWithFieldType(ADatatype : TFieldType; ASQLTypeDecl : string);
  14. procedure TestFieldDeclaration(ADatatype: TFieldType; ADataSize: integer);
  15. procedure TestXXParamQuery(ADatatype : TFieldType; ASQLTypeDecl : string; testValuescount : integer; Cross : boolean = false);
  16. protected
  17. procedure SetUp; override;
  18. procedure TearDown; override;
  19. procedure RunTest; override;
  20. published
  21. procedure TestInsertLargeStrFields; // bug 9600
  22. procedure TestRowsAffected; // bug 9758
  23. procedure TestStringsReplace;
  24. procedure TestCircularParams;
  25. procedure Test11Params;
  26. procedure TestBug9744;
  27. procedure TestCrossStringDateParam;
  28. procedure TestGetFieldNames;
  29. procedure TestUpdateIndexDefs;
  30. procedure TestSetBlobAsMemoParam;
  31. procedure TestSetBlobAsStringParam;
  32. procedure TestGetIndexDefs;
  33. procedure TestDblQuoteEscComments;
  34. procedure TestpfInUpdateFlag; // bug 7565
  35. procedure TestInt;
  36. procedure TestScript;
  37. procedure TestTemporaryTable;
  38. procedure TestParametersAndDates;
  39. procedure TestExceptOnsecClose;
  40. procedure TestBlob;
  41. procedure TestChangeBlob;
  42. procedure TestBlobGetText;
  43. procedure TestLargeRecordSize;
  44. procedure TestNumeric;
  45. procedure TestFloat;
  46. procedure TestDateTime; // bug 6925
  47. procedure TestString;
  48. procedure TestUnlVarChar;
  49. procedure TestDate;
  50. procedure TestNullValues;
  51. procedure TestParamQuery;
  52. procedure TestStringParamQuery;
  53. procedure TestDateParamQuery;
  54. procedure TestIntParamQuery;
  55. procedure TestFloatParamQuery;
  56. procedure TestAggregates;
  57. end;
  58. implementation
  59. uses sqldbtoolsunit,toolsunit, variants, sqldb, bufdataset;
  60. Type HackedDataset = class(TDataset);
  61. const
  62. testFloatValuesCount = 21;
  63. testFloatValues : Array[0..testFloatValuesCount-1] of double = (-maxSmallint-1,-maxSmallint,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint+1,0.123456,-0.123456,4.35,12.434E7,9.876e-5,123.45678);
  64. testIntValuesCount = 17;
  65. testIntValues : Array[0..testIntValuesCount-1] of integer = (-maxInt,-maxSmallint-1,-maxSmallint,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint+1,MaxInt);
  66. testStringValuesCount = 20;
  67. testStringValues : Array[0..testStringValuesCount-1] of string = (
  68. '',
  69. 'a',
  70. 'ab',
  71. 'abc',
  72. 'abcd',
  73. 'abcde',
  74. 'abcdef',
  75. 'abcdefg',
  76. 'abcdefgh',
  77. 'abcdefghi',
  78. 'abcdefghij',
  79. 'lMnOpQrStU',
  80. '1234567890',
  81. '_!@#$%^&*(',
  82. ' ''quotes'' ',
  83. ')-;:/?.<>',
  84. '~`|{}- =', // note that there's no \ (backslash) since some db's uses that as escape-character
  85. ' WRaP ',
  86. 'wRaP ',
  87. ' wRAP'
  88. );
  89. testDateValuesCount = 18;
  90. testDateValues : Array[0..testDateValuesCount-1] of string = (
  91. '2000-01-01',
  92. '1999-12-31',
  93. '2004-02-29',
  94. '2004-03-01',
  95. '1991-02-28',
  96. '1991-03-01',
  97. '2040-10-16',
  98. '1977-09-29',
  99. '1800-03-30',
  100. '1650-05-10',
  101. '1754-06-04',
  102. '0904-04-12',
  103. '0199-07-09',
  104. '0001-01-01',
  105. '1899-12-29',
  106. '1899-12-30',
  107. '1899-12-31',
  108. '1900-01-01'
  109. );
  110. procedure TTestFieldTypes.TestpfInUpdateFlag;
  111. var ds : TBufDataset;
  112. AFld1, AFld2, AFld3 : Tfield;
  113. begin
  114. ds := (DBConnector.GetNDataset(True,5) as TBufDataset);
  115. with ds do
  116. begin
  117. AFld1 := TIntegerField.Create(ds);
  118. AFld1.FieldName := 'ID';
  119. AFld1.DataSet := ds;
  120. AFld1.ProviderFlags := AFld1.ProviderFlags + [pfInKey];
  121. AFld2 := TStringField.Create(ds);
  122. AFld2.FieldName := 'NAME';
  123. AFld2.DataSet := ds;
  124. AFld3 := TIntegerField.Create(ds);
  125. AFld3.FieldName := 'CALCFLD';
  126. AFld3.DataSet := ds;
  127. Afld3.FieldKind := fkCalculated;
  128. AFld3.ProviderFlags := [];
  129. Open;
  130. Edit;
  131. FieldByName('ID').AsInteger := 254;
  132. Post;
  133. ApplyUpdates;
  134. Append;
  135. FieldByName('ID').AsInteger := 255;
  136. Post;
  137. ApplyUpdates;
  138. Close;
  139. AFld1.Free;
  140. AFld2.Free;
  141. AFld3.Free;
  142. end;
  143. end;
  144. procedure TTestFieldTypes.TestScript;
  145. var Ascript : TSQLScript;
  146. begin
  147. Ascript := tsqlscript.create(nil);
  148. with Ascript do
  149. begin
  150. DataBase := TSQLDBConnector(DBConnector).Connection;
  151. transaction := TSQLDBConnector(DBConnector).Transaction;
  152. script.clear;
  153. script.append('create table a (id int);');
  154. script.append('create table b (id int);');
  155. ExecuteScript;
  156. end;
  157. end;
  158. procedure TTestFieldTypes.TestInt;
  159. var
  160. i : byte;
  161. begin
  162. CreateTableWithFieldType(ftInteger,'INT');
  163. TestFieldDeclaration(ftInteger,4);
  164. for i := 0 to testIntValuesCount-1 do
  165. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (' + inttostr(testIntValues[i]) + ')');
  166. with TSQLDBConnector(DBConnector).Query do
  167. begin
  168. Open;
  169. for i := 0 to testIntValuesCount-1 do
  170. begin
  171. AssertEquals(testIntValues[i],fields[0].AsInteger);
  172. Next;
  173. end;
  174. close;
  175. end;
  176. end;
  177. procedure TTestFieldTypes.TestLargeRecordSize;
  178. var
  179. i : byte;
  180. begin
  181. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (plant varchar(8192),sampling_type varchar(8192),area varchar(8192), area_description varchar(8192), batch varchar(8192), sampling_datetime timestamp, status varchar(8192), batch_commentary varchar(8192))');
  182. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  183. if UpperCase(dbconnectorparams)='INTERBASE' then TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  184. with TSQLDBConnector(DBConnector).Query do
  185. begin
  186. sql.clear;
  187. sql.append('insert into FPDEV2 (plant,sampling_type,batch,sampling_datetime,status,batch_commentary) values (''ZUBNE PASTE'',''OTISCI POVR￿INA'',''000037756'',''2005-07-01'',''NE ODGOVARA'',''Ovdje se upisuje komentar o kontrolnom broju..............'')');
  188. ExecSQL;
  189. sql.clear;
  190. sql.append('select * from FPDEV2');
  191. open;
  192. AssertEquals('ZUBNE PASTE',FieldByName('plant').AsString);
  193. AssertEquals(EncodeDate(2005,07,01),FieldByName('sampling_datetime').AsDateTime);
  194. close;
  195. end;
  196. end;
  197. procedure TTestFieldTypes.TestNumeric;
  198. const
  199. testValuesCount = 13;
  200. testValues : Array[0..testValuesCount-1] of currency = (-123456.789,-10200,-10000,-1875.25,-10,-0.5,0,0.5,10,1875.25,10000,10200,123456.789);
  201. var
  202. i : byte;
  203. begin
  204. CreateTableWithFieldType(ftBCD,'NUMERIC(10,4)');
  205. TestFieldDeclaration(ftBCD,sizeof(Currency));
  206. for i := 0 to testValuesCount-1 do
  207. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (' + CurrToStrF(testValues[i],ffFixed,3) + ')');
  208. with TSQLDBConnector(DBConnector).Query do
  209. begin
  210. Open;
  211. for i := 0 to testValuesCount-1 do
  212. begin
  213. AssertEquals(testValues[i],fields[0].AsCurrency);
  214. Next;
  215. end;
  216. close;
  217. end;
  218. end;
  219. procedure TTestFieldTypes.TestString;
  220. const
  221. testValuesCount = 19;
  222. testValues : Array[0..testValuesCount-1] of string = (
  223. '',
  224. 'a',
  225. 'ab',
  226. 'abc',
  227. 'abcd',
  228. 'abcde',
  229. 'abcdef',
  230. 'abcdefg',
  231. 'abcdefgh',
  232. 'abcdefghi',
  233. 'abcdefghij',
  234. 'lMnOpQrStU',
  235. '1234567890',
  236. '_!@#$%^&*(',
  237. ')-;:/?.<>',
  238. '~`|{}- =', // note that there's no \ (backslash) since some db's uses that as escape-character
  239. ' WRaP ',
  240. 'wRaP ',
  241. ' wRAP'
  242. );
  243. var
  244. i : byte;
  245. begin
  246. CreateTableWithFieldType(ftString,'VARCHAR(10)');
  247. TestFieldDeclaration(ftString,11);
  248. for i := 0 to testValuesCount-1 do
  249. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + testValues[i] + ''')');
  250. with TSQLDBConnector(DBConnector).Query do
  251. begin
  252. Open;
  253. for i := 0 to testValuesCount-1 do
  254. begin
  255. if (SQLDbType in MySQLdbTypes) then
  256. AssertEquals(TrimRight(testValues[i]),fields[0].AsString) // MySQL automatically trims strings
  257. else
  258. AssertEquals(testValues[i],fields[0].AsString);
  259. Next;
  260. end;
  261. close;
  262. end;
  263. end;
  264. procedure TTestFieldTypes.TestUnlVarChar;
  265. const
  266. testValuesCount = 21;
  267. testValues : Array[0..testValuesCount-1] of string = (
  268. '',
  269. 'a',
  270. 'ab',
  271. 'abc',
  272. 'abcd',
  273. 'abcde',
  274. 'abcdef',
  275. 'abcdefg',
  276. 'abcdefgh',
  277. 'abcdefghi',
  278. 'abcdefghij',
  279. 'lMnOpQrStU',
  280. '1234567890',
  281. '_!@#$%^&*(',
  282. ')-;:/?.<>',
  283. '~`|{}- =',
  284. ' WRaP ',
  285. 'wRaP ',
  286. ' wRAP',
  287. '0123456789',
  288. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?' + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?'
  289. + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?' + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?'
  290. );
  291. var
  292. i : byte;
  293. begin
  294. // AssertTrue(SIgnoreAssertion,SQLDbType = postgresql); // Only postgres accept this type-definition
  295. CreateTableWithFieldType(ftString,'VARCHAR');
  296. TestFieldDeclaration(ftString,dsMaxStringSize+1);
  297. for i := 0 to testValuesCount-1 do
  298. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + testValues[i] + ''')');
  299. with TSQLDBConnector(DBConnector).Query do
  300. begin
  301. Open;
  302. for i := 0 to testValuesCount-1 do
  303. begin
  304. AssertEquals(testValues[i],fields[0].AsString);
  305. Next;
  306. end;
  307. close;
  308. end;
  309. end;
  310. procedure TTestFieldTypes.TestDate;
  311. var
  312. i : byte;
  313. begin
  314. CreateTableWithFieldType(ftDate,'DATE');
  315. TestFieldDeclaration(ftDate,8);
  316. for i := 0 to testDateValuesCount-1 do
  317. if SQLDbType=oracle then
  318. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (to_date (''' + testDateValues[i] + ''',''YYYY-MM-DD''))')
  319. else
  320. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + testDateValues[i] + ''')');
  321. // TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For debug-purposes
  322. with TSQLDBConnector(DBConnector).Query do
  323. begin
  324. Open;
  325. for i := 0 to testDateValuesCount-1 do
  326. begin
  327. AssertEquals(testDateValues[i],FormatDateTime('yyyy/mm/dd',fields[0].AsDateTime));
  328. Next;
  329. end;
  330. close;
  331. end;
  332. end;
  333. procedure TTestFieldTypes.TestChangeBlob;
  334. var s : string;
  335. begin
  336. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (ID int,FT '+FieldtypeDefinitions[ftblob]+')');
  337. TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For interbase
  338. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (ID,FT) values (1,''Test deze blob'')');
  339. with TSQLDBConnector(DBConnector).Query do
  340. begin
  341. sql.clear;
  342. sql.add('select * from FPDEV2');
  343. Open;
  344. fields[1].ProviderFlags := [pfInUpdate]; // blob niet in de where
  345. UpdateMode := upWhereAll;
  346. AssertEquals('Test deze blob',fields[1].AsString);
  347. edit;
  348. // Dat werkt niet lekker, omdat de stream vernield wordt...
  349. // fields[0].asstring := 'Deze blob is gewijzigd!';
  350. With Createblobstream(fields[1],bmwrite) do
  351. begin
  352. s := 'Deze blob is gewijzigd!';
  353. WriteBuffer(Pointer(s)^,Length(s));
  354. post;
  355. free;
  356. end;
  357. AssertEquals('Deze blob is gewijzigd!',fields[1].AsString);
  358. ApplyUpdates(0);
  359. TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For debug-purposes
  360. close;
  361. open;
  362. AssertEquals('Deze blob is gewijzigd!',fields[1].AsString);
  363. close;
  364. end;
  365. end;
  366. procedure TTestFieldTypes.TestBlobGetText;
  367. begin
  368. CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
  369. TestFieldDeclaration(ftBlob,0);
  370. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
  371. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (Null)');
  372. // TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For debug-purposes
  373. with TSQLDBConnector(DBConnector).Query do
  374. begin
  375. Open;
  376. AssertFalse(fields[0].IsNull);
  377. AssertEquals('(BLOB)',fields[0].DisplayText);
  378. AssertEquals('Test deze blob',fields[0].AsString);
  379. Next;
  380. AssertTrue(fields[0].IsNull);
  381. AssertEquals('(blob)',fields[0].Text);
  382. AssertEquals('',fields[0].AsString);
  383. close;
  384. end;
  385. end;
  386. procedure TTestFieldTypes.TestSetBlobAsStringParam;
  387. var
  388. i : byte;
  389. ASQL : TSQLQuery;
  390. begin
  391. CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
  392. // CreateTableWithFieldType(ftBlob,'TEXT');
  393. TestFieldDeclaration(ftBlob,0);
  394. ASQL := DBConnector.GetNDataset(True,1) as tsqlquery;
  395. with ASql do
  396. begin
  397. sql.Text := 'insert into FPDEV2 (FT) values (:BlobParam)';
  398. Params.ParamByName('blobParam').AsString := 'Test deze BLob';
  399. ExecSQL;
  400. end;
  401. with TSQLDBConnector(DBConnector).Query do
  402. begin
  403. Open;
  404. if not eof then
  405. AssertEquals('Test deze BLob',fields[0].AsString);
  406. close;
  407. end;
  408. end;
  409. procedure TTestFieldTypes.TestBlob;
  410. var
  411. i : byte;
  412. begin
  413. CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
  414. TestFieldDeclaration(ftBlob,0);
  415. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
  416. // TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For debug-purposes
  417. with TSQLDBConnector(DBConnector).Query do
  418. begin
  419. Open;
  420. AssertEquals('Test deze blob',fields[0].AsString);
  421. close;
  422. end;
  423. end;
  424. procedure TTestFieldTypes.TestDateTime;
  425. const
  426. testValuesCount = 31;
  427. testValues : Array[0..testValuesCount-1] of string = (
  428. '2000-01-01',
  429. '1999-12-31',
  430. '2004-02-29',
  431. '2004-03-01',
  432. '1991-02-28',
  433. '1991-03-01',
  434. '1977-09-29',
  435. '2000-01-01 10:00:00',
  436. '2000-01-01 23:59:59',
  437. '1994-03-06 11:54:30',
  438. '2040-10-16', // MySQL 4.0 doesn't support datetimes before 1970 or after 2038
  439. '1400-02-03 12:21:53',
  440. '0354-11-20 21:25:15',
  441. '1333-02-03 21:44:21',
  442. '1800-03-30',
  443. '1650-05-10',
  444. '1754-06-04',
  445. '0904-04-12',
  446. '0199-07-09',
  447. '0001-01-01',
  448. '1899-12-29',
  449. '1899-12-30',
  450. '1899-12-31',
  451. '1900-01-01',
  452. '1899-12-30 18:00:51',
  453. '1899-12-30 04:00:51',
  454. '1899-12-29 04:00:51',
  455. '1899-12-29 18:00:51',
  456. '1903-04-02 01:04:02',
  457. '1815-09-24 03:47:22',
  458. '2100-01-01 01:01:01'
  459. );
  460. var
  461. i, corrTestValueCount : byte;
  462. begin
  463. CreateTableWithFieldType(ftDateTime,FieldtypeDefinitions[ftDateTime]);
  464. TestFieldDeclaration(ftDateTime,8);
  465. if SQLDbType=mysql40 then corrTestValueCount := testValuesCount-21
  466. else corrTestValueCount := testValuesCount;
  467. for i := 0 to corrTestValueCount-1 do
  468. if SQLDbType=oracle then
  469. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (to_date (''' + testValues[i] + ''',''YYYY-MM-DD HH24:MI:SS''))')
  470. else
  471. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + testValues[i] + ''')');
  472. with TSQLDBConnector(DBConnector).Query do
  473. begin
  474. Open;
  475. for i := 0 to corrTestValueCount-1 do
  476. begin
  477. if length(testValues[i]) < 12 then
  478. AssertEquals(testValues[i],FormatDateTime('yyyy/mm/dd',fields[0].AsDateTime))
  479. else
  480. AssertEquals(testValues[i],FormatDateTime('yyyy/mm/dd hh:mm:ss',fields[0].AsDateTime));
  481. Next;
  482. end;
  483. close;
  484. end;
  485. end;
  486. procedure TTestFieldTypes.TestFloat;
  487. const
  488. testValuesCount = 21;
  489. testValues : Array[0..testValuesCount-1] of double = (-maxSmallint-1,-maxSmallint,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint+1,0.123456,-0.123456,4.35,12.434E7,9.876e-5,123.45678);
  490. var
  491. i : byte;
  492. begin
  493. CreateTableWithFieldType(ftFloat,'FLOAT');
  494. TestFieldDeclaration(ftFloat,sizeof(double));
  495. for i := 0 to testValuesCount-1 do
  496. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (' + floattostr(testValues[i]) + ')');
  497. with TSQLDBConnector(DBConnector).Query do
  498. begin
  499. Open;
  500. for i := 0 to testValuesCount-1 do
  501. begin
  502. AssertEquals(testValues[i],fields[0].AsFloat);
  503. Next;
  504. end;
  505. close;
  506. end;
  507. end;
  508. procedure TTestFieldTypes.TestNullValues;
  509. begin
  510. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (FIELD1 INT, FIELD2 INT)');
  511. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  512. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  513. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FIELD1) values (1)');
  514. with TSQLDBConnector(DBConnector).Query do
  515. begin
  516. sql.clear;
  517. sql.append('select * from FPDEV2');
  518. open;
  519. AssertEquals(1,FieldByName('FIELD1').AsInteger);
  520. AssertTrue('Null-values test failed',FieldByName('FIELD2').IsNull);
  521. close;
  522. end;
  523. end;
  524. procedure TTestFieldTypes.TestParamQuery;
  525. begin
  526. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (FIELD1 INT, FIELD2 INT, FIELD3 INT, DECOY VARCHAR(30))');
  527. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  528. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  529. with TSQLDBConnector(DBConnector).Query do
  530. begin
  531. sql.clear;
  532. sql.append('insert into FPDEV2 (field1) values (:field1)');
  533. Params.ParamByName('field1').AsInteger := 1;
  534. ExecSQL;
  535. sql.clear;
  536. sql.append('insert into FPDEV2 (field1,field2,decoy) values (:field1,:field2,''decoytest'')');
  537. Params.ParamByName('field1').AsInteger := 2;
  538. Params.ParamByName('field2').DataType := ftInteger;
  539. Params.ParamByName('field2').Value := Null;
  540. ExecSQL;
  541. sql.clear;
  542. sql.append('insert into FPDEV2 (field1,field2,field3) values (:field1,:field2,:field3)');
  543. Params.ParamByName('field1').AsInteger := 3;
  544. Params.ParamByName('field2').AsInteger := 2;
  545. Params.ParamByName('field3').AsInteger := 3;
  546. ExecSQL;
  547. sql.clear;
  548. sql.append('insert into FPDEV2 (field1,field2,field3,decoy) values (:field1,:field2,:field3,'':decoy ::test $decoy2 $$2'')');
  549. Params.ParamByName('field1').AsInteger := 4;
  550. Params.ParamByName('field2').AsInteger := 2;
  551. Params.ParamByName('field3').AsInteger := 3;
  552. ExecSQL;
  553. sql.clear;
  554. sql.append('insert into FPDEV2 (field1,field2,field3) values (:field1,:field2,:field1)');
  555. Params.ParamByName('field1').AsInteger := 5;
  556. Params.ParamByName('field2').AsInteger := 2;
  557. ExecSQL;
  558. sql.clear;
  559. sql.append('select * from FPDEV2 order by FIELD1');
  560. open;
  561. AssertEquals(1,FieldByName('FIELD1').asinteger);
  562. AssertTrue(FieldByName('FIELD2').IsNull);
  563. AssertTrue(FieldByName('FIELD3').IsNull);
  564. AssertTrue(FieldByName('DECOY').IsNull);
  565. next;
  566. AssertEquals(2,FieldByName('FIELD1').asinteger);
  567. AssertTrue(FieldByName('FIELD2').IsNull);
  568. AssertTrue(FieldByName('FIELD3').IsNull);
  569. AssertEquals('decoytest',FieldByName('DECOY').AsString);
  570. next;
  571. AssertEquals(3,FieldByName('FIELD1').asinteger);
  572. AssertEquals(2,FieldByName('FIELD2').asinteger);
  573. AssertEquals(3,FieldByName('FIELD3').asinteger);
  574. AssertTrue(FieldByName('DECOY').IsNull);
  575. next;
  576. AssertEquals(4,FieldByName('FIELD1').asinteger);
  577. AssertEquals(2,FieldByName('FIELD2').asinteger);
  578. AssertEquals(3,FieldByName('FIELD3').asinteger);
  579. AssertEquals(':decoy ::test $decoy2 $$2',FieldByName('DECOY').AsString);
  580. next;
  581. AssertEquals(5,FieldByName('FIELD1').asinteger);
  582. AssertEquals(2,FieldByName('FIELD2').asinteger);
  583. AssertEquals(5,FieldByName('FIELD3').asinteger);
  584. AssertTrue(FieldByName('DECOY').IsNull);
  585. close;
  586. end;
  587. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  588. end;
  589. procedure TTestFieldTypes.TestIntParamQuery;
  590. begin
  591. TestXXParamQuery(ftInteger,'INT',testIntValuesCount);
  592. end;
  593. procedure TTestFieldTypes.TestFloatParamQuery;
  594. begin
  595. TestXXParamQuery(ftFloat,'FLOAT',testFloatValuesCount);
  596. end;
  597. procedure TTestFieldTypes.TestStringParamQuery;
  598. begin
  599. TestXXParamQuery(ftString,'VARCHAR(10)',testStringValuesCount);
  600. end;
  601. procedure TTestFieldTypes.TestDateParamQuery;
  602. begin
  603. TestXXParamQuery(ftDate,'DATE',testDateValuesCount);
  604. end;
  605. procedure TTestFieldTypes.TestXXParamQuery(ADatatype : TFieldType; ASQLTypeDecl : string; testValuescount : integer; Cross : boolean = false);
  606. var i : integer;
  607. begin
  608. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (ID INT, FIELD1 '+ASQLTypeDecl+')');
  609. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  610. if SQLDbType=interbase then TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  611. with TSQLDBConnector(DBConnector).Query do
  612. begin
  613. PacketRecords := -1;
  614. sql.clear;
  615. sql.append('insert into FPDEV2 (ID,FIELD1) values (:id,:field1)');
  616. ShortDateFormat := 'yyyy-mm-dd';
  617. for i := 0 to testValuesCount -1 do
  618. begin
  619. Params.ParamByName('id').AsInteger := i;
  620. case ADataType of
  621. ftInteger: Params.ParamByName('field1').asinteger := testIntValues[i];
  622. ftFloat : Params.ParamByName('field1').AsFloat := testFloatValues[i];
  623. ftString : Params.ParamByName('field1').AsString := testStringValues[i];
  624. ftDate : if cross then
  625. Params.ParamByName('field1').AsString:= testDateValues[i]
  626. else
  627. Params.ParamByName('field1').AsDateTime:= StrToDate(testDateValues[i]);
  628. else
  629. AssertTrue('no test for paramtype available',False);
  630. end;
  631. ExecSQL;
  632. end;
  633. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  634. sql.clear;
  635. sql.append('select * from FPDEV2 order by ID');
  636. open;
  637. for i := 0 to testValuesCount -1 do
  638. begin
  639. AssertEquals(i,FieldByName('ID').AsInteger);
  640. case ADataType of
  641. ftInteger: AssertEquals(testIntValues[i],FieldByName('FIELD1').AsInteger);
  642. ftFloat : AssertEquals(testFloatValues[i],FieldByName('FIELD1').AsFloat);
  643. ftString : AssertEquals(testStringValues[i],FieldByName('FIELD1').AsString);
  644. ftdate : AssertEquals(testDateValues[i],FormatDateTime('yyyy/mm/dd',FieldByName('FIELD1').AsDateTime));
  645. else
  646. AssertTrue('no test for paramtype available',False);
  647. end;
  648. Next;
  649. end;
  650. close;
  651. end;
  652. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  653. end;
  654. procedure TTestFieldTypes.TestAggregates;
  655. begin
  656. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (FIELD1 INT, FIELD2 INT)');
  657. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  658. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  659. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 values (1,1)');
  660. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 values (2,3)');
  661. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 values (3,4)');
  662. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 values (4,4)');
  663. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  664. with TSQLDBConnector(DBConnector).Query do
  665. begin
  666. sql.clear;
  667. sql.append('select count(*) from FPDEV2');
  668. open;
  669. AssertEquals(4,Fields[0].AsInteger);
  670. close;
  671. sql.clear;
  672. sql.append('select sum(FIELD1) from FPDEV2');
  673. open;
  674. AssertEquals(10,Fields[0].AsInteger);
  675. close;
  676. sql.clear;
  677. sql.append('select avg(FIELD2) from FPDEV2');
  678. open;
  679. AssertEquals(3,Fields[0].AsInteger);
  680. close;
  681. end;
  682. end;
  683. procedure TTestFieldTypes.CreateTableWithFieldType(ADatatype: TFieldType;
  684. ASQLTypeDecl: string);
  685. begin
  686. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (FT ' +ASQLTypeDecl+ ')');
  687. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  688. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  689. end;
  690. procedure TTestFieldTypes.TestFieldDeclaration(ADatatype: TFieldType;
  691. ADataSize: integer);
  692. begin
  693. with TSQLDBConnector(DBConnector).Query do
  694. begin
  695. SQL.Clear;
  696. SQL.Add('select * from FPDEV2');
  697. Open;
  698. AssertEquals(1,FieldCount);
  699. AssertTrue(CompareText('FT',fields[0].FieldName)=0);
  700. AssertEquals(ADataSize,fields[0].DataSize);
  701. AssertTrue(ADatatype=fields[0].DataType);
  702. Close;
  703. end;
  704. end;
  705. procedure TTestFieldTypes.SetUp;
  706. begin
  707. InitialiseDBConnector;
  708. end;
  709. procedure TTestFieldTypes.TearDown;
  710. begin
  711. if assigned(DBConnector) then
  712. TSQLDBConnector(DBConnector).Transaction.Rollback;
  713. FreeAndNil(DBConnector);
  714. end;
  715. procedure TTestFieldTypes.RunTest;
  716. begin
  717. // if (SQLDbType in TSQLDBTypes) then
  718. inherited RunTest;
  719. end;
  720. procedure TTestFieldTypes.TestInsertLargeStrFields;
  721. begin
  722. with TSQLDBConnector(DBConnector) do
  723. begin
  724. Connection.ExecuteDirect('create table FPDEV2 ( ' +
  725. ' ID INT NOT NULL , ' +
  726. ' NAME VARCHAR(16000), ' +
  727. ' PRIMARY KEY (ID) ' +
  728. ') ');
  729. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  730. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  731. Query.SQL.Text := 'insert into FPDEV2(ID,NAME) values (1,''test1'')';
  732. Query.ExecSQL;
  733. query.sql.Text:='select * from FPDEV2';
  734. Query.Open;
  735. AssertEquals(query.FieldByName('NAME').AsString,'test1');
  736. Query.insert;
  737. query.fields[1].AsString:='11';
  738. query.Close;
  739. end;
  740. end;
  741. procedure TTestFieldTypes.TestRowsAffected;
  742. begin
  743. with TSQLDBConnector(DBConnector) do
  744. begin
  745. Connection.ExecuteDirect('create table FPDEV2 ( ' +
  746. ' ID INT NOT NULL , ' +
  747. ' NAME VARCHAR(250), ' +
  748. ' PRIMARY KEY (ID) ' +
  749. ') ');
  750. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  751. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  752. Query.SQL.Text := 'insert into FPDEV2(ID,NAME) values (1,''test1'')';
  753. Query.ExecSQL;
  754. AssertEquals(1,query.RowsAffected);
  755. Query.SQL.Text := 'insert into FPDEV2(ID,NAME) values (2,''test2'')';
  756. Query.ExecSQL;
  757. AssertEquals(1,query.RowsAffected);
  758. Query.SQL.Text := 'update FPDEV2 set NAME=''NewTest''';
  759. Query.ExecSQL;
  760. AssertEquals(2,query.RowsAffected);
  761. Query.SQL.Text := 'delete from FPDEV2';
  762. Query.ExecSQL;
  763. AssertEquals(2,query.RowsAffected);
  764. Query.SQL.Text := 'delete from FPDEV2';
  765. Query.ExecSQL;
  766. AssertEquals(0,query.RowsAffected);
  767. end;
  768. end;
  769. procedure TTestFieldTypes.TestStringsReplace;
  770. begin
  771. AssertEquals('dit is een string',StringsReplace('dit was een string',['was'],['is'],[]));
  772. AssertEquals('dit is een string was een string',StringsReplace('dit was een string was een string',['was'],['is'],[]));
  773. AssertEquals('dit is een string is een string',StringsReplace('dit was een string was een string',['was'],['is'],[rfReplaceAll]));
  774. AssertEquals('dit is een char is een char',StringsReplace('dit was een string was een string',['was','string'],['is','char'],[rfReplaceAll]));
  775. AssertEquals('dit is een string was een string',StringsReplace('dit was een string was een string',['string','was'],['char','is'],[]));
  776. AssertEquals('dit is een char is een strin',StringsReplace('dit was een string was een strin',['string','was'],['char','is'],[rfReplaceAll]));
  777. AssertEquals('dit Was een char is een char',StringsReplace('dit Was een string was een string',['was','string'],['is','char'],[rfReplaceAll]));
  778. AssertEquals('dit wAs een char is een char',StringsReplace('dit wAs een string was een string',['was','string'],['is','char'],[rfReplaceAll]));
  779. AssertEquals('dit is een char is een char',StringsReplace('dit Was een sTring was een string',['was','string'],['is','char'],[rfReplaceAll,rfIgnoreCase]));
  780. AssertEquals('dit is een char is een char',StringsReplace('dit wAs een STRING was een string',['was','string'],['is','char'],[rfReplaceAll,rfIgnoreCase]));
  781. AssertEquals('dit was een si was een sa',StringsReplace('dit was een string was een straat',['straat','string'],['sa','si'],[rfReplaceAll]));
  782. AssertEquals('dit was een si was een sa',StringsReplace('dit was een string was een straat',['string','straat'],['si','sa'],[rfReplaceAll]));
  783. AssertEquals('dit was een sing was een saat',StringsReplace('dit was een string was een straat',['str','string'],['s','si'],[rfReplaceAll]));
  784. AssertEquals('dit was een si was een saat',StringsReplace('dit was een string was een straat',['string','str'],['si','s'],[rfReplaceAll]));
  785. AssertEquals('dit was een string was een string',StringsReplace('dit was een string was een string',[''],['is'],[rfReplaceAll]));
  786. AssertEquals('dit een string een string',StringsReplace('dit was een string was een string',['was'],[''],[rfReplaceAll]));
  787. end;
  788. procedure TTestFieldTypes.TestCircularParams;
  789. begin
  790. with TSQLDBConnector(dbconnector) do
  791. begin
  792. Connection.ExecuteDirect('create table FPDEV2 (id1 int, id2 int,vchar varchar(10))');
  793. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  794. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  795. Query.sql.Text := 'insert into FPDEV2 values(:id1,:id2,:vchar)';
  796. query.params[0].asinteger := 1;
  797. query.params[1].asinteger := 1;
  798. query.params[2].asstring := '$1 :id2 $';
  799. query.ExecSQL;
  800. query.sql.text := 'select * from FPDEV2';
  801. query.open;
  802. AssertEquals(1,query.fields[0].asinteger);
  803. AssertEquals(1,query.fields[1].asinteger);
  804. AssertEquals('$1 :id2 $',query.fields[2].AsString);
  805. query.close;
  806. end;
  807. end;
  808. procedure TTestFieldTypes.Test11Params;
  809. var i : integer;
  810. begin
  811. with TSQLDBConnector(dbconnector) do
  812. begin
  813. Connection.ExecuteDirect('create table FPDEV2 (id1 int, id2 int, id3 int, id4 int,id5 int,id6 int,id7 int,id8 int, id9 int, id10 int, id11 int)');
  814. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  815. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  816. Query.sql.Text := 'insert into FPDEV2 values(:id1,:id2,:id3,:id4,:id5,:id6,:id7,:id8,:id9,:id10,:id11)';
  817. for i := 0 to 10 do
  818. query.params[i].asinteger := 1;
  819. query.ExecSQL;
  820. query.sql.text := 'select * from FPDEV2';
  821. query.open;
  822. for i := 0 to 10 do
  823. AssertEquals(1,query.fields[i].asinteger);
  824. query.close;
  825. end;
  826. end;
  827. procedure TTestFieldTypes.TestBug9744;
  828. var i : integer;
  829. begin
  830. AssertTrue('This test does not apply to Interbase/Firebird, since it has no double field-type',SQLDbType<>interbase);
  831. with TSQLDBConnector(DBConnector) do
  832. begin
  833. try
  834. Connection.ExecuteDirect('create table TTTOBJ ( ' +
  835. ' ID INT NOT NULL, ' +
  836. ' NAME VARCHAR(250), ' +
  837. ' PRIMARY KEY (ID) ' +
  838. ') ');
  839. Connection.ExecuteDirect('create table TTTXY ( ' +
  840. ' ID INT NOT NULL, ' +
  841. ' NP INT NOT NULL, ' +
  842. ' X DOUBLE, ' +
  843. ' Y DOUBLE, ' +
  844. ' PRIMARY KEY (ID,NP) ' +
  845. ') ');
  846. for i := 0 to 7 do
  847. begin
  848. connection.ExecuteDirect('insert into TTTOBJ(ID,NAME) values ('+inttostr(i)+',''A'+inttostr(i)+''')');
  849. connection.ExecuteDirect('insert into TTTXY(ID,NP,X,Y) values ('+inttostr(i)+',1,1,1)');
  850. connection.ExecuteDirect('insert into TTTXY(ID,NP,X,Y) values ('+inttostr(i)+',2,2,2)');
  851. end;
  852. Query.SQL.Text := 'select OBJ.ID, OBJ.NAME, count(XY.NP) as NPF from TTTOBJ as OBJ, TTTXY as XY where (OBJ.ID=XY.ID) group by OBJ.ID';
  853. query.Prepare;
  854. query.open;
  855. query.close;
  856. finally
  857. Connection.ExecuteDirect('drop table TTTXY');
  858. Connection.ExecuteDirect('drop table TTTOBJ');
  859. end
  860. end;
  861. end;
  862. procedure TTestFieldTypes.TestCrossStringDateParam;
  863. begin
  864. TestXXParamQuery(ftDate,'DATE',testDateValuesCount,True);
  865. end;
  866. procedure TTestFieldTypes.TestGetFieldNames;
  867. var FieldNames : TStringList;
  868. begin
  869. with TSQLDBConnector(DBConnector) do
  870. begin
  871. FieldNames := TStringList.Create;
  872. try
  873. Connection.GetFieldNames('FpDEv',FieldNames);
  874. AssertEquals(2,FieldNames.Count);
  875. AssertEquals('ID',UpperCase(FieldNames[0]));
  876. AssertEquals('NAME',UpperCase(FieldNames[1]));
  877. finally
  878. FieldNames.Free;
  879. end;
  880. end;
  881. end;
  882. procedure TTestFieldTypes.TestUpdateIndexDefs;
  883. var ds : TSQLQuery;
  884. begin
  885. ds := DBConnector.GetNDataset(1) as TSQLQuery;
  886. ds.Prepare;
  887. ds.IndexDefs.Update;
  888. AssertEquals(1,ds.IndexDefs.count);
  889. AssertEquals('ID',ds.indexdefs[0].Fields);
  890. Asserttrue(ds.indexdefs[0].Options=[ixPrimary,ixUnique]);
  891. ds.IndexDefs.Update;
  892. AssertEquals(1,ds.IndexDefs.count);
  893. AssertEquals('ID',ds.indexdefs[0].Fields);
  894. Asserttrue(ds.indexdefs[0].Options=[ixPrimary,ixUnique]);
  895. end;
  896. procedure TTestFieldTypes.TestSetBlobAsMemoParam;
  897. var
  898. i : byte;
  899. ASQL : TSQLQuery;
  900. begin
  901. CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
  902. TestFieldDeclaration(ftBlob,0);
  903. ASQL := DBConnector.GetNDataset(True,1) as tsqlquery;
  904. with ASql do
  905. begin
  906. sql.Text := 'insert into FPDEV2 (FT) values (:BlobParam)';
  907. Params.ParamByName('blobParam').AsMemo := 'Test deze BLob';
  908. ExecSQL;
  909. end;
  910. with TSQLDBConnector(DBConnector).Query do
  911. begin
  912. Open;
  913. if not eof then
  914. AssertEquals('Test deze BLob',fields[0].AsString);
  915. close;
  916. end;
  917. end;
  918. procedure TTestFieldTypes.TestTemporaryTable;
  919. begin
  920. with TSQLDBConnector(DBConnector).Query do
  921. begin
  922. SQL.Clear;
  923. SQL.Add('CREATE TEMPORARY TABLE TEMP1 (id int)');
  924. ExecSQL;
  925. SQL.Text := 'INSERT INTO TEMP1(id) values (5)';
  926. ExecSQL;
  927. SQL.Text := 'SELECT * FROM TEMP1';
  928. Open;
  929. AssertEquals(5,fields[0].AsInteger);
  930. Close;
  931. end;
  932. end;
  933. procedure TTestFieldTypes.TestGetIndexDefs;
  934. var ds : TSQLQuery;
  935. inddefs : TIndexDefs;
  936. begin
  937. ds := DBConnector.GetNDataset(1) as TSQLQuery;
  938. ds.Open;
  939. AssertEquals(1,ds.IndexDefs.count);
  940. inddefs := HackedDataset(ds).GetIndexDefs(ds.IndexDefs,[ixPrimary]);
  941. AssertEquals(1,inddefs.count);
  942. AssertEquals('ID',inddefs[0].Fields);
  943. Asserttrue(inddefs[0].Options=[ixPrimary,ixUnique]);
  944. inddefs.Free;
  945. inddefs := HackedDataset(ds).GetIndexDefs(ds.IndexDefs,[ixPrimary,ixUnique]);
  946. AssertEquals(1,inddefs.count);
  947. AssertEquals('ID',inddefs[0].Fields);
  948. Asserttrue(inddefs[0].Options=[ixPrimary,ixUnique]);
  949. inddefs.Free;
  950. inddefs := HackedDataset(ds).GetIndexDefs(ds.IndexDefs,[ixDescending]);
  951. AssertEquals(0,inddefs.count);
  952. inddefs.Free;
  953. end;
  954. procedure TTestFieldTypes.TestDblQuoteEscComments;
  955. begin
  956. with TSQLDBConnector(DBConnector).Query do
  957. begin
  958. SQL.Clear;
  959. SQL.Add('select * from FPDEV where name=''test '''' and :ThisIsNotAParameter ''');
  960. open;
  961. close;
  962. end;
  963. end;
  964. procedure TTestFieldTypes.TestParametersAndDates;
  965. // See bug 7205
  966. begin
  967. with TSQLDBConnector(DBConnector).Query do
  968. begin
  969. SQL.Clear;
  970. sql.add('select now()::date as current_date where 1=1');
  971. open;
  972. first;
  973. writeln(fields[0].asstring); // return the correct date
  974. close;
  975. sql.clear;
  976. sql.add('select now()::date as current_date where cast(1 as integer) = :PARAM1');
  977. params.parambyname('PARAM1').asinteger:= 1;
  978. open;
  979. first;
  980. writeln(fields[0].asstring); // return invalid date
  981. close;
  982. end
  983. end;
  984. procedure TTestFieldTypes.TestExceptOnsecClose;
  985. begin
  986. with TSQLDBConnector(DBConnector).Query do
  987. begin
  988. SQL.Clear;
  989. SQL.Add('select * from FPDEV');
  990. Open;
  991. close;
  992. SQL.Clear;
  993. SQL.Add('select blaise from FPDEV');
  994. {$IFDEF FPC}
  995. // AssertException(EIBDatabaseError,@Open);
  996. {$ELSE}
  997. // AssertException(EIBDatabaseError,Open);
  998. {$ENDIF}
  999. Open;
  1000. Close;
  1001. end;
  1002. end;
  1003. initialization
  1004. if uppercase(dbconnectorname)='SQL' then RegisterTest(TTestFieldTypes);
  1005. end.