PROFOUNDly LOGIC on IBM i - Part 2
I started my blog’s to keep a
track of things I try on IBM i - REGEXP, Python, Nodes or ProfoundJS. Btw, I'm totally unfamiliar to JavaScript code but I feel it is good to learn but doing something. I’m learning JavaScript in every little free time I have :-)
Now, in this blog its about ProfoundUI with ProfoundJS.
Profoundjs.connector provides
connectivity to database i.e IBM i Db2.
Similarly Profound.js has set of API to
run processes.
Profound.js has RLA & SQL API’s. I tried CRUD with SQL API. I’m
writing lot of SQLRPGLE these days so I prefer it over RLA (Record level Access
). There is couple of videos with Profound TV about SQL access on how to
retrieve the data.
Below screen is the display
file, the usual function keys are CSS buttons - click on it then enter the
field name and make it as Boolean indicator, for screen field name i.e. country code
is set with Label and for input field i.e. COUNTRYCD we must chose TextBox.
Save
the changes in IFS folder and click on the Launch at top of the screen. Upon Launch,
we will see the screen below -
The
below script is written in IFS folder i.e. /profoundjs/modules/pjssamples/updcnty.js.
The
config file - /profoundjs/config.js has “initial modules” and we must specify
the .js file name here. We can have different folder name, but I maintained the
pjssamples folder.
Set the SQL with
process.env command to get the logs.
Below is Javascript
coding, define a module (as function) and at the end export the module.
Exports. Run (function
name)
Display the
screen with the code below –
process.env["PJS_SQL_DEBUG"]
= "1";
function updcnty() {
pjs.defineDisplay("display","updcnty.json");
while(!exit) {
display.updrcd.execute();
Let's look at how to search i.e. retrieve record based on country code
// ** Get records based on
country code **
if (fetch) {
var record = pjs.query("Select * from
country where countrycd = ?",countrycd,1);
pjs.setFields(record);
display.updrcd.execute();
}
NOTE - For next
update task, I misspelled the country name.
Click Modify - to update any field.
// ** Upd record based on
country code **
if (modify) {
var record1 = {};
// pjs.getFields(record1);
record1.COUNTRYNM = countrynm;
record1.NATSPORT = natsport ;
record1.NATCURR = natcurr
;
record1.NATBIRD = natbird
;
pjs.query("UPDATE PJSSAMPLES.COUNTRY SET
? WHERE countrycd = ? with NC",[record1,countrycd]
if (sqlstate == '00000') {
console.log(record1);
}
display.updrcd.execute();
}
Click Add -
// ** Insert record **
if (add) {
var newrcd = {} ;
newrcd.COUNTRYCD = countrycd;
newrcd.COUNTRYNM = countrynm;
newrcd.NATSPORT = natsport ;
newrcd.NATCURR = natcurr
;
newrcd.NATBIRD = natbird
;
console.log('The new
rcd:',newrcd);
pjs.query("INSERT
INTO PJSSAMPLES.COUNTRY SET ? with NC", newrcd);
if (sqlstate == '00000')
{
console.log('Added');
}
}
Click Delete -
// ** Delete record **
if (del) {
pjs.query("DELETE FROM
PJSSAMPLES.COUNTRY WHERE COUNTRYCD = ? with NC", countrycd);
if (sqlstate == '00000') {
console.log(countrycd);
console.log(countrycd);
console.log('deleted');
}
}
}
I know the code is not perfect :-( missing error handling, message windows etc but that for later...