Procedures.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * The contents of this file are subject to the Initial
  3. * Developer's Public License Version 1.0 (the "License");
  4. * you may not use this file except in compliance with the
  5. * License. You may obtain a copy of the License at
  6. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  7. *
  8. * Software distributed under the License is distributed AS IS,
  9. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing rights
  11. * and limitations under the License.
  12. *
  13. * The Original Code was created by Adriano dos Santos Fernandes
  14. * for the Firebird Open Source RDBMS project.
  15. *
  16. * Copyright (c) 2008 Adriano dos Santos Fernandes <[email protected]>
  17. * and all contributors signed below.
  18. *
  19. * All Rights Reserved.
  20. * Contributor(s): ______________________________________.
  21. */
  22. #include "UdrCppExample.h"
  23. using namespace Firebird;
  24. //------------------------------------------------------------------------------
  25. /***
  26. create procedure gen_rows (
  27. start_n integer not null,
  28. end_n integer not null
  29. ) returns (
  30. n integer not null
  31. )
  32. external name 'udrcpp_example!gen_rows'
  33. engine udr;
  34. ***/
  35. FB_UDR_BEGIN_PROCEDURE(gen_rows)
  36. // Without InMessage/OutMessage definitions, messages will be byte-based.
  37. // Procedure variables.
  38. unsigned inOffsetStart, inOffsetEnd, outNullOffset, outOffset;
  39. // Get offsets once per procedure.
  40. FB_UDR_CONSTRUCTOR
  41. {
  42. AutoRelease<IMessageMetadata> inMetadata(metadata->getInputMetadata(status));
  43. inOffsetStart = inMetadata->getOffset(status, 0);
  44. inOffsetEnd = inMetadata->getOffset(status, 1);
  45. AutoRelease<IMessageMetadata> outMetadata(metadata->getOutputMetadata(status));
  46. outNullOffset = outMetadata->getNullOffset(status, 0);
  47. outOffset = outMetadata->getOffset(status, 0);
  48. }
  49. /*** Procedure destructor.
  50. FB_UDR_DESTRUCTOR
  51. {
  52. }
  53. ***/
  54. FB_UDR_EXECUTE_PROCEDURE
  55. {
  56. counter = *(ISC_LONG*) (in + procedure->inOffsetStart);
  57. end = *(ISC_LONG*) (in + procedure->inOffsetEnd);
  58. *(ISC_SHORT*) (out + procedure->outNullOffset) = FB_FALSE;
  59. }
  60. // After procedure's execute definition, starts the result set definition.
  61. FB_UDR_FETCH_PROCEDURE
  62. {
  63. if (counter > end)
  64. return false;
  65. *(ISC_LONG*) (out + procedure->outOffset) = counter++;
  66. return true;
  67. }
  68. /*** ResultSet destructor.
  69. ~ResultSet()
  70. {
  71. }
  72. ***/
  73. // ResultSet variables.
  74. ISC_LONG counter;
  75. ISC_LONG end;
  76. FB_UDR_END_PROCEDURE
  77. /***
  78. create procedure gen_rows2 (
  79. start_n integer not null,
  80. end_n integer not null
  81. ) returns (
  82. n integer not null
  83. )
  84. external name 'udrcpp_example!gen_rows2'
  85. engine udr;
  86. ***/
  87. FB_UDR_BEGIN_PROCEDURE(gen_rows2)
  88. FB_UDR_MESSAGE(InMessage,
  89. (FB_INTEGER, start)
  90. (FB_INTEGER, end)
  91. );
  92. FB_UDR_MESSAGE(OutMessage,
  93. (FB_INTEGER, result)
  94. );
  95. FB_UDR_EXECUTE_PROCEDURE
  96. {
  97. out->resultNull = FB_FALSE;
  98. out->result = in->start - 1;
  99. }
  100. FB_UDR_FETCH_PROCEDURE
  101. {
  102. return out->result++ < in->end;
  103. }
  104. FB_UDR_END_PROCEDURE
  105. /***
  106. create procedure inc (
  107. count_n integer not null
  108. ) returns (
  109. n0 integer not null,
  110. n1 integer not null,
  111. n2 integer not null,
  112. n3 integer not null,
  113. n4 integer not null
  114. )
  115. external name 'udrcpp_example!inc'
  116. engine udr;
  117. ***/
  118. // This is a sample procedure demonstrating how the scopes of variables works.
  119. // n1 and n2 are on the Procedure scope, i.e., they're shared for each execution of the same cached
  120. // metadata object.
  121. // n3 and n4 are on the ResultSet scope, i.e., each procedure execution have they own instances.
  122. FB_UDR_BEGIN_PROCEDURE(inc)
  123. FB_UDR_MESSAGE(InMessage,
  124. (FB_INTEGER, count)
  125. );
  126. FB_UDR_MESSAGE(OutMessage,
  127. (FB_INTEGER, n0)
  128. (FB_INTEGER, n1)
  129. (FB_INTEGER, n2)
  130. (FB_INTEGER, n3)
  131. (FB_INTEGER, n4)
  132. );
  133. ISC_LONG n1;
  134. // This is how a procedure (class) initializer is written.
  135. // ResultSet variables are not accessible here.
  136. // If there is nothing to initialize, it can be completelly suppressed.
  137. FB_UDR_CONSTRUCTOR
  138. , n1(0),
  139. n2(0)
  140. {
  141. }
  142. ISC_LONG n2;
  143. // FB_UDR_EXECUTE_PROCEDURE starts the ResultSet scope.
  144. FB_UDR_EXECUTE_PROCEDURE
  145. // This is the ResultSet (class) initializer.
  146. , n3(procedure->n1), // n3 will start with the next value for n1 of the last execution
  147. n4(0)
  148. {
  149. out->n0Null = out->n1Null = out->n2Null = out->n3Null = out->n4Null = FB_FALSE;
  150. out->n0 = 0;
  151. // In the execute method, the procedure scope must be accessed using the 'procedure' pointer.
  152. procedure->n1 = 0;
  153. // We don't touch n2 here, so it incremented counter will be kept after each execution.
  154. // The ResultSet scope must be accessed directly, i.e., they're member variables of the
  155. // 'this' pointer.
  156. ++n4;
  157. }
  158. ISC_LONG n3;
  159. // FB_UDR_FETCH_PROCEDURE must be always after FB_UDR_EXECUTE_PROCEDURE.
  160. FB_UDR_FETCH_PROCEDURE
  161. {
  162. if (out->n0++ <= in->count)
  163. {
  164. out->n1 = ++procedure->n1;
  165. out->n2 = ++procedure->n2;
  166. out->n3 = ++n3;
  167. out->n4 = ++n4;
  168. return true;
  169. }
  170. return false;
  171. }
  172. ISC_LONG n4;
  173. FB_UDR_END_PROCEDURE