
DB2, SQL, and Open source languages on IBM i Alan Seiden with help from Scott Forstie DB2 Lessons from the Trenches of Open Source on IBM i Database-centric DB2 techniques (thanks, Scott and DB2 team!)... …that make open source and scripting languages easier in Alan’s world Alan Seiden Seiden Group ([email protected]) • Our team has PHP on IBM i focus • Builder of communities • PHP project leader, Zend/IBM Toolkit • Contributor, Zend Framework DB2 enhancements • www.seidengroup.com Scott Forstie DB2 for i Business Architect ([email protected]) • Responsible for DB2 for i • Frequently published author • Sporadic speaker at events • Happy to hear from you: https://twitter.com/Forstie_IBMi • Owner of this fantastic wiki: www.ibm.com/developerworks/ibmi/techupdates Open source languages (In order of “official” appearance on IBM i) • Zend + IBM PHP • 2006 • Added with TR7 Ruby • 2013 • Option 1 NodeJS • 5733-OPS • Option 2 Python • 5733-OPS IBM has integrated DB2 with open source languages • PHP: ibm_db2 <?php Ø https://pecl.php.net/package/ibm_db2 $pet = array(0, 'cat', 'Pook', 3.2); • Python: ibm_db $insert = 'INSERT INTO animals (id, breed, name, weight) Ø https://github.com/ibmdb/python-ibmdb VALUES (?, ?, ?, ?)'; $stmt = db2_prepare($conn, $insert); • Ruby: ibm_db if ($stmt) { Ø https://rubygems.org/gems/ibm_db/versions/3.0.1 $result = db2_execute($stmt, $pet); if ($result) { • Nods.js: ibm_db print "Successfully added new pet."; } Ø https://github.com/ibmdb/node-ibm_db } DB2 for i – One database, many interfaces Native Interfaces Commands & SQL Interfaces APIs JDBC CLI RUNSQL, RUNSQLSTM, Toolbox JDBC Driver Cobol RPG C or C++ Cobol RPG C or C++ STRQRY, CRTDUPOBJ, Native JDBC Driver etc… … SQLExecDirect … Embedded SQL SQL FETCHSQLExecDirect … … … SQLFETCHSQLExecDirect … SQLFETCH START Cobol RPG C or C++ … … READ SETLL … … SQL Table or View or … READ Ropen EXEC SQL … FETCH EXEC SQL … Rread … … DDS Physical or Logical File FETCH EXEC SQL … … FETCH … 123 Fish, Joe 555 20000 001 Cobol RPG C or C++ 124 Olson, Ole 501 20000 501 125 Smith, Sally 555 32000 555 … QSQPRCED 126 Johnson, John 501 38000 001 … … QSQPRCED … QSQPRCED 127 Smith, Jim 555 30000 555 … QSQPRCED … API Distributed Data Host Server (ZDA) Distributed Relational Extended Dynamic Remote Management (DDM Database (DRDA) SQL (XDA) IBM i Access Optimized IBM i Access Drivers DB2 Connect Drivers Cobol RPG C or C++ Cobol RPG C or C++ APIs … … EXEC SQL JDE START … … FETCH EXEC SQL READ … … SETLL FETCH … … EXEC SQL SAP READ Ropen … … JDBC ODBC JDBC ODBC FETCH Rread … … .NET ADO .NET ADO … 7 © 2016 IBM Corporation What is SQL? Structured Query Language Standard’s based technology Universal language of the Database Just another programming language Best tool in the toolbox Scott’s Query Language Why should you use (or expand your use of) SQL on i? ▪ Strategic database interface for industry ▪ Portability of code & skills ▪ Strategic interface for IBM i ▪ Faster delivery on IT requirements ▪ Performance & Scalability ▪ Increased Data Integrity ▪ Easy access to remote databases ▪ IBM i Navigator tooling (both Windows & Access Client Solutions) 9 © 2016 IBM Corporation Leverage what you already own ▪ State of the art SQL Query Engine (SQE) – Cost-based optimization – determine the most efficient data access methods to fulfill the query – Self learning and adapting, based on statistics – Query rewrite - rewrite the query to a more efficient equivalent statement – Graph based representation of the query ▪ SQL Plan Cache – system wide cache of pre-optimized statements, no matter the interface that was used ▪ Global Statistics manager – vital statistics for the cost-based optimizer ▪ IBM i Navigator tools for the database user/developer – Index Advisor – Show Statements – Create Based On – SQL Examples and CL prompting – SQL Details for jobs – Performance Monitoring and Analyze – Visual Explain for Queries 10 © 2016 IBM Corporation DB2 for i 7.3 • Standard compliant § Temporal Tables • Secure § Generated • Scalable columns for 7.2 • Functionally Advanced Value Proposition auditing § Row and Column § OLAP Extensions • Excellent Performance Access Control • Easier to use 7.1 § XMLTABLE § OFFSET and LIMIT • Easier to maintain § XML Support § CONNECT BY § Inline User-Defined § Encryption Table Functions enhancements § TRANSFER 6.1 (FIELDPROCs) OWNERSHIP § New Aggregate § Omnifind § Result set support in § TRUNCATE Functions § MySQL storage embedded SQL § More SQL Scalar § Index Merge V5R4 engine § CURRENTLY functions Ordering § DECFLOAT § WebQuery COMMITTED § Named arguments and § EVI Only Access § SSD Media § Grouping sets / § MERGE super groups defaults for parameters Preference § Global variables § More Built-in V5R3 § INSERT in FROM § Obfuscation of SQL § Partitioned tables § On Demand § Array support in routines Global Variables Performance § VALUES in FROM procedures § UFT-8 & UTF-16 Center § Extended Indicator § Array support in UDFs § More SQL Scalar § ICU sort sequence Variables § Three-part names and functions § Health Center aliases § Timestamp precision V5R2 § MQTs § Completion of SQL § Expression in § Increased routine § SQE Stage 1 § Sequences Core Indexes § SQE Logical file § Multiple-action Triggers and view limits § IASPs § Implicit char/ § Scalar fullselect § ROW CHANGE support TIMESTAMP § Identity columns numeric § Recursive CTE § SQE Adaptive Query § Built-in Global § More Services § Savepoints § BINARY / § OLAP expressions § Statistics catalog Processing Variables § CREATE OR VARBINARY views § UNION in views § EVI enhancements § 1.7 Terabyte Indexes § GET § INSTEAD OF § CLIENT special REPLACE TABLE § Scalar subselect DIAGNOSTICS triggers registers § Inline functions § Navigator Graphs and § ATTACH & § UDTFs § DRDA Alias § Descriptor area § SQE Stage 6 § CREATE OR Charts REPLACE DETACH Partition § DECLARE § DECIMAL(63) § XA support § DDM and DRDA § Regular Expressions GLOBAL § Pipelined Table § SQE Stage 3 § DDM 2-phase IPv6 § Partition table TEMPORARY § Scrollable cursor enhancements § SQL Dynamic Functions TABLE § Ragged SWA § Deferred Restore of § 2M SQL statement MQT and Logicals § Expressions in CALL Compound § Catalog views § QDBRPLAY § Environmental limits § UPDATE ROW across § Online Reorganize § 1000 tables in a § TR-timed query enhancements partitions § System Limits - IFS Enhancements delivered via DB2 PTF Groups 7.2 – TR4 7.2 – TR5 7.2 – TR6 7.3 – GA 7.3 – TR1 7.3 – TR2 2016 2017 SF99702 Level 11 SF99702 Level 14 SF99702 Level 16 SF99703 Level 1 SF99703 Level 3 SF99703 Level 4 Enhancements timed with TR4 Enhancements timed with TR1 & TR5 Enhancements timed with TR2 & TR6 • Inlined UDTFs • JSON_TABLE() • Performance Improvements • Trigger (re)deployment • INCLUDE for SQL Routines • JSON predicates • More IBM i Services • Database features in ACS • Additional Database features in ACS • New DB2 built-in Global Variables• Faster Scalar Functions • New and enhanced SQL Scalar • Enhanced SQL Scalar functions • More IBM i Services Functions • Evaluation option for DB2 SMP & • New DB2 for i Services • New IBM i Services DB2 Multisystem • And much more… • Enhanced DB2 for i Services • And more… www.ibm.com/developerworks/ibmi/techupdates/db2 CREATE TABLE + Auto Incrementing Row IDs CREATE OR REPLACE TABLE Data Definition Language (DDL) SQL statements that support the optional ‘OR REPLACE’ clause: ❑ CREATE OR REPLACE ALIAS ❑ CREATE OR REPLACE FUNCTION ❑ CREATE OR REPLACE MASK ❑ CREATE OR REPLACE PERMISSION Replacing a table: ❑ CREATE OR REPLACE PROCEDURE ✓ Data-Centric ✓ Dependent Views & MQTs preserved ❑ CREATE OR REPLACE SEQUENCE ✓ Triggers preserved ❑ CREATE OR REPLACE TABLE ✓ RCAC controls preserved ❑ CREATE OR REPLACE TRIGGER ✓ Auditing preserved ✓ Authorizations preserved ❑ CREATE OR REPLACE VARIABLE ✓ Comments and Labels preserved ❑ CREATE OR REPLACE VIEW ✓ Rows optionally deleted Knowledge Center www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_72/db2/rbafzhctabl.htm?lang=en Article for previous OR REPLACE statements iprodeveloper.com/database/use-sql-create-or-replace-improve-db2-i-object-management 15 Identity (autoincrement) columns CREATE TABLE mylib.best_data_table FOR SYSTEM NAME datatbl ( id INTEGER GENERATED ALWAYS AS IDENTITY ( START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE NO ORDER CACHE 20 ), data_column FOR COLUMN datacol VARCHAR(32) NOT NULL, more_data_column FOR COLUMN moredata VARCHAR(128) NOT NULL CONSTRAINT mylib.datatbl_pk PRIMARY KEY( id ) ); How to retrieve id value from PHP • function db2_last_insert_id() • “Returns the auto generated ID of the last insert query that successfully executed on this connection" • http://php.net/manual/en/function.db2-last-insert-id.php /* Checking for single row inserted. */ $stmt = db2_exec($conn, $insertTable); $ret = db2_last_insert_id($conn); if($ret) { echo "Last Insert ID is : " . $ret . "\n"; } else { echo "No Last insert ID.\n"; } Global Variables DB2 for i Built-in Global Variables • Use these variables to deploy advanced logic in triggers, RCAC rules, and more Variable name Schema Data Type Size Added with IBM i 7.2 JOB_NAME QSYS2 VARCHAR 28 SF99702 SERVER_MODE_JOB_NAME QSYS2 VARCHAR 28 Level 3 CLIENT_IPADDR SYSIBM VARCHAR 128 CLIENT_HOST SYSIBM VARCHAR 255 Available CLIENT_PORT SYSIBM INTEGER - with base PACKAGE_NAME SYSIBM VARCHAR 128 IBM i 7.2 PACKAGE_SCHEMA SYSIBM VARCHAR 128 PACKAGE_VERSION SYSIBM VARCHAR 64 ROUTINE_SCHEMA SYSIBM VARCHAR 128 ROUTINE_SPECIFIC_NAME SYSIBM VARCHAR 128 ROUTINE_TYPE SYSIBM CHAR 1 Create Variable ▪ Creates a global variable object which can
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages76 Page
-
File Size-