testfieldtypes.pas 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. unit TestFieldTypes;
  2. {$mode objfpc}{$H+}
  3. {$modeswitch nestedprocvars}
  4. interface
  5. uses
  6. Classes, SysUtils, fpcunit, testutils, testregistry,
  7. db;
  8. type
  9. TParamProc = procedure(AParam:TParam; i : integer);
  10. TFieldProc = procedure(AField:TField; i : integer);
  11. TGetSQLTextProc = function(const i: integer) : string; { is nested;}
  12. TCheckFieldValueProc = procedure(AField:TField; i : integer) is nested;
  13. { TTestFieldTypes }
  14. TTestFieldTypes= class(TTestCase)
  15. private
  16. procedure CreateTableWithFieldType(ADatatype : TFieldType; ASQLTypeDecl : string);
  17. procedure TestFieldDeclaration(ADatatype: TFieldType; ADataSize: integer);
  18. procedure TestSQLFieldType(ADatatype: TFieldType; ASQLTypeDecl: string;
  19. ADataSize: integer; AGetSQLTextProc: TGetSQLTextProc;
  20. ACheckFieldValueProc: TCheckFieldValueProc);
  21. procedure TestXXParamQuery(ADatatype : TFieldType; ASQLTypeDecl : string; testValuescount : integer; Cross : boolean = false);
  22. procedure TestSetBlobAsParam(asWhat : integer);
  23. protected
  24. procedure SetUp; override;
  25. procedure TearDown; override;
  26. procedure RunTest; override;
  27. published
  28. procedure TestEmptyUpdateQuery; // bug 13654
  29. procedure TestParseJoins; // bug 10148
  30. procedure TestDoubleFieldNames; // bug 8457
  31. procedure TestParseUnion; // bug 8442
  32. procedure TestInsertLargeStrFields; // bug 9600
  33. procedure TestNumericNames; // Bug9661
  34. procedure TestApplyUpdFieldnames; // Bug 12275;
  35. procedure TestServerFilter; // bug 15456
  36. procedure Test11Params;
  37. procedure TestRowsAffected; // bug 9758
  38. procedure TestLocateNull;
  39. procedure TestLocateOnMoreRecords;
  40. procedure TestStringsReplace;
  41. procedure TestCircularParams;
  42. procedure TestBug9744;
  43. procedure TestCrossStringDateParam;
  44. procedure TestGetFieldNames;
  45. procedure TestUpdateIndexDefs;
  46. procedure TestMultipleFieldPKIndexDefs;
  47. procedure TestSetBlobAsMemoParam;
  48. procedure TestSetBlobAsBlobParam;
  49. procedure TestSetBlobAsStringParam;
  50. procedure TestNonNullableParams;
  51. procedure TestGetIndexDefs;
  52. procedure TestDblQuoteEscComments;
  53. procedure TestpfInUpdateFlag; // bug 7565
  54. procedure TestInt;
  55. procedure TestScript;
  56. procedure TestInsertReturningQuery;
  57. procedure TestOpenStoredProc;
  58. procedure TestOpenSpecialStatements;
  59. procedure TestTemporaryTable;
  60. procedure TestRefresh;
  61. procedure TestParametersAndDates;
  62. procedure TestExceptOnsecClose;
  63. procedure TestErrorOnEmptyStatement;
  64. procedure TestBlob;
  65. procedure TestChangeBlob;
  66. procedure TestBlobGetText;
  67. procedure TestBlobSize;
  68. procedure TestLargeRecordSize;
  69. procedure TestNumeric;
  70. procedure TestFloat;
  71. procedure TestDateTime; // bug 6925
  72. procedure TestString;
  73. procedure TestUnlVarChar;
  74. procedure TestDate;
  75. procedure TestNullValues;
  76. procedure TestParamQuery;
  77. procedure TestStringParamQuery;
  78. procedure TestFixedStringParamQuery;
  79. procedure TestDateParamQuery;
  80. procedure TestIntParamQuery;
  81. procedure TestTimeParamQuery;
  82. procedure TestDateTimeParamQuery;
  83. procedure TestFmtBCDParamQuery;
  84. procedure TestFloatParamQuery;
  85. procedure TestBCDParamQuery;
  86. procedure TestBytesParamQuery;
  87. procedure TestVarBytesParamQuery;
  88. procedure TestBooleanParamQuery;
  89. procedure TestAggregates;
  90. procedure TestStringLargerThen8192;
  91. procedure TestQueryAfterReconnect; // bug 16438
  92. // SchemaType tests
  93. procedure TestTableNames;
  94. procedure TestFieldNames;
  95. procedure TestClearUpdateableStatus;
  96. procedure TestReadOnlyParseSQL; // bug 9254
  97. procedure TestGetTables;
  98. // Test SQL-field type recognition
  99. procedure TestSQLClob;
  100. procedure TestSQLLargeint;
  101. procedure TestSQLInterval;
  102. procedure TestSQLIdentity;
  103. end;
  104. implementation
  105. uses sqldbtoolsunit,toolsunit, variants, sqldb, bufdataset, strutils, dbconst, FmtBCD;
  106. Type HackedDataset = class(TDataset);
  107. const
  108. testFloatValuesCount = 21;
  109. 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);
  110. testBCDValuesCount = 10;
  111. testBCDValues : Array[0..testBCDValuesCount-1] of currency = (-100,54.53,1.2345,123.5345,0,1,-1,0,1.42,1324.4324);
  112. testIntValuesCount = 17;
  113. 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);
  114. testDateValuesCount = 18;
  115. testDateValues : Array[0..testDateValuesCount-1] of string = (
  116. '2000-01-01',
  117. '1999-12-31',
  118. '2004-02-29',
  119. '2004-03-01',
  120. '1991-02-28',
  121. '1991-03-01',
  122. '2040-10-16',
  123. '1977-09-29',
  124. '1899-12-29',
  125. '1899-12-30',
  126. '1899-12-31',
  127. '1900-01-01',
  128. '1800-03-30',
  129. '1754-06-04',
  130. '1650-05-10',
  131. '0904-04-12',
  132. '0199-07-09',
  133. '0001-01-01'
  134. );
  135. testBytesValuesCount = 5;
  136. testVarBytesValuesCount = 8;
  137. testBytesValues : Array[0..testVarBytesValuesCount-1] of shortstring = (
  138. #1#0#1#0#1, #0#0#1#0#1, #0''''#13#0#1, '\'#0'"\'#13, #13#13#0#10#10,
  139. '', #0, #0#1#2#3#4#5#6#7#8#9
  140. );
  141. STestNotApplicable = 'This test does not apply to this sqldb-connection type';
  142. procedure TTestFieldTypes.TestpfInUpdateFlag;
  143. var ds : TCustomBufDataset;
  144. AFld1, AFld2, AFld3 : Tfield;
  145. begin
  146. ds := (DBConnector.GetNDataset(True,5) as TCustomBufDataset);
  147. with ds do
  148. begin
  149. AFld1 := TIntegerField.Create(ds);
  150. AFld1.FieldName := 'ID';
  151. AFld1.DataSet := ds;
  152. AFld1.ProviderFlags := AFld1.ProviderFlags + [pfInKey];
  153. AFld2 := TStringField.Create(ds);
  154. AFld2.FieldName := 'NAME';
  155. AFld2.DataSet := ds;
  156. AFld3 := TIntegerField.Create(ds);
  157. AFld3.FieldName := 'CALCFLD';
  158. AFld3.DataSet := ds;
  159. Afld3.FieldKind := fkCalculated;
  160. AFld3.ProviderFlags := [];
  161. Open;
  162. Edit;
  163. FieldByName('ID').AsInteger := 254;
  164. Post;
  165. ApplyUpdates;
  166. Append;
  167. FieldByName('ID').AsInteger := 255;
  168. Post;
  169. ApplyUpdates;
  170. Close;
  171. AFld1.Free;
  172. AFld2.Free;
  173. AFld3.Free;
  174. end;
  175. end;
  176. procedure TTestFieldTypes.TestScript;
  177. var Ascript : TSQLScript;
  178. begin
  179. Ascript := tsqlscript.create(nil);
  180. try
  181. with Ascript do
  182. begin
  183. DataBase := TSQLDBConnector(DBConnector).Connection;
  184. transaction := TSQLDBConnector(DBConnector).Transaction;
  185. script.clear;
  186. script.append('create table a (id int);');
  187. script.append('create table b (id int);');
  188. ExecuteScript;
  189. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  190. if SQLConnType=interbase then TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  191. end;
  192. finally
  193. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('drop table a');
  194. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('drop table b');
  195. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  196. if SQLConnType=interbase then TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  197. end;
  198. end;
  199. procedure TTestFieldTypes.TestInt;
  200. var
  201. i : byte;
  202. begin
  203. CreateTableWithFieldType(ftInteger,'INT');
  204. TestFieldDeclaration(ftInteger,4);
  205. for i := 0 to testIntValuesCount-1 do
  206. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (' + inttostr(testIntValues[i]) + ')');
  207. with TSQLDBConnector(DBConnector).Query do
  208. begin
  209. Open;
  210. for i := 0 to testIntValuesCount-1 do
  211. begin
  212. AssertEquals(testIntValues[i],fields[0].AsInteger);
  213. Next;
  214. end;
  215. close;
  216. end;
  217. end;
  218. procedure TTestFieldTypes.TestLargeRecordSize;
  219. begin
  220. 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))');
  221. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  222. if UpperCase(dbconnectorparams)='INTERBASE' then TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  223. with TSQLDBConnector(DBConnector).Query do
  224. begin
  225. sql.clear;
  226. 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..............'')');
  227. ExecSQL;
  228. sql.clear;
  229. sql.append('select * from FPDEV2');
  230. open;
  231. AssertEquals('ZUBNE PASTE',FieldByName('plant').AsString);
  232. AssertEquals(EncodeDate(2005,07,01),FieldByName('sampling_datetime').AsDateTime);
  233. close;
  234. end;
  235. end;
  236. procedure TTestFieldTypes.TestNumeric;
  237. const
  238. testValuesCount = 13;
  239. testValues4 : Array[0..testValuesCount-1] of currency = (-99.99,-12.34,-10.2,-10,-0.5,-0.01,0,0.01,0.5,10,10.2,12.34,99.99);
  240. testValues9 : Array[0..testValuesCount-1] of currency = (-123456.789,-10000,-1875.25,-10,-0.5,-0.001,0,0.001,0.5,10,1875.25,10000,123456.789);
  241. FieldTypes: array [0..7] of TFieldType = (ftBCD, ftBCD, ftBCD, ftFmtBCD, ftLargeInt, ftFmtBCD, ftFmtBCD, ftFmtBCD);
  242. FieldSizes: array [0..7] of integer = (4,2,3,5,0,3,5,0); //scale
  243. var
  244. i,d : integer;
  245. s,s4,s9 : string;
  246. t : TFieldType;
  247. begin
  248. with TSQLDBConnector(DBConnector) do begin
  249. if SQLConnType = INTERBASE then
  250. begin
  251. //Interbase internal storage of exact numeric data types based on precision:
  252. // 1-4 (smallint), 5-9 (integer), 10-18 (int64)
  253. s := ''; //Interbase supports precision up to 18 only
  254. FieldTypes[5] := ftBCD; //ATM TIBConnection incorrectly maps NUMERIC(18,3) to ftBCD
  255. end
  256. else
  257. s := ', N19_0 NUMERIC(19,0)';
  258. Connection.ExecuteDirect('create table FPDEV2 (FT NUMERIC(18,4), N4_2 NUMERIC(4,2), N9_3 NUMERIC(9,3), N9_5 NUMERIC(9,5), N18_0 NUMERIC(18,0), N18_3 NUMERIC(18,3), N18_5 NUMERIC(18,5)' + s + ')');
  259. Transaction.CommitRetaining;
  260. with Query do
  261. begin
  262. SQL.Text := 'select * from FPDEV2';
  263. Open;
  264. for i := 0 to FieldCount-1 do
  265. begin
  266. case Fields[i].DataType of
  267. ftBCD: d := sizeof(Currency);
  268. ftFmtBCD: d := sizeof(TBCD);
  269. ftLargeInt: d := sizeof(int64);
  270. else d := 0;
  271. end;
  272. t := FieldTypes[i];
  273. if t = ftLargeInt then t := ftFmtBCD; //acceptable alternative
  274. AssertEquals(Fields[i].DataSize, d);
  275. AssertTrue(Fields[i].DataType in [FieldTypes[i], t]);
  276. AssertEquals(Fields[i].Size, FieldSizes[i]);
  277. end;
  278. Close;
  279. end;
  280. for i := 0 to testValuesCount-1 do
  281. begin
  282. s4 := CurrToStrF(testValues4[i],ffFixed,2,DBConnector.FormatSettings);
  283. s9 := CurrToStrF(testValues9[i],ffFixed,3,DBConnector.FormatSettings);
  284. Connection.ExecuteDirect(format('insert into FPDEV2 (N4_2,N9_5,FT,N9_3,N18_3,N18_5) values (%s,%s,%s,%s,%s,%s)', [s4,s4,s9,s9,s9,s9]));
  285. end;
  286. with Query do
  287. begin
  288. Open;
  289. for i := 0 to testValuesCount-1 do
  290. begin
  291. AssertEquals(testValues4[i], Fields[1].AsCurrency);
  292. AssertEquals(testValues4[i], Fields[3].AsCurrency);
  293. AssertEquals(testValues9[i], Fields[0].AsCurrency);
  294. AssertEquals(testValues9[i], Fields[2].AsCurrency);
  295. AssertEquals(testValues9[i], Fields[5].AsCurrency);
  296. AssertEquals(testValues9[i], Fields[6].AsCurrency);
  297. Next;
  298. end;
  299. Close;
  300. end;
  301. end;
  302. end;
  303. procedure TTestFieldTypes.TestString;
  304. const
  305. testValuesCount = 19;
  306. testValues : Array[0..testValuesCount-1] of string = (
  307. '',
  308. 'a',
  309. 'ab',
  310. 'abc',
  311. 'abcd',
  312. 'abcde',
  313. 'abcdef',
  314. 'abcdefg',
  315. 'abcdefgh',
  316. 'abcdefghi',
  317. 'abcdefghij',
  318. 'lMnOpQrStU',
  319. '1234567890',
  320. '_!@#$%^&*(',
  321. ')-;:/?.<>',
  322. '~`|{}- =', // note that there's no \ (backslash) since some db's uses that as escape-character
  323. ' WRaP ',
  324. 'wRaP ',
  325. ' wRAP'
  326. );
  327. var
  328. i : byte;
  329. begin
  330. CreateTableWithFieldType(ftString,'VARCHAR(10)');
  331. TestFieldDeclaration(ftString,11);
  332. for i := 0 to testValuesCount-1 do
  333. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + testValues[i] + ''')');
  334. with TSQLDBConnector(DBConnector).Query do
  335. begin
  336. Open;
  337. for i := 0 to testValuesCount-1 do
  338. begin
  339. AssertEquals(testValues[i], Fields[0].AsString);
  340. Next;
  341. end;
  342. close;
  343. end;
  344. end;
  345. procedure TTestFieldTypes.TestUnlVarChar;
  346. const
  347. testValuesCount = 21;
  348. testValues : Array[0..testValuesCount-1] of string = (
  349. '',
  350. 'a',
  351. 'ab',
  352. 'abc',
  353. 'abcd',
  354. 'abcde',
  355. 'abcdef',
  356. 'abcdefg',
  357. 'abcdefgh',
  358. 'abcdefghi',
  359. 'abcdefghij',
  360. 'lMnOpQrStU',
  361. '1234567890',
  362. '_!@#$%^&*(',
  363. ')-;:/?.<>',
  364. '~`|{}- =',
  365. ' WRaP ',
  366. 'wRaP ',
  367. ' wRAP',
  368. '0123456789',
  369. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?' + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?'
  370. + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?' + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_+-=][|}{;:,./<>?'
  371. );
  372. var
  373. i : byte;
  374. begin
  375. if SQLConnType<>postgresql then Ignore('This test does only apply to Postgres, since others don''t support varchars without length given');
  376. CreateTableWithFieldType(ftString,'VARCHAR');
  377. TestFieldDeclaration(ftString,dsMaxStringSize+1);
  378. for i := 0 to testValuesCount-1 do
  379. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + testValues[i] + ''')');
  380. with TSQLDBConnector(DBConnector).Query do
  381. begin
  382. Open;
  383. for i := 0 to testValuesCount-1 do
  384. begin
  385. AssertEquals(testValues[i],fields[0].AsString);
  386. Next;
  387. end;
  388. close;
  389. end;
  390. end;
  391. procedure TTestFieldTypes.TestDate;
  392. var
  393. i : byte;
  394. begin
  395. CreateTableWithFieldType(ftDate,'DATE');
  396. TestFieldDeclaration(ftDate,8);
  397. for i := 0 to testDateValuesCount-1 do
  398. if SQLConnType=oracle then
  399. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (to_date (''' + testDateValues[i] + ''',''YYYY-MM-DD''))')
  400. else
  401. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + testDateValues[i] + ''')');
  402. // TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For debug-purposes
  403. with TSQLDBConnector(DBConnector).Query do
  404. begin
  405. Open;
  406. for i := 0 to testDateValuesCount-1 do
  407. begin
  408. AssertEquals(testDateValues[i],FormatDateTime('yyyy/mm/dd', fields[0].AsDateTime, DBConnector.FormatSettings));
  409. Next;
  410. end;
  411. close;
  412. end;
  413. end;
  414. procedure TTestFieldTypes.TestChangeBlob;
  415. var s : string;
  416. begin
  417. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (ID int,FT '+FieldtypeDefinitions[ftblob]+')');
  418. TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For interbase
  419. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (ID,FT) values (1,''Test deze blob'')');
  420. with TSQLDBConnector(DBConnector).Query do
  421. begin
  422. sql.clear;
  423. sql.add('select * from FPDEV2');
  424. Open;
  425. fields[1].ProviderFlags := [pfInUpdate]; // blob niet in de where
  426. UpdateMode := upWhereAll;
  427. AssertEquals('Test deze blob',fields[1].AsString);
  428. edit;
  429. // Dat werkt niet lekker, omdat de stream vernield wordt...
  430. // fields[0].asstring := 'Deze blob is gewijzigd!';
  431. With Createblobstream(fields[1],bmwrite) do
  432. begin
  433. s := 'Deze blob is gewijzigd!';
  434. WriteBuffer(Pointer(s)^,Length(s));
  435. post;
  436. free;
  437. end;
  438. AssertEquals('Deze blob is gewijzigd!',fields[1].AsString);
  439. ApplyUpdates(0);
  440. TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For debug-purposes
  441. close;
  442. open;
  443. AssertEquals('Deze blob is gewijzigd!',fields[1].AsString);
  444. close;
  445. end;
  446. end;
  447. procedure TTestFieldTypes.TestBlobGetText;
  448. begin
  449. CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
  450. TestFieldDeclaration(ftBlob,0);
  451. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
  452. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (Null)');
  453. // TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For debug-purposes
  454. with TSQLDBConnector(DBConnector).Query do
  455. begin
  456. Open;
  457. AssertFalse(fields[0].IsNull);
  458. AssertEquals('(BLOB)',fields[0].DisplayText);
  459. AssertEquals('Test deze blob',fields[0].AsString);
  460. Next;
  461. AssertTrue(fields[0].IsNull);
  462. AssertEquals('(blob)',fields[0].Text);
  463. AssertEquals('',fields[0].AsString);
  464. close;
  465. end;
  466. end;
  467. procedure TTestFieldTypes.TestBlobSize;
  468. begin
  469. CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
  470. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
  471. with TSQLDBConnector(DBConnector).Query do
  472. begin
  473. sql.text := 'select * from FPDEV2';
  474. Open;
  475. AssertEquals(14,TBlobField(fields[0]).BlobSize);
  476. close;
  477. end;
  478. end;
  479. procedure TTestFieldTypes.TestSetBlobAsStringParam;
  480. begin
  481. TestSetBlobAsParam(1);
  482. end;
  483. procedure TTestFieldTypes.TestBlob;
  484. begin
  485. CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
  486. TestFieldDeclaration(ftBlob,0);
  487. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''Test deze blob'')');
  488. // TSQLDBConnector(DBConnector).Transaction.CommitRetaining; // For debug-purposes
  489. with TSQLDBConnector(DBConnector).Query do
  490. begin
  491. Open;
  492. AssertEquals('Test deze blob',fields[0].AsString);
  493. close;
  494. end;
  495. end;
  496. procedure TTestFieldTypes.TestDateTime;
  497. const
  498. testValuesCount = 31;
  499. testValues : Array[0..testValuesCount-1] of string = (
  500. '2000-01-01',
  501. '1999-12-31',
  502. '2004-02-29',
  503. '2004-03-01',
  504. '1991-02-28',
  505. '1991-03-01',
  506. '1977-09-29',
  507. '2000-01-01 10:00:00',
  508. '2000-01-01 23:59:59',
  509. '1994-03-06 11:54:30',
  510. '2040-10-16', // MySQL 4.0 doesn't support datetimes before 1970 or after 2038
  511. '2100-01-01 01:01:01',
  512. '1903-04-02 01:04:02',
  513. '1900-01-01',
  514. '1899-12-31',
  515. '1899-12-30',
  516. '1899-12-29',
  517. '1899-12-30 18:00:51',
  518. '1899-12-30 04:00:51',
  519. '1899-12-29 04:00:51',
  520. '1899-12-29 18:00:51',
  521. '1815-09-24 03:47:22',
  522. '1800-03-30',
  523. '1754-06-04',
  524. '1650-05-10', // MS SQL 2005 doesn't support datetimes before 1753
  525. '1400-02-03 12:21:53',
  526. '1333-02-03 21:44:21',
  527. '0904-04-12',
  528. '0354-11-20 21:25:15',
  529. '0199-07-09',
  530. '0001-01-01'
  531. );
  532. var
  533. i : byte;
  534. begin
  535. CreateTableWithFieldType(ftDateTime,FieldtypeDefinitions[ftDateTime]);
  536. TestFieldDeclaration(ftDateTime,8);
  537. for i := 0 to testValuesCount-1 do
  538. if SQLConnType=oracle then
  539. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (to_date (''' + testValues[i] + ''',''YYYY-MM-DD HH24:MI:SS''))')
  540. else
  541. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + testValues[i] + ''')');
  542. with TSQLDBConnector(DBConnector).Query do
  543. begin
  544. Open;
  545. for i := 0 to testValuesCount-1 do
  546. begin
  547. if length(testValues[i]) < 12 then
  548. AssertEquals(testValues[i],FormatDateTime('yyyy/mm/dd', fields[0].AsDateTime, DBConnector.FormatSettings))
  549. else
  550. AssertEquals(testValues[i],FormatDateTime('yyyy/mm/dd hh:mm:ss', fields[0].AsDateTime, DBConnector.FormatSettings));
  551. Next;
  552. end;
  553. close;
  554. end;
  555. end;
  556. procedure TTestFieldTypes.TestFloat;
  557. const
  558. testValuesCount = 21;
  559. 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);
  560. var
  561. i : byte;
  562. begin
  563. CreateTableWithFieldType(ftFloat,'FLOAT');
  564. TestFieldDeclaration(ftFloat,sizeof(double));
  565. for i := 0 to testValuesCount-1 do
  566. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (' + floattostr(testValues[i],DBConnector.FormatSettings) + ')');
  567. with TSQLDBConnector(DBConnector).Query do
  568. begin
  569. Open;
  570. for i := 0 to testValuesCount-1 do
  571. begin
  572. AssertEquals(testValues[i],fields[0].AsFloat);
  573. Next;
  574. end;
  575. close;
  576. end;
  577. end;
  578. procedure TTestFieldTypes.TestNullValues;
  579. begin
  580. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (FIELD1 INT, FIELD2 INT)');
  581. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  582. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  583. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FIELD1) values (1)');
  584. with TSQLDBConnector(DBConnector).Query do
  585. begin
  586. sql.clear;
  587. sql.append('select * from FPDEV2');
  588. open;
  589. AssertEquals(1,FieldByName('FIELD1').AsInteger);
  590. AssertTrue('Null-values test failed',FieldByName('FIELD2').IsNull);
  591. close;
  592. end;
  593. end;
  594. procedure TTestFieldTypes.TestParamQuery;
  595. begin
  596. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (FIELD1 INT, FIELD2 INT, FIELD3 INT, DECOY VARCHAR(30))');
  597. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  598. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  599. with TSQLDBConnector(DBConnector).Query do
  600. begin
  601. sql.clear;
  602. sql.append('insert into FPDEV2 (field1) values (:field1)');
  603. Params.ParamByName('field1').AsInteger := 1;
  604. ExecSQL;
  605. sql.clear;
  606. sql.append('insert into FPDEV2 (field1,field2,decoy) values (:field1,:field2,''decoytest'')');
  607. Params.ParamByName('field1').AsInteger := 2;
  608. Params.ParamByName('field2').DataType := ftInteger;
  609. Params.ParamByName('field2').Value := Null;
  610. ExecSQL;
  611. sql.clear;
  612. sql.append('insert into FPDEV2 (field1,field2,field3) values (:field1,:field2,:field3)');
  613. Params.ParamByName('field1').AsInteger := 3;
  614. Params.ParamByName('field2').AsInteger := 2;
  615. Params.ParamByName('field3').AsInteger := 3;
  616. ExecSQL;
  617. sql.clear;
  618. sql.append('insert into FPDEV2 (field1,field2,field3,decoy) values (:field1,:field2,:field3,'':decoy ::test $decoy2 $$2'')');
  619. Params.ParamByName('field1').AsInteger := 4;
  620. Params.ParamByName('field2').AsInteger := 2;
  621. Params.ParamByName('field3').AsInteger := 3;
  622. ExecSQL;
  623. sql.clear;
  624. sql.append('insert into FPDEV2 (field1,field2,field3) values (:field1,:field2,:field1)');
  625. Params.ParamByName('field1').AsInteger := 5;
  626. Params.ParamByName('field2').AsInteger := 2;
  627. ExecSQL;
  628. sql.clear;
  629. sql.append('select * from FPDEV2 order by FIELD1');
  630. open;
  631. AssertEquals(1,FieldByName('FIELD1').asinteger);
  632. AssertTrue(FieldByName('FIELD2').IsNull);
  633. AssertTrue(FieldByName('FIELD3').IsNull);
  634. AssertTrue(FieldByName('DECOY').IsNull);
  635. next;
  636. AssertEquals(2,FieldByName('FIELD1').asinteger);
  637. AssertTrue(FieldByName('FIELD2').IsNull);
  638. AssertTrue(FieldByName('FIELD3').IsNull);
  639. AssertEquals('decoytest',FieldByName('DECOY').AsString);
  640. next;
  641. AssertEquals(3,FieldByName('FIELD1').asinteger);
  642. AssertEquals(2,FieldByName('FIELD2').asinteger);
  643. AssertEquals(3,FieldByName('FIELD3').asinteger);
  644. AssertTrue(FieldByName('DECOY').IsNull);
  645. next;
  646. AssertEquals(4,FieldByName('FIELD1').asinteger);
  647. AssertEquals(2,FieldByName('FIELD2').asinteger);
  648. AssertEquals(3,FieldByName('FIELD3').asinteger);
  649. AssertEquals(':decoy ::test $decoy2 $$2',FieldByName('DECOY').AsString);
  650. next;
  651. AssertEquals(5,FieldByName('FIELD1').asinteger);
  652. AssertEquals(2,FieldByName('FIELD2').asinteger);
  653. AssertEquals(5,FieldByName('FIELD3').asinteger);
  654. AssertTrue(FieldByName('DECOY').IsNull);
  655. close;
  656. end;
  657. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  658. end;
  659. procedure TTestFieldTypes.TestIntParamQuery;
  660. begin
  661. TestXXParamQuery(ftInteger,'INT',testIntValuesCount);
  662. end;
  663. procedure TTestFieldTypes.TestFmtBCDParamQuery;
  664. begin
  665. TestXXParamQuery(ftFMTBcd,FieldtypeDefinitionsConst[ftFMTBcd],testValuesCount);
  666. end;
  667. procedure TTestFieldTypes.TestDateParamQuery;
  668. begin
  669. TestXXParamQuery(ftDate,FieldtypeDefinitions[ftDate],testDateValuesCount);
  670. end;
  671. procedure TTestFieldTypes.TestTimeParamQuery;
  672. begin
  673. TestXXParamQuery(ftTime,FieldtypeDefinitions[ftTime],testValuesCount);
  674. end;
  675. procedure TTestFieldTypes.TestDateTimeParamQuery;
  676. begin
  677. TestXXParamQuery(ftDateTime,FieldtypeDefinitions[ftDateTime],testValuesCount);
  678. end;
  679. procedure TTestFieldTypes.TestFloatParamQuery;
  680. begin
  681. TestXXParamQuery(ftFloat,FieldtypeDefinitions[ftFloat],testFloatValuesCount);
  682. end;
  683. procedure TTestFieldTypes.TestBCDParamQuery;
  684. begin
  685. TestXXParamQuery(ftBCD,'NUMERIC(10,4)',testBCDValuesCount);
  686. end;
  687. procedure TTestFieldTypes.TestBytesParamQuery;
  688. begin
  689. TestXXParamQuery(ftBytes, FieldtypeDefinitions[ftBytes], testBytesValuesCount, true);
  690. end;
  691. procedure TTestFieldTypes.TestVarBytesParamQuery;
  692. begin
  693. TestXXParamQuery(ftVarBytes, FieldtypeDefinitions[ftVarBytes], testVarBytesValuesCount, not(SQLServerType in [ssMSSQL, ssSybase]));
  694. end;
  695. procedure TTestFieldTypes.TestBooleanParamQuery;
  696. begin
  697. TestXXParamQuery(ftBoolean, FieldtypeDefinitions[ftBoolean], testValuesCount);
  698. end;
  699. procedure TTestFieldTypes.TestStringParamQuery;
  700. begin
  701. TestXXParamQuery(ftString,'VARCHAR(10)',testValuesCount);
  702. end;
  703. procedure TTestFieldTypes.TestFixedStringParamQuery;
  704. begin
  705. TestXXParamQuery(ftFixedChar,'CHAR(10)',testValuesCount);
  706. end;
  707. procedure TTestFieldTypes.TestXXParamQuery(ADatatype : TFieldType; ASQLTypeDecl : string; testValuesCount : integer; Cross : boolean = false);
  708. var i : integer;
  709. begin
  710. if ASQLTypeDecl = '' then
  711. Ignore('Fields of the type ' + FieldTypeNames[ADatatype] + ' are not supported by this sqldb-connection type');
  712. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (ID INT, FIELD1 '+ASQLTypeDecl+')');
  713. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  714. if SQLConnType=interbase then TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  715. with TSQLDBConnector(DBConnector).Query do
  716. begin
  717. sql.clear;
  718. sql.append('insert into FPDEV2 (ID,FIELD1) values (:id,:field1)');
  719. // There is no Param.AsFixedChar, so the datatype has to be set manually
  720. if ADatatype=ftFixedChar then
  721. Params.ParamByName('field1').DataType := ftFixedChar;
  722. for i := 0 to testValuesCount -1 do
  723. begin
  724. Params.ParamByName('id').AsInteger := i;
  725. case ADataType of
  726. ftInteger: Params.ParamByName('field1').asInteger := testIntValues[i];
  727. ftBoolean: Params.ParamByName('field1').AsBoolean := testBooleanValues[i];
  728. ftFloat : Params.ParamByName('field1').AsFloat := testFloatValues[i];
  729. ftBCD : Params.ParamByName('field1').AsCurrency:= testBCDValues[i];
  730. ftFixedChar,
  731. ftString : Params.ParamByName('field1').AsString := testValues[ADataType,i];
  732. ftTime : Params.ParamByName('field1').AsTime := TimeStringToDateTime(testTimeValues[i]);
  733. ftDate : if cross then
  734. Params.ParamByName('field1').AsString:= testDateValues[i]
  735. else
  736. Params.ParamByName('field1').AsDate := StrToDate(testDateValues[i],'yyyy/mm/dd','-');
  737. ftDateTime:Params.ParamByName('field1').AsDateTime := StrToDateTime(testValues[ADataType,i], DBConnector.FormatSettings);
  738. ftFMTBcd : Params.ParamByName('field1').AsFMTBCD := StrToBCD(testFmtBCDValues[i],DBConnector.FormatSettings);
  739. ftBytes : if cross then
  740. Params.ParamByName('field1').Value := StringToByteArray(testBytesValues[i])
  741. else
  742. Params.ParamByName('field1').AsBlob := testBytesValues[i];
  743. ftVarBytes:if cross then
  744. Params.ParamByName('field1').AsString := testBytesValues[i]
  745. else
  746. Params.ParamByName('field1').AsBlob := testBytesValues[i];
  747. else
  748. AssertTrue('no test for paramtype available',False);
  749. end;
  750. ExecSQL;
  751. end;
  752. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  753. sql.clear;
  754. sql.append('select * from FPDEV2 order by ID');
  755. open;
  756. for i := 0 to testValuesCount -1 do
  757. begin
  758. AssertEquals(i,FieldByName('ID').AsInteger);
  759. case ADataType of
  760. ftInteger: AssertEquals(testIntValues[i],FieldByName('FIELD1').AsInteger);
  761. ftBoolean: AssertEquals(testBooleanValues[i],FieldByName('FIELD1').AsBoolean);
  762. ftFloat : AssertEquals(testFloatValues[i],FieldByName('FIELD1').AsFloat);
  763. ftBCD : AssertEquals(testBCDValues[i],FieldByName('FIELD1').AsCurrency);
  764. ftFixedChar : AssertEquals(PadRight(testStringValues[i],10),FieldByName('FIELD1').AsString);
  765. ftString : AssertEquals(testStringValues[i],FieldByName('FIELD1').AsString);
  766. ftTime : AssertEquals(testTimeValues[i],DateTimeToTimeString(FieldByName('FIELD1').AsDateTime));
  767. ftDate : AssertEquals(testDateValues[i],DateTimeToStr(FieldByName('FIELD1').AsDateTime, DBConnector.FormatSettings));
  768. ftDateTime : AssertEquals(testValues[ADataType,i], DateTimeToStr(FieldByName('FIELD1').AsDateTime, DBConnector.FormatSettings));
  769. ftFMTBcd : AssertEquals(testFmtBCDValues[i], BCDToStr(FieldByName('FIELD1').AsBCD, DBConnector.FormatSettings));
  770. ftVarBytes,
  771. ftBytes : AssertEquals(testBytesValues[i], shortstring(FieldByName('FIELD1').AsString));
  772. else
  773. AssertTrue('no test for paramtype available',False);
  774. end;
  775. Next;
  776. end;
  777. close;
  778. end;
  779. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  780. end;
  781. procedure TTestFieldTypes.TestSetBlobAsParam(asWhat: integer);
  782. var
  783. ASQL : TSQLQuery;
  784. begin
  785. CreateTableWithFieldType(ftBlob,FieldtypeDefinitions[ftBlob]);
  786. TestFieldDeclaration(ftBlob,0);
  787. ASQL := DBConnector.GetNDataset(True,1) as tsqlquery;
  788. with ASql do
  789. begin
  790. sql.Text := 'insert into FPDEV2 (FT) values (:BlobParam)';
  791. case asWhat of
  792. 0: Params.ParamByName('blobParam').AsMemo := 'Test deze BLob';
  793. 1: Params.ParamByName('blobParam').AsString := 'Test deze BLob';
  794. 2: Params.ParamByName('blobParam').AsBlob := 'Test deze BLob';
  795. end;
  796. ExecSQL;
  797. end;
  798. with TSQLDBConnector(DBConnector).Query do
  799. begin
  800. Open;
  801. if not eof then
  802. AssertEquals('Test deze BLob',fields[0].AsString);
  803. close;
  804. end;
  805. end;
  806. procedure TTestFieldTypes.TestAggregates;
  807. begin
  808. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (FIELD1 INT, FIELD2 INT)');
  809. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  810. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  811. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 values (1,1)');
  812. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 values (2,3)');
  813. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 values (3,4)');
  814. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 values (4,4)');
  815. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  816. with TSQLDBConnector(DBConnector).Query do
  817. begin
  818. sql.clear;
  819. sql.append('select count(*) from FPDEV2');
  820. open;
  821. AssertEquals(4,Fields[0].AsInteger);
  822. close;
  823. sql.clear;
  824. sql.append('select sum(FIELD1) from FPDEV2');
  825. open;
  826. AssertEquals(10,Fields[0].AsInteger);
  827. close;
  828. sql.clear;
  829. sql.append('select avg(FIELD2) from FPDEV2');
  830. open;
  831. AssertEquals(3,Fields[0].AsInteger);
  832. close;
  833. end;
  834. end;
  835. procedure TTestFieldTypes.CreateTableWithFieldType(ADatatype: TFieldType;
  836. ASQLTypeDecl: string);
  837. begin
  838. with TSQLDBConnector(DBConnector) do
  839. begin
  840. Connection.ExecuteDirect('create table FPDEV2 (FT ' +ASQLTypeDecl+ ')');
  841. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  842. Transaction.CommitRetaining;
  843. end;
  844. end;
  845. procedure TTestFieldTypes.TestFieldDeclaration(ADatatype: TFieldType;
  846. ADataSize: integer);
  847. begin
  848. with TSQLDBConnector(DBConnector).Query do
  849. begin
  850. SQL.Clear;
  851. SQL.Add('select * from FPDEV2');
  852. Open;
  853. AssertEquals(1,FieldCount);
  854. AssertTrue(CompareText('FT',fields[0].FieldName)=0);
  855. AssertEquals('DataSize', ADataSize, Fields[0].DataSize);
  856. AssertEquals('DataType', ord(ADatatype), ord(Fields[0].DataType));
  857. Close;
  858. end;
  859. end;
  860. procedure TTestFieldTypes.SetUp;
  861. begin
  862. InitialiseDBConnector;
  863. end;
  864. procedure TTestFieldTypes.TearDown;
  865. begin
  866. if assigned(DBConnector) then
  867. TSQLDBConnector(DBConnector).Transaction.Rollback;
  868. FreeDBConnector;
  869. end;
  870. procedure TTestFieldTypes.RunTest;
  871. begin
  872. // if (SQLConnType in TSQLConnType) then
  873. inherited RunTest;
  874. end;
  875. procedure TTestFieldTypes.TestQueryAfterReconnect;
  876. var DS: TDataset;
  877. begin
  878. ds := DBConnector.GetNDataset(true,5);
  879. with ds do
  880. begin
  881. open;
  882. close;
  883. TSQLDBConnector(DBConnector).Connection.Close;
  884. TSQLDBConnector(DBConnector).Connection.Open;
  885. open;
  886. close;
  887. end;
  888. end;
  889. procedure TTestFieldTypes.TestLocateNull;
  890. var DS: TCustomBufDataset;
  891. begin
  892. ds := TSQLDBConnector(DBConnector).GetNDataset(true,5) as TCustomBufDataset;
  893. with ds do
  894. begin
  895. open;
  896. edit;
  897. fieldbyname('name').Clear;
  898. post;
  899. next;
  900. AssertFalse(Locate('name',VarArrayOf(['TestName1']),[]));
  901. AssertTrue(Locate('name',VarArrayOf([Null]),[]));
  902. AssertEquals(1,fieldbyname('ID').AsInteger);
  903. end;
  904. end;
  905. procedure TTestFieldTypes.TestLocateOnMoreRecords;
  906. var DS: TCustomBufDataset;
  907. begin
  908. with TSQLDBConnector(DBConnector) do
  909. begin
  910. ds := GetNDataset(true,30) as TCustomBufDataset;
  911. with query do
  912. begin
  913. SQL.Text:='update FPDEV set NAME = null where ID<11;';
  914. ExecSQL;
  915. SQL.Text:='update FPDEV set NAME = null where (ID>11) and (ID<23);';
  916. ExecSQL;
  917. end;
  918. with ds do
  919. begin
  920. Open;
  921. // Must be exactly 11 to trigger bug/test
  922. AssertTrue(Locate('name',VarArrayOf(['TestName11']),[]));
  923. AssertEquals(11,fieldbyname('ID').AsInteger);
  924. // Must be exactly 23 to trigger bug/test
  925. AssertTrue(Locate('name',VarArrayOf(['TestName23']),[]));
  926. AssertEquals(23,fieldbyname('ID').AsInteger);
  927. end;
  928. end;
  929. end;
  930. procedure TTestFieldTypes.TestRefresh;
  931. var ADataset: TDataset;
  932. i: integer;
  933. AFldID, AFldName: TField;
  934. begin
  935. ADataset := TSQLDBConnector(DBConnector).GetNDataset(true,5);
  936. Adataset.Open;
  937. AFldId:=Adataset.Fields[0];
  938. AFldName:=Adataset.Fields[1];
  939. for i := 1 to 5 do
  940. begin
  941. AssertEquals(i,AFldID.asinteger);
  942. AssertEquals('TestName'+inttostr(i),AFldName.asstring);
  943. ADataset.Next;
  944. end;
  945. ADataset.Next;
  946. AssertTrue(ADataset.EOF);
  947. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('update FPDEV set NAME=''test'' where ID=2');
  948. ADataset.Refresh;
  949. ADataset.First;
  950. for i := 1 to 5 do
  951. begin
  952. AssertEquals(i,AFldID.AsInteger);
  953. if i = 2 then
  954. AssertEquals('test',AFldName.AsString)
  955. else
  956. AssertEquals('TestName'+inttostr(i),AFldName.AsString);
  957. ADataset.Next;
  958. end;
  959. ADataset.Next;
  960. AssertTrue(ADataset.EOF);
  961. end;
  962. procedure TTestFieldTypes.TestEmptyUpdateQuery;
  963. begin
  964. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('update FPDEV set name=''nothing'' where (1=0)');
  965. end;
  966. procedure TTestFieldTypes.TestNonNullableParams;
  967. var ASQLQuery : TSQLQuery;
  968. Passed: Boolean;
  969. begin
  970. // Check for an exception when a null value is stored into a non-nullable
  971. // field using a parameter
  972. // There was a bug in IBConnection so that in this case the last used value
  973. // for the parameter was used.
  974. // To make sure that any changes are cancelled in the case the test fails
  975. TSQLDBConnector(DBConnector).GetNDataset(true,5);
  976. ASQLQuery := TSQLDBConnector(DBConnector).Query;
  977. ASQLQuery.SQL.text := 'update fpdev set ID=:ID1 where id = :ID2';
  978. ASQLQuery.Params[0].Clear;
  979. ASQLQuery.Params[1].AsInteger := 1;
  980. AssertTrue(ASQLQuery.Params[0].IsNull);
  981. Passed:=False;
  982. try
  983. @ASQLQuery.ExecSQL;
  984. except
  985. on E: Exception do
  986. if E.ClassType.InheritsFrom(EDatabaseError) then
  987. Passed := true;
  988. end;
  989. AssertTrue(Passed);
  990. end;
  991. procedure TTestFieldTypes.TestStringLargerThen8192;
  992. var
  993. s : string;
  994. i : integer;
  995. begin
  996. CreateTableWithFieldType(ftString,'VARCHAR(9000)');
  997. TestFieldDeclaration(ftString,9001);
  998. setlength(s,9000);
  999. for i := 1 to 9000 do
  1000. s[i]:=chr((i mod 10)+ord('a'));
  1001. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (''' + s + ''')');
  1002. with TSQLDBConnector(DBConnector).Query do
  1003. begin
  1004. Open;
  1005. AssertEquals(s,fields[0].AsString);
  1006. close;
  1007. end;
  1008. end;
  1009. procedure TTestFieldTypes.TestTableNames;
  1010. var TableList : TStringList;
  1011. i : integer;
  1012. begin
  1013. TableList := TStringList.Create;
  1014. try
  1015. TSQLDBConnector(DBConnector).Connection.GetTableNames(TableList);
  1016. AssertTrue(TableList.Find('fpdev',i));
  1017. finally
  1018. TableList.Free;
  1019. end;
  1020. end;
  1021. procedure TTestFieldTypes.TestFieldNames;
  1022. var FieldList : TStringList;
  1023. i : integer;
  1024. begin
  1025. FieldList := TStringList.Create;
  1026. try
  1027. TSQLDBConnector(DBConnector).Connection.GetFieldNames('fpdev',FieldList);
  1028. AssertTrue(FieldList.Find('id',i));
  1029. finally
  1030. FieldList.Free;
  1031. end;
  1032. end;
  1033. procedure TTestFieldTypes.TestInsertReturningQuery;
  1034. begin
  1035. if not(SQLConnType in [postgresql,interbase,oracle]) then Ignore(STestNotApplicable);
  1036. with TSQLDBConnector(DBConnector) do
  1037. begin
  1038. // This only works with databases that supports 'insert into .. returning'
  1039. // for example: PostgreSQL, Oracle, Firebird version 2.0 and up
  1040. CreateTableWithFieldType(ftInteger,'int');
  1041. Query.SQL.Text:='insert into FPDEV2 values(154) returning FT';
  1042. Query.Open;
  1043. AssertTrue(CompareText('FT',Query.Fields[0].FieldName)=0);
  1044. AssertEquals(154,Query.fields[0].AsInteger);
  1045. Query.Close;
  1046. end;
  1047. end;
  1048. procedure TTestFieldTypes.TestOpenStoredProc;
  1049. begin
  1050. with TSQLDBConnector(DBConnector) do
  1051. begin
  1052. case SQLServerType of
  1053. ssMySQL:
  1054. begin
  1055. Connection.ExecuteDirect('create procedure FPDEV_PROC() select 1 union select 2;');
  1056. Query.SQL.Text:='call FPDEV_PROC';
  1057. end;
  1058. ssFirebird, ssInterbase:
  1059. begin
  1060. Connection.ExecuteDirect('create procedure FPDEV_PROC returns (r integer) as begin r=1; end');
  1061. Query.SQL.Text:='execute procedure FPDEV_PROC';
  1062. end;
  1063. ssMSSQL, ssSybase:
  1064. begin
  1065. Connection.ExecuteDirect('create procedure FPDEV_PROC as select 1 union select 2;');
  1066. Query.SQL.Text:='execute FPDEV_PROC';
  1067. end;
  1068. else
  1069. begin
  1070. Ignore('This test does not apply to this sqldb-connection type, since it does not support selectable stored procedures.');
  1071. Exit;
  1072. end;
  1073. end;
  1074. Transaction.CommitRetaining;
  1075. try
  1076. Query.Open;
  1077. AssertEquals(1, Query.Fields[0].AsInteger);
  1078. Query.Next;
  1079. if not(SQLConnType in [interbase]) then
  1080. begin
  1081. AssertFalse('Eof after 1st row', Query.Eof);
  1082. AssertEquals(2, Query.Fields[0].AsInteger);
  1083. Query.Next;
  1084. end;
  1085. AssertTrue('No Eof after last row', Query.Eof);
  1086. Query.Close;
  1087. finally
  1088. Connection.ExecuteDirect('drop procedure FPDEV_PROC');
  1089. Transaction.CommitRetaining;
  1090. end;
  1091. end;
  1092. end;
  1093. procedure TTestFieldTypes.TestOpenSpecialStatements;
  1094. const CTE_SELECT = 'WITH a AS (SELECT * FROM FPDEV) SELECT * FROM a';
  1095. type TTestStatements = array of string;
  1096. var statements: TTestStatements;
  1097. s: string;
  1098. begin
  1099. // tests non-select statements (other than "SELECT ..."), which return result-set
  1100. // at least one row must be returned
  1101. with TSQLDBConnector(DBConnector) do
  1102. begin
  1103. case SQLConnType of
  1104. sqlite3:
  1105. statements := TTestStatements.Create('pragma table_info(FPDEV)');
  1106. interbase:
  1107. statements := TTestStatements.Create(
  1108. CTE_SELECT (*FB 2.1*),
  1109. 'EXECUTE BLOCK RETURNS (U VARCHAR(255)) AS BEGIN SELECT rdb$get_context(''SYSTEM'',''CURRENT_USER'') FROM rdb$database INTO U; SUSPEND; END' (*FB 2.0*)
  1110. );
  1111. postgresql:
  1112. statements := TTestStatements.Create(CTE_SELECT);
  1113. mssql:
  1114. statements := TTestStatements.Create(CTE_SELECT (*MS SQL 2005*));
  1115. else
  1116. if SQLConnType in MySQLConnTypes then
  1117. statements := TTestStatements.Create(
  1118. 'check table FPDEV', // bug 14519
  1119. 'show tables from '+Connection.DatabaseName // bug 16842
  1120. )
  1121. else
  1122. Ignore(STestNotApplicable);
  1123. end;
  1124. for s in statements do
  1125. begin
  1126. Query.SQL.Text := s;
  1127. Query.Open;
  1128. AssertTrue(Query.FieldCount>0);
  1129. AssertFalse('Eof after open', Query.Eof);
  1130. Query.Next;
  1131. Query.Close;
  1132. end;
  1133. end;
  1134. end;
  1135. procedure TTestFieldTypes.TestClearUpdateableStatus;
  1136. // Test if CanModify is correctly disabled in case of a select query without
  1137. // a from-statement.
  1138. begin
  1139. if not (SQLConnType in MySQLConnTypes) then Ignore('This test does only apply to MySQL because the used SQL-statement is MySQL only.');
  1140. with TSQLDBConnector(DBConnector) do
  1141. begin
  1142. with (GetNDataset(false,5) as TSQLQuery) do
  1143. begin
  1144. Open;
  1145. AssertEquals(True,CanModify);
  1146. Close;
  1147. SQL.Text:='select last_insert_id();';
  1148. Open;
  1149. AssertEquals(False,CanModify);
  1150. close;
  1151. end;
  1152. end;
  1153. end;
  1154. procedure TTestFieldTypes.TestReadOnlyParseSQL;
  1155. begin
  1156. with TSQLDBConnector(DBConnector) do
  1157. begin
  1158. GetFieldDataset(True);
  1159. with query do
  1160. begin
  1161. AssertFalse(ReadOnly);
  1162. AssertTrue(ParseSQL);
  1163. // If ParseSQL is false, and no update-queries are given, the query
  1164. // shouldn't be updateable after open.
  1165. ParseSQL := False;
  1166. AssertFalse(ParseSQL);
  1167. AssertFalse(ReadOnly);
  1168. SQL.Text := 'select * from FPDEV;';
  1169. open;
  1170. AssertFalse(ParseSQL);
  1171. AssertFalse(ReadOnly);
  1172. AssertFalse(CanModify);
  1173. close;
  1174. // If ParseSQL is true, the query should be updateable after open.
  1175. ReadOnly := False;
  1176. ParseSQL := True;
  1177. AssertTrue(ParseSQL);
  1178. AssertFalse(ReadOnly);
  1179. SQL.Text := 'select * from FPDEV';
  1180. open;
  1181. AssertTrue(ParseSQL);
  1182. AssertFalse(ReadOnly);
  1183. AssertTrue(CanModify);
  1184. edit;
  1185. FieldByName('ID').AsInteger:=321;
  1186. post;
  1187. Applyupdates;
  1188. close;
  1189. // If ParseSQL is true, but the supplied query isn't updateable, then
  1190. // the query shouldn't be updateable after open.
  1191. ReadOnly := False;
  1192. SQL.Text:='select ID,NAME from FPDEV where ID<5';
  1193. sql.Add('union');
  1194. sql.Add('select ID,NAME from FPDEV where ID>5');
  1195. AssertTrue(ParseSQL);
  1196. AssertFalse(ReadOnly);
  1197. open;
  1198. AssertTrue(ParseSQL);
  1199. AssertFalse(ReadOnly);
  1200. AssertFalse(CanModify);
  1201. close;
  1202. // As above, but now with an update-query, so that the query should
  1203. // be updateable again.
  1204. ReadOnly := False;
  1205. AssertTrue(ParseSQL);
  1206. AssertFalse(ReadOnly);
  1207. UpdateSQL.Text:='update FPDEV set ID=:ID where ID=:OLD_ID';
  1208. open;
  1209. AssertTrue(ParseSQL);
  1210. AssertFalse(ReadOnly);
  1211. AssertTrue(CanModify);
  1212. edit;
  1213. post;
  1214. Applyupdates;
  1215. close;
  1216. // Also if ParseSQL is False, the query should be updateable if a update-
  1217. // query is given.
  1218. ReadOnly := False;
  1219. ParseSQL := False;
  1220. AssertFalse(ParseSQL);
  1221. AssertFalse(ReadOnly);
  1222. open;
  1223. AssertFalse(ParseSQL);
  1224. AssertFalse(ReadOnly);
  1225. AssertTrue(CanModify);
  1226. edit;
  1227. FieldByName('ID').AsInteger:=1;
  1228. post;
  1229. Applyupdates;
  1230. close;
  1231. // But if ReadOnly is true, then CanModify should always be false
  1232. ReadOnly := True;
  1233. ParseSQL := False;
  1234. AssertFalse(ParseSQL);
  1235. AssertTrue(ReadOnly);
  1236. open;
  1237. AssertFalse(ParseSQL);
  1238. AssertTrue(ReadOnly);
  1239. AssertFalse(CanModify);
  1240. close;
  1241. end;
  1242. end;
  1243. end;
  1244. procedure TTestFieldTypes.TestParseJoins;
  1245. begin
  1246. with TSQLDBConnector(DBConnector) do
  1247. begin
  1248. with query do
  1249. begin
  1250. SQL.Text:='select TT.NAME from FPDEV left join FPDEV TT on TT.ID=FPDEV.ID';
  1251. Open;
  1252. AssertFalse(CanModify);
  1253. Close;
  1254. SQL.Text:='select T1.NAME from FPDEV T1,FPDEV T2 where T1.ID=T2.ID';
  1255. Open;
  1256. AssertFalse(CanModify);
  1257. Close;
  1258. end;
  1259. end;
  1260. end;
  1261. procedure TTestFieldTypes.TestDoubleFieldNames;
  1262. begin
  1263. with TSQLDBConnector(DBConnector) do
  1264. begin
  1265. with query do
  1266. begin
  1267. SQL.Text:='select FPDEV.*,TT.* from FPDEV left join FPDEV TT on TT.ID=FPDEV.ID';
  1268. Open;
  1269. AssertTrue(assigned(FindField('ID')));
  1270. AssertTrue (assigned(FindField('ID_1')));
  1271. AssertTrue(assigned(FindField('NAME')));
  1272. AssertTrue(assigned(FindField('NAME_1')));
  1273. AssertEquals(1,fieldbyname('ID').AsInteger);
  1274. AssertEquals(1,fieldbyname('ID_1').AsInteger);
  1275. AssertEquals('TestName1',fieldbyname('NAME').AsString);
  1276. AssertEquals('TestName1',fieldbyname('NAME_1').AsString);
  1277. close;
  1278. end;
  1279. end;
  1280. end;
  1281. procedure TTestFieldTypes.TestParseUnion;
  1282. begin
  1283. with TSQLDBConnector(DBConnector) do
  1284. begin
  1285. with query do
  1286. begin
  1287. SQL.Text:='select NAME from FPDEV where ID<5';
  1288. sql.Add('union');
  1289. sql.Add('select NAME from FPDEV where ID>5');
  1290. Open;
  1291. close;
  1292. end;
  1293. end;
  1294. end;
  1295. procedure TTestFieldTypes.TestInsertLargeStrFields;
  1296. const
  1297. FieldValue='test1';
  1298. begin
  1299. with TSQLDBConnector(DBConnector) do
  1300. begin
  1301. Connection.ExecuteDirect('create table FPDEV2 ( ' +
  1302. ' ID INT NOT NULL , ' +
  1303. ' NAME VARCHAR(16000), ' +
  1304. ' PRIMARY KEY (ID) ' +
  1305. ') ');
  1306. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  1307. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1308. query.sql.Text:='select * from FPDEV2';
  1309. Query.Open;
  1310. Query.InsertRecord([1,FieldValue]);
  1311. Query.ApplyUpdates;
  1312. Query.Close;
  1313. Query.Open;
  1314. AssertEquals(FieldValue, Query.FieldByName('NAME').AsString);
  1315. Query.Close;
  1316. end;
  1317. end;
  1318. procedure TTestFieldTypes.TestNumericNames;
  1319. begin
  1320. with TSQLDBConnector(DBConnector) do
  1321. begin
  1322. Connection.ExecuteDirect('create table FPDEV2 ( ' +
  1323. ' '+connection.FieldNameQuoteChars[0]+'2ID'+connection.FieldNameQuoteChars[1]+' INT NOT NULL, ' +
  1324. ' '+connection.FieldNameQuoteChars[0]+'3TEST'+connection.FieldNameQuoteChars[1]+' VARCHAR(10), ' +
  1325. ' PRIMARY KEY ('+connection.FieldNameQuoteChars[0]+'2ID'+connection.FieldNameQuoteChars[0]+') ' +
  1326. ') ');
  1327. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  1328. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1329. with query do
  1330. begin
  1331. SQL.Text:='select * from FPDEV2';
  1332. Open;
  1333. Edit;
  1334. fieldbyname('2ID').AsInteger:=1;
  1335. fieldbyname('3TEST').AsString:='3test';
  1336. Post;
  1337. ApplyUpdates(0);
  1338. close;
  1339. open;
  1340. AssertEquals('3test',FieldByName('3TEST').AsString);
  1341. Edit;
  1342. fieldbyname('3TEST').AsString:='test3';
  1343. Post;
  1344. ApplyUpdates(0);
  1345. open;
  1346. AssertEquals('test3',FieldByName('3TEST').AsString);
  1347. close;
  1348. end;
  1349. end;
  1350. end;
  1351. procedure TTestFieldTypes.TestApplyUpdFieldnames;
  1352. begin
  1353. with TSQLDBConnector(DBConnector) do
  1354. begin
  1355. AssertEquals(-1,query.RowsAffected);
  1356. Connection.ExecuteDirect('create table FPDEV2 ( ' +
  1357. ' ID INT NOT NULL , ' +
  1358. ' '+Connection.FieldNameQuoteChars[0]+'NAME-TEST'+Connection.FieldNameQuoteChars[1]+' VARCHAR(250), ' +
  1359. ' PRIMARY KEY (ID) ' +
  1360. ') ');
  1361. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  1362. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1363. Connection.ExecuteDirect('insert into FPDEV2(ID,'+Connection.FieldNameQuoteChars[0]+'NAME-TEST'+Connection.FieldNameQuoteChars[1]+') values (1,''test1'')');
  1364. Query.SQL.Text := 'select * from FPDEV2';
  1365. Query.Open;
  1366. AssertEquals(1,Query.FieldByName('ID').AsInteger);
  1367. AssertEquals('test1',Query.FieldByName('NAME-TEST').AsString);
  1368. Query.Edit;
  1369. Query.FieldByName('NAME-TEST').AsString:='Edited';
  1370. Query.Post;
  1371. Query.ApplyUpdates;
  1372. Query.Close;
  1373. Query.Open;
  1374. AssertEquals(1,Query.FieldByName('ID').AsInteger);
  1375. AssertEquals('Edited',Query.FieldByName('NAME-TEST').AsString);
  1376. Query.Close;
  1377. end;
  1378. end;
  1379. procedure TTestFieldTypes.TestServerFilter;
  1380. begin
  1381. // Tests SQLParser and ServerFilter
  1382. with TSQLDBConnector(DBConnector).Query do
  1383. begin
  1384. ServerFilter:='ID=21';
  1385. ServerFiltered:=true;
  1386. // tests parsing SELECT without WHERE
  1387. SQL.Text:='select * from FPDEV';
  1388. Open;
  1389. CheckTrue(CanModify, SQL.Text);
  1390. CheckEquals(1, RecordCount);
  1391. Close;
  1392. SQL.Text:='select *'#13'from FPDEV'#13'order by 1';
  1393. Open;
  1394. CheckTrue(CanModify, SQL.Text);
  1395. CheckEquals(1, RecordCount);
  1396. Close;
  1397. // tests parsing SELECT with simple WHERE
  1398. SQL.Text:='select *'#9'from FPDEV'#9'where NAME<>''''';
  1399. Open;
  1400. CheckTrue(CanModify, SQL.Text);
  1401. CheckEquals(1, RecordCount);
  1402. Close;
  1403. // tests parsing SELECT with simple WHERE followed by ORDER BY
  1404. SQL.Text:='select *'#10'from FPDEV'#10'where NAME>'''' order by 1';
  1405. Open;
  1406. CheckTrue(CanModify, SQL.Text);
  1407. CheckEquals(1, RecordCount);
  1408. Close;
  1409. // tests parsing of WHERE ... LIMIT
  1410. case SQLServerType of
  1411. ssFirebird, ssInterbase:
  1412. SQL.Text:='select first 1 NAME from FPDEV where NAME=''TestName21''';
  1413. ssMSSQL, ssSybase:
  1414. SQL.Text:='select top 1 NAME from FPDEV where NAME=''TestName21''';
  1415. else
  1416. SQL.Text:='select NAME from FPDEV where NAME=''TestName21'' limit 1';
  1417. end;
  1418. Open;
  1419. CheckTrue(CanModify, SQL.Text);
  1420. Close;
  1421. // tests parsing SELECT with table alias and embedded comments (MySQL requires space after -- )
  1422. SQL.Text:='/**/select * from/**/FPDEV as fp-- comment'#13'where(NAME>''TestName20'')/**/order by 1';
  1423. Open;
  1424. CheckTrue(CanModify, 'CanModify: '+SQL.Text);
  1425. CheckTrue(ServerIndexDefs.Count >= 1, 'ServerIndexDefs: '+SQL.Text); // is TableName extracted correctly? (MS SQL Server can automatically creates index)
  1426. Close;
  1427. // tests parsing SELECT with quoted identifiers (MySQL requires sql-mode=ANSI_QUOTES)
  1428. SQL.Text:='SELECT"ID"FROM"FPDEV"ORDER BY"ID"';
  1429. if SQLServerType in [ssPostgreSQL] then SQL.Text:=lowercase(SQL.Text); // The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard
  1430. Open;
  1431. CheckTrue(CanModify, SQL.Text);
  1432. Close;
  1433. end;
  1434. end;
  1435. procedure TTestFieldTypes.TestRowsAffected;
  1436. begin
  1437. with TSQLDBConnector(DBConnector) do
  1438. begin
  1439. AssertEquals(-1,query.RowsAffected);
  1440. Connection.ExecuteDirect('create table FPDEV2 ( ' +
  1441. ' ID INT NOT NULL , ' +
  1442. ' NAME VARCHAR(250), ' +
  1443. ' PRIMARY KEY (ID) ' +
  1444. ') ');
  1445. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  1446. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1447. Query.SQL.Text := 'insert into FPDEV2(ID,NAME) values (1,''test1'')';
  1448. Query.ExecSQL;
  1449. AssertEquals(1,query.RowsAffected);
  1450. Query.SQL.Text := 'insert into FPDEV2(ID,NAME) values (2,''test2'')';
  1451. Query.ExecSQL;
  1452. AssertEquals(1,query.RowsAffected);
  1453. Query.SQL.Text := 'update FPDEV2 set NAME=''NewTest''';
  1454. Query.ExecSQL;
  1455. AssertEquals(2,query.RowsAffected);
  1456. Query.SQL.Text := 'select * from FPDEV2';
  1457. Query.Open;
  1458. AssertTrue(query.RowsAffected<>0); // It should return -1 or the number of selected rows.
  1459. query.Close;
  1460. AssertTrue(query.RowsAffected<>0); // It should return -1 or the same as the last time it was called.
  1461. if (SQLConnType = sqlite3) then // sqlite doesn't count the rowsaffected if there is no where-clause
  1462. Query.SQL.Text := 'delete from FPDEV2 where 1'
  1463. else
  1464. Query.SQL.Text := 'delete from FPDEV2';
  1465. Query.ExecSQL;
  1466. AssertEquals(2,query.RowsAffected);
  1467. Query.SQL.Text := 'delete from FPDEV2';
  1468. Query.ExecSQL;
  1469. AssertEquals(0,query.RowsAffected);
  1470. end;
  1471. end;
  1472. procedure TTestFieldTypes.TestStringsReplace;
  1473. begin
  1474. AssertEquals('dit is een string',StringsReplace('dit was een string',['was'],['is'],[]));
  1475. AssertEquals('dit is een string was een string',StringsReplace('dit was een string was een string',['was'],['is'],[]));
  1476. AssertEquals('dit is een string is een string',StringsReplace('dit was een string was een string',['was'],['is'],[rfReplaceAll]));
  1477. AssertEquals('dit is een char is een char',StringsReplace('dit was een string was een string',['was','string'],['is','char'],[rfReplaceAll]));
  1478. AssertEquals('dit is een string was een string',StringsReplace('dit was een string was een string',['string','was'],['char','is'],[]));
  1479. AssertEquals('dit is een char is een strin',StringsReplace('dit was een string was een strin',['string','was'],['char','is'],[rfReplaceAll]));
  1480. AssertEquals('dit Was een char is een char',StringsReplace('dit Was een string was een string',['was','string'],['is','char'],[rfReplaceAll]));
  1481. AssertEquals('dit wAs een char is een char',StringsReplace('dit wAs een string was een string',['was','string'],['is','char'],[rfReplaceAll]));
  1482. AssertEquals('dit is een char is een char',StringsReplace('dit Was een sTring was een string',['was','string'],['is','char'],[rfReplaceAll,rfIgnoreCase]));
  1483. AssertEquals('dit is een char is een char',StringsReplace('dit wAs een STRING was een string',['was','string'],['is','char'],[rfReplaceAll,rfIgnoreCase]));
  1484. AssertEquals('dit was een si was een sa',StringsReplace('dit was een string was een straat',['straat','string'],['sa','si'],[rfReplaceAll]));
  1485. AssertEquals('dit was een si was een sa',StringsReplace('dit was een string was een straat',['string','straat'],['si','sa'],[rfReplaceAll]));
  1486. AssertEquals('dit was een sing was een saat',StringsReplace('dit was een string was een straat',['str','string'],['s','si'],[rfReplaceAll]));
  1487. AssertEquals('dit was een si was een saat',StringsReplace('dit was een string was een straat',['string','str'],['si','s'],[rfReplaceAll]));
  1488. AssertEquals('dit was een string was een string',StringsReplace('dit was een string was een string',[''],['is'],[rfReplaceAll]));
  1489. AssertEquals('dit een string een string',StringsReplace('dit was een string was een string',['was'],[''],[rfReplaceAll]));
  1490. end;
  1491. procedure TTestFieldTypes.TestCircularParams;
  1492. begin
  1493. with TSQLDBConnector(dbconnector) do
  1494. begin
  1495. Connection.ExecuteDirect('create table FPDEV2 (id1 int, id2 int,vchar varchar(10))');
  1496. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  1497. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1498. Query.sql.Text := 'insert into FPDEV2 values(:id1,:id2,:vchar)';
  1499. query.params[0].asinteger := 1;
  1500. query.params[1].asinteger := 1;
  1501. query.params[2].asstring := '$1 :id2 $';
  1502. query.ExecSQL;
  1503. query.sql.text := 'select * from FPDEV2';
  1504. query.open;
  1505. AssertEquals(1,query.fields[0].asinteger);
  1506. AssertEquals(1,query.fields[1].asinteger);
  1507. AssertEquals('$1 :id2 $',query.fields[2].AsString);
  1508. query.close;
  1509. end;
  1510. end;
  1511. procedure TTestFieldTypes.Test11Params;
  1512. var i : integer;
  1513. begin
  1514. with TSQLDBConnector(dbconnector) do
  1515. begin
  1516. 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)');
  1517. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  1518. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1519. Query.sql.Text := 'insert into FPDEV2 values(:id1,:id2,:id3,:id4,:id5,:id6,:id7,:id8,:id9,:id10,:id11)';
  1520. for i := 0 to 10 do
  1521. query.params[i].asinteger := 1;
  1522. query.ExecSQL;
  1523. query.sql.text := 'select * from FPDEV2';
  1524. query.open;
  1525. for i := 0 to 10 do
  1526. AssertEquals(1,query.fields[i].asinteger);
  1527. query.close;
  1528. end;
  1529. end;
  1530. procedure TTestFieldTypes.TestBug9744;
  1531. var i : integer;
  1532. begin
  1533. if not(SQLServerType in [ssMySQL, ssSQLite]) then Ignore('This test does not apply to this db-engine, since it has no double field-type');
  1534. with TSQLDBConnector(DBConnector) do
  1535. begin
  1536. try
  1537. Connection.ExecuteDirect('create table TTTOBJ ( ' +
  1538. ' ID INT NOT NULL, ' +
  1539. ' NAME VARCHAR(250), ' +
  1540. ' PRIMARY KEY (ID) ' +
  1541. ') ');
  1542. Connection.ExecuteDirect('create table TTTXY ( ' +
  1543. ' ID INT NOT NULL, ' +
  1544. ' NP INT NOT NULL, ' +
  1545. ' X DOUBLE, ' +
  1546. ' Y DOUBLE, ' +
  1547. ' PRIMARY KEY (ID,NP) ' +
  1548. ') ');
  1549. for i := 0 to 7 do
  1550. begin
  1551. connection.ExecuteDirect('insert into TTTOBJ(ID,NAME) values ('+inttostr(i)+',''A'+inttostr(i)+''')');
  1552. connection.ExecuteDirect('insert into TTTXY(ID,NP,X,Y) values ('+inttostr(i)+',1,1,1)');
  1553. connection.ExecuteDirect('insert into TTTXY(ID,NP,X,Y) values ('+inttostr(i)+',2,2,2)');
  1554. end;
  1555. 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';
  1556. query.Prepare;
  1557. query.open;
  1558. query.close;
  1559. finally
  1560. Connection.ExecuteDirect('drop table TTTXY');
  1561. Connection.ExecuteDirect('drop table TTTOBJ');
  1562. end
  1563. end;
  1564. end;
  1565. procedure TTestFieldTypes.TestCrossStringDateParam;
  1566. begin
  1567. TestXXParamQuery(ftDate,'DATE',testDateValuesCount,True);
  1568. end;
  1569. procedure TTestFieldTypes.TestGetFieldNames;
  1570. var FieldNames : TStringList;
  1571. begin
  1572. with TSQLDBConnector(DBConnector) do
  1573. begin
  1574. FieldNames := TStringList.Create;
  1575. try
  1576. if SQLConnType in MySQLConnTypes then
  1577. Connection.GetFieldNames('FPDEV',FieldNames)
  1578. else
  1579. Connection.GetFieldNames('fpDEv',FieldNames);
  1580. AssertEquals(2,FieldNames.Count);
  1581. AssertEquals('ID',UpperCase(FieldNames[0]));
  1582. AssertEquals('NAME',UpperCase(FieldNames[1]));
  1583. finally
  1584. FieldNames.Free;
  1585. end;
  1586. end;
  1587. end;
  1588. procedure TTestFieldTypes.TestGetTables;
  1589. var TableNames : TStringList;
  1590. begin
  1591. with TSQLDBConnector(DBConnector) do
  1592. begin
  1593. TableNames := TStringList.Create;
  1594. try
  1595. Connection.GetTableNames(TableNames);
  1596. AssertTrue(TableNames.Count>0);
  1597. AssertTrue(TableNames.IndexOf('FPDEV')>-1);
  1598. AssertTrue(TableNames.IndexOf('FPDEV_FIELD')>-1);
  1599. finally
  1600. TableNames.Free;
  1601. end;
  1602. end;
  1603. end;
  1604. procedure TTestFieldTypes.TestSQLFieldType(ADatatype : TFieldType; ASQLTypeDecl : string; ADataSize: integer; AGetSQLTextProc: TGetSQLTextProc; ACheckFieldValueProc: TCheckFieldValueProc);
  1605. var
  1606. i : byte;
  1607. s: string;
  1608. begin
  1609. CreateTableWithFieldType(ADatatype,ASQLTypeDecl);
  1610. TestFieldDeclaration(ADatatype,ADataSize);
  1611. for i := 0 to testValuesCount-1 do
  1612. begin
  1613. s := AGetSQLTextProc(i);
  1614. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 (FT) values (' + s + ')');
  1615. end;
  1616. with TSQLDBConnector(DBConnector).Query do
  1617. begin
  1618. Open;
  1619. for i := 0 to testValuesCount-1 do
  1620. begin
  1621. ACheckFieldValueProc(fields[0],i);
  1622. Next;
  1623. end;
  1624. close;
  1625. end;
  1626. end;
  1627. // Placed here, as long as bug 18702 is not solved
  1628. function TestSQLClob_GetSQLText(const a: integer) : string;
  1629. begin
  1630. result := QuotedStr(testStringValues[a]);
  1631. end;
  1632. procedure TTestFieldTypes.TestSQLClob;
  1633. procedure CheckFieldValue(AField:TField; a : integer);
  1634. begin
  1635. AssertEquals(testStringValues[a],AField.AsString);
  1636. end;
  1637. var datatype: string;
  1638. begin
  1639. if SQLConnType=sqlite3 then
  1640. datatype:='CLOB'
  1641. else
  1642. datatype:=FieldtypeDefinitions[ftMemo];
  1643. TestSQLFieldType(ftMemo, datatype, 0, @TestSQLClob_GetSQLText, @CheckFieldValue);
  1644. end;
  1645. // Placed here, as long as bug 18702 is not solved
  1646. function TestSQLLargeInt_GetSQLText(const a: integer) : string;
  1647. begin
  1648. result := IntToStr(testLargeIntValues[a]);
  1649. end;
  1650. procedure TTestFieldTypes.TestSQLLargeint;
  1651. procedure CheckFieldValue(AField:TField; a : integer);
  1652. begin
  1653. AssertEquals(testLargeIntValues[a],AField.AsLargeInt);
  1654. end;
  1655. var datatype: string;
  1656. begin
  1657. if SQLConnType=sqlite3 then
  1658. datatype:='LARGEINT'
  1659. else
  1660. datatype:='BIGINT';
  1661. TestSQLFieldType(ftLargeint, datatype, 8, @TestSQLLargeint_GetSQLText, @CheckFieldValue);
  1662. end;
  1663. var testIntervalValuesCount: integer;
  1664. const testIntervalValues: array[0..4] of shortstring = ('00:00:00.000','00:00:01.000','23:59:59.000','838:59:59.000','1000:00:00.000');
  1665. // Placed here, as long as bug 18702 is not solved
  1666. function TestSQLInterval_GetSQLText(const a: integer) : string;
  1667. begin
  1668. if a < testIntervalValuesCount then
  1669. Result := QuotedStr(testIntervalValues[a])
  1670. else
  1671. Result := 'NULL'
  1672. end;
  1673. procedure TTestFieldTypes.TestSQLInterval;
  1674. procedure CheckFieldValue(AField: TField; a: integer);
  1675. begin
  1676. if a < testIntervalValuesCount then
  1677. AssertEquals(testIntervalValues[a], DateTimeToTimeString(AField.AsDateTime))
  1678. else
  1679. AssertTrue(AField.IsNull);
  1680. end;
  1681. var datatype: string;
  1682. begin
  1683. if SQLConnType = postgresql then
  1684. begin
  1685. datatype:='INTERVAL';
  1686. testIntervalValuesCount := 5;
  1687. end
  1688. else
  1689. begin
  1690. datatype:=FieldtypeDefinitions[ftTime];
  1691. if datatype = '' then
  1692. Ignore(STestNotApplicable);
  1693. if SQLConnType = sqlite3 then
  1694. testIntervalValuesCount := 5
  1695. else if SQLConnType in MySQLConnTypes then
  1696. testIntervalValuesCount := 4
  1697. else
  1698. testIntervalValuesCount := 3;
  1699. end;
  1700. TestSQLFieldType(ftTime, datatype, sizeof(TDateTime), @TestSQLInterval_GetSQLText, @CheckFieldValue);
  1701. end;
  1702. procedure TTestFieldTypes.TestSQLIdentity;
  1703. var datatype, values: string;
  1704. fieldtype: TFieldType;
  1705. i: integer;
  1706. updatable: boolean;
  1707. begin
  1708. if SQLConnType in MySQLConnTypes then
  1709. begin
  1710. datatype:='INT AUTO_INCREMENT PRIMARY KEY';
  1711. values:='VALUES(DEFAULT)';
  1712. fieldtype:=ftAutoInc;
  1713. updatable:=true;
  1714. end
  1715. else if SQLConnType = sqlite3 then
  1716. begin
  1717. datatype:='INTEGER PRIMARY KEY';
  1718. values:='DEFAULT VALUES';
  1719. fieldtype:=ftInteger;
  1720. updatable:=true;
  1721. end
  1722. else if SQLConnType = postgresql then
  1723. begin
  1724. datatype:='SERIAL';
  1725. values:='DEFAULT VALUES';
  1726. fieldtype:=ftInteger;
  1727. updatable:=true;
  1728. end
  1729. else if SQLConnType = mssql then
  1730. begin
  1731. datatype:='INTEGER IDENTITY';
  1732. values:='DEFAULT VALUES';
  1733. fieldtype:=ftAutoInc;
  1734. updatable:=false;
  1735. end
  1736. else
  1737. Ignore(STestNotApplicable);
  1738. CreateTableWithFieldType(fieldtype, datatype);
  1739. TestFieldDeclaration(fieldtype, sizeof(longint));
  1740. for i := 1 to 3 do
  1741. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('insert into FPDEV2 '+values);
  1742. with TSQLDBConnector(DBConnector).Query do
  1743. begin
  1744. Open;
  1745. AssertTrue(Locate('FT',1,[])); // bug 17624
  1746. for i := 1 to 3 do
  1747. begin
  1748. AssertEquals(i, Fields[0].AsInteger);
  1749. Next;
  1750. end;
  1751. // some databases (like MS SQL Server) do not allow updating identity columns
  1752. AssertEquals('ReadOnly', Fields[0].ReadOnly, not updatable);
  1753. // some databases (like PostgreSQL, MySQL) allow inserting explicit values and updating auto incrementing columns
  1754. if updatable then
  1755. begin
  1756. UpdateMode:=upWhereAll; // if there is no PK for FPDEV2 table
  1757. // updating:
  1758. Last;
  1759. while not Bof do
  1760. begin
  1761. Edit;
  1762. Fields[0].AsInteger:=Fields[0].AsInteger+2;
  1763. Post;
  1764. Prior;
  1765. end;
  1766. // inserting:
  1767. Append;
  1768. Fields[0].AsInteger:=6;
  1769. Post;
  1770. ApplyUpdates;
  1771. end;
  1772. Close;
  1773. end;
  1774. end;
  1775. procedure TTestFieldTypes.TestUpdateIndexDefs;
  1776. var ds : TSQLQuery;
  1777. begin
  1778. ds := DBConnector.GetNDataset(1) as TSQLQuery;
  1779. ds.Prepare;
  1780. ds.ServerIndexDefs.Update;
  1781. AssertEquals(1,ds.ServerIndexDefs.count);
  1782. AssertTrue(CompareText('ID',ds.ServerIndexDefs[0].Fields)=0);
  1783. Asserttrue(ds.ServerIndexDefs[0].Options=[ixPrimary,ixUnique]);
  1784. ds.ServerIndexDefs.Update;
  1785. AssertEquals(1,ds.ServerIndexDefs.count);
  1786. AssertTrue(CompareText('ID',ds.ServerIndexDefs[0].Fields)=0);
  1787. Asserttrue(ds.ServerIndexDefs[0].Options=[ixPrimary,ixUnique]);
  1788. end;
  1789. procedure TTestFieldTypes.TestMultipleFieldPKIndexDefs;
  1790. var ds : TSQLQuery;
  1791. begin
  1792. TSQLDBConnector(DBConnector).Connection.ExecuteDirect('create table FPDEV2 (' +
  1793. ' ID1 INT NOT NULL, ' +
  1794. ' ID2 INT NOT NULL, ' +
  1795. ' NAME VARCHAR(50), ' +
  1796. ' PRIMARY KEY (ID1, ID2) ' +
  1797. ') ');
  1798. // Firebird/Interbase need a commit after a DDL statement. Not necessary for the other connections
  1799. if SQLConnType=interbase then TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1800. ds := TSQLDBConnector(DBConnector).Query;
  1801. ds.sql.Text:='select * from FPDEV2';
  1802. ds.Prepare;
  1803. ds.ServerIndexDefs.Update;
  1804. AssertEquals(1,ds.ServerIndexDefs.count);
  1805. AssertTrue(SameText('ID1;ID2',ds.ServerIndexDefs[0].Fields));
  1806. Asserttrue(ds.ServerIndexDefs[0].Options=[ixPrimary,ixUnique]);
  1807. end;
  1808. procedure TTestFieldTypes.TestSetBlobAsMemoParam;
  1809. begin
  1810. TestSetBlobAsParam(0);
  1811. end;
  1812. procedure TTestFieldTypes.TestSetBlobAsBlobParam;
  1813. begin
  1814. TestSetBlobAsParam(2);
  1815. end;
  1816. procedure TTestFieldTypes.TestTemporaryTable;
  1817. begin
  1818. if SQLServerType in [ssMSSQL, ssSybase] then Ignore('This test does not apply to this sqldb-connection type, since it doesn''t support temporary tables');
  1819. with TSQLDBConnector(DBConnector).Query do
  1820. begin
  1821. SQL.Clear;
  1822. if SQLConnType=interbase then
  1823. // Global temporary table: introduced in Firebird 2.1
  1824. // has persistent metadata; data is per transaction (default) or per connection
  1825. SQL.Add('CREATE GLOBAL TEMPORARY TABLE FPDEV_TEMP (id int)')
  1826. else
  1827. SQL.Add('CREATE TEMPORARY TABLE FPDEV_TEMP (id int)');
  1828. ExecSQL;
  1829. try
  1830. // Firebird/Interbase needs a commit after DDL:
  1831. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1832. SQL.Text := 'INSERT INTO FPDEV_TEMP(id) values (5)';
  1833. ExecSQL;
  1834. SQL.Text := 'SELECT * FROM FPDEV_TEMP';
  1835. Open;
  1836. AssertEquals(5, Fields[0].AsInteger);
  1837. Close;
  1838. finally
  1839. // For Firebird/Interbase, we need to explicitly delete the table as well (it's active within the transaction)
  1840. if SQLConnType=interbase then
  1841. begin
  1842. SQL.Text := 'DROP TABLE FPDEV_TEMP';
  1843. ExecSQL;
  1844. TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
  1845. end;
  1846. end;
  1847. end;
  1848. end;
  1849. procedure TTestFieldTypes.TestGetIndexDefs;
  1850. var ds : TSQLQuery;
  1851. inddefs : TIndexDefs;
  1852. begin
  1853. ds := DBConnector.GetNDataset(1) as TSQLQuery;
  1854. ds.Open;
  1855. AssertEquals('ServerIndexDefs.Count', 1, ds.ServerIndexDefs.Count);
  1856. inddefs := HackedDataset(ds).GetIndexDefs(ds.ServerIndexDefs,[ixPrimary]);
  1857. AssertEquals('ixPrimary', 1, inddefs.count);
  1858. AssertTrue(CompareText('ID',inddefs[0].Fields)=0);
  1859. AssertTrue(inddefs[0].Options=[ixPrimary,ixUnique]);
  1860. inddefs.Free;
  1861. inddefs := HackedDataset(ds).GetIndexDefs(ds.ServerIndexDefs,[ixPrimary,ixUnique]);
  1862. AssertEquals('ixPrimary,ixUnique', 1, inddefs.count);
  1863. AssertTrue(CompareText('ID',inddefs[0].Fields)=0);
  1864. AssertTrue(inddefs[0].Options=[ixPrimary,ixUnique]);
  1865. inddefs.Free;
  1866. inddefs := HackedDataset(ds).GetIndexDefs(ds.ServerIndexDefs,[ixDescending]);
  1867. AssertEquals('ixDescending', 0, inddefs.count);
  1868. inddefs.Free;
  1869. end;
  1870. procedure TTestFieldTypes.TestDblQuoteEscComments;
  1871. begin
  1872. with TSQLDBConnector(DBConnector).Query do
  1873. begin
  1874. SQL.Clear;
  1875. SQL.Add('select * from FPDEV where name=''test '''' and :ThisIsNotAParameter ''');
  1876. open;
  1877. close;
  1878. end;
  1879. end;
  1880. procedure TTestFieldTypes.TestParametersAndDates;
  1881. // See bug 7205
  1882. var ADateStr : String;
  1883. begin
  1884. if not(SQLConnType in [postgresql,odbc,oracle]) then
  1885. Ignore('This test does not apply to this sqldb-connection type, since it doesn''t use semicolons for casts');
  1886. with TSQLDBConnector(DBConnector).Query do
  1887. begin
  1888. SQL.Clear;
  1889. sql.add('select now()::date as current_date where 1=1');
  1890. open;
  1891. first;
  1892. ADateStr:=fields[0].asstring; // return the correct date
  1893. // writeln(fields[0].asstring);
  1894. close;
  1895. sql.clear;
  1896. sql.add('select now()::date as current_date where cast(1 as integer) = :PARAM1');
  1897. params.parambyname('PARAM1').asinteger:= 1;
  1898. open;
  1899. first;
  1900. AssertEquals(ADateStr,fields[0].asstring); // return invalid date
  1901. // writeln(fields[0].asstring);
  1902. close;
  1903. end
  1904. end;
  1905. procedure TTestFieldTypes.TestExceptOnsecClose;
  1906. var passed : boolean;
  1907. begin
  1908. with TSQLDBConnector(DBConnector).Query do
  1909. begin
  1910. SQL.Clear;
  1911. SQL.Add('select * from FPDEV');
  1912. Open;
  1913. close;
  1914. SQL.Clear;
  1915. SQL.Add('select blaise from FPDEV');
  1916. passed := false;
  1917. try
  1918. open;
  1919. except
  1920. on E: Exception do
  1921. passed := (E.ClassType.InheritsFrom(EDatabaseError))
  1922. end;
  1923. AssertTrue(passed);
  1924. Close;
  1925. end;
  1926. end;
  1927. procedure TTestFieldTypes.TestErrorOnEmptyStatement;
  1928. var PassException : boolean;
  1929. begin
  1930. PassException:=False;
  1931. with TSQLDBConnector(DBConnector).Query do
  1932. begin
  1933. sql.Text := '';
  1934. try
  1935. Open;
  1936. except
  1937. on E:EDatabaseError do
  1938. if Pos(SErrNoStatement,E.Message) > 0 then
  1939. PassException := True;
  1940. end;
  1941. AssertTrue(PassException);
  1942. end;
  1943. end;
  1944. initialization
  1945. if uppercase(dbconnectorname)='SQL' then RegisterTest(TTestFieldTypes);
  1946. end.