testsqlscript.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2008 by the Free Pascal development team
  4. FPCUnit SQLScript test.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit testsqlscript;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, testregistry, sqlscript, fpcunit;
  16. type
  17. { TMyScript }
  18. TMyScript = class (TCustomSQLScript)
  19. private
  20. FExcept: string;
  21. FStatements : TStrings;
  22. FDirectives : TStrings;
  23. FCommits : integer;
  24. protected
  25. procedure ExecuteStatement (SQLStatement: TStrings; var StopExecution: Boolean); override;
  26. procedure ExecuteDirective (Directive, Argument: String; var StopExecution: Boolean); override;
  27. procedure ExecuteCommit(CommitRetaining: boolean=true); override;
  28. procedure DefaultDirectives; override;
  29. public
  30. constructor create (AnOwner: TComponent); override;
  31. destructor destroy; override;
  32. function StatementsExecuted : string;
  33. function DirectivesExecuted : string;
  34. property DoException : string read FExcept write FExcept;
  35. property Aborted;
  36. property Line;
  37. property UseDollarString;
  38. property dollarstrings;
  39. property Directives;
  40. property Defines;
  41. property Script;
  42. property Terminator;
  43. property CommentsinSQL;
  44. property UseSetTerm;
  45. property UseCommit;
  46. property UseDefines;
  47. property OnException;
  48. end;
  49. { TTestSQLScript }
  50. TTestSQLScript = class (TTestCase)
  51. private
  52. Script : TMyScript;
  53. exceptionstatement,
  54. exceptionmessage : string;
  55. UseContinue : boolean;
  56. procedure Add (s :string);
  57. procedure AssertStatDir (Statements, Directives : string);
  58. procedure DoExecution;
  59. procedure ExceptionHandler(Sender: TObject; Statement: TStrings; TheException: Exception; var Continue: boolean);
  60. procedure TestDirectiveOnException3;
  61. protected
  62. procedure SetUp; override;
  63. procedure TearDown; override;
  64. published
  65. procedure TestCreateDefaults;
  66. procedure TestTerminator;
  67. procedure TestSetTerm;
  68. procedure TestUseSetTerm;
  69. procedure TestComments;
  70. procedure TestUseComments;
  71. procedure TestCommit;
  72. procedure TestUseCommit;
  73. procedure TestDefine;
  74. procedure TestUndefine;
  75. procedure TestUndef;
  76. procedure TestIfdef1;
  77. procedure TestIfdef2;
  78. procedure TestIfndef1;
  79. procedure TestIfndef2;
  80. procedure TestElse1;
  81. procedure TestElse2;
  82. procedure TestEndif1;
  83. procedure TestEndif2;
  84. procedure TestUseDefines;
  85. procedure TestTermInComment;
  86. procedure TestTermInQuotes1;
  87. procedure TestTermInQuotes2;
  88. procedure TestCommentInComment;
  89. procedure TestCommentInQuotes1;
  90. procedure TestCommentInQuotes2;
  91. Procedure TestDashDashComment;
  92. procedure TestQuote1InComment;
  93. procedure TestQuote2InComment;
  94. procedure TestQuoteInQuotes1;
  95. procedure TestQuoteInQuotes2;
  96. procedure TestStatementStop;
  97. procedure TestDirectiveStop;
  98. procedure TestStatementExeception;
  99. procedure TestDirectiveException;
  100. procedure TestCommitException;
  101. procedure TestStatementOnExeception1;
  102. procedure TestStatementOnExeception2;
  103. procedure TestDirectiveOnException1;
  104. procedure TestDirectiveOnException2;
  105. procedure TestCommitOnException1;
  106. procedure TestCommitOnException2;
  107. procedure TestUseDollarSign;
  108. procedure TestUseDollarSign2;
  109. procedure TestUseDollarSign3;
  110. end;
  111. { TTestEventSQLScript }
  112. TTestEventSQLScript = class (TTestCase)
  113. private
  114. Script : TEventSQLScript;
  115. StopToSend : boolean;
  116. Received : string;
  117. notifycount : integer;
  118. LastSender : TObject;
  119. procedure Notify (Sender : TObject);
  120. procedure NotifyStatement (Sender: TObject; SQL_Statement: TStrings; var StopExecution: Boolean);
  121. procedure NotifyDirective (Sender: TObject; Directive, Argument: AnsiString; var StopExecution: Boolean);
  122. protected
  123. procedure SetUp; override;
  124. procedure TearDown; override;
  125. published
  126. procedure TestStatement;
  127. procedure TestStatementStop;
  128. procedure TestDirective;
  129. procedure TestDirectiveStop;
  130. procedure TestCommit;
  131. procedure TestBeforeExec;
  132. procedure TestAfterExec;
  133. end;
  134. implementation
  135. { TMyScript }
  136. procedure TMyScript.ExecuteStatement(SQLStatement: TStrings; var StopExecution: Boolean);
  137. var s : string;
  138. r : integer;
  139. begin
  140. if (SQLStatement.count = 1) and (compareText(SQLStatement[0],'END')=0) then
  141. StopExecution := true;
  142. s := '';
  143. for r := 0 to SQLstatement.count-1 do
  144. begin
  145. if s <> '' then
  146. s := s + ' ';
  147. s := s + SQLStatement[r];
  148. end;
  149. FStatements.Add (s);
  150. if DoException <> '' then
  151. raise exception.create(DoException);
  152. end;
  153. procedure TMyScript.ExecuteDirective(Directive, Argument: String; var StopExecution: Boolean);
  154. begin
  155. if Directive = 'STOP' then
  156. StopExecution := true;
  157. if Argument = '' then
  158. FDirectives.Add (Directive)
  159. else
  160. FDirectives.Add (format('%s(%s)', [Directive, Argument]));
  161. if DoException <> '' then
  162. raise exception.create(DoException);
  163. end;
  164. procedure TMyScript.ExecuteCommit(CommitRetaining: boolean=true);
  165. begin
  166. inc (FCommits);
  167. if DoException <> '' then
  168. raise exception.create(DoException);
  169. end;
  170. procedure TMyScript.DefaultDirectives;
  171. begin
  172. inherited DefaultDirectives;
  173. directives.add ('STOP');
  174. end;
  175. constructor TMyScript.create (AnOwner: TComponent);
  176. begin
  177. inherited create (AnOwner);
  178. FStatements := TStringlist.Create;
  179. FDirectives := TStringlist.Create;
  180. FCommits := 0;
  181. DoException := '';
  182. end;
  183. destructor TMyScript.destroy;
  184. begin
  185. FStatements.Free;
  186. FDirectives.Free;
  187. inherited destroy;
  188. end;
  189. function TMyScript.StatementsExecuted: string;
  190. begin
  191. result := FStatements.Commatext;
  192. end;
  193. function TMyScript.DirectivesExecuted: string;
  194. begin
  195. result := FDirectives.Commatext;
  196. end;
  197. { TTestSQLScript }
  198. procedure TTestSQLScript.Add(s: string);
  199. begin
  200. Script.Script.Add (s);
  201. end;
  202. procedure TTestSQLScript.AssertStatDir(Statements, Directives: string);
  203. begin
  204. AssertEquals ('Executed Statements', Statements, script.StatementsExecuted);
  205. AssertEquals ('Executed Directives', Directives, script.DirectivesExecuted);
  206. end;
  207. procedure TTestSQLScript.DoExecution;
  208. begin
  209. script.execute;
  210. end;
  211. procedure TTestSQLScript.ExceptionHandler(Sender: TObject; Statement: TStrings;
  212. TheException: Exception; var Continue: boolean);
  213. var r : integer;
  214. s : string;
  215. begin
  216. Continue := UseContinue;
  217. if Statement.count > 0 then
  218. s := Statement[0];
  219. for r := 1 to Statement.count-1 do
  220. s := s + ',' + Statement[r];
  221. exceptionstatement := s;
  222. exceptionmessage := TheException.message;
  223. end;
  224. procedure TTestSQLScript.SetUp;
  225. begin
  226. inherited SetUp;
  227. Script := TMyscript.Create (nil);
  228. end;
  229. procedure TTestSQLScript.TearDown;
  230. begin
  231. Script.Free;
  232. inherited TearDown;
  233. end;
  234. procedure TTestSQLScript.TestCreateDefaults;
  235. begin
  236. with Script do
  237. begin
  238. AssertEquals ('Terminator', ';', Terminator);
  239. AssertTrue ('UseCommit', UseCommit);
  240. AssertTrue ('UseSetTerm', UseSetTerm);
  241. AssertTrue ('UseDefines', UseDefines);
  242. AssertTrue ('CommentsInSQL', CommentsInSQL);
  243. AssertFalse ('Aborted', Aborted);
  244. AssertEquals ('Line', 0, Line);
  245. AssertEquals ('Defines', 0, Defines.count);
  246. AssertEquals ('Directives', 12, Directives.count);
  247. AssertTrue('Have SET TERM',Directives.IndexOf('SET TERM')<>-1);
  248. AssertTrue('Have COMMIT WORK',Directives.IndexOf('COMMIT WORK')<>-1);
  249. AssertTrue('Have COMMIT RETAIN',Directives.IndexOf('COMMIT RETAIN')<>-1);
  250. AssertTrue('Have COMMIT',Directives.IndexOf('COMMIT')<>-1);
  251. AssertTrue('Have #IFDEF',Directives.IndexOf('#IFDEF')<>-1);
  252. AssertTrue('Have #IFNDEF',Directives.IndexOf('#IFNDEF')<>-1);
  253. AssertTrue('Have #ELSE',Directives.IndexOf('#ELSE')<>-1);
  254. AssertTrue('Have #ENDIF',Directives.IndexOf('#ENDIF')<>-1);
  255. AssertTrue('Have #DEFINE',Directives.IndexOf('#DEFINE')<>-1);
  256. AssertTrue('Have #UNDEF',Directives.IndexOf('#UNDEF')<>-1);
  257. AssertTrue('Have #UNDEFINE',Directives.IndexOf('#UNDEFINE')<>-1);
  258. // This is defined in our test class.
  259. AssertTrue('Have STOP',Directives.IndexOf('STOP')<>-1);
  260. end;
  261. end;
  262. procedure TTestSQLScript.TestTerminator;
  263. begin
  264. script.terminator := '!';
  265. Add('doe!iets!');
  266. Add('anders!');
  267. script.execute;
  268. AssertStatDir('doe,iets,anders', '');
  269. end;
  270. procedure TTestSQLScript.TestSetTerm;
  271. begin
  272. script.UseSetTerm:=true;
  273. Add('SET TERM !;');
  274. script.execute;
  275. AssertEquals ('terminator', '!', script.terminator);
  276. AssertStatDir('', '');
  277. end;
  278. procedure TTestSQLScript.TestUseSetTerm;
  279. begin
  280. script.UseSetTerm:=false;
  281. Script.Directives.Add ('SET TERM');
  282. Add('SET TERM !;');
  283. script.execute;
  284. AssertEquals ('terminator', ';', script.terminator);
  285. AssertStatDir('', '"SET TERM(!)"');
  286. end;
  287. procedure TTestSQLScript.TestComments;
  288. begin
  289. script.CommentsInSQL := true;
  290. Add('/* comment */');
  291. Add('statement;');
  292. script.execute;
  293. AssertStatDir ('"/* comment */ statement"', '');
  294. end;
  295. procedure TTestSQLScript.TestUseComments;
  296. begin
  297. script.CommentsInSQL := false;
  298. Add('/* comment */');
  299. Add('statement;');
  300. script.execute;
  301. AssertStatDir ('statement', '');
  302. end;
  303. procedure TTestSQLScript.TestCommit;
  304. begin
  305. script.UseCommit := true;
  306. Add('commit;');
  307. script.execute;
  308. AssertEquals ('Commits', 1, script.FCommits);
  309. AssertStatDir ('', '');
  310. end;
  311. procedure TTestSQLScript.TestUseCommit;
  312. begin
  313. script.UseCommit := false;
  314. with script.Directives do
  315. Delete(IndexOf('COMMIT'));
  316. Add('commit;');
  317. script.execute;
  318. AssertEquals ('Commits', 0, script.FCommits);
  319. AssertStatDir ('commit', '');
  320. end;
  321. procedure TTestSQLScript.TestDefine;
  322. begin
  323. script.UseDefines := true;
  324. Add ('#define iets;');
  325. script.execute;
  326. AssertStatDir ('', '');
  327. AssertEquals ('Aantal defines', 1, script.defines.count);
  328. AssertEquals ('Juiste define', 'iets', script.Defines[0]);
  329. end;
  330. procedure TTestSQLScript.TestUndefine;
  331. begin
  332. script.UseDefines := true;
  333. script.defines.Add ('iets');
  334. Add ('#undefine iets;');
  335. script.execute;
  336. AssertStatDir ('', '');
  337. AssertEquals ('Aantal defines', 0, script.defines.count);
  338. end;
  339. procedure TTestSQLScript.TestUndef;
  340. begin
  341. script.UseDefines := true;
  342. script.defines.Add ('iets');
  343. Add ('#Undef iets;');
  344. script.execute;
  345. AssertStatDir ('', '');
  346. AssertEquals ('Aantal defines', 0, script.defines.count);
  347. end;
  348. procedure TTestSQLScript.TestIfdef1;
  349. begin
  350. script.UseDefines := true;
  351. script.defines.add ('iets');
  352. Add('#ifdef iets;');
  353. Add('doe iets;');
  354. script.execute;
  355. AssertStatDir('"doe iets"', '');
  356. end;
  357. procedure TTestSQLScript.TestIfdef2;
  358. begin
  359. script.UseDefines := true;
  360. Add('#ifdef iets;');
  361. Add('doe iets;');
  362. script.execute;
  363. AssertStatDir('', '');
  364. end;
  365. procedure TTestSQLScript.TestIfndef1;
  366. begin
  367. script.UseDefines := true;
  368. Add('#ifndef iets;');
  369. Add('doe iets;');
  370. script.execute;
  371. AssertStatDir('"doe iets"', '');
  372. end;
  373. procedure TTestSQLScript.TestIfndef2;
  374. begin
  375. script.UseDefines := true;
  376. script.defines.add ('iets');
  377. Add('#ifndef iets;');
  378. Add('doe iets;');
  379. script.execute;
  380. AssertStatDir('', '');
  381. end;
  382. procedure TTestSQLScript.TestElse1;
  383. begin
  384. script.UseDefines := true;
  385. script.defines.add ('iets');
  386. Add('#ifdef iets;');
  387. Add('doe iets;');
  388. add('#else;');
  389. add('anders;');
  390. script.execute;
  391. AssertStatDir('"doe iets"', '');
  392. end;
  393. procedure TTestSQLScript.TestElse2;
  394. begin
  395. script.UseDefines := true;
  396. script.defines.add ('iets');
  397. Add('#ifndef iets;');
  398. Add('doe iets;');
  399. add('#else;');
  400. add('anders;');
  401. script.execute;
  402. AssertStatDir('anders', '');
  403. end;
  404. procedure TTestSQLScript.TestEndif1;
  405. begin
  406. script.UseDefines := true;
  407. Add('#ifdef iets;');
  408. Add('doe iets;');
  409. add('#endif;');
  410. add('anders;');
  411. script.execute;
  412. AssertStatDir('anders', '');
  413. end;
  414. procedure TTestSQLScript.TestEndif2;
  415. begin
  416. script.UseDefines := true;
  417. Add('#ifndef iets;');
  418. Add('doe iets;');
  419. add('#endif;');
  420. add('anders;');
  421. script.execute;
  422. AssertStatDir('"doe iets",anders', '');
  423. end;
  424. procedure TTestSQLScript.TestUseDefines;
  425. begin
  426. script.UseDefines := false;
  427. Add('#ifndef iets;');
  428. Add('doe iets;');
  429. add('#endif;');
  430. add('anders;');
  431. script.execute;
  432. AssertStatDir('"doe iets",anders', '#IFNDEF(iets),#ENDIF');
  433. end;
  434. procedure TTestSQLScript.TestTermInComment;
  435. begin
  436. script.CommentsInSQL := false;
  437. Add('/* terminator ; */iets;');
  438. script.execute;
  439. AssertStatDir('iets', '');
  440. end;
  441. procedure TTestSQLScript.TestTermInQuotes1;
  442. begin
  443. script.CommentsInSQL := false;
  444. Add('iets '';'';');
  445. script.execute;
  446. AssertStatDir('"iets '';''"', '');
  447. end;
  448. procedure TTestSQLScript.TestTermInQuotes2;
  449. begin
  450. script.CommentsInSQL := false;
  451. Add('iets ";";');
  452. script.execute;
  453. AssertStatDir('"iets "";"""', '');
  454. end;
  455. procedure TTestSQLScript.TestCommentInComment;
  456. begin
  457. script.CommentsInSQL := false;
  458. Add('/* meer /* */iets;');
  459. script.execute;
  460. AssertStatDir('iets', '');
  461. end;
  462. procedure TTestSQLScript.TestCommentInQuotes1;
  463. begin
  464. script.CommentsInSQL := false;
  465. Add('iets ''/* meer */'';');
  466. script.execute;
  467. AssertStatDir('"iets ''/* meer */''"', '');
  468. end;
  469. procedure TTestSQLScript.TestCommentInQuotes2;
  470. begin
  471. script.CommentsInSQL := false;
  472. Add('iets "/* meer */";');
  473. script.execute;
  474. AssertStatDir('"iets ""/* meer */"""', '');
  475. end;
  476. procedure TTestSQLScript.TestDashDashComment;
  477. begin
  478. script.CommentsInSQL := false;
  479. Add('-- my comment');
  480. Add('CREATE TABLE "tPatients" (');
  481. Add(' "BloodGroup" character(2),');
  482. Add(' CONSTRAINT "ck_tPatients_BloodGroup" CHECK (("BloodGroup" = ANY (ARRAY[''A''::bpchar, ''B''::bpchar, ''AB''::bpchar, ''0''::bpchar]))),');
  483. Add(');');
  484. script.execute;
  485. AssertStatDir('"CREATE TABLE ""tPatients"" ( ""BloodGroup"" character(2), CONSTRAINT ""ck_tPatients_BloodGroup"" CHECK ((""BloodGroup"" = ANY (ARRAY[''A''::bpchar, ''B''::bpchar, ''AB''::bpchar, ''0''::bpchar]))), )"', '');
  486. end;
  487. procedure TTestSQLScript.TestQuote1InComment;
  488. begin
  489. script.CommentsInSQL := false;
  490. Add('/* s''morgens */iets;');
  491. script.execute;
  492. AssertStatDir('iets', '');
  493. end;
  494. procedure TTestSQLScript.TestQuote2InComment;
  495. begin
  496. script.CommentsInSQL := false;
  497. Add('/* s"morgens */iets;');
  498. script.execute;
  499. AssertStatDir('iets', '');
  500. end;
  501. procedure TTestSQLScript.TestQuoteInQuotes1;
  502. begin
  503. script.CommentsInSQL := false;
  504. Add('iets ''s"morgens'';');
  505. script.execute;
  506. AssertStatDir('"iets ''s""morgens''"', '');
  507. end;
  508. procedure TTestSQLScript.TestQuoteInQuotes2;
  509. begin
  510. script.CommentsInSQL := false;
  511. Add('iets "s''morgens";');
  512. script.execute;
  513. AssertStatDir('"iets ""s''morgens"""', '');
  514. end;
  515. procedure TTestSQLScript.TestStatementStop;
  516. begin
  517. Add('END;meer;');
  518. script.execute;
  519. AssertStatDir('END', '');
  520. end;
  521. procedure TTestSQLScript.TestDirectiveStop;
  522. begin
  523. Add('Stop;meer;');
  524. script.execute;
  525. AssertStatDir('', 'STOP');
  526. end;
  527. procedure TTestSQLScript.TestStatementExeception;
  528. begin
  529. Add('iets;');
  530. script.DoException:='FOUT';
  531. AssertException (exception, @DoExecution);
  532. AssertStatDir('iets', '');
  533. end;
  534. procedure TTestSQLScript.TestDirectiveException;
  535. begin
  536. Add('iets;');
  537. script.Directives.Add('IETS');
  538. script.DoException := 'FOUT';
  539. AssertException (exception, @DoExecution);
  540. AssertStatDir('', 'IETS');
  541. end;
  542. procedure TTestSQLScript.TestCommitException;
  543. begin
  544. Add ('commit;');
  545. script.DoException := 'FOUT';
  546. AssertException (exception, @DoExecution);
  547. AssertStatDir('', '');
  548. AssertEquals ('Commit count', 1, Script.FCommits);
  549. end;
  550. procedure TTestSQLScript.TestStatementOnExeception1;
  551. begin
  552. UseContinue := true;
  553. script.DoException := 'Fout';
  554. Add ('foutief;');
  555. script.OnException:=@ExceptionHandler;
  556. Script.Execute;
  557. AssertEquals ('exception message', 'Fout', exceptionmessage);
  558. AssertEquals ('exception statement', 'foutief', exceptionstatement);
  559. end;
  560. procedure TTestSQLScript.TestStatementOnExeception2;
  561. begin
  562. UseContinue := false;
  563. script.DoException := 'Fout';
  564. Add ('foutief;');
  565. script.OnException:=@ExceptionHandler;
  566. AssertException (exception, @DoExecution);
  567. AssertEquals ('exception message', 'Fout', exceptionmessage);
  568. AssertEquals ('exception statement', 'foutief', exceptionstatement);
  569. end;
  570. procedure TTestSQLScript.TestDirectiveOnException1;
  571. begin
  572. UseContinue := true;
  573. script.DoException := 'Fout';
  574. Add ('foutief;');
  575. Script.Directives.Add ('FOUTIEF');
  576. script.OnException:=@ExceptionHandler;
  577. Script.Execute;
  578. AssertEquals ('exception message', 'Fout', exceptionmessage);
  579. AssertEquals ('exception statement', 'FOUTIEF', exceptionstatement);
  580. end;
  581. procedure TTestSQLScript.TestDirectiveOnException2;
  582. begin
  583. UseContinue := False;
  584. script.DoException := 'Fout';
  585. Add ('foutief;');
  586. Script.Directives.Add ('FOUTIEF');
  587. script.OnException:=@ExceptionHandler;
  588. AssertException (exception, @DoExecution);
  589. AssertEquals ('exception message', 'Fout', exceptionmessage);
  590. AssertEquals ('exception statement', 'FOUTIEF', exceptionstatement);
  591. end;
  592. procedure TTestSQLScript.TestDirectiveOnException3;
  593. begin
  594. UseContinue := true;
  595. script.DoException := 'Fout';
  596. Add ('foutief probleem;');
  597. Script.Directives.Add ('FOUTIEF');
  598. script.OnException:=@ExceptionHandler;
  599. Script.Execute;
  600. AssertEquals ('exception message', 'Fout', exceptionmessage);
  601. AssertEquals ('exception statement', 'FOUTIEF,probleem', exceptionstatement);
  602. end;
  603. procedure TTestSQLScript.TestCommitOnException1;
  604. begin
  605. UseContinue := true;
  606. script.DoException := 'Fout';
  607. Add ('Commit;');
  608. script.OnException:=@ExceptionHandler;
  609. Script.Execute;
  610. AssertEquals ('exception message', 'Fout', exceptionmessage);
  611. AssertEquals ('exception statement', 'COMMIT', exceptionstatement);
  612. AssertEquals ('commit count', 1, Script.FCommits);
  613. end;
  614. procedure TTestSQLScript.TestCommitOnException2;
  615. begin
  616. UseContinue := false;
  617. script.DoException := 'Fout';
  618. Add ('Commit;');
  619. script.OnException:=@ExceptionHandler;
  620. AssertException (exception, @DoExecution);
  621. AssertEquals ('exception message', 'Fout', exceptionmessage);
  622. AssertEquals ('exception statement', 'COMMIT', exceptionstatement);
  623. AssertEquals ('commit count', 1, Script.FCommits);
  624. end;
  625. Const
  626. PLSQL1 = 'CREATE or replace FUNCTION test_double_bad_sum ( value1 int, value2 int ) '+
  627. 'RETURNS int AS $$ '+
  628. 'DECLARE '+
  629. ' TheDoubleSum int; '+
  630. 'BEGIN '+
  631. ' -- Start '+
  632. ' TheDoubleSum := value1; '+
  633. ' /* sum '+
  634. ' number '+
  635. ' 1 */ '+
  636. ' TheDoubleSum := TheDoubleSum + value2; '+
  637. ' TheDoubleSum := TheDoubleSum + value2; -- Sum number 2 '+
  638. ' return TheDoubleSum; '+
  639. 'END; '+
  640. '$$ '+
  641. 'LANGUAGE plpgsql';
  642. PLSQL2 = 'COMMENT ON FUNCTION test_double_bad_sum(IN integer, IN integer) '+
  643. ' IS ''Just a '+
  644. ' test function '+
  645. ' !!!''';
  646. PLSQL3 = 'CREATE or replace FUNCTION test_double_bad_sum ( value1 int, value2 int ) '+
  647. 'RETURNS int AS $BOB$ '+
  648. 'DECLARE '+
  649. ' TheDoubleSum int; '+
  650. 'BEGIN '+
  651. ' -- Start '+
  652. ' TheDoubleSum := value1; '+
  653. ' /* sum '+
  654. ' number '+
  655. ' 1 */ '+
  656. ' TheDoubleSum := TheDoubleSum + value2; '+
  657. ' TheDoubleSum := TheDoubleSum + value2; -- Sum number 2 '+
  658. ' return TheDoubleSum; '+
  659. 'END; '+
  660. '$BOB$ '+
  661. 'LANGUAGE plpgsql';
  662. procedure TTestSQLScript.TestUseDollarSign;
  663. begin
  664. script.UseDollarString:=True;
  665. Add(PLSQL1+';');
  666. script.execute;
  667. // Double quotes because there are spaces.
  668. AssertStatDir('"'+plsql1+'"', '');
  669. end;
  670. procedure TTestSQLScript.TestUseDollarSign2;
  671. begin
  672. script.UseDollarString:=True;
  673. Add(PLSQL1+';');
  674. Add(PLSQL2+';');
  675. script.execute;
  676. // Double quotes because there are spaces.
  677. AssertStatDir('"'+plsql1+'","'+PLSQL2+'"', '');
  678. end;
  679. procedure TTestSQLScript.TestUseDollarSign3;
  680. begin
  681. script.UseDollarString:=True;
  682. script.DollarStrings.Add('BOB');
  683. Add(PLSQL3+';');
  684. script.execute;
  685. // Double quotes because there are spaces.
  686. AssertStatDir('"'+plsql3+'"', '');
  687. end;
  688. { TTestEventSQLScript }
  689. procedure TTestEventSQLScript.Notify(Sender: TObject);
  690. begin
  691. inc (NotifyCount);
  692. LastSender := Sender;
  693. end;
  694. procedure TTestEventSQLScript.NotifyStatement(Sender: TObject;
  695. SQL_Statement: TStrings; var StopExecution: Boolean);
  696. var r : integer;
  697. s : string;
  698. begin
  699. StopExecution := StopToSend;
  700. if SQL_Statement.count > 0 then
  701. begin
  702. s := SQL_Statement[0];
  703. for r := 1 to SQL_Statement.count-1 do
  704. s := s + ';' + SQL_Statement[r];
  705. if SQL_Statement.count > 1 then
  706. s := '"' + s + '"';
  707. end
  708. else
  709. s := '';
  710. if received <> '' then
  711. received := received + ';' + s
  712. else
  713. received := s;
  714. LastSender := Sender;
  715. end;
  716. procedure TTestEventSQLScript.NotifyDirective(Sender: TObject; Directive,
  717. Argument: AnsiString; var StopExecution: Boolean);
  718. var s : string;
  719. begin
  720. StopExecution := StopToSend;
  721. if Argument = '' then
  722. s := Directive
  723. else
  724. s := format ('%s(%s)', [Directive, Argument]);
  725. if received <> '' then
  726. received := received + ';' + s
  727. else
  728. received := s;
  729. LastSender := Sender;
  730. end;
  731. procedure TTestEventSQLScript.SetUp;
  732. begin
  733. inherited SetUp;
  734. Script := TEventSQLScript.Create (nil);
  735. notifycount := 0;
  736. Received := '';
  737. LastSender := nil;
  738. end;
  739. procedure TTestEventSQLScript.TearDown;
  740. begin
  741. Script.Free;
  742. inherited TearDown;
  743. end;
  744. procedure TTestEventSQLScript.TestStatement;
  745. begin
  746. StopToSend:=false;
  747. Script.OnSQLStatement := @NotifyStatement;
  748. Script.Script.Text := 'stat1;stat2;';
  749. script.execute;
  750. AssertEquals ('Received', 'stat1;stat2', received);
  751. AssertSame ('Sender', script, LastSender);
  752. end;
  753. procedure TTestEventSQLScript.TestStatementStop;
  754. begin
  755. StopToSend:=true;
  756. Script.OnSQLStatement := @NotifyStatement;
  757. Script.Script.Text := 'stat1;stat2;';
  758. script.execute;
  759. AssertEquals ('Received', 'stat1', received);
  760. AssertSame ('Sender', script, LastSender);
  761. end;
  762. procedure TTestEventSQLScript.TestDirective;
  763. begin
  764. StopToSend:=false;
  765. Script.OnSQLStatement := @NotifyStatement;
  766. Script.OnDirective := @NotifyDirective;
  767. script.Directives.Add ('STAT1');
  768. Script.Script.Text := 'stat1 ik;stat2;';
  769. script.execute;
  770. AssertEquals ('Received', 'STAT1(ik);stat2', received);
  771. AssertSame ('Sender', script, LastSender);
  772. end;
  773. procedure TTestEventSQLScript.TestDirectiveStop;
  774. begin
  775. StopToSend:=true;
  776. Script.OnSQLStatement := @NotifyStatement;
  777. Script.OnDirective := @NotifyDirective;
  778. script.Directives.Add ('STAT1');
  779. Script.Script.Text := 'stat1 ik;stat2;';
  780. script.execute;
  781. AssertEquals ('Received', 'STAT1(ik)', received);
  782. AssertSame ('Sender', script, LastSender);
  783. end;
  784. procedure TTestEventSQLScript.TestCommit;
  785. begin
  786. Script.OnCommit := @Notify;
  787. Script.Script.Text := 'iets; commit; anders;';
  788. script.execute;
  789. AssertEquals ('NotifyCount', 1, NotifyCount);
  790. AssertSame ('Sender', script, LastSender);
  791. end;
  792. procedure TTestEventSQLScript.TestBeforeExec;
  793. begin
  794. Script.BeforeExecute := @Notify;
  795. Script.Script.Text := 'update iets; anders iets;';
  796. script.execute;
  797. AssertEquals ('NotifyCount', 1, NotifyCount);
  798. AssertSame ('Sender', script, LastSender);
  799. end;
  800. procedure TTestEventSQLScript.TestAfterExec;
  801. begin
  802. Script.AfterExecute := @Notify;
  803. Script.Script.Text := 'update iets; anders iets; en meer;';
  804. script.execute;
  805. AssertEquals ('NotifyCount', 1, NotifyCount);
  806. AssertSame ('Sender', script, LastSender);
  807. end;
  808. initialization
  809. RegisterTests ([TTestSQLScript, TTestEventSQLScript]);
  810. end.