BxDolUpgradeDb.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?
  2. /***************************************************************************
  3. * Dolphin Smart Community Builder
  4. * -------------------
  5. * begin : Mon Mar 23 2006
  6. * copyright : (C) 2007 BoonEx Group
  7. * website : http://www.boonex.com
  8. * This file is part of Dolphin - Smart Community Builder
  9. *
  10. * Dolphin is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the
  13. * License, or any later version.
  14. *
  15. * Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  16. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU General Public License for more details.
  18. * You should have received a copy of the GNU General Public License along with Dolphin,
  19. * see license.txt file; if not, write to marketing@boonex.com
  20. ***************************************************************************/
  21. define( 'BX_UPGRADE_DB_FULL_VISUAL_PROCESSING', true );
  22. define( 'BX_UPGRADE_DB_FULL_DEBUG_MODE', true );
  23. class BxDolUpgradeDb
  24. {
  25. var $error_checking = true;
  26. var $host, $port, $socket, $dbname, $user, $password, $link;
  27. var $current_res, $current_arr_type;
  28. var $oParams;
  29. /*
  30. *set database parameters and connect to it
  31. */
  32. function BxDolUpgradeDb(){
  33. $this->host = DATABASE_HOST;
  34. $this->port = DATABASE_PORT;
  35. $this->socket = DATABASE_SOCK;
  36. $this->dbname = DATABASE_NAME;
  37. $this->user = DATABASE_USER;
  38. $this->password = DATABASE_PASS;
  39. $this->current_arr_type = MYSQL_ASSOC;
  40. $this->connect();
  41. }
  42. /**
  43. * connect to database with appointed parameters
  44. */
  45. function connect()
  46. {
  47. $full_host = $this->host;
  48. $full_host .= $this->port ? ':'.$this->port : '';
  49. $full_host .= $this->socket ? ':'.$this->socket : '';
  50. $this->link = @mysql_pconnect($full_host, $this->user, $this->password);
  51. if (!$this->link)
  52. $this->error('Database connect failed', true);
  53. if (!$this->select_db())
  54. $this->error('Database select failed', true);
  55. $this->res("SET NAMES 'utf8'");
  56. $this->res("SET sql_mode = ''");
  57. }
  58. function select_db()
  59. {
  60. return @mysql_select_db($this->dbname, $this->link) or $this->error('Cannot complete query (select_db)');
  61. }
  62. /**
  63. * close mysql connection
  64. */
  65. function close()
  66. {
  67. mysql_close($this->link);
  68. }
  69. /**
  70. * execute sql query and return one row result
  71. */
  72. function getRow($query, $arr_type = MYSQL_ASSOC)
  73. {
  74. if(!$query)
  75. return array();
  76. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  77. $arr_type = MYSQL_ASSOC;
  78. $res = $this->res ($query);
  79. $arr_res = array();
  80. if($res && mysql_num_rows($res))
  81. {
  82. $arr_res = mysql_fetch_array($res, $arr_type);
  83. mysql_free_result($res);
  84. }
  85. return $arr_res;
  86. }
  87. /**
  88. * execute sql query and return a column as result
  89. */
  90. function getColumn($sQuery) {
  91. if(!$sQuery)
  92. return array();
  93. $rResult = $this->res($sQuery);
  94. $aResult = array();
  95. if($rResult) {
  96. while($aRow = mysql_fetch_array($rResult, MYSQL_NUM))
  97. $aResult[] = $aRow[0];
  98. mysql_free_result($rResult);
  99. }
  100. return $aResult;
  101. }
  102. /**
  103. * execute sql query and return one value result
  104. */
  105. function getOne($query, $index = 0)
  106. {
  107. if(!$query)
  108. return false;
  109. $res = $this->res ($query);
  110. $arr_res = array();
  111. if($res && mysql_num_rows($res))
  112. $arr_res = mysql_fetch_array($res);
  113. if(count($arr_res))
  114. return $arr_res[$index];
  115. else
  116. return false;
  117. }
  118. /**
  119. * execute sql query and return the first row of result
  120. * and keep $array type and poiter to all data
  121. */
  122. function getFirstRow($query, $arr_type = MYSQL_ASSOC)
  123. {
  124. if(!$query)
  125. return array();
  126. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM)
  127. $this->current_arr_type = MYSQL_ASSOC;
  128. else
  129. $this->current_arr_type = $arr_type;
  130. $this->current_res = $this->res ($query);
  131. $arr_res = array();
  132. if($this->current_res && mysql_num_rows($this->current_res))
  133. $arr_res = mysql_fetch_array($this->current_res, $this->current_arr_type);
  134. return $arr_res;
  135. }
  136. /**
  137. * return next row of pointed last getFirstRow calling data
  138. */
  139. function getNextRow()
  140. {
  141. $arr_res = mysql_fetch_array($this->current_res, $this->current_arr_type);
  142. if($arr_res)
  143. return $arr_res;
  144. else
  145. {
  146. mysql_free_result($this->current_res);
  147. $this->current_arr_type = MYSQL_ASSOC;
  148. return array();
  149. }
  150. }
  151. /**
  152. * return number of affected rows in current mysql result
  153. */
  154. function getNumRows($res = false)
  155. {
  156. if ($res)
  157. return (int)@mysql_num_rows($res);
  158. elseif (!$this->current_res)
  159. return (int)@mysql_num_rows($this->current_res);
  160. else
  161. return 0;
  162. }
  163. /**
  164. * execute any query return number of rows affected/false
  165. */
  166. function getAffectedRows()
  167. {
  168. return mysql_affected_rows($this->link);
  169. }
  170. /**
  171. * execute any query return number of rows affected/false
  172. */
  173. function query($query)
  174. {
  175. $res = $this->res($query);
  176. if($res)
  177. return mysql_affected_rows($this->link);
  178. return false;
  179. }
  180. /**
  181. * execute any query
  182. */
  183. function res($query, $error_checking = true)
  184. {
  185. if(!$query)
  186. return false;
  187. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginQuery($query);
  188. $res = mysql_query($query, $this->link);
  189. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endQuery($res);
  190. if (!$res && $error_checking)
  191. $this->error('Database query error', false, $query);
  192. return $res;
  193. }
  194. /**
  195. * execute sql query and return table of records as result
  196. */
  197. function getAll($query, $arr_type = MYSQL_ASSOC)
  198. {
  199. if(!$query)
  200. return array();
  201. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  202. $arr_type = MYSQL_ASSOC;
  203. $res = $this->res ($query);
  204. $arr_res = array();
  205. if($res)
  206. {
  207. while($row = mysql_fetch_array($res, $arr_type))
  208. $arr_res[] = $row;
  209. mysql_free_result($res);
  210. }
  211. return $arr_res;
  212. }
  213. /**
  214. * execute sql query and return table of records as result
  215. */
  216. function fillArray($res, $arr_type = MYSQL_ASSOC)
  217. {
  218. if(!$res)
  219. return array();
  220. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  221. $arr_type = MYSQL_ASSOC;
  222. $arr_res = array();
  223. while($row = mysql_fetch_array($res, $arr_type))
  224. $arr_res[] = $row;
  225. mysql_free_result($res);
  226. return $arr_res;
  227. }
  228. /**
  229. * execute sql query and return table of records as result
  230. */
  231. function getAllWithKey($query, $sFieldKey)
  232. {
  233. if(!$query)
  234. return array();
  235. $res = $this->res ($query);
  236. $arr_res = array();
  237. if($res)
  238. {
  239. while($row = mysql_fetch_array($res, MYSQL_ASSOC))
  240. {
  241. $arr_res[$row[$sFieldKey]] = $row;
  242. }
  243. mysql_free_result($res);
  244. }
  245. return $arr_res;
  246. }
  247. /**
  248. * execute sql query and return table of records as result
  249. */
  250. function getPairs($query, $sFieldKey, $sFieldValue, $arr_type = MYSQL_ASSOC)
  251. {
  252. if(!$query)
  253. return array();
  254. $res = $this->res ($query);
  255. $arr_res = array();
  256. if($res)
  257. {
  258. while($row = mysql_fetch_array($res, MYSQL_ASSOC))
  259. {
  260. $arr_res[$row[$sFieldKey]] = $row[$sFieldValue];
  261. }
  262. mysql_free_result($res);
  263. }
  264. return $arr_res;
  265. }
  266. function lastId()
  267. {
  268. return mysql_insert_id($this->link);
  269. }
  270. function error($text, $isForceErrorChecking = false, $sSqlQuery = '')
  271. {
  272. if ($this->error_checking || $isForceErrorChecking)
  273. $this->genMySQLErr ($text, $sSqlQuery);
  274. else
  275. $this->log($text.': '.mysql_error($this->link));
  276. }
  277. function listTables() {
  278. return mysql_list_tables($GLOBALS['db']['db'], $this->link);
  279. //return mysql_list_tables($GLOBALS['db']['db'], $this->link) or $this->error('Database get encoding error');
  280. }
  281. function getEncoding() {
  282. return mysql_client_encoding($this->link) or $this->error('Database get encoding error');
  283. }
  284. function genMySQLErr( $out, $query ='' ) {
  285. $aBackTrace = debug_backtrace();
  286. unset( $aBackTrace[0] );
  287. if( $query )
  288. {
  289. //try help to find error
  290. $aFoundError = array();
  291. foreach( $aBackTrace as $aCall )
  292. {
  293. foreach( $aCall['args'] as $argNum => $argVal )
  294. {
  295. if( is_string($argVal) and strcmp( $argVal, $query ) == 0 )
  296. {
  297. $aFoundError['file'] = $aCall['file'];
  298. $aFoundError['line'] = $aCall['line'];
  299. $aFoundError['function'] = $aCall['function'];
  300. $aFoundError['arg'] = $argNum;
  301. }
  302. }
  303. }
  304. if( $aFoundError )
  305. {
  306. $sFoundError = <<<EOJ
  307. Found error in the file '<b>{$aFoundError['file']}</b>' at line <b>{$aFoundError['line']}</b>.<br />
  308. Called '<b>{$aFoundError['function']}</b>' function with erroneous argument #<b>{$aFoundError['arg']}</b>.<br /><br />
  309. EOJ;
  310. }
  311. }
  312. if( BX_UPGRADE_DB_FULL_VISUAL_PROCESSING )
  313. {
  314. ?>
  315. <div style="border:2px solid red;padding:4px;width:600px;margin:0px auto;">
  316. <div style="text-align:center;background-color:red;color:white;font-weight:bold;">Error</div>
  317. <div style="text-align:center;"><?=$out?></div>
  318. <?
  319. if( BX_UPGRADE_DB_FULL_DEBUG_MODE )
  320. {
  321. if( strlen( $query ) )
  322. echo "<div><b>Query:</b><br />{$query}</div>";
  323. echo '<div><b>Mysql error:</b><br />'.mysql_error($this->link).'</div>';
  324. echo '<div style="overflow:scroll;height:300px;border:1px solid gray;">';
  325. echo $sFoundError;
  326. echo "<b>Debug backtrace:</b><br />";
  327. echoDbg( $aBackTrace );
  328. echo "<b>Called script:</b> {$_SERVER['PHP_SELF']}<br />";
  329. echo "<b>Request parameters:</b><br />";
  330. echoDbg( $_REQUEST );
  331. echo '</div>';
  332. }
  333. ?>
  334. </div>
  335. <?
  336. }
  337. else
  338. echo $out;
  339. exit;
  340. }
  341. function setErrorChecking ($b) {
  342. $this->error_checking = $b;
  343. }
  344. function escape ($s) {
  345. return mysql_real_escape_string($s);
  346. }
  347. function executeSQL($sPath, $aReplace = array (), $isBreakOnError = true) {
  348. if(!file_exists($sPath) || !($rHandler = fopen($sPath, "r")))
  349. return array ('query' => "fopen($sPath, 'r')", 'error' => 'file not found or permission denied');
  350. $sQuery = "";
  351. $sDelimiter = ';';
  352. $aResult = array();
  353. while(!feof($rHandler)) {
  354. $sStr = trim(fgets($rHandler));
  355. if(empty($sStr) || $sStr[0] == "" || $sStr[0] == "#" || ($sStr[0] == "-" && $sStr[1] == "-"))
  356. continue;
  357. //--- Change delimiter ---//
  358. if(strpos($sStr, "DELIMITER //") !== false || strpos($sStr, "DELIMITER ;") !== false) {
  359. $sDelimiter = trim(str_replace('DELIMITER', '', $sStr));
  360. continue;
  361. }
  362. $sQuery .= $sStr;
  363. //--- Check for multiline query ---//
  364. if(substr($sStr, -strlen($sDelimiter)) != $sDelimiter)
  365. continue;
  366. //--- Execute query ---//
  367. if ($aReplace)
  368. $sQuery = str_replace($aReplace['from'], $aReplace['to'], $sQuery);
  369. if($sDelimiter != ';')
  370. $sQuery = str_replace($sDelimiter, "", $sQuery);
  371. $rResult = $this->res(trim($sQuery), false);
  372. if(!$rResult) {
  373. $aResult[] = array('query' => $sQuery, 'error' => mysql_error($this->link));
  374. if ($isBreakOnError)
  375. break;
  376. }
  377. $sQuery = "";
  378. }
  379. fclose($rHandler);
  380. return empty($aResult) ? true : $aResult;
  381. }
  382. }
  383. ?>