testfieldtypes.pas 72 KB

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