1 |
(*
|
2 |
* Firebird UDR Support (fbudrtestbed). The fbudr components provide a set of
|
3 |
* Pascal language bindings for the Firebird API in support of server
|
4 |
* side User Defined Routines (UDRs). The fbudr package is an extension
|
5 |
* to the Firebird Pascal API.
|
6 |
*
|
7 |
* The contents of this file are subject to the Initial Developer's
|
8 |
* Public License Version 1.0 (the "License"); you may not use this
|
9 |
* file except in compliance with the License. You may obtain a copy
|
10 |
* of the License here:
|
11 |
*
|
12 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
|
13 |
*
|
14 |
* Software distributed under the License is distributed on an "AS
|
15 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
16 |
* implied. See the License for the specific language governing rights
|
17 |
* and limitations under the License.
|
18 |
*
|
19 |
* The Initial Developer of the Original Code is Tony Whyman.
|
20 |
*
|
21 |
* The Original Code is (C) 2021 Tony Whyman, MWA Software
|
22 |
* (http://www.mwasoftware.co.uk).
|
23 |
*
|
24 |
* All Rights Reserved.
|
25 |
*
|
26 |
* Contributor(s): ______________________________________.
|
27 |
*
|
28 |
*)
|
29 |
unit udr_test01;
|
30 |
|
31 |
{$IFDEF MSWINDOWS}
|
32 |
{$DEFINE WINDOWS}
|
33 |
{$ENDIF}
|
34 |
|
35 |
{$IFDEF FPC}
|
36 |
{$mode delphi}
|
37 |
{$codepage UTF8}
|
38 |
{$interfaces COM}
|
39 |
{$ENDIF}
|
40 |
|
41 |
interface
|
42 |
|
43 |
uses
|
44 |
Classes, SysUtils, IB, FBUDRController, FBUDRIntf;
|
45 |
|
46 |
{This unit provides the implementation of selected number of UDR functions used
|
47 |
to test out various aspects of the TFBUDRFunction class. Note that each class is
|
48 |
registered with the FBUDRController at initialization time.}
|
49 |
|
50 |
type
|
51 |
{ TMyRowCountFunction implements a simple User Defined Routine (UDR) Function.
|
52 |
The DDL for this UDR is:
|
53 |
|
54 |
create or alter function MyRowCount (
|
55 |
table_name varchar(31)
|
56 |
) returns integer
|
57 |
external name 'fbudrtests!row_count'
|
58 |
engine udr;
|
59 |
|
60 |
It is called with a single parameter (the name of a table or view in the database.
|
61 |
The function then runs a simple select query to determine the number of rows
|
62 |
in the table and returns the row count.
|
63 |
}
|
64 |
|
65 |
{ TMyRowCountFunction }
|
66 |
|
67 |
TMyRowCountFunction = class(TFBUDRFunction)
|
68 |
public
|
69 |
function Execute(context: IFBUDRExternalContext;
|
70 |
ProcMetadata: IFBUDRProcMetadata;
|
71 |
InputParams: IFBUDRInputParams;
|
72 |
ResultSQLType: cardinal): variant; override;
|
73 |
end;
|
74 |
|
75 |
{UDR function with deliberate error. This has the same signature as above,
|
76 |
but deliberately tries to invoke an OpenCursor with a null sql string.
|
77 |
This is to demonstrate exception handling.
|
78 |
|
79 |
create or alter function BadRowCount (
|
80 |
table_name varchar(31)
|
81 |
) returns integer
|
82 |
external name 'fbudrtests!bad_row_count'
|
83 |
engine udr;
|
84 |
}
|
85 |
|
86 |
{ TBadRowCountFunction }
|
87 |
|
88 |
TBadRowCountFunction = class(TFBUDRFunction)
|
89 |
public
|
90 |
function Execute(context: IFBUDRExternalContext;
|
91 |
ProcMetadata: IFBUDRProcMetadata;
|
92 |
InputParams: IFBUDRInputParams;
|
93 |
ResultSQLType: cardinal): variant; override;
|
94 |
end;
|
95 |
|
96 |
{TReturnInfoFunction implements a function that obtains and returns the info
|
97 |
string from the external name. This is to demonstrate correct handling and
|
98 |
parsing of the function entry point, as well as an empty input string.
|
99 |
|
100 |
create or alter function UDRInfo (
|
101 |
) returns VarChar(31)
|
102 |
external name 'fbudrtests!return_info!Hello World'
|
103 |
engine udr;
|
104 |
|
105 |
}
|
106 |
|
107 |
{ TReturnInfoFunction }
|
108 |
|
109 |
TReturnInfoFunction = class(TFBUDRFunction)
|
110 |
public
|
111 |
function Execute(context: IFBUDRExternalContext;
|
112 |
ProcMetadata: IFBUDRProcMetadata;
|
113 |
InputParams: IFBUDRInputParams;
|
114 |
ResultSQLType: cardinal): variant; override;
|
115 |
end;
|
116 |
|
117 |
|
118 |
implementation
|
119 |
|
120 |
{ TReturnInfoFunction }
|
121 |
|
122 |
function TReturnInfoFunction.Execute(context: IFBUDRExternalContext;
|
123 |
ProcMetadata: IFBUDRProcMetadata; InputParams: IFBUDRInputParams;
|
124 |
ResultSQLType: cardinal): variant;
|
125 |
begin
|
126 |
Result := ProcMetadata.getInfo;
|
127 |
end;
|
128 |
|
129 |
{ TBadRowCountFunction }
|
130 |
|
131 |
function TBadRowCountFunction.Execute(context: IFBUDRExternalContext;
|
132 |
ProcMetadata: IFBUDRProcMetadata; InputParams: IFBUDRInputParams;
|
133 |
ResultSQLType: cardinal): variant;
|
134 |
begin
|
135 |
with context do
|
136 |
begin
|
137 |
Result := GetAttachment.OpenCursorAtStart(GetTransaction,'')[0].AsInteger;
|
138 |
end;
|
139 |
end;
|
140 |
|
141 |
{ TMyRowCountFunction }
|
142 |
|
143 |
function TMyRowCountFunction.Execute(context: IFBUDRExternalContext;
|
144 |
ProcMetadata: IFBUDRProcMetadata; InputParams: IFBUDRInputParams;
|
145 |
ResultSQLType: cardinal): variant;
|
146 |
begin
|
147 |
with context do
|
148 |
begin
|
149 |
Result := GetAttachment.OpenCursorAtStart(GetTransaction,
|
150 |
'Select count(*) from ' + InputParams.ByName('table_name').AsString)[0].AsInteger;
|
151 |
end;
|
152 |
end;
|
153 |
|
154 |
Initialization
|
155 |
FBRegisterUDRFunction('row_count',TMyRowCountFunction);
|
156 |
FBRegisterUDRFunction('bad_row_count',TBadRowCountFunction);
|
157 |
FBRegisterUDRFunction('return_info',TReturnInfoFunction);
|
158 |
|
159 |
end.
|
160 |
|