Functions.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 function wait_event (
  27. event_name varchar(31) character set utf8 not null
  28. ) returns integer not null
  29. external name 'udrcpp_example!wait_event'
  30. engine udr;
  31. ***/
  32. FB_UDR_BEGIN_FUNCTION(wait_event)
  33. FB_UDR_MESSAGE(InMessage,
  34. (FB_VARCHAR(31 * 4), name)
  35. );
  36. FB_UDR_MESSAGE(OutMessage,
  37. (FB_INTEGER, result)
  38. );
  39. FB_UDR_EXECUTE_FUNCTION
  40. {
  41. char* s = new char[in->name.length + 1];
  42. memcpy(s, in->name.str, in->name.length);
  43. s[in->name.length] = '\0';
  44. unsigned char* eveBuffer;
  45. unsigned char* eveResult;
  46. int eveLen = isc_event_block(&eveBuffer, &eveResult, 1, s);
  47. delete [] s;
  48. ISC_STATUS_ARRAY statusVector = {0};
  49. isc_db_handle dbHandle = Helper::getIscDbHandle(status, context);
  50. ISC_ULONG counter = 0;
  51. FbException::check(isc_wait_for_event(
  52. statusVector, &dbHandle, eveLen, eveBuffer, eveResult), status, statusVector);
  53. isc_event_counts(&counter, eveLen, eveBuffer, eveResult);
  54. FbException::check(isc_wait_for_event(
  55. statusVector, &dbHandle, eveLen, eveBuffer, eveResult), status, statusVector);
  56. isc_event_counts(&counter, eveLen, eveBuffer, eveResult);
  57. isc_free((char*) eveBuffer);
  58. isc_free((char*) eveResult);
  59. out->resultNull = FB_FALSE;
  60. out->result = counter;
  61. }
  62. FB_UDR_END_FUNCTION
  63. /***
  64. create function sum_args (
  65. n1 integer,
  66. n2 integer,
  67. n3 integer
  68. ) returns integer
  69. external name 'udrcpp_example!sum_args'
  70. engine udr;
  71. ***/
  72. FB_UDR_BEGIN_FUNCTION(sum_args)
  73. // Without InMessage/OutMessage definitions, messages will be byte-based.
  74. FB_UDR_CONSTRUCTOR
  75. // , inCount(0)
  76. {
  77. // Get input metadata.
  78. AutoRelease<IMessageMetadata> inMetadata(metadata->getInputMetadata(status));
  79. // Get count of input parameters.
  80. inCount = inMetadata->getCount(status);
  81. inNullOffsets.reset(new unsigned[inCount]);
  82. inOffsets.reset(new unsigned[inCount]);
  83. for (unsigned i = 0; i < inCount; ++i)
  84. {
  85. // Get null offset of the i-th input parameter.
  86. inNullOffsets[i] = inMetadata->getNullOffset(status, i);
  87. // Get the offset of the i-th input parameter.
  88. inOffsets[i] = inMetadata->getOffset(status, i);
  89. }
  90. // Get output metadata.
  91. AutoRelease<IMessageMetadata> outMetadata(metadata->getOutputMetadata(status));
  92. // Get null offset of the return value.
  93. outNullOffset = outMetadata->getNullOffset(status, 0);
  94. // Get offset of the return value.
  95. outOffset = outMetadata->getOffset(status, 0);
  96. }
  97. // This function requires the INTEGER parameters and return value, otherwise it will crash.
  98. // Metadata is inspected dynamically (in execute). This is not the fastest method.
  99. FB_UDR_EXECUTE_FUNCTION
  100. {
  101. *(ISC_SHORT*) (out + outNullOffset) = FB_FALSE;
  102. // Get a reference to the return value.
  103. ISC_LONG& ret = *(ISC_LONG*) (out + outOffset);
  104. // The return value is automatically initialized to 0.
  105. ///ret = 0;
  106. for (unsigned i = 0; i < inCount; ++i)
  107. {
  108. // If the i-th input parameter is NULL, set the output to NULL and finish.
  109. if (*(ISC_SHORT*) (in + inNullOffsets[i]))
  110. {
  111. *(ISC_SHORT*) (out + outNullOffset) = FB_TRUE;
  112. return;
  113. }
  114. // Read the i-th input parameter value and sum it in the referenced return value.
  115. ret += *(ISC_LONG*) (in + inOffsets[i]);
  116. }
  117. }
  118. unsigned inCount;
  119. AutoArrayDelete<unsigned> inNullOffsets;
  120. AutoArrayDelete<unsigned> inOffsets;
  121. unsigned outNullOffset;
  122. unsigned outOffset;
  123. FB_UDR_END_FUNCTION
  124. /***
  125. create function mult (
  126. a decfloat(34) not null,
  127. b decfloat(34) not null
  128. ) returns decfloat(34) not null
  129. external name 'udrcpp_example!mult'
  130. engine udr;
  131. ***/
  132. FB_UDR_BEGIN_FUNCTION(mult)
  133. // Without InMessage/OutMessage definitions, messages will be byte-based.
  134. FB_UDR_CONSTRUCTOR
  135. {
  136. AutoRelease<IMessageMetadata> inMetadata(metadata->getInputMetadata(status));
  137. aOffset = inMetadata->getOffset(status, 0);
  138. bOffset = inMetadata->getOffset(status, 1);
  139. AutoRelease<IMessageMetadata> outMetadata(metadata->getOutputMetadata(status));
  140. outOffset = outMetadata->getOffset(status, 0);
  141. outNullOffset = outMetadata->getNullOffset(status, 0);
  142. df34 = master->getUtilInterface()->getDecFloat34(status);
  143. }
  144. // This function requires the INTEGER parameters and return value, otherwise it will crash.
  145. // Metadata is inspected dynamically (in execute). This is not the fastest method.
  146. FB_UDR_EXECUTE_FUNCTION
  147. {
  148. struct ExampleBCD
  149. {
  150. unsigned char bcd[IDecFloat34::BCD_SIZE];
  151. int sign, exp;
  152. void load(void* from, IDecFloat34* df34)
  153. {
  154. df34->toBcd((FB_DEC34*) from, &sign, bcd, &exp);
  155. }
  156. void store(void* to, IDecFloat34* df34) const
  157. {
  158. df34->fromBcd(sign, bcd, exp, (FB_DEC34*) to);
  159. }
  160. };
  161. ExampleBCD a, b, rc;
  162. a.load(in + aOffset, df34);
  163. b.load(in + bOffset, df34);
  164. // multiply (trivial example - a lot of features are missing)
  165. rc.sign = a.sign ^ b.sign;
  166. rc.exp = a.exp + b.exp;
  167. unsigned char buf[2 * IDecFloat34::BCD_SIZE + 1];
  168. memset(buf, 0, sizeof(buf));
  169. for (unsigned i = IDecFloat34::BCD_SIZE; i--;)
  170. {
  171. for (unsigned j = IDecFloat34::BCD_SIZE; j--;)
  172. {
  173. unsigned char v = a.bcd[i] * b.bcd[j] + buf[i + j + 1];
  174. buf[i + j + 1] = v % 10;
  175. buf[i + j] += v / 10;
  176. }
  177. }
  178. unsigned offset = 0;
  179. for (; offset < IDecFloat34::BCD_SIZE; ++offset)
  180. {
  181. if (buf[offset])
  182. break;
  183. }
  184. memcpy(rc.bcd, buf + offset, sizeof rc.bcd);
  185. rc.exp += (IDecFloat34::BCD_SIZE - offset);
  186. rc.store(out + outOffset, df34);
  187. *(ISC_SHORT*) (out + outNullOffset) = FB_FALSE;
  188. }
  189. unsigned aOffset, bOffset, outOffset, outNullOffset;
  190. IDecFloat34* df34;
  191. FB_UDR_END_FUNCTION
  192. //------------------------------------------------------------------------------
  193. // This should be used in only one of the UDR library files.
  194. // Build must export firebird_udr_plugin function.
  195. FB_UDR_IMPLEMENT_ENTRY_POINT