demomacros.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. program macrotest;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, db, sqldb, ibconnection;
  5. type
  6. { TTestMacroApp }
  7. TTestMacroApp = class(TCustomApplication)
  8. DB : TIBConnection;
  9. TR : TSQLTransaction;
  10. Q : TSQLQuery;
  11. protected
  12. Procedure SetupDatabase;
  13. procedure DoRun; override;
  14. public
  15. constructor Create(TheOwner: TComponent); override;
  16. destructor Destroy; override;
  17. procedure WriteHelp(aMsg : String); virtual;
  18. end;
  19. { TTestMacroApp }
  20. procedure TTestMacroApp.SetupDatabase;
  21. begin
  22. DB:=TIBConnection.Create(Self);
  23. TR:=TSQLTransaction.Create(Self);
  24. With DB do
  25. begin
  26. Hostname:='localhost';
  27. DatabaseName:=GetOptionValue('d','database');
  28. if DatabaseName='' then
  29. DatabaseName:='employees'; // Alias
  30. UserName:=GetOptionValue('u','username');
  31. if UserName='' then
  32. UserName:='SYSDBA';
  33. Password:=GetOptionValue('p','password');
  34. if Password='' then
  35. Password:='masterkey';
  36. Charset:='UTF8';
  37. DB.Transaction:=TR;
  38. end;
  39. Q:=TSQLQuery.Create(Self);
  40. Q.Database:=DB;
  41. Q.Transaction:=TR;
  42. Q.SQL.Text:='Select * from ('+sLineBreak+
  43. ' Select 1 as id from rdb$database'+sLineBreak+
  44. ' union all'+sLineBreak+
  45. ' Select 2 as id from rdb$database'+sLineBreak+
  46. ' )'+sLineBreak+
  47. '%WHERE_CL' +sLineBreak+
  48. '%ORDER_CL' +sLineBreak;
  49. Q.MacroCheck:=true;
  50. Q.MacroByName('WHERE_CL').AsString:='where 1=1';
  51. Q.MacroByName('ORDER_CL').AsString:='order by 1';
  52. end;
  53. procedure TTestMacroApp.DoRun;
  54. var
  55. ErrorMsg: String;
  56. begin
  57. Terminate;
  58. // quick check parameters
  59. ErrorMsg:=CheckOptions('hd:u:p:', ['help','database:','user:','password:']);
  60. if (ErrorMsg<>'') or HasOption('h','help') then
  61. begin
  62. WriteHelp(ErrorMsg);
  63. Exit;
  64. end;
  65. SetupDatabase;
  66. With Q do
  67. begin
  68. WriteLn( 'Execution of SQL Statement :' + LineEnding+LineEnding+SQl.Text );
  69. Writeln('Initial macro values:');
  70. WriteLn( '%WHERE_CL = "'+MacroByName('WHERE_CL').AsString+'"');
  71. WriteLn( '%ORDER_CL = "'+MacroByName('ORDER_CL').AsString+'"');
  72. Writeln;
  73. Open;
  74. Writeln( 'First field value (expect "1") using default macro order (with default order by clause): '+Fields[0].AsString);
  75. Writeln;
  76. Close;
  77. MacroByName('ORDER_CL').AsString := 'Order by 1 DESC';
  78. WriteLn('Set new value to %ORDER_CL = "'+MacroByName('ORDER_CL').AsString+'"');
  79. Writeln;
  80. Open;
  81. WriteLn('First field value (expect "2") using new macro order (after new order by clause): '+Fields[0].AsString);
  82. Writeln;
  83. Close;
  84. end;
  85. // stop program loop
  86. Terminate;
  87. end;
  88. constructor TTestMacroApp.Create(TheOwner: TComponent);
  89. begin
  90. inherited Create(TheOwner);
  91. StopOnException:=True;
  92. end;
  93. destructor TTestMacroApp.Destroy;
  94. begin
  95. inherited Destroy;
  96. end;
  97. procedure TTestMacroApp.WriteHelp(aMsg : string);
  98. begin
  99. if AMsg<>'' then
  100. Writeln('Error: ',aMsg);
  101. { add your help code here }
  102. writeln('Usage: ', ExeName, ' [options]');
  103. Writeln('Where options is one or more of:');
  104. Writeln('-h --help this text');
  105. Writeln('-d --database=DB Name of firebird database to connect to');
  106. Writeln('-u --user=Name Name of user to connect with');
  107. Writeln('-p --password=PW Password of user to connect with');
  108. end;
  109. var
  110. Application: TTestMacroApp;
  111. begin
  112. Application:=TTestMacroApp.Create(nil);
  113. Application.Title:='Macro test application';
  114. Application.Run;
  115. Application.Free;
  116. end.