ibm-information-center/dist/eclipse/plugins/i5OS.ic.ddp_5.4.0.1/rbal1excprogram.htm

302 lines
21 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: C program using embedded SQL" />
<meta name="DC.subject" content="example, programming, C/400 language, C/400, examples" />
<meta name="keywords" content="example, programming, C/400 language, C/400, examples" />
<meta name="DC.Relation" scheme="URI" content="rbal1progxmp.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rbal1excprogram" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: C program using embedded SQL</title>
</head>
<body id="rbal1excprogram"><a name="rbal1excprogram"><!-- --></a>
<img src="./delta.gif" alt="Start of change" /><!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: C program using embedded SQL</h1>
<div><div class="section"> <div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
</div>
<div class="example"> <div class="fignone" id="rbal1excprogram__rbal1cxmpl"><a name="rbal1excprogram__rbal1cxmpl"><!-- --></a><span class="figcap">Figure 1. C program example using embedded SQL</span><pre>/******************************************************************************/
/* PROGRAM NAME: SAMPEMBC */
/* */
/* DESCRIPTIVE NAME: Sample embedded C application using DRDA */
/* */
/* FUNCTION: This module processes the PART_STOCK table and */
/* for each part below the ROP (REORDER POINT) */
/* creates a supply order. */
/* */
/* LOCAL TABLES: PART_STOCK */
/* */
/* REMOTE TABLES: PART_ORDER, PART_ORDLN, SHIPMENTLN */
/* */
/* COMPILE OPTIONS: */
/* CRTSQLCI OBJ(SAMPEMBC) COMMIT(*CHG) RDB(rdbname) OBJTYPE(*PGM) */
/* RDBCNNMTH(*RUW) */
/* */
/* INVOKED BY: */
/* CALL PGM(SAMPEMBC) PARM('lcldbname' 'rmtdbname') */
/******************************************************************************/
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
EXEC SQL INCLUDE SQLCA;
/******************************************************************************/
/* SQL Variables */
/******************************************************************************/
EXEC SQL BEGIN DECLARE SECTION;
char loc[4] = "SQLA"; /* dealer's database name */
char remote_db[18] = " ";
/* sample remote database */
char local_db[18] = " ";
/* sample local database */
char part_table[5] = " "; /* part number in table part_stock */
long quant_table; /* quantity in stock, tbl part_stock */
long rop_table; /* reorder point , tbl part_stock */
long eoq_table; /* reorder quantity , tbl part_stock */
short next_num; /* next order nbr,table part_order */
short ord_table; /* order nbr. , tbl order_line */
short orl_table; /* order line , tbl order_line */
long qty_table; /* ordered quantity , tbl order_line */
long line_count = 0; /* total number of order lines */
short ind_null; /* null indicator for qty_table */
short contl = 0; /* continuation line, tbl order_line */
EXEC SQL END DECLARE SECTION;
/******************************************************************************/
/* Other Variables */
/******************************************************************************/
char first_time, what_time;
long qty_rec = 0, qty_req = 0;
EXEC SQL WHENEVER SQLERROR GOTO error_tag;
EXEC SQL WHENEVER SQLWARNING CONTINUE; </pre>
</div>
<div class="fignone"></div>
</div>
<div class="example"><div class="fignone"><pre>/******************************************************************************/
/* Function Declarations */
/******************************************************************************/
/******************************************************************************/
/* Function For Declaring Cursors */
/******************************************************************************/
declare_cursors() {
/* SQL Cursor declaration and reposition for local UW */
EXEC SQL DECLARE NEXT_PART CURSOR FOR
SELECT PART_NUM, PART_QUANT, PART_ROP, PART_EOQ
FROM DRDA/PART_STOCK
WHERE PART_ROP &gt; PART_QUANT AND
PART_NUM &gt; :part_table
ORDER BY PART_NUM;
/* SQL Cursor declaration and connect for RUW */
EXEC SQL DECLARE NEXT_OLINE CURSOR FOR
SELECT A.ORDER_NUM, ORDER_LINE, QUANT_REQ
FROM DRDA/PART_ORDLN A,
DRDA/PART_ORDER B
WHERE PART_NUM = :part_table AND
LINE_STAT &lt;&gt; 'C' AND
A.ORDER_NUM = B.ORDER_NUM AND
ORDER_TYPE = 'R';
/* upline exit function in connectable state */
goto function_exit;
error_tag:
error_function();
function_exit:
;
} /* function declare_cursor */</pre>
</div>
</div>
<div class="example"><div class="fignone"><pre>/******************************************************************************/
/* Function For Reseting Tables */
/******************************************************************************/
reset_tables() {
/* Clean up for rerunability in test environment */
EXEC SQL CONNECT TO :remote_db;
EXEC SQL DELETE FROM DRDA/PART_ORDLN
WHERE ORDER_NUM IN
(SELECT ORDER_NUM
FROM DRDA/PART_ORDER
WHERE ORDER_TYPE = 'R');
EXEC SQL DELETE FROM DRDA/PART_ORDER
WHERE ORDER_TYPE = 'R';
/* Exit function in connectable state */
EXEC SQL COMMIT;
goto function_exit;
error_tag:
error_function();
function_exit:
;
} /* function delete_for_rerun */</pre>
</div>
</div>
<div class="example"><div class="fignone"><pre>/******************************************************************************/
/* Function For Declaring Cursors */
/******************************************************************************/
calculate_order_quantity() {
/* Set current connection to local database */
EXEC SQL CONNECT TO :local_db;
/* available qty = Stock qty + qty in order - qty received */
EXEC SQL OPEN NEXT_PART;
EXEC SQL FETCH NEXT_PART
INTO :part_table, :quant_table, :rop_table, :eoq_table;
if (sqlca.sqlcode == 100) {
printf("--------------------------------\n");
printf("NUMBER OF LINES CREATED = %d\n",line_count);
printf("--------------------------------\n");
printf("***** END OF PROGRAM *********\n");
rop_table = 0; /* no (more) orders to process */
}
else {
qty_rec = 0;
qty_req = 0;
EXEC SQL COMMIT;
EXEC SQL CONNECT TO :remote_db;
EXEC SQL OPEN NEXT_OLINE;
do {
EXEC SQL FETCH NEXT_OLINE
INTO :ord_table, :orl_table, :qty_table;
qty_rec = qty_rec + qty_table;
} while(sqlca.sqlcode != 100);
EXEC SQL CLOSE NEXT_OLINE;
EXEC SQL SELECT SUM(QUANT_RECV)
INTO :qty_table:ind_null
FROM DRDA/SHIPMENTLN
WHERE ORDER_LOC = :loc AND
ORDER_NUM = :ord_table AND
ORDER_LINE = :orl_table;
if (ind_null != 0)
qty_rec = qty_rec + qty_table;
} /* end of else branch */
goto function_exit;
error_tag:
error_function();
function_exit:
;
} /* end of calculate_order_quantity */</pre>
</div>
</div>
<div class="example"><div class="fignone"><pre>/******************************************************************************/
/* Function For Declaring Cursors */
/******************************************************************************/
process_order() {
/* insert order and order_line in remote database */
if (contl == 0) {
EXEC SQL SELECT (MAX(ORDER_NUM) + 1)
INTO :next_num
FROM DRDA/PART_ORDER;
EXEC SQL INSERT INTO DRDA/PART_ORDER
(ORDER_NUM, ORIGIN_LOC, ORDER_TYPE, ORDER_STAT, CREAT_TIME)
VALUES (:next_num, :loc, 'R', 'O', CURRENT TIMESTAMP);
printf("***** ROP PROCESSING *********\n");
printf("ORDER NUMBER = %d \n\n",next_num);
printf("--------------------------------\n");
printf(" LINE PART QTY \n");
printf(" NBR NBR REQUESTED\n");
printf("--------------------------------\n");
contl = contl + 1;
} /* if contl == 0 */
EXEC SQL INSERT INTO DRDA/PART_ORDLN
(ORDER_NUM, ORDER_LINE, PART_NUM, QUANT_REQ, LINE_STAT)
VALUES (:next_num, :contl, :part_table, :eoq_table, 'O');
line_count = line_count + 1;
printf(" %d %.5s %d\n",line_count,part_table,eoq_table);
contl = contl + 1;
/* Exit function in connectable state */
EXEC SQL COMMIT;
goto function_exit;
error_tag:
error_function();
function_exit:
;
} /* end of function process_order */</pre>
</div>
</div>
<div class="example"><div class="fignone"><pre>/******************************************************************************/
/* Function For Declaring Cursors */
/******************************************************************************/
error_function() {
printf("************************\n");
printf("* SQL ERROR *\n");
printf("************************\n");
printf("SQLCODE = %d\n",sqlca.sqlcode);
printf("SQLSTATE = %5s",sqlca.sqlstate);
printf("\n**********************\n");
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL ROLLBACK;
/* Reset Current Connection To Local Database */
EXEC SQL CONNECT RESET;
exit(999);
} /* end of function error_function */
/******************************************************************************/
/* Mainline */
/******************************************************************************/
main(int argc, char *argv[]) {
memcpy(local_db,argv[1],strlen(argv[1]));
memcpy(remote_db,argv[2],strlen(argv[2]));
/* Initialization */
declare_cursors();
reset_tables();
/* Main Work */
do {
calculate_order_quantity();
if (rop_table &gt; quant_table + qty_req - qty_rec) {
process_order();
quant_table = qty_req = qty_rec = 0;
}
} while (sqlca.sqlcode == 0);
EXEC SQL COMMIT;
/* Reset Current Connection To Local Database */
EXEC SQL DISCONNECT :local_db;
exit(0);
} /* end of main */</pre>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbal1progxmp.htm" title="This example application for distributed relational database use is written in RPG/400, COBOL/400, Java and ILE C/400 programming languages. This example shows how to use a distributed relational database for functional specification tasks.">Examples: Application programming</a></div>
</div>
</div>
<img src="./deltaend.gif" alt="End of change" /></body>
</html>