Skip to content

Commit 0599b53

Browse files
committed
Test case for issue 92
1 parent 1b4d734 commit 0599b53

File tree

4 files changed

+236
-64
lines changed

4 files changed

+236
-64
lines changed

examples/JSONPARS9A.JSON-ForEach-iterator.rpgle

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
Ctl-Opt BndDir('NOXDB') dftactgrp(*NO) ACTGRP('QILE');
2424
/include qrpgleRef,noxdb
2525

26-
// Always set your ccsid for constants:
26+
// Always set your ccsid for constants:
2727
json_setDelimitersByCcsid(500);
2828

2929
example1();
30-
example2();
31-
example3();
30+
example2();
31+
example3();
32+
example4();
3233
*inlr = *ON;
3334

3435
// ------------------------------------------------------------------------------------
@@ -40,10 +41,10 @@ dcl-proc example1;
4041
Dcl-S pRows Pointer;
4142
Dcl-S t VarChar(512);
4243
Dcl-DS list likeds(json_iterator);
43-
44+
4445
// return an simple array with rows
4546
pRows = json_sqlResultSet('-
46-
Select * -
47+
Select * -
4748
from noxdbdemo.icproduct -
4849
where manuid = ''NIKON'' -
4950
');
@@ -54,19 +55,19 @@ dcl-proc example1;
5455
Return;
5556
EndIf;
5657

57-
// Now print each item from the list;
58+
// Now print each item from the list;
5859
// here just the product id.
59-
// Note: 'this' in the iterartor is the current element
60+
// Note: 'this' in the iterator is the current element
6061
list = json_setIterator(pRows);
6162
DoW json_ForEach(list);
62-
t = json_getStr(list.this : 'prodid');
63+
t = json_getStr(list.this : 'prodid');
6364
json_joblog(t);
6465
EndDo;
6566

6667
// Cleanup: dispose the rows in the array
6768
json_delete(pRows);
6869

69-
end-proc;
70+
end-proc;
7071
// ------------------------------------------------------------------------------------
7172
// example2
7273
// traverse an object.
@@ -91,7 +92,7 @@ dcl-proc example2;
9192
Return;
9293
EndIf;
9394

94-
// print the name (n) and the value(v)
95+
// print the name (n) and the value(v)
9596
list = json_setIterator(pJson);
9697
DoW json_ForEach(list);
9798
n = json_GetName (list.this);
@@ -101,18 +102,18 @@ dcl-proc example2;
101102

102103
json_delete(pJson);
103104

104-
end-proc;
105+
end-proc;
105106
// ------------------------------------------------------------------------------------
106107
// example3
107108
// Use iterators for looking up values;
108-
// Note: The SQL where clause is more efficient, but this is for an example:
109+
// Note: The SQL where clause is more efficient, but this is for an example:
109110
// ------------------------------------------------------------------------------------
110111
dcl-proc example3;
111112

112113
Dcl-S pRows Pointer;
113114
Dcl-S t VarChar(512);
114115
Dcl-DS list likeds(json_iterator);
115-
116+
116117
// return an simple array with rows
117118
pRows = json_sqlResultSet(
118119
'Select * from noxdbdemo.icproduct'
@@ -124,36 +125,36 @@ dcl-proc example3;
124125
Return;
125126
EndIf;
126127

127-
// "Lookup" done by iterator.
128-
// Note the usage of the "break"; if set the iterator stops the loop
129-
// and "this" contains the node where "break" was set.
128+
// "Lookup" done by iterator.
129+
// Note the usage of the "break"; if set the iterator stops the loop
130+
// and "this" contains the node where "break" was set.
130131
list = json_setIterator(pRows);
131132
DoW json_ForEach(list);
132-
if json_getStr (list.this:'manuid') = 'OLYMPUS'
133+
if json_getStr (list.this:'manuid') = 'OLYMPUS'
133134
and json_getNum (list.this:'price') > 2000;
134135
list.break= *ON; // Found !! ( break the loop and dont't continue)
135136
endif;
136137
EndDo;
137138

138-
// Only when "break" is *ON, the list contains "this" specific value.
139+
// Only when "break" is *ON, the list contains "this" specific value.
139140
if list.break = *ON; // Found;
140-
t = json_getStr(list.this : 'prodid');
141+
t = json_getStr(list.this : 'prodid');
141142
json_joblog(t);
142143
endif;
143144

144145

145-
// Now try somthing that does not exists
146+
// Now try somthing that does not exists
146147
list = json_setIterator(pRows);
147148
DoW json_ForEach(list);
148-
if json_getStr (list.this:'manuid') = 'IBM'
149+
if json_getStr (list.this:'manuid') = 'IBM'
149150
and json_getNum (list.this:'price') > 2000;
150151
list.break= *ON; // Found !! ( break the loop and dont't continue)
151152
endif;
152153
EndDo;
153154

154-
// This time it will not be *ON ( it actually will be *OFF or *NULL)
155+
// This time it will not be *ON ( it actually will be *OFF or *NULL)
155156
if list.break = *ON; // Found;
156-
t = json_getStr(list.this : 'prodid');
157+
t = json_getStr(list.this : 'prodid');
157158
json_joblog(t);
158159
endif;
159160

@@ -162,4 +163,46 @@ dcl-proc example3;
162163
json_delete(pRows);
163164

164165
end-proc;
166+
// ------------------------------------------------------------------------------------
167+
// example4
168+
// Use iterators for nested results
169+
// ------------------------------------------------------------------------------------
170+
dcl-proc example4;
171+
172+
Dcl-S pManufacturer Pointer;
173+
Dcl-S pProducts Pointer;
174+
Dcl-s dollar char(1) inz(x'67');
175+
Dcl-DS manufacturereList likeds(json_iterator);
176+
177+
// return an simple array with rows
178+
pManufacturer = json_sqlResultSet(
179+
'Select * from noxdbdemo.icManufacturer'
180+
);
165181

182+
// Test errors:
183+
If json_Error(pManufacturer) ;
184+
json_joblog(json_Message(pManufacturer));
185+
Return;
186+
EndIf;
187+
188+
manufacturereList = json_setIterator(pManufacturer);
189+
DoW json_ForEach(manufacturereList );
190+
191+
pProducts = json_sqlResultSet(
192+
'Select * from noxdbdemo.icProduct where manuid = ' + dollar + 'manuid':
193+
1: // first row
194+
-1: // all rows
195+
0: // simple array
196+
manufacturereList.this // from where to pick the key
197+
);
198+
json_MoveObjectInto (manufacturereList.this : 'products' : pProducts );
199+
EndDo;
200+
201+
json_WriteJsonStmf(pManufacturer:'/prj/noxdb/testout/nested.json':1208:*OFF);
202+
203+
204+
on-exit;
205+
// Cleanup: dispose the rows in the array
206+
json_delete(pManufacturer);
207+
208+
end-proc;

examples/sql/icmanufactuer.sql

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
create schema noxdbdemo;
2+
drop table noxdbdemo.icManufacturer;
3+
4+
create or replace table noxdbdemo.icManufacturer (
5+
manuid char(30) primary key ,
6+
description varchar(64),
7+
logourl varchar(256)
8+
)
9+
rcdfmt manufactr ;
10+
11+
label on table noxdbdemo.icManufacturer
12+
is 'noxDb demo: Manufacturer' ;
13+
14+
label on column noxdbdemo.icManufacturer (
15+
manuid is 'Manufacturer ID' ,
16+
description is 'Description' ,
17+
logourl is 'Logo url'
18+
) ;
19+
20+
label on column noxdbdemo.icManufacturer(
21+
manuid text is 'Manufacturer ID' ,
22+
description text is 'Description' ,
23+
logourl text is 'Logo url'
24+
) ;
25+
26+
27+
insert into noxdbdemo.icManufacturer (
28+
manuid, description, logourl
29+
) values
30+
('ACER', 'Acer', 'Images/Acer.gif'),
31+
('CANON', 'Canon', 'Images/Canon.gif'),
32+
('CASIO', 'Casio', 'Images/Casio.gif'),
33+
('FUJIFILM', 'Fujifilm', 'Images/Fujifilm.gif'),
34+
('HP', 'HP', 'Images/HP.gif'),
35+
('KODAK', 'Kodak', 'Images/Kodak.gif'),
36+
('KONICA', 'Konica', 'Images/Konica.gif'),
37+
('NIKON', 'Nikon', 'Images/Nikon.gif'),
38+
('OLYMPUS', 'Olympus', 'Images/Olympus.gif'),
39+
('PANASONIC', 'Panasonic', 'Images/Panasonic.gif'),
40+
('SAMSUNG', 'Samsung', 'Images/Samsung.gif'),
41+
('SONY', 'Sony', 'Images/Sony.gif');

examples/sql/icproduct.sql

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
1-
create schema noxdbdemo;
1+
create schema noxdbdemo;
22
drop table noxdbdemo.icproduct;
3-
CREATE or REPLACE TABLE noxdbdemo.icproduct (
4-
PRODKEY DECIMAL(11, 0) NOT NULL DEFAULT 0 primary key,
5-
PRODID CHAR(30) NOT NULL DEFAULT '' ,
6-
DESCRIPTION CHAR(256) NOT NULL DEFAULT '' ,
7-
MANUID CHAR(30) NOT NULL DEFAULT '' ,
8-
PRICE DECIMAL(11, 2) NOT NULL DEFAULT 0 ,
9-
STOCKCNT DECIMAL(11, 0) NOT NULL DEFAULT 0 ,
10-
STOCKDATE DATE NOT NULL DEFAULT CURRENT_DATE )
11-
12-
RCDFMT PRODUCTR ;
13-
14-
LABEL ON TABLE noxdbdemo.icproduct
15-
IS 'noxDb demo: Product master' ;
16-
17-
LABEL ON COLUMN noxdbdemo.icproduct
18-
( PRODKEY IS 'Product Key' ,
19-
PRODID IS 'Product ID' ,
20-
DESCRIPTION IS 'Description' ,
21-
MANUID IS 'Manufacturer ID' ,
22-
PRICE IS 'Price' ,
23-
STOCKCNT IS 'Stock Count' ,
24-
STOCKDATE IS 'Stock Date' ) ;
25-
26-
LABEL ON COLUMN noxdbdemo.icproduct
27-
( PRODKEY TEXT IS 'Product Key' ,
28-
PRODID TEXT IS 'Product ID' ,
29-
DESCRIPTION TEXT IS 'Description' ,
30-
MANUID TEXT IS 'Manufacturer ID' ,
31-
PRICE TEXT IS 'Price' ,
32-
STOCKCNT TEXT IS 'Stock Count' ,
33-
STOCKDATE TEXT IS 'Stock Date' ) ;
34-
35-
GRANT DELETE , INSERT , SELECT , UPDATE
36-
ON noxdbdemo.icproduct TO PUBLIC ;
37-
38-
GRANT ALTER , DELETE , INDEX , INSERT , REFERENCES , SELECT , UPDATE
39-
ON noxdbdemo.icproduct TO QPGMR WITH GRANT OPTION ;
403

41-
insert into noxdbdemo.icproduct
4+
create or replace table noxdbdemo.icproduct (
5+
prodkey decimal(11, 0) not null default 0 primary key,
6+
prodid char(30) not null default '' ,
7+
description char(256) not null default '' ,
8+
manuid char(30) not null default '' ,
9+
price decimal(11, 2) not null default 0 ,
10+
stockcnt decimal(11, 0) not null default 0 ,
11+
stockdate date not null default current_date
12+
) rcdfmt productr ;
13+
14+
label on table noxdbdemo.icproduct
15+
is 'noxDb demo: Product master' ;
16+
17+
label on column noxdbdemo.icproduct (
18+
prodkey is 'Product Key' ,
19+
prodid is 'Product ID' ,
20+
description is 'Description' ,
21+
manuid is 'Manufacturer ID' ,
22+
price is 'Price' ,
23+
stockcnt is 'Stock Count' ,
24+
stockdate is 'Stock Date'
25+
) ;
26+
27+
label on column noxdbdemo.icproduct (
28+
prodkey text is 'Product Key' ,
29+
prodid text is 'Product ID' ,
30+
description text is 'Description' ,
31+
manuid text is 'Manufacturer ID' ,
32+
price text is 'Price' ,
33+
stockcnt text is 'Stock Count' ,
34+
stockdate text is 'Stock Date'
35+
) ;
36+
37+
grant delete , insert , select , update
38+
on noxdbdemo.icproduct to public ;
39+
40+
grant alter , delete , index , insert , references , select , update
41+
on noxdbdemo.icproduct to qpgmr with grant option ;
42+
43+
insert into noxdbdemo.icproduct
4244
(prodkey,prodid,description,manuid,price,stockcnt,stockdate)
43-
values
45+
values
4446
('10','µDIGITAL 600 ax''','Camera Plus','CASIO','-1.43','9',now()),
4547
('20','µDIGITAL 600 ex','Super flex XL plus','OLYMPUS','3005.80','234',now()),
4648
('30','µ-mini DIGITAL','Digitalkamera - 50 Mpix - optisk zoom: 2 x - understøttet hukommelse: xD-Picture Card','OLYMPUS','2667.12','200',now()),
@@ -50,7 +52,6 @@ values
5052
('70','µmini DIGITAL S','Digitalkamera - 50 Mpix - optisk zoom: 2 x - understøtet hukommelse: xD-Picture Card - guld','OLYMPUS','1378.86','30',now()),
5153
('80','µ[MJU:] DIGITAL 800','Digitalkamera - 80 Mpix - optisk zoom: 3 x - understøtet hukommelse: xD-Picture Card - møkeblåCard - møkeblå','OLYMPUS','3095.73','9',now()),
5254
('90','µ[MJU:] DIGITAL 800','Digitalkamera - 50 Mpix - optisk zoom: 3 x - understøtet hukommelse: xD-Picture Card','OLYMPUS','3069.59','2',now()),
53-
('100','µ[MJU:] mini DIGITAL','x--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xx--------xyyyyyy','OLYMPUS','2828.08','122',now()),
5455
('110','µ[MJU:] mini DIGITAL','Digitalkamera - 40 Mpix - optisk zoom: 2 x - understøttet hukommelse: xD-Picture Card - kosmisk sort','OLYMPUS','2461.64','3',now()),
5556
('120','µ[MJU:] mini DIGITAL','Digitalkamera - 40 Mpix - optisk zoom: 2 x - understøttet hukommelse: xD-Picture Card - ren hvid','SAMSUNG','1234.00','22',now()),
5657
('130','CAMEDIA AZ-2 Zoom','Digitalkamera - 40 Mpix - optisk zoom: 28 x - understøttet hukommelse: xD-Picture Card','SAMSUNG','1234.00','22',now()),

0 commit comments

Comments
 (0)