11<?php
22namespace ToolkitApi ;
33
4+ /**
5+ * Class odbcsupp
6+ *
7+ * @package ToolkitApi
8+ */
49class odbcsupp
510{
6- private $ last_errorcode = '' ; // SQL State
7- private $ last_errormsg = '' ; // SQL Code with message
8-
9- // 'persistent' is one option
11+ private $ last_errorcode = '' ;
12+ private $ last_errormsg = '' ;
13+
14+ /**
15+ *
16+ * @todo should perhaps handle this method differently if $options are not passed
17+ *
18+ * @param $database
19+ * @param $user
20+ * @param $password
21+ * @param null $options
22+ * @return bool|resource
23+ */
1024 public function connect ($ database , $ user , $ password , $ options = null )
1125 {
12- $ connectFunc = 'odbc_connect ' ; // default
1326 if ($ options ) {
1427 if ((isset ($ options ['persistent ' ])) && $ options ['persistent ' ]) {
15- $ connectFunc = 'odbc_pconnect ' ;
28+ $ conn = odbc_pconnect ($ database , $ user , $ password );
29+ } else {
30+ $ conn = odbc_connect ($ database , $ user , $ password );
31+ }
32+
33+ if (is_resource ($ conn )) {
34+ return $ conn ;
1635 }
1736 }
1837
19- // could be connect or pconnect
20- $ conn = $ connectFunc ( $ database , $ user , $ password );
21-
22- if (is_resource ($ conn )) {
23- return $ conn ;
24- } else {
2538 $ this ->setError ();
2639 return false ;
27- }
2840 }
29-
30- public function disconnect ( $ conn )
41+
42+ /**
43+ * @param $conn
44+ */
45+ public function disconnect ($ conn )
3146 {
32- if (is_resource ($ conn )) {
47+ if (is_resource ($ conn )) {
3348 odbc_close ($ conn );
3449 }
3550 }
36-
37- public function getErrorCode ()
38- {
39- return $ this ->last_errorcode ;
40- }
41-
42- // added
43- public function getErrorMsg ()
44- {
45- return $ this ->last_errormsg ;
46- }
47-
51+
52+ /**
53+ * set error code and message based on last odbc connection/prepare/execute error.
54+ *
55+ * @todo: consider using GET DIAGNOSTICS for even more message text:
56+ * http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzala%2Frzalafinder.htm
57+ *
58+ * @param null $conn
59+ */
4860 protected function setError ($ conn = null )
4961 {
50- // set error code and message based on last odbc connection/prepare/execute error.
51-
52- // @todo: consider using GET DIAGNOSTICS for even more message text:
53- // http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzala%2Frzalafinder.htm
54-
62+ // is conn resource provided, or do we get last error?
5563 if ($ conn ) {
56- // specific connection resource was provided
5764 $ this ->setErrorCode (odbc_error ($ conn ));
5865 $ this ->setErrorMsg (odbc_errormsg ($ conn ));
5966 } else {
60- // no specific statemtent. Get last error
6167 $ this ->setErrorCode (odbc_error ());
6268 $ this ->setErrorMsg (odbc_errormsg ());
6369 }
6470 }
65-
71+
72+ /**
73+ * @param $errorCode
74+ */
6675 protected function setErrorCode ($ errorCode )
6776 {
6877 $ this ->last_errorcode = $ errorCode ;
6978 }
70-
79+
80+ /**
81+ * @return string
82+ */
83+ public function getErrorCode ()
84+ {
85+ return $ this ->last_errorcode ;
86+ }
87+
88+ /**
89+ * @param $errorMsg
90+ */
7191 protected function setErrorMsg ($ errorMsg )
7292 {
7393 $ this ->last_errormsg = $ errorMsg ;
7494 }
75-
76- /* this function used for special stored procedure call only */
77- public function execXMLStoredProcedure ( $ conn , $ stmt , $ bindArray )
95+
96+ /**
97+ * @return string
98+ */
99+ public function getErrorMsg ()
78100 {
79- $ internalKey = $ bindArray ['internalKey ' ];
80- $ controlKey = $ bindArray ['controlKey ' ];
81- $ inputXml = $ bindArray ['inputXml ' ];
82- $ outputXml = $ bindArray ['outputXml ' ];
83- $ disconnect = $ bindArray ['disconnect ' ];
101+ return $ this ->last_errormsg ;
102+ }
84103
85- $ crsr = odbc_prepare ( $ conn , $ stmt );
104+ /**
105+ * this function used for special stored procedure call only
106+ *
107+ * @param $conn
108+ * @param $stmt
109+ * @param $bindArray
110+ * @return string
111+ */
112+ public function execXMLStoredProcedure ($ conn , $ stmt , $ bindArray )
113+ {
114+ $ crsr = odbc_prepare ($ conn , $ stmt );
86115
87- if ( !$ crsr ) {
116+ if ( !$ crsr ) {
88117 $ this ->setError ($ conn );
89118 return false ;
90119 }
91120
92- /* extension problem: sends an warning message into the php_log or to stdout
93- * about of number of result sets . ( switch on return code of SQLExecute()
94- * SQL_SUCCESS_WITH_INFO */
95- $ ret = @odbc_execute ( $ crsr , array ($ internalKey , $ controlKey , $ inputXml ));
96- if (!$ ret ) {
121+ // extension problem: sends warning message into the php_log or stdout
122+ // about number of result sets. (switch on return code of SQLExecute()
123+ // SQL_SUCCESS_WITH_INFO
124+ if (!@odbc_execute ($ crsr , array ($ bindArray ['internalKey ' ], $ bindArray ['controlKey ' ], $ bindArray ['inputXml ' ]))) {
97125 $ this ->setError ($ conn );
98-
99126 return "ODBC error code: " . $ this ->getErrorCode () . ' msg: ' . $ this ->getErrorMsg ();
100127 }
101128
102- //disconnect operation cause crush in fetch ,
103- //nothing appears as sql script.
129+ // disconnect operation cause crush in fetch, nothing appears as sql script.
104130 $ row ='' ;
105131 $ outputXML = '' ;
106- if (!$ disconnect ) {
107- while ( odbc_fetch_row ($ crsr )) {
132+ if (!$ bindArray [ ' disconnect ' ] ) {
133+ while ( odbc_fetch_row ($ crsr )) {
108134 $ tmp = odbc_result ($ crsr , 1 );
109- if ($ tmp ) {
110- /*because of some problem in odbc Blob transfering
111- * shoudl be executed some "clean" in returned data */
112- if (strstr ($ tmp , "</script> " )){
113- $ stopFetch = true ;
135+
136+ if ($ tmp ) {
137+ // because of ODBC problem blob transferring should execute some "clean" on returned data
138+ if (strstr ($ tmp , "</script> " )) {
114139 $ pos = strpos ($ tmp , "</script> " );
115- $ pos += strlen ("</script> " );
116- $ row .= substr ($ tmp ,0 , $ pos );
140+ $ pos += strlen ("</script> " ); // @todo why append this value?
141+ $ row .= substr ($ tmp , 0 , $ pos );
117142 break ;
118143 } else {
119- $ row .=$ tmp ;
144+ $ row .= $ tmp ;
120145 }
121146 }
122147 }
@@ -125,16 +150,21 @@ public function execXMLStoredProcedure( $conn, $stmt, $bindArray )
125150
126151 return $ outputXML ;
127152 }
128-
129- public function executeQuery ($ conn , $ stmt )
153+
154+ /**
155+ * @param $conn
156+ * @param $stmt
157+ * @return array
158+ */
159+ public function executeQuery ($ conn , $ stmt )
130160 {
131161 $ crsr = odbc_exec ($ conn , $ stmt );
132162
133- if (is_resource ($ crsr )) {
134- while ( odbc_fetch_row ( $ crsr )) {
163+ if (is_resource ($ crsr )) {
164+ while ( odbc_fetch_row ($ crsr )) {
135165 $ row = odbc_result ($ crsr , 1 );
136166
137- if (!$ row ) {
167+ if (!$ row ) {
138168 break ;
139169 }
140170
0 commit comments