2
0

01.create.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. {
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 01.create.pas
  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. * Status - returns the status of executed command
  11. * Provider - main interface to access DB / service
  12. * Attachment - database attachment interface
  13. * Transaction - transaction interface
  14. * Util - helper calls here and there
  15. * XpbBuilder - build various parameters blocks
  16. *
  17. * Run something like this to build: fpc -Fu<path-to-Firebird.pas> -Mdelphi 01.create.pas
  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) 2015 Alexander Peshkoff <[email protected]>
  34. * and all contributors signed below.
  35. *
  36. * All Rights Reserved.
  37. * Contributor(s): ______________________________________. }
  38. Program create;
  39. uses Sysutils, Firebird;
  40. var
  41. // Declare pointers to required interfaces
  42. // Status is used to return wide error description to user
  43. st : IStatus;
  44. // This is main interface of firebird, and the only one
  45. // for getting which there is special function in our API
  46. master : IMaster;
  47. util : IUtil;
  48. // XpbBuilder helps to create various parameter blocks for API calls
  49. dpb : IXpbBuilder;
  50. // Provider is needed to start to work with database (or service)
  51. prov : IProvider;
  52. // Attachment and Transaction contain methods to work with
  53. // database attachment and transaction
  54. att : IAttachment;
  55. tra : ITransaction;
  56. procedure PrintError(s : IStatus);
  57. var
  58. maxMessage : Integer;
  59. outMessage : PAnsiChar;
  60. begin
  61. maxMessage := 256;
  62. outMessage := StrAlloc(maxMessage);
  63. util.formatStatus(outMessage, maxMessage, s);
  64. writeln(outMessage);
  65. StrDispose(outMessage);
  66. end;
  67. begin
  68. // Here we get access to master interface and helper utility interface
  69. // no error return may happen - these functions always succeed
  70. master := fb_get_master_interface;
  71. util := master.getUtilInterface;
  72. // status vector and main dispatcher are returned by calls to IMaster functions
  73. // no error return may happen - these functions always succeed
  74. st := master.getStatus;
  75. prov := master.getDispatcher;
  76. try
  77. // create DPB
  78. dpb := util.getXpbBuilder(st, IXpbBuilder.DPB, nil, 0);
  79. dpb.insertInt(st, isc_dpb_page_size, 4 * 1024);
  80. dpb.insertString(st, isc_dpb_user_name, 'sysdba');
  81. dpb.insertString(st, isc_dpb_password, 'masterkey');
  82. // create empty database
  83. att := prov.createDatabase(st, 'fbtests.fdb', dpb.getBufferLength(st), dpb.getBuffer(st));
  84. writeln ('Database fbtests.fdb created');
  85. // detach from database
  86. att.detach(st);
  87. att := nil;
  88. // remove unneeded any more tag from DPB
  89. if dpb.findFirst(st, isc_dpb_page_size)
  90. then dpb.removeCurrent(st);
  91. // attach it once again
  92. att := prov.attachDatabase(st, 'fbtests.fdb', dpb.getBufferLength(st), dpb.getBuffer(st));
  93. writeln ('Re-attached database fbtests.fdb');
  94. // start transaction
  95. tra := att.startTransaction(st, 0, nil);
  96. // create table
  97. att.execute(st, tra, 0, 'create table dates_table (d1 date)', 3,
  98. nil, nil, nil, nil); // Input parameters and output data not used
  99. // commit transaction retaining
  100. tra.commitRetaining(st);
  101. writeln ('Table dates_table created');
  102. // insert a record into dates_table
  103. att.execute(st, tra, 0, 'insert into dates_table values (CURRENT_DATE)', 3,
  104. nil, nil, nil, nil); // Input parameters and output data not used
  105. // commit transaction (will close interface)
  106. tra.commit(st);
  107. tra := nil;
  108. writeln ('Record inserted into dates_table');
  109. // detach from database (will close interface)
  110. att.detach(st);
  111. att := nil;
  112. dpb.dispose;
  113. dpb := nil;
  114. except
  115. on e: FbException do PrintError(e.getStatus);
  116. end;
  117. prov.release;
  118. end.