-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathDataArea.php
269 lines (232 loc) · 9.88 KB
/
DataArea.php
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace ToolkitApi;
/**
* Class DataArea
*
* @package ToolkitApi
*/
class DataArea
{
private $DataAreaName =null;
private $DataAreaLib =null;
private $ToolkitSrvObj=null;
private $ErrMessage;
/**
* @param ToolkitInterface $ToolkitSrvObj
*/
public function __construct(?ToolkitInterface $ToolkitSrvObj = null)
{
if ($ToolkitSrvObj instanceof Toolkit) {
$this->ToolkitSrvObj = $ToolkitSrvObj;
return $this;
} else {
return false;
}
}
/**
* for *char data area. According to create other data area types
* use CL command
*
* @param string $DataAreaName
* @param string $DataAreaLib *CURLIB is correct, the default with CRTDTAARA.
* @param int $size
* @return bool
* @throws \Exception
*/
public function createDataArea($DataAreaName = '', $DataAreaLib = "*CURLIB", $size = 2000)
{
if ($DataAreaName !='' && $this->DataAreaName == NULL) { /*was not set before*/
$this->setDataAreaName($DataAreaName , $DataAreaLib);
}
if ($size > 2000 || $size <= 0) {
$dataAreaLen = 2000;
} else {
$dataAreaLen = $size;
}
$cmd = sprintf("QSYS/CRTDTAARA DTAARA(%s/%s) TYPE(*CHAR) LEN($dataAreaLen)",
($DataAreaLib != '' ? $DataAreaLib : $this->DataAreaLib),
($DataAreaName !='' ? $DataAreaName : $this->DataAreaName));
// @todo get CPF code
if (!$this->ToolkitSrvObj->CLCommand($cmd)) {
$this->ErrMessage = "Create Data Area failed." . $this->ToolkitSrvObj->getLastError();
throw new \Exception($this->ErrMessage);
}
return true;
}
/**
* @return string
*/
private function getAPIDataAreaName()
{
return (sprintf("%-10s%-10s", $this->DataAreaName, $this->DataAreaLib));
}
/**
* @return null|string
*/
protected function getQualifiedDataAreaName() {
// return dtaara name in lib/dtaara format.
if ($this->DataAreaLib) {
return "{$this->DataAreaLib}/{$this->DataAreaName}";
} else {
// no library (e.g. *LDA dtaara). Return only dtaara name.
return $this->DataAreaName;
}
}
/**
* *LIBL to read/write data area. *CURLIB to create.
*
* @param $dataAreaName
* @param string $dataAreaLib
* @throws \Exception
*/
public function setDataAreaName($dataAreaName, $dataAreaLib = "*LIBL")
{
/**
* special values:
* LDA Local data area
* GDA Group data area
* PDA Program initialization parameter data area
*/
$dataAreaName = trim(strtoupper($dataAreaName));
if ($dataAreaName == '') {
throw new \Exception("Data Area name parameter should be defined ");
}
// no library allowed for these special values.
if (in_array($dataAreaName, array('*LDA', '*GDA', '*PDA'))) {
$dataAreaLib = '';
}
$this->DataAreaName = $dataAreaName;
$this->DataAreaLib = $dataAreaLib;
}
/**
* @param int $fromPosition
* @param string $dataLen
* @return bool
*/
public function readDataArea($fromPosition = 1 , $dataLen = '*ALL')
{
if (!$this->ToolkitSrvObj instanceof Toolkit)
return false;
$Err = ' ';
$value = '';
if ($fromPosition == 0) {
$fromPosition = 1;
}
$maxValueSize = 2000; // largest allowed data area size
$adjustedStartRequested = $fromPosition;
$adjustedLengthRequested = $dataLen;
// if data len is *ALL, position and length receive special values..
if (strtoupper($dataLen) == '*ALL') { // either numeric or *ALL
$adjustedStartRequested = -1; // *ALL;
$adjustedLengthRequested = $maxValueSize;
}
$toolkit = $this->ToolkitSrvObj;
/**
* Retrieve Data Area (QWCRDTAA) API
* http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Fapis%2Fqwcrdtaa.htm
*
* Required Parameter Group:
* 1 Receiver variable Output Char(*)
* 2 Length of receiver variable Input Binary(4) Max length is 2000
* 3 Qualified data area name Input Char(20)
* 4 Starting position Input Binary(4)
* 5 Length of data Input Binary(4)
* 6 Error code I/O Char(*)
*
*
* format of receiver variable
* 0 0 BINARY(4) Bytes available (The length of all data available to return. All available data is returned if enough space is provided.)
* 4 4 BINARY(4) Bytes returned (The length of all data actually returned)
* 8 8 CHAR(10) Type of value returned (*CHAR, *DEC, *LGL)
* 18 12 CHAR(10) Library name (blank if *LDA et al.)
* 28 1C BINARY(4) Length of value returned
* 32 20 BINARY(4) Number of decimal positions
* 36 24 CHAR(*) Value (contents of data area)
*/
// @todo allow data structure in data area, if packed/binary allowed.
$receiverVar = array();
$receiverVar[] = $toolkit->AddParameterInt32('out', 'Bytes available: length of all data available to return', 'bytesAvail', $maxValueSize);
$receiverVar[] = $toolkit->AddParameterInt32('out', 'Length of all data returned, limited by size of receiver', 'bytesReturned', 0);
$receiverVar[] = $toolkit->AddParameterChar('out', '10', 'Type of value returned (*CHAR, *DEC, *LGL)', 'Type', '');
$receiverVar[] = $toolkit->AddParameterChar('out', '10', 'Library where data area was found', 'Library', '');
$receiverVar[] = $toolkit->AddParameterInt32('out', 'Length of value returned', 'lengthReturned', 0);
$receiverVar[] = $toolkit->AddParameterInt32('out', 'Number of decimal positions', 'decimalPositions', 0);
$receiverVar[] = $toolkit->AddParameterChar('out', $maxValueSize, 'Value returned', 'value', ''); // set length to $maxValueSize to be safe
$receiverLength = $maxValueSize + 36; // 4 4-byte integers + 2 10-byte character strings = 36
// "NAME LIB " no slash. Left-aligned.
$twentyCharQualifiedName = $this->getAPIDataAreaName();
$toolkitParams = array();
$toolkitParams [] = $toolkit->AddDataStruct($receiverVar, 'receiver');
$toolkitParams [] = $toolkit->AddParameterInt32('in', 'Length of receiver variable', 'receiverLen', $receiverLength);
$toolkitParams [] = $toolkit->AddParameterChar('in', 20, 'Data area name', 'DtaaraName', $twentyCharQualifiedName);
// Starting position: The first byte of the data area to be retrieved. A value of 1 will identify the first character in the data area. The maximum value allowed for the starting position is 2000. A value of -1 will return all the characters in the data area.
$toolkitParams [] = $toolkit->AddParameterInt32('in', 'Starting position requested', 'fromPosition', $adjustedStartRequested);
$toolkitParams [] = $toolkit->AddParameterInt32('in', 'Data length requested', 'dataLength', $adjustedLengthRequested);
$toolkitParams [] = $toolkit->AddErrorDataStructZeroBytes(); // so errors bubble up to joblog
// we're using a data structure here so integrity must be on
// @todo pass as option on the program call
$dsIntegrity = $toolkit->getOption('dataStructureIntegrity'); // save original value
$toolkit->setOptions(array('dataStructureIntegrity'=>true));
$retPgmArr = $toolkit->PgmCall('QWCRDTAA', '', $toolkitParams);
$toolkit->setOptions(array('dataStructureIntegrity'=>$dsIntegrity)); // restore original value
// check for any errors
if ($toolkit->getErrorCode()) {
// an error
return false;
} else {
// extricate the data from the receiver variable ds wrapper
$value = $retPgmArr['io_param']['receiver']['value'];
}
return ($value) ? $value : false;
}
/**
* @param $msg
*/
private function setError($msg){
$this->ErrMessage = $msg;
}
/**
* @return mixed
*/
public function getError() {
return $this->ErrMessage;
}
/**
* @param $value
* @param int $fromPosition
* @param int $dataLen
* @throws \Exception
*/
public function writeDataArea($value, $fromPosition = 0, $dataLen = 0)
{
$substring = ''; // init
if ($fromPosition > 0) {
$substring = sprintf("(%d %d)", $fromPosition, $dataLen);
}
// @todo use API instead. Handle numeric and character data., *CHAR and *DEC as well.
// and/or quote the string. Needs to be case-sensitive and handle numeric input.
$cmd = sprintf("CHGDTAARA DTAARA(%s $substring) VALUE($value)",
$this->getQualifiedDataAreaName());
if (!$this->ToolkitSrvObj->CLCommand($cmd)) {
$this->ErrMessage = "Write into Data Area failed." . $this->ToolkitSrvObj->getLastError();
throw new \Exception($this->ErrMessage);
}
}
/**
* requires explicit library
*
* @param string $DataAreaName
* @param string $DataAreaLib
* @throws \Exception
*/
public function deleteDataArea($DataAreaName = '', $DataAreaLib = '')
{
$cmd = sprintf("QSYS/DLTDTAARA DTAARA(%s/%s)",
($DataAreaLib != '' ? $DataAreaLib : $this->DataAreaLib),
($DataAreaName != NULL ? $DataAreaName : $this->DataAreaName));
if (!$this->ToolkitSrvObj->CLCommand($cmd)) {
$this->ErrMessage = "Delete Data Area failed." . $this->ToolkitSrvObj->getLastError();
throw new \Exception($this->ErrMessage);
}
}
}