-
-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy path02.update.pas
More file actions
224 lines (181 loc) · 6.5 KB
/
02.update.pas
File metadata and controls
224 lines (181 loc) · 6.5 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
Program update;
{*
* PROGRAM: Object oriented API samples.
* MODULE: 02.update.cpp
* DESCRIPTION: Run once prepared statement with parameters
* a few times, committing transaction after each run.
* Learns how to prepare statement, manually define parameters
* for it, execute that statement with different parameters
* and perform non-default error processing.
*
* Example for the following interfaces:
* IAttachment - database attachment
* ITransaction - transaction
* IStatement - SQL statement execution
* IMessageMetadata - describe input and output data format
* IMetadataBuilder - tool to modify/create metadata
* IStatus - return state holder
*
* Note that all updates are rolled back in this version. (see *** later)
*
* Run something like this to build the program :
*
* fpc -Fu./common -Fu/opt/firebird/include/firebird -FUlib -oupdate 02.update.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
* https://www.ibphoenix.com/about/firebird/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 Paul Reeves
* for the Firebird Open Source RDBMS project.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*}
{$mode Delphi}{$H+}
Uses
SysUtils,
Firebird;
Type
Buffer = String [255];
Var
// master and status are required for all access to the API.
// This is main interface of firebird, and the only one
// for getting which there is special function in our API
master: IMaster;
// Status is used to return error description to user
status: IStatus;
// Provider is needed to start to work with database (or service)
prov: IProvider;
// declare pointers to required interfaces
att: IAttachment;
tra: ITransaction;
// Interface executes prepared SQL statement
stmt: IStatement;
// Interfaces provides access to format of data in messages
meta: IMessageMetadata;
// Interface makes it possible to change format of data or define it yourself
builder: IMetadataBuilder;
Dept_Data: Array[0..4] Of String = ( '622', '100', '116', '900', '0' );
Percent_data: Array[0..4] Of Double = ( 0.05, 1.00, 0.075, 0.10, 0 );
i: Integer;
InputBuffer: Buffer;
len: Integer;
PPercent_inc: PChar;
PDept_no: PChar;
Const
UpdateString = 'UPDATE department SET budget = ? * budget + budget WHERE dept_no = ?';
SQL_DIALECT_V6 = 3;
SQL_DIALECT_CURRENT = SQL_DIALECT_V6;
SQL_TEXT = 452; // CHAR
SQL_DOUBLE = 480; // DOUBLE PRECISION
Procedure PrintError( AMaster: IMaster; AStatus: IStatus );
Var
maxMessage: Integer;
outMessage: PAnsiChar;
Begin
maxMessage := 256;
outMessage := StrAlloc( maxMessage );
AMaster.getUtilInterface.formatStatus( outMessage, maxMessage, AStatus );
writeln( outMessage );
StrDispose( outMessage );
End;
// Get the department and percent parameters for an example to run.
Begin
master := fb_get_master_interface;
status := master.getStatus;
Try
// the main dispatcher is returned by a call to IMaster
// no errors can occur - this function will always succeed
prov := master.getDispatcher;
// We assume that ISC_USER and ISC_PASSWORD env vars are set. Otherwise,
// see code in 01.create for an example of setting the un/pw via the dpb.
att := prov.attachDatabase( status, 'employee', 0, nil );
writeln( 'Attached to database employee.fdb' );
// start transaction
tra := att.startTransaction( status, 0, nil );
// prepare statement
stmt := att.prepare( status, tra, 0, UpdateString, SQL_DIALECT_CURRENT, 0 );
// build metadata
// IMaster creates empty new metadata in builder
builder := master.getMetadataBuilder( status, 2 );
// set required info on fields
builder.setType( status, 0, SQL_DOUBLE + 1 );
builder.setType( status, 1, SQL_TEXT + 1 );
builder.setLength( status, 1, 3 );
// IMetadata should be ready
meta := builder.getMetadata( status );
// no need for builder any more
builder.Release( );
builder := nil;
len := meta.getMessageLength( status );
If ( len > sizeof( InputBuffer ) ) Then
Raise Exception.Create( 'Input message length too big - cannot continue' )
Else
FillChar( InputBuffer, SizeOf( InputBuffer ), 0 );
i := meta.getNullOffset( status, 0 );
InputBuffer[i] := Char( 0 );
i := meta.getNullOffset( status, 1 );
InputBuffer[i] := Char( 0 );
Try
// locations of parameters in input message
PPercent_inc := PChar( @InputBuffer [meta.getOffset( status, 0 )] );
PDept_no := PChar( @InputBuffer [meta.getOffset( status, 1 )] );
For i := 0 To length( Dept_Data ) - 1 Do Begin
If ( Dept_Data [i] = '0' ) Or ( Percent_data [i] = 0 ) Then
break;
StrPCopy( PPercent_inc, Percent_data [i].ToString );
StrPCopy( PDept_no, Dept_Data [i] );
WriteLn( 'Increasing budget for department: ' + PDept_no + ' by ' + PPercent_inc + ' percent.' );
Try
stmt.Execute( status, tra, meta, @InputBuffer, nil, nil );
// Save/Cancel each department's update independently.
// *** Change to commitRetaining() to see changes
// *** tra.commitRetaining(status);
tra.rollbackRetaining( status );
Except
on E: FBException Do Begin
PrintError( master, status );
tra.rollbackRetaining( status );
End;
End;
End;
stmt.Free( status );
stmt := nil;
meta.Release;
meta := nil;
tra.commit( status );
tra := nil;
att.detach( status );
att := nil;
Except
on E: FBException Do Begin
PrintError( master, status );
tra.rollbackRetaining( status );
End;
on E: Exception Do
WriteLn( E.Message );
End;
Finally
If assigned( meta ) Then
meta.Release;
If assigned( builder ) Then
builder.Release;
If assigned( stmt ) Then
stmt.Release;
If assigned( tra ) Then
tra.Release;
If assigned( att ) Then
att.Release;
prov.Release;
status.dispose;
End;
End.