RPG & DB2 Stored Procedures with PHP - Schedschd.ws/hosted_files/commons17/77/Stored...
Transcript of RPG & DB2 Stored Procedures with PHP - Schedschd.ws/hosted_files/commons17/77/Stored...
1© 2017 Rogue Wave Software, Inc. All Rights Reserved. 1
RPG & DB2 Stored Procedures with PHP
2© 2017 Rogue Wave Software, Inc. All Rights Reserved. 2
PHPish Sessions
• Workshop – Make your IBM i Sizzle with Wordpress (Alan)A4AA – 12:30 PM
• Connecting the dots: Building Web Apps w/PHP, HTML & CSS (John)13BH – 10:45 AM
• PHP on IBM i: Getting Started (Mike)14BI – 12:45 PM
• OO and Ahh! Understanding Object Oriented PHP (John)16BI –3:45 PM
• Open Lab: Basic PHP to access your Data on IBM i, Object Oriented PHP (John)21AB – 8:00 AM to Noon
• Introduction to Maria DB for IBM i (Mike)21BJ – 8:00 AM
• PHP and DB2 Essentials (Alan)25BJ – 2:00 PM
• PHP Tricks for the RPG Programmer (Alan & Birgita)25CA – 2:00 PM
• Building Reusable APIs (Stephanie)27BJ – 5:00 PM
• Watson and PHP Inventory Integration (Mike & Clark)32CL – 9:30 AM
• Intro to Python33CL – 11:00 AM
• Using DB2 and SQL with Open Source Languages (Alan & Scott)35CL – 2:00 PM
• IBM i and PHP System Integration (Jim O)35BH – 2:00 PM
• Debugging your web application from A to Zend (Stephanie)41BI – 8:00 AM
• A Real Zend Expressive Application (Clark)42BH – 9:30 AM
• Bring RPG/COBOL Business Logic to the web with PHP Toolkit (Alan)42BI – 9:30 AM
• Object Oriented Business Programming :An intro to DDD (Chuk)46AD – 3:30 PM
• RPG and DB2 Stored procedures with PHP on IBM i (Mike)46BJ – 3:30 PM
• Object Oriented Business Programming :An intro to DDD (Chuk)46CP – 3:30 PM
• Case Study jQuery and PHP – Drag & Drop Scheduling for BPCS (John)46BH – 3:30 PM
• Getting Started with PHP Frameworks (Stephanie)47BH – 5:00 PM
3© 2017 Rogue Wave Software, Inc. All Rights Reserved. 3
Agenda
• DB2
– SQL Stored procedures
– Examples
• RPG
– Hello World from RPG (sp1)
– Pass an array from RPG to PHP (sp2)
– Pass a record set from RPG to PHP (sp3)
• Q&A
4© 2017 Rogue Wave Software, Inc. All Rights Reserved. 4
Introduction to SPs
5© 2017 Rogue Wave Software, Inc. All Rights Reserved. 5
Stored Procedures…What are they?
• Database artifacts that perform a specific task.
• Zend Server PHP on IBM i supports access to multiple DB’s
– DB2
– MySQL
– MS SQL Server
– Oracle
– Mongo DB (Zend Server 5.6)
• Therefore, SP’s on each are available to PHP on IBM i
6© 2017 Rogue Wave Software, Inc. All Rights Reserved. 6
Stored Procedures on IBM i…What are they?
• Operating System Objects but Database attributes
• Typically written in SQL or RPG, but…
• Can be written in nearly any HLL like
– Java, REXX, C, CL, C++, COBOL, FORTRAN, PL1
7© 2017 Rogue Wave Software, Inc. All Rights Reserved. 7
What do they look like?
Simple SP data retrieval:
CREATE PROCEDURE sales_price(
IN Customer# CHAR(8),
IN Product# CHAR(8),
OUT price DECIMAL(12,2))
LANGUAGE SQL
BEGIN
SELECT CustPrice FROM priceTable
WHERE CUSTNO=Customer# AND
PRODNO=Product# AND
active = ‘Y’
END
8© 2017 Rogue Wave Software, Inc. All Rights Reserved. 8
Open Source Toolkit has examples
9© 2017 Rogue Wave Software, Inc. All Rights Reserved. 9
Command line procedure creation
From PASE:
QSH CMD(‘/usr/bin/db2 -t -f
/QSYS.LIB/MPAVLAK.lib/QSQLSRC.FILE/MPSQL1.MBR')
Or…
From Native Command Line
RUNSQLSTM
SRCFILE(MPAVLAK/QSQLSRC) SRCMBR(MPSQL1)
10© 2017 Rogue Wave Software, Inc. All Rights Reserved. 10
IBM i Navigator
11© 2017 Rogue Wave Software, Inc. All Rights Reserved. 11
DB2 Housekeeping
• Supports 2 types
– External (RPG, COBOL, etc.)
– SQL
• Implemented via C programs (transparently)
• Enhancements with each release of IBM i
– Be Careful if developing for multiple OS versions
• SQL details:
– Supports multiple statements
– Supports iterative processing (Loop, Repeat Until, While)
– Get Diagnostics and feedback (SQLSTATE, SQLCODE)
– Can return discrete results or result sets
12© 2017 Rogue Wave Software, Inc. All Rights Reserved. 12
Example
13© 2017 Rogue Wave Software, Inc. All Rights Reserved. 13
Before…
14© 2017 Rogue Wave Software, Inc. All Rights Reserved. 14
Output
15© 2017 Rogue Wave Software, Inc. All Rights Reserved. 15
After
16© 2017 Rogue Wave Software, Inc. All Rights Reserved. 16
Looks the same!
17© 2017 Rogue Wave Software, Inc. All Rights Reserved. 17
And now a word about library lists
18© 2017 Rogue Wave Software, Inc. All Rights Reserved. 18
Option parameter of DB2_Connect
• Options parameter is an array.
• Supports three kinds if library list
– i5_lib – Single library
– i5_naming – Enable library list via user profile
– i5_libl – Specify library list in PHP
• Supports many other parameters
19© 2017 Rogue Wave Software, Inc. All Rights Reserved. 19
Single library approach
• Similar to the world of MySQL/MariaDB.
20© 2017 Rogue Wave Software, Inc. All Rights Reserved. 20
Multiple libraries hardcoded in PHP
• More like real life
21© 2017 Rogue Wave Software, Inc. All Rights Reserved. 21
Job Description – PHPUSER2
• WRKJOBD, DSPJOBD
22© 2017 Rogue Wave Software, Inc. All Rights Reserved. 22
Multiple libraries via User Profile
• Parallels Green Screen access
23© 2017 Rogue Wave Software, Inc. All Rights Reserved. 23
Back to SPs
24© 2017 Rogue Wave Software, Inc. All Rights Reserved. 24
Observations about SPs
• Advantages?
– Static objects
– Indexing, permanent objects
– Manage queries, DBA role is emerging
– Ever heard of little Bobby Tables?
• Disadvantages
– Another thing to manage
25© 2017 Rogue Wave Software, Inc. All Rights Reserved. 25
Security is no laughing matter?
26© 2017 Rogue Wave Software, Inc. All Rights Reserved. 26
Understand which Exec to use
• DB2_Exec
– Fire and forget
– Use when no parameters necessary
• DB2_Execute
– Prepared SQL via DB2_Prepare & DB2 bind
– Always use when using parameters
– Example in Samples
27© 2017 Rogue Wave Software, Inc. All Rights Reserved. 27
Hello World
28© 2017 Rogue Wave Software, Inc. All Rights Reserved. 28
Start slow
• Don’t try to boil the ocean with your first SP!
• How did you learn RPG?
– Reports
– Batch
– Interactive
– Subfiles, etc…
• Start with simple SP’s and grow..
29© 2017 Rogue Wave Software, Inc. All Rights Reserved. 29
An approach
• Process:
– Create RPG
– Identify parameter interface
– Create procedure
– Test in Database Navigator
– Try it in PHP
– Increase complexity a little
– Repeat!
30© 2017 Rogue Wave Software, Inc. All Rights Reserved. 30
RPG
31© 2017 Rogue Wave Software, Inc. All Rights Reserved. 31
Create the procedure & test
CREATE PROCEDURE MPAVLAK/RPGSP1(INOUT Response CHAR ( 32)) LANGUAGE RPGLE
NOT DETERMINISTIC CONTAINS SQL EXTERNAL NAME MPAVLAK/RPGSP1 PARAMETER STYLE GENERAL
Then fire up System i Navigator
32© 2017 Rogue Wave Software, Inc. All Rights Reserved. 32
Admire results in DB Nav
33© 2017 Rogue Wave Software, Inc. All Rights Reserved. 33
Now try it in PHP
34© 2017 Rogue Wave Software, Inc. All Rights Reserved. 34
Results from Script
35© 2017 Rogue Wave Software, Inc. All Rights Reserved. 35
Array from RPG to PHP
36© 2017 Rogue Wave Software, Inc. All Rights Reserved. 36
Now an array
• Data elements are nice, but…
• Sometimes you need a little more…
• How about an array of information?
37© 2017 Rogue Wave Software, Inc. All Rights Reserved. 37
SQL to create the SP & test in iNav
CREATE PROCEDURE mpavlak/rpgsp2() LANGUAGE RPGLE
EXTERNAL NAME mpavlak/rpgsp2 GENERAL
38© 2017 Rogue Wave Software, Inc. All Rights Reserved. 38
Open results in PHP
39© 2017 Rogue Wave Software, Inc. All Rights Reserved. 39
Results in browser
40© 2017 Rogue Wave Software, Inc. All Rights Reserved. 40
Record set from RPG
41© 2017 Rogue Wave Software, Inc. All Rights Reserved. 41
Have RPG build the result set and use SQL…
42© 2017 Rogue Wave Software, Inc. All Rights Reserved. 42
SQL to create the SP & test in iNav
CREATE PROCEDURE mpavlak/rpgsp3() LANGUAGE RPGLE
EXTERNAL NAME mpavlak/rpgsp3 GENERAL
43© 2017 Rogue Wave Software, Inc. All Rights Reserved. 43
Now open results in PHP
44© 2017 Rogue Wave Software, Inc. All Rights Reserved. 44
Now open results in browser
45© 2017 Rogue Wave Software, Inc. All Rights Reserved. 45
Wrap it up…
47© 2017 Rogue Wave Software, Inc. All Rights Reserved. 47
ATTENDConnect with experts.Become an authority.Register now:zendcon.com/register-now
EXPLOREShare, inspire, educate.Network
SPONSORSpotlight your best in enterprise PHP, opensource & frameworks:[email protected]
Visit zendcon.com
48© 2017 Rogue Wave Software, Inc. All Rights Reserved. 48