Commit 92e3527137275636dd33eb412150dfa94caa90de

Authored by Conrad Vermeulen
1 parent 99b3d0d7

KTS-2358

"php5 migration"
Updated. Removed & from &new. Added statics. Also updated db error logging.

Committed By: Conrad Vermeulen
Reviewed By: Kevin Fourie

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7177 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 25 additions and 18 deletions
lib/database/dbutil.inc
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 * License Version 1.1.2 ("License"); You may not use this file except in 8 * License Version 1.1.2 ("License"); You may not use this file except in
9 * compliance with the License. You may obtain a copy of the License at 9 * compliance with the License. You may obtain a copy of the License at
10 * http://www.knowledgetree.com/KPL 10 * http://www.knowledgetree.com/KPL
11 - * 11 + *
12 * Software distributed under the License is distributed on an "AS IS" 12 * Software distributed under the License is distributed on an "AS IS"
13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
14 * See the License for the specific language governing rights and 14 * See the License for the specific language governing rights and
@@ -19,9 +19,9 @@ @@ -19,9 +19,9 @@
19 * (ii) the KnowledgeTree copyright notice 19 * (ii) the KnowledgeTree copyright notice
20 * in the same form as they appear in the distribution. See the License for 20 * in the same form as they appear in the distribution. See the License for
21 * requirements. 21 * requirements.
22 - * 22 + *
23 * The Original Code is: KnowledgeTree Open Source 23 * The Original Code is: KnowledgeTree Open Source
24 - * 24 + *
25 * The Initial Developer of the Original Code is The Jam Warehouse Software 25 * The Initial Developer of the Original Code is The Jam Warehouse Software
26 * (Pty) Ltd, trading as KnowledgeTree. 26 * (Pty) Ltd, trading as KnowledgeTree.
27 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright 27 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
@@ -33,7 +33,7 @@ @@ -33,7 +33,7 @@
33 require_once ('PEAR.php'); 33 require_once ('PEAR.php');
34 34
35 class DBUtil { 35 class DBUtil {
36 - function &getDB($db = null) { 36 + static function &getDB($db = null) {
37 global $default; 37 global $default;
38 if (is_null($db)) { 38 if (is_null($db)) {
39 $db =& $default->_db; 39 $db =& $default->_db;
@@ -47,7 +47,7 @@ class DBUtil { @@ -47,7 +47,7 @@ class DBUtil {
47 return $db; 47 return $db;
48 } 48 }
49 49
50 - function &runQuery($query, $db = null) { 50 + static function &runQuery($query, $db = null) {
51 global $default; 51 global $default;
52 $aParams = null; 52 $aParams = null;
53 53
@@ -55,7 +55,7 @@ class DBUtil { @@ -55,7 +55,7 @@ class DBUtil {
55 55
56 if (is_array($query)) { 56 if (is_array($query)) {
57 $sQuery = $query[0]; 57 $sQuery = $query[0];
58 - $aParams = $query[1]; 58 + $aParams = (count($query) > 1)?$query[1]:array();
59 } else { 59 } else {
60 $sQuery = $query; 60 $sQuery = $query;
61 } 61 }
@@ -63,13 +63,17 @@ class DBUtil { @@ -63,13 +63,17 @@ class DBUtil {
63 if ($default->queryLog) { 63 if ($default->queryLog) {
64 $default->queryLog->debug('Query: ' . DBUtil::lastQuery($db)); 64 $default->queryLog->debug('Query: ' . DBUtil::lastQuery($db));
65 } 65 }
  66 + if (PEAR::isError($res))
  67 + {
  68 + DBUtil::logQueryError(DBUtil::lastQuery($db), $res);
  69 + }
66 return $res; 70 return $res;
67 } 71 }
68 72
69 - function getOneResult($query, $db = null) { 73 + static function getOneResult($query, $db = null) {
70 $result = DBUtil::runQuery($query, $db); 74 $result = DBUtil::runQuery($query, $db);
71 if (PEAR::isError($result)) { 75 if (PEAR::isError($result)) {
72 - DBUtil::logQueryError($query, $result); 76 + // logging by runQuery
73 return $result; 77 return $result;
74 } 78 }
75 $aRow = $result->fetchRow(); 79 $aRow = $result->fetchRow();
@@ -77,18 +81,19 @@ class DBUtil { @@ -77,18 +81,19 @@ class DBUtil {
77 return $aRow; 81 return $aRow;
78 } 82 }
79 83
80 - function getOneResultKey($query, $key, $db = null) { 84 + static function getOneResultKey($query, $key, $db = null) {
81 $aRow = DBUtil::getOneResult($query, $db); 85 $aRow = DBUtil::getOneResult($query, $db);
82 if (PEAR::isError($aRow)) { 86 if (PEAR::isError($aRow)) {
  87 + // logging by runQuery
83 return $aRow; 88 return $aRow;
84 } 89 }
85 return $aRow[$key]; 90 return $aRow[$key];
86 } 91 }
87 92
88 - function getResultArray($query, $db = null) { 93 + static function getResultArray($query, $db = null) {
89 $result = DBUtil::runQuery($query, $db); 94 $result = DBUtil::runQuery($query, $db);
90 if (PEAR::isError($result)) { 95 if (PEAR::isError($result)) {
91 - DBUtil::logQueryError($query, $result); 96 + // logging by runQuery
92 return $result; 97 return $result;
93 } 98 }
94 99
@@ -100,10 +105,10 @@ class DBUtil { @@ -100,10 +105,10 @@ class DBUtil {
100 return $aReturn; 105 return $aReturn;
101 } 106 }
102 107
103 - function getResultArrayKey($query, $key, $db = null) { 108 + static function getResultArrayKey($query, $key, $db = null) {
104 $result = DBUtil::runQuery($query, $db); 109 $result = DBUtil::runQuery($query, $db);
105 if (PEAR::isError($result)) { 110 if (PEAR::isError($result)) {
106 - DBUtil::logQueryError($query, $result); 111 + // logging by runQuery
107 return $result; 112 return $result;
108 } 113 }
109 114
@@ -117,8 +122,8 @@ class DBUtil { @@ -117,8 +122,8 @@ class DBUtil {
117 122
118 function logQueryError($query, $result) { 123 function logQueryError($query, $result) {
119 global $default; 124 global $default;
120 - $default->log->error($result->toString());  
121 - $default->queryLog->error($result->toString()); 125 + $default->log->error('Problem Query: ' . $query);
  126 + $default->log->error('Problem: ' . $result->getMessage());
122 } 127 }
123 128
124 function nextId($seqname, $ondemand = false, $db = null) { 129 function nextId($seqname, $ondemand = false, $db = null) {
@@ -161,6 +166,7 @@ class DBUtil { @@ -161,6 +166,7 @@ class DBUtil {
161 return $aFieldValues['id']; 166 return $aFieldValues['id'];
162 } 167 }
163 if (PEAR::isError($res)) { 168 if (PEAR::isError($res)) {
  169 + DBUtil::logQueryError(DBUtil::lastQuery($db), $res);
164 return $res; 170 return $res;
165 } 171 }
166 return PEAR::raiseError(_kt('Unknown return value for autoInsert')); 172 return PEAR::raiseError(_kt('Unknown return value for autoInsert'));
@@ -176,7 +182,8 @@ class DBUtil { @@ -176,7 +182,8 @@ class DBUtil {
176 return $res; 182 return $res;
177 } 183 }
178 if (PEAR::isError($res)) { 184 if (PEAR::isError($res)) {
179 - return $res; 185 + DBUtil::logQueryError(DBUtil::lastQuery($db), $res);
  186 + return $res;
180 } 187 }
181 return PEAR::raiseError(_kt('Unknown return value for autoUpdate')); 188 return PEAR::raiseError(_kt('Unknown return value for autoUpdate'));
182 } 189 }
@@ -206,7 +213,7 @@ class DBUtil { @@ -206,7 +213,7 @@ class DBUtil {
206 return PEAR::raiseError(_kt('Unknown return value for whereUpdate')); 213 return PEAR::raiseError(_kt('Unknown return value for whereUpdate'));
207 } 214 }
208 215
209 - function &lastQuery($db = null) { 216 + static function &lastQuery($db = null) {
210 $db =& DBUtil::getDB(); 217 $db =& DBUtil::getDB();
211 return $db->last_query; 218 return $db->last_query;
212 } 219 }
@@ -219,7 +226,7 @@ class DBUtil { @@ -219,7 +226,7 @@ class DBUtil {
219 $aParams = array($iId); 226 $aParams = array($iId);
220 return DBUtil::runQuery(array($sQuery, $aParams), $db); 227 return DBUtil::runQuery(array($sQuery, $aParams), $db);
221 } 228 }
222 - 229 +
223 function deReference($sTable, $iId, $db = null) { 230 function deReference($sTable, $iId, $db = null) {
224 global $default; 231 global $default;
225 // $default->log->debug('AutoDelete called for table ' . $sTable . ' with id ' . $iId); 232 // $default->log->debug('AutoDelete called for table ' . $sTable . ' with id ' . $iId);