11<?php
22namespace ToolkitApi ;
33
4- class db2supp {
4+ /**
5+ * Class db2supp
6+ *
7+ * @todo define common transport class/interface extended/implemented by all transports
8+ *
9+ * @package ToolkitApi
10+ */
11+ class db2supp
12+ {
13+ private $ last_errorcode ;
14+ private $ last_errormsg ;
515
6- // @todo define common transport class/interface extended/implemented by all transports
7- // They have a lot in common.
8-
9- private $ last_errorcode = '' ; // SQL State
10- private $ last_errormsg = '' ; // SQL Code with message
11-
12- // 'persistent' is one option
16+ /**
17+ *
18+ *
19+ * @todo Throw in your "transport/adapter" framework for a real OO look and feel ....
20+ * Throw new Exception("Fail execute ($sql) ".db2_stmt_errormsg(),db2_stmt_error());
21+ * ... and retrieve via try/catch + Exception methods.
22+ *
23+ * @param $database
24+ * @param $user
25+ * @param $password
26+ * @param null $options 'persistent' is one option
27+ * @return bool
28+ */
1329 public function connect ($ database , $ user , $ password , $ options = null )
1430 {
15-
16- /*
17- * @todo Throw in your "transport/adapter" framework for a real OO look and feel ....
18- Throw new Exception( "Fail execute ($sql) ".db2_stmt_errormsg(),db2_stmt_error());
19- ... and retrieve via try/catch + Exception methods.
20- */
21-
22- // check for blank password with non-blank user. If so, throw the same error that wrong password would generate.
2331 // Compensate for older ibm_db2 driver that may not do this check.
2432 if ($ user && empty ($ password )) {
25-
2633 $ this ->setErrorCode ('08001 ' );
2734 $ this ->setErrorMsg ('Authorization failure on distributed database connection attempt. SQLCODE=-30082 ' );
2835
2936 return false ;
3037 }
3138
32- $ connectFunc = 'db2_connect ' ; // default
3339 if ($ options ) {
3440 if ((isset ($ options ['persistent ' ])) && $ options ['persistent ' ]) {
35- $ connectFunc = 'db2_pconnect ' ;
41+ $ conn = db2_pconnect ($ database , $ user , $ password );
42+ } else {
43+ $ conn = db2_connect ($ database , $ user , $ password );
44+ }
45+
46+ if (is_resource ($ conn )) {
47+ return $ conn ;
3648 }
3749 }
3850
39- // could be connect or pconnect
40- $ conn = $ connectFunc ( $ database , $ user , $ password );
41-
42- if (is_resource ($ conn )) {
43- return $ conn ;
44- }
45-
46- // error
4751 $ this ->setErrorCode (db2_conn_error ());
4852 $ this ->setErrorMsg (db2_conn_errormsg ());
4953
5054 return false ;
5155 }
52-
53- public function disconnect ( $ conn )
56+
57+ /**
58+ * @param $conn
59+ */
60+ public function disconnect ($ conn )
5461 {
55- if (is_resource ($ conn )) {
62+ if (is_resource ($ conn )) {
5663 db2_close ($ conn );
5764 }
5865 }
5966
60- // disconnect, truly close, a persistent connection.
61- public function disconnectPersistent ( $ conn )
67+ /**
68+ * disconnect, truly close, a persistent connection.
69+ *
70+ * @param $conn
71+ */
72+ public function disconnectPersistent ($ conn )
6273 {
63- if (is_resource ($ conn )) {
74+ if (is_resource ($ conn )) {
6475 db2_pclose ($ conn );
6576 }
6677 }
67-
78+
79+ /**
80+ * @return string
81+ */
6882 public function getErrorCode ()
6983 {
7084 return $ this ->last_errorcode ;
7185 }
72-
73- // added
86+
87+ /**
88+ * @return string
89+ */
7490 public function getErrorMsg ()
7591 {
7692 return $ this ->last_errormsg ;
7793 }
78-
94+
95+ /**
96+ * set error code and message based on last db2 prepare or execute error.
97+ *
98+ * @todo: consider using GET DIAGNOSTICS for even more message text:
99+ * http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzala%2Frzalafinder.htm
100+ *
101+ * @param null $stmt
102+ */
79103 protected function setStmtError ($ stmt = null )
80104 {
81- // set error code and message based on last db2 prepare or execute error.
82-
83- // @todo: consider using GET DIAGNOSTICS for even more message text:
84- // http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzala%2Frzalafinder.htm
105+ // is statement resource provided, or do we get last error?
85106 if ($ stmt ) {
86- // specific statement resource was provided
87107 $ this ->setErrorCode (db2_stmt_error ($ stmt ));
88108 $ this ->setErrorMsg (db2_stmt_errormsg ($ stmt ));
89109 } else {
90- // no specific statemtent. Get last error
91110 $ this ->setErrorCode (db2_stmt_error ());
92111 $ this ->setErrorMsg (db2_stmt_errormsg ());
93- } //(if ($stmt))
94-
112+ }
95113 }
96-
114+
115+ /**
116+ * @param $errorCode
117+ */
97118 protected function setErrorCode ($ errorCode )
98119 {
99120 $ this ->last_errorcode = $ errorCode ;
100121 }
101-
122+
123+ /**
124+ * @param $errorMsg
125+ */
102126 protected function setErrorMsg ($ errorMsg )
103127 {
104128 $ this ->last_errormsg = $ errorMsg ;
@@ -107,26 +131,21 @@ protected function setErrorMsg($errorMsg)
107131 /**
108132 * this function used for special stored procedure call only
109133 *
134+ * @todo the end result of this function is actually passed into the function. Why do the return?
135+ *
110136 * @param $conn
111137 * @param $sql
112138 * @param $bindArray
113139 * @return bool
114- *
115- * @todo this function seems strange, needs cleaned up
116140 */
117- public function execXMLStoredProcedure ( $ conn , $ sql , $ bindArray )
141+ public function execXMLStoredProcedure ($ conn , $ sql , $ bindArray )
118142 {
119- $ internalKey = $ bindArray ['internalKey ' ];
120- $ controlKey = $ bindArray ['controlKey ' ];
121- $ inputXml = $ bindArray ['inputXml ' ];
122- $ outputXml = $ bindArray ['outputXml ' ];
123-
124- // @todo error doesn't properly bubble up to top level.
143+ // @todo see why error doesn't properly bubble up to top level.
125144 // But added some error handling in ToolkitService.php, ExecuteProgram, looking at error code.
126- $ crsr = @db2_prepare ( $ conn , $ sql );
145+ $ crsr = @db2_prepare ( $ conn , $ sql );
127146
128147 // if the prepare failed
129- if ( !$ crsr ) {
148+ if ( !$ crsr ) {
130149 $ this ->setStmtError ();
131150 return false ;
132151 }
@@ -141,50 +160,47 @@ public function execXMLStoredProcedure( $conn, $sql, $bindArray )
141160
142161 // bind the four parameters
143162 foreach ($ params as $ param ) {
144-
145- $ ret = db2_bind_param ( $ crsr , $ param ['position ' ], $ param ['name ' ], $ param ['inout ' ] );
146- if (!$ ret ) {
163+ if (!db2_bind_param ($ crsr , $ param ['position ' ], $ param ['name ' ], $ param ['inout ' ])) {
147164 // unable to bind a param. Set error and exit
148165 $ this ->setStmtError ($ crsr );
149166 return false ;
150167 }
151168 }
152169
153- // execute the stored procedure.
154- // @ hides any warnings. Deal with !$ret on next line.
155- $ ret = @db2_execute ( $ crsr );
156-
157- if (!$ ret ) {
158- // execution of XMLSERVICE stored procedure failed.
159- $ this ->setStmtError ($ crsr ); // set error and exit
170+ if (!@db2_execute ($ crsr )) {
171+ $ this ->setStmtError ($ crsr );
160172 return false ;
161173 }
162174
163- return $ outputXml ;
175+ return $ bindArray [ ' outputXml ' ] ;
164176 }
165-
166- /*returns a first column from sql stmt result set*/
167- // used in one place: iToolkitService's ReadSPLFData().
168- // @todo eliminate this method if possible.
169- public function executeQuery ($ conn , $ sql )
177+
178+ /**
179+ * returns a first column from sql stmt result set
180+ *
181+ * used in one place: iToolkitService's ReadSPLFData().
182+ *
183+ * @todo eliminate this method if possible.
184+ *
185+ * @param $conn
186+ * @param $sql
187+ * @throws \Exception
188+ * @return array
189+ */
190+ public function executeQuery ($ conn , $ sql )
170191 {
171- $ Txt = '' ;
192+ $ txt = array () ;
172193 $ stmt = db2_exec ($ conn , $ sql , array ('cursor ' => DB2_SCROLLABLE ));
173- if (is_resource ($ stmt )) {
174- while (true ) {
175- $ row = db2_fetch_row ( $ stmt );
176- if (!$ row )
177- break ;
178-
194+ if (is_resource ($ stmt )) {
195+ if (db2_fetch_row ($ stmt )) {
179196 $ column = db2_result ($ stmt , 0 );
180- $ Txt [] = $ column ;
197+ $ txt [] = $ column ;
181198 }
182199 } else {
183- //$err = db2_stmt_error();
184200 $ this ->setStmtError ();
185- Throw new \Exception ( "Failure executing SQL: ( $ sql) " . db2_stmt_errormsg (), db2_stmt_error ());
201+ Throw new \Exception ("Failure executing SQL: ( $ sql) " . db2_stmt_errormsg (), db2_stmt_error ());
186202 }
187203
188- return $ Txt ;
204+ return $ txt ;
189205 }
190206}
0 commit comments