01.create.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 01.create.cpp
  4. * DESCRIPTION: A sample of creating new database and new table in it.
  5. * Run second time (when database already exists) to see
  6. * how FbException is caught and handled by this code.
  7. *
  8. * Example for the following interfaces:
  9. * IMaster - main inteface to access all the rest
  10. * IStatus - returns the status of executed command
  11. * IProvider - main interface to access DB / service
  12. * IAttachment - database attachment interface
  13. * ITransaction - transaction interface
  14. * IUtil - helper calls here and there
  15. * IXpbBuilder - build various parameters blocks
  16. *
  17. * Run something like this to build: c++ 01.create.cpp -lfbclient
  18. *
  19. * The contents of this file are subject to the Initial
  20. * Developer's Public License Version 1.0 (the "License");
  21. * you may not use this file except in compliance with the
  22. * License. You may obtain a copy of the License at
  23. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  24. *
  25. * Software distributed under the License is distributed AS IS,
  26. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing rights
  28. * and limitations under the License.
  29. *
  30. * The Original Code was created by Alexander Peshkoff
  31. * for the Firebird Open Source RDBMS project.
  32. *
  33. * Copyright (c) 2013 Alexander Peshkoff <[email protected]>
  34. * and all contributors signed below.
  35. *
  36. * All Rights Reserved.
  37. * Contributor(s): ______________________________________.
  38. */
  39. #include "ifaceExamples.h"
  40. // Here we get access to master interface. This is main interface of firebird,
  41. // and the only one for getting which there is special function in our API.
  42. static IMaster* master = fb_get_master_interface();
  43. int main()
  44. {
  45. int rc = 0;
  46. // Declare pointers to required interfaces
  47. // IStatus is used to return wide error description to user
  48. // IProvider is needed to start to work with database (or service)
  49. // Status vector, main dispatcher and utility interfaces are returned by IMaster functions
  50. // No error return may happen - these functions always succeed
  51. IStatus* st = master->getStatus();
  52. IProvider* prov = master->getDispatcher();
  53. IUtil* utl = master->getUtilInterface();
  54. // IAttachment and ITransaction contain methods to work with database attachment
  55. // and transactions
  56. IAttachment* att = NULL;
  57. ITransaction* tra = NULL;
  58. // IXpbBuilder is used to access various parameters blocks used in API
  59. IXpbBuilder* dpb = NULL;
  60. try
  61. {
  62. // Status wrapper - will be used later in all calls where status interface is needed
  63. // With ThrowStatusWrapper passed as status interface FbException will be thrown on error
  64. ThrowStatusWrapper status(st);
  65. // create DPB - use non-default page size 4Kb
  66. dpb = utl->getXpbBuilder(&status, IXpbBuilder::DPB, NULL, 0);
  67. dpb->insertInt(&status, isc_dpb_page_size, 4 * 1024);
  68. dpb->insertString(&status, isc_dpb_user_name, "sysdba");
  69. dpb->insertString(&status, isc_dpb_password, "masterkey");
  70. // create empty database
  71. att = prov->createDatabase(&status, "fbtests.fdb", dpb->getBufferLength(&status),
  72. dpb->getBuffer(&status));
  73. printf("Database fbtests.fdb created\n");
  74. // detach from database
  75. att->detach(&status);
  76. att = NULL;
  77. // attach it once again
  78. att = prov->attachDatabase(&status, "fbtests.fdb",
  79. dpb->getBufferLength(&status), dpb->getBuffer(&status));
  80. printf("Re-attached database fbtests.fdb\n");
  81. // start transaction
  82. tra = att->startTransaction(&status, 0, NULL);
  83. // create table
  84. att->execute(&status, tra, 0, "create table dates_table (d1 date)", SAMPLES_DIALECT,
  85. NULL, NULL, NULL, NULL); // Input parameters and output data not used
  86. // commit transaction retaining
  87. tra->commitRetaining(&status);
  88. printf("Table dates_table created\n");
  89. // insert a record into dates_table
  90. att->execute(&status, tra, 0, "insert into dates_table values (CURRENT_DATE)", SAMPLES_DIALECT,
  91. NULL, NULL, NULL, NULL); // Input parameters and output data not used
  92. // commit transaction (will close interface)
  93. tra->commit(&status);
  94. tra = NULL;
  95. printf("Record inserted into dates_table\n");
  96. // detach from database (will close interface)
  97. att->detach(&status);
  98. att = NULL;
  99. }
  100. catch (const FbException& error)
  101. {
  102. // handle error
  103. rc = 1;
  104. char buf[256];
  105. utl->formatStatus(buf, sizeof(buf), error.getStatus());
  106. fprintf(stderr, "%s\n", buf);
  107. }
  108. // release interfaces after error caught
  109. if (tra)
  110. tra->release();
  111. if (att)
  112. att->release();
  113. if (dpb)
  114. dpb->dispose();
  115. st->dispose();
  116. prov->release();
  117. return rc;
  118. }