File: 01.create.pas

package info (click to toggle)
firebird3.0 3.0.13.ds7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,632 kB
  • sloc: ansic: 374,403; cpp: 319,973; sql: 14,691; pascal: 14,532; yacc: 7,557; fortran: 5,645; sh: 5,336; makefile: 1,041; perl: 194; sed: 83; awk: 76; xml: 19; csh: 15
file content (143 lines) | stat: -rw-r--r-- 4,483 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
{
 *	PROGRAM:	Object oriented API samples.
 *	MODULE:		01.create.pas
 *	DESCRIPTION:	A sample of creating new database and new table in it.
 *					Run second time (when database already exists) to see
 *					how FbException is caught and handled by this code.
 *
 *					Example for the following interfaces:
 *					IMaster - main inteface to access all the rest
 *					Status - returns the status of executed command
 *					Provider - main interface to access DB / service
 *					Attachment - database attachment interface
 *					Transaction - transaction interface
 *					Util - helper calls here and there
 *					XpbBuilder - build various parameters blocks
 *
 *					Run something like this to build: fpc -Fu<path-to-Firebird.pas> -Mdelphi 01.create.pas
 *
 *  The contents of this file are subject to the Initial
 *  Developer's Public License Version 1.0 (the "License");
 *  you may not use this file except in compliance with the
 *  License. You may obtain a copy of the License at
 *  http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
 *
 *  Software distributed under the License is distributed AS IS,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied.
 *  See the License for the specific language governing rights
 *  and limitations under the License.
 *
 *  The Original Code was created by Alexander Peshkoff
 *  for the Firebird Open Source RDBMS project.
 *
 *  Copyright (c) 2015 Alexander Peshkoff <peshkoff@mail.ru>
 *  and all contributors signed below.
 *
 *  All Rights Reserved.
 *  Contributor(s): ______________________________________. }

Program create;

uses Sysutils, Firebird;

var
	// Declare pointers to required interfaces

	// Status is used to return wide error description to user
	st : IStatus;

	// This is main interface of firebird, and the only one
	// for getting which there is special function in our API
	master : IMaster;
	util : IUtil;

	// XpbBuilder helps to create various parameter blocks for API calls
	dpb : IXpbBuilder;

	// Provider is needed to start to work with database (or service)
	prov : IProvider;

	// Attachment and Transaction contain methods to work with
	// database attachment and transaction
	att : IAttachment;
	tra : ITransaction;

	procedure PrintError(s : IStatus);
	var
		maxMessage : Integer;
		outMessage : PAnsiChar;
	begin
		maxMessage := 256;
		outMessage := StrAlloc(maxMessage);
		util.formatStatus(outMessage, maxMessage, s);
		writeln(outMessage);
		StrDispose(outMessage);
	end;

begin
	// Here we get access to master interface and helper utility interface
	// no error return may happen - these functions always succeed
	master := fb_get_master_interface;
	util := master.getUtilInterface;

	// status vector and main dispatcher are returned by calls to IMaster functions
	// no error return may happen - these functions always succeed
	st := master.getStatus;
	prov := master.getDispatcher;

	try
		// create DPB
		dpb := util.getXpbBuilder(st, IXpbBuilder.DPB, nil, 0);
		dpb.insertInt(st, isc_dpb_page_size, 4 * 1024);
		dpb.insertString(st, isc_dpb_user_name, 'sysdba');
		dpb.insertString(st, isc_dpb_password, 'masterkey');

		// create empty database
		att := prov.createDatabase(st, 'fbtests.fdb', dpb.getBufferLength(st), dpb.getBuffer(st));
		writeln ('Database fbtests.fdb created');

		// detach from database
		att.detach(st);
		att := nil;

		// remove unneeded any more tag from DPB
		if dpb.findFirst(st, isc_dpb_page_size)
			then dpb.removeCurrent(st);

		// attach it once again
		att := prov.attachDatabase(st, 'fbtests.fdb', dpb.getBufferLength(st), dpb.getBuffer(st));
		writeln ('Re-attached database fbtests.fdb');

		// start transaction
		tra := att.startTransaction(st, 0, nil);

		// create table
		att.execute(st, tra, 0, 'create table dates_table (d1 date)', 3,
			nil, nil, nil, nil);	// Input parameters and output data not used

		// commit transaction retaining
		tra.commitRetaining(st);
		writeln ('Table dates_table created');

		// insert a record into dates_table
		att.execute(st, tra, 0, 'insert into dates_table values (CURRENT_DATE)', 3,
			nil, nil, nil, nil);	// Input parameters and output data not used

		// commit transaction (will close interface)
		tra.commit(st);
		tra := nil;

		writeln ('Record inserted into dates_table');

		// detach from database (will close interface)
		att.detach(st);
		att := nil;

		dpb.dispose;
		dpb := nil;
	except
		on e: FbException do PrintError(e.getStatus);
	end;

	prov.release;
end.