02.update.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 02.update.cpp
  4. * DESCRIPTION: Run once prepared statement with parameters
  5. * a few times, committing transaction after each run.
  6. * Learns how to prepare statement, manually define parameters
  7. * for it, execute that statement with different parameters
  8. * and perform non-default error processing.
  9. *
  10. * Example for the following interfaces:
  11. * IAttachment - database attachment
  12. * ITransaction - transaction
  13. * IStatement - SQL statement execution
  14. * IMessageMetadata - describe input and output data format
  15. * IMetadataBuilder - tool to modify/create metadata
  16. * IStatus - return state holder
  17. *
  18. * Note that all updates are rolled back in this version. (see *** later)
  19. *
  20. * The contents of this file are subject to the Interbase Public
  21. * License Version 1.0 (the "License"); you may not use this file
  22. * except in compliance with the License. You may obtain a copy
  23. * of the License at http://www.Inprise.com/IPL.html
  24. *
  25. * Software distributed under the License is distributed on an
  26. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  27. * or implied. See the License for the specific language governing
  28. * rights and limitations under the License.
  29. *
  30. * The Original Code was created by Inprise Corporation
  31. * and its predecessors. Portions created by Inprise Corporation are
  32. * Copyright (C) Inprise Corporation.
  33. *
  34. * All Rights Reserved.
  35. * Contributor(s): ______________________________________.
  36. * Alex Peshkov, 2013
  37. */
  38. #include "ifaceExamples.h"
  39. static IMaster* master = fb_get_master_interface();
  40. int get_input(char*, double*);
  41. static const char* Dept_data[] =
  42. {"622", "100", "116", "900", 0};
  43. static double Percent_data[] =
  44. {0.05, 1.00, 0.075, 0.10, 0};
  45. int Input_ptr = 0;
  46. int main()
  47. {
  48. int rc = 0;
  49. // set default password if none specified in environment
  50. setenv("ISC_USER", "sysdba", 0);
  51. setenv("ISC_PASSWORD", "masterkey", 0);
  52. // status vector and main dispatcher
  53. ThrowStatusWrapper status(master->getStatus());
  54. IProvider* prov = master->getDispatcher();
  55. // declare pointers to required interfaces
  56. IAttachment* att = NULL;
  57. ITransaction* tra = NULL;
  58. // Interface executes prepared SQL statement
  59. IStatement* stmt = NULL;
  60. // Interfaces provides access to format of data in messages
  61. IMessageMetadata* meta = NULL;
  62. // Interface makes it possible to change format of data or define it yourself
  63. IMetadataBuilder* builder = NULL;
  64. const char* updstr =
  65. "UPDATE department SET budget = ? * budget + budget WHERE dept_no = ?";
  66. try
  67. {
  68. // attach employee db
  69. att = prov->attachDatabase(&status, "employee", 0, NULL);
  70. // start transaction
  71. tra = att->startTransaction(&status, 0, NULL);
  72. // prepare statement
  73. stmt = att->prepare(&status, tra, 0, updstr, SAMPLES_DIALECT, 0);
  74. // build metadata
  75. // IMaster creates empty new metadata in builder
  76. builder = master->getMetadataBuilder(&status, 2);
  77. // set required info on fields
  78. builder->setType(&status, 0, SQL_DOUBLE + 1);
  79. builder->setType(&status, 1, SQL_TEXT + 1);
  80. builder->setLength(&status, 1, 3);
  81. // IMetadata should be ready
  82. meta = builder->getMetadata(&status);
  83. // no need in builder any more
  84. builder->release();
  85. builder = NULL;
  86. // allocate buffer on stack
  87. char buffer[256];
  88. unsigned len = meta->getMessageLength(&status);
  89. if (len > sizeof(buffer))
  90. {
  91. throw "Input message length too big - can't continue";
  92. }
  93. // locations of parameters in input message
  94. char* dept_no = &buffer[meta->getOffset(&status, 1)];
  95. double* percent_inc = (double*) &buffer[meta->getOffset(&status, 0)];
  96. // null IDs (set to NOT NULL)
  97. short* flag = (short*)&buffer[meta->getNullOffset(&status, 0)];
  98. *flag = 0;
  99. flag = (short*) &buffer[meta->getNullOffset(&status, 1)];
  100. *flag = 0;
  101. // Get the next department-percent increase input pair.
  102. while (get_input(dept_no, percent_inc))
  103. {
  104. printf("\nIncreasing budget for department: %s by %5.2lf percent.\n",
  105. dept_no, *percent_inc);
  106. // Update the budget.
  107. try
  108. {
  109. stmt->execute(&status, tra, meta, buffer, NULL, NULL);
  110. }
  111. catch (const FbException& error)
  112. {
  113. // Handle exception raised during statement execution
  114. if (error.getStatus()->getErrors()[1] == isc_not_valid)
  115. {
  116. // Don't save the update, if the new budget exceeds the limit.
  117. printf("\tExceeded budget limit -- not updated.\n");
  118. tra->rollbackRetaining(&status);
  119. continue;
  120. }
  121. // Another error - use default handler
  122. throw;
  123. }
  124. // Save each department's update independently.
  125. // *** Change to commitRetaining() to see changes
  126. // *** tra->commitRetaining(&status);
  127. tra->rollbackRetaining(&status);
  128. }
  129. // close interfaces
  130. stmt->free(&status);
  131. stmt = NULL;
  132. meta->release();
  133. meta = NULL;
  134. tra->commit(&status);
  135. tra = NULL;
  136. att->detach(&status);
  137. att = NULL;
  138. }
  139. catch (const FbException& error)
  140. {
  141. // handle error
  142. rc = 1;
  143. char buf[256];
  144. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  145. fprintf(stderr, "%s\n", buf);
  146. }
  147. // release interfaces after error caught
  148. if (builder)
  149. builder->release();
  150. if (meta)
  151. meta->release();
  152. if (stmt)
  153. stmt->release();
  154. if (tra)
  155. tra->release();
  156. if (att)
  157. att->release();
  158. prov->release();
  159. status.dispose();
  160. return rc;
  161. }
  162. /*
  163. * Get the department and percent parameters for an example to run.
  164. */
  165. int get_input (char *dept_no, double *percent)
  166. {
  167. if (Dept_data[Input_ptr] == 0)
  168. return 0;
  169. strcpy(dept_no, Dept_data[Input_ptr]);
  170. if ((*percent = Percent_data[Input_ptr++]) == 0)
  171. return 0;
  172. return 1;
  173. }