Functions.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // This should be used in only one of the UDR library files.
  126. // Build must export firebird_udr_plugin function.
  127. FB_UDR_IMPLEMENT_ENTRY_POINT