01.create.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 01.create.c
  4. * DESCRIPTION: Minimal sample of using interfaces from plain C.
  5. *
  6. * Run something like this to build: cc 01.create.c -lfbclient
  7. *
  8. * The contents of this file are subject to the Initial
  9. * Developer's Public License Version 1.0 (the "License");
  10. * you may not use this file except in compliance with the
  11. * License. You may obtain a copy of the License at
  12. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  13. *
  14. * Software distributed under the License is distributed AS IS,
  15. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing rights
  17. * and limitations under the License.
  18. *
  19. * The Original Code was created by Alexander Peshkoff
  20. * for the Firebird Open Source RDBMS project.
  21. *
  22. * Copyright (c) 2024 Alexander Peshkoff <[email protected]>
  23. * and all contributors signed below.
  24. *
  25. * All Rights Reserved.
  26. * Contributor(s): ______________________________________.
  27. */
  28. #include "firebird/fb_c_api.h"
  29. #include <stdio.h>
  30. #define CHECK_STATUS(st) if (IStatus_getState(st) & IStatus_STATE_ERRORS) {char buf[256]; IUtil_formatStatus(utl, buf, sizeof buf, st); puts(buf); return 1;}
  31. int main()
  32. {
  33. // Here we get access to master interface. This is main interface of firebird,
  34. // and the only one for getting which there is special function in our API.
  35. struct IMaster* master = fb_get_master_interface();
  36. // Declare pointers to required interfaces
  37. // IStatus is used to return wide error description to user
  38. // IProvider is needed to start to work with database (or service)
  39. // Status vector, main dispatcher and utility interfaces are returned by IMaster functions
  40. // No error return may happen - these functions always succeed
  41. struct IStatus* st = IMaster_getStatus(master);
  42. struct IProvider* prov = IMaster_getDispatcher(master);
  43. struct IUtil* utl = IMaster_getUtilInterface(master);
  44. // IAttachment and ITransaction contain methods to work with database attachment
  45. // and transactions
  46. struct IAttachment* att = NULL;
  47. struct ITransaction* tra = NULL;
  48. // IXpbBuilder is used to access various parameters blocks used in API
  49. struct IXpbBuilder* dpb = NULL;
  50. // create DPB - use non-default page size 4Kb
  51. dpb = IUtil_getXpbBuilder(utl, st, IXpbBuilder_DPB, NULL, 0);
  52. CHECK_STATUS(st);
  53. IXpbBuilder_insertInt(dpb, st, isc_dpb_page_size, 4 * 1024);
  54. CHECK_STATUS(st);
  55. IXpbBuilder_insertString(dpb, st, isc_dpb_user_name, "sysdba");
  56. CHECK_STATUS(st);
  57. IXpbBuilder_insertString(dpb, st, isc_dpb_password, "masterkey");
  58. CHECK_STATUS(st);
  59. // create empty database
  60. att = IProvider_createDatabase(prov, st, "fbtests.fdb",
  61. IXpbBuilder_getBufferLength(dpb, st), IXpbBuilder_getBuffer(dpb, st));
  62. CHECK_STATUS(st);
  63. printf("Database fbtests.fdb created\n");
  64. // start transaction
  65. tra = IAttachment_startTransaction(att, st, 0, NULL);
  66. CHECK_STATUS(st);
  67. // create table
  68. IAttachment_execute(att, st, tra, 0, "create table dates_table (d1 date)", 3,
  69. NULL, NULL, NULL, NULL); // Input parameters and output data not used
  70. CHECK_STATUS(st);
  71. // commit transaction retaining
  72. ITransaction_commitRetaining(tra, st);
  73. CHECK_STATUS(st);
  74. printf("Table dates_table created\n");
  75. // insert a record into dates_table
  76. IAttachment_execute(att, st, tra, 0, "insert into dates_table values (CURRENT_DATE)", 3,
  77. NULL, NULL, NULL, NULL); // Input parameters and output data not used
  78. CHECK_STATUS(st);
  79. // commit transaction (will close interface)
  80. ITransaction_commit(tra, st);
  81. CHECK_STATUS(st);
  82. printf("Record inserted into dates_table\n");
  83. // detach from database (will close interface)
  84. IAttachment_detach(att, st);
  85. CHECK_STATUS(st);
  86. IStatus_dispose(st);
  87. IProvider_release(prov);
  88. return 0;
  89. }