PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/db/mysql/query_factory.php

http://phreedom.googlecode.com/
PHP | 296 lines | 249 code | 23 blank | 24 comment | 57 complexity | 42f1bfa492769f386ab602df24a1b93b MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. // +-----------------------------------------------------------------+
  3. // | PhreeBooks Open Source ERP |
  4. // +-----------------------------------------------------------------+
  5. // | Copyright (c) 2008, 2009, 2010, 2011, 2012 PhreeSoft, LLC |
  6. // | http://www.PhreeSoft.com |
  7. // +-----------------------------------------------------------------+
  8. // | This program is free software: you can redistribute it and/or |
  9. // | modify it under the terms of the GNU General Public License as |
  10. // | published by the Free Software Foundation, either version 3 of |
  11. // | the License, or any later version. |
  12. // | |
  13. // | This program is distributed in the hope that it will be useful, |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  16. // | GNU General Public License for more details. |
  17. // +-----------------------------------------------------------------+
  18. // Path: /includes/db/mysql/query_factory.php
  19. //
  20. class queryFactory {
  21. function __construct() {
  22. $this->count_queries = 0;
  23. $this->total_query_time = 0;
  24. }
  25. function connect($zf_host, $zf_user, $zf_password, $zf_database) {
  26. $this->database = $zf_database;
  27. // pconnect disabled
  28. $this->link = @mysql_connect($zf_host, $zf_user, $zf_password, true);
  29. if ($this->link) {
  30. if (@mysql_select_db($zf_database, $this->link)) {
  31. $this->db_connected = true;
  32. // set the character set
  33. mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $this->link);
  34. // mysql_query("SET NAMES utf8", $this->link);
  35. // mysql_query("SET CHARACTER SET utf8", $this->link);
  36. // mysql_set_charset('utf8', $this->link);
  37. return true;
  38. } else {
  39. $this->set_error(mysql_errno(), mysql_error(), false);
  40. }
  41. } else {
  42. $this->set_error(mysql_errno(), mysql_error(), false);
  43. }
  44. return false;
  45. }
  46. function selectdb($zf_database) {
  47. @mysql_select_db($zf_database, $this->link);
  48. }
  49. function transStart() {
  50. $this->Execute("START TRANSACTION");
  51. }
  52. function transCommit() {
  53. $this->Execute("COMMIT");
  54. }
  55. function transRollback() {
  56. $this->Execute("ROLLBACK");
  57. }
  58. function prepare_input($zp_string) {
  59. if (function_exists('mysql_real_escape_string')) {
  60. return mysql_real_escape_string($zp_string);
  61. } elseif (function_exists('mysql_escape_string')) {
  62. return mysql_escape_string($zp_string);
  63. } else {
  64. return addslashes($zp_string);
  65. }
  66. }
  67. function close() {
  68. @mysql_close($this->link);
  69. }
  70. function set_error($zp_err_num, $zp_err_text, $zp_fatal = true) {
  71. $this->error_number = $zp_err_num;
  72. $this->error_text = $zp_err_text;
  73. if ($zp_fatal && $zp_err_num != 1141) {
  74. $this->show_error();
  75. die();
  76. }
  77. }
  78. function show_error() {
  79. echo $this->error_number . ' ' . $this->error_text;
  80. }
  81. function Execute($zf_sql, $zf_limit = false, $zf_cache = false, $zf_cachetime = 0) {
  82. global $zc_cache, $messageStack;
  83. if ($zf_limit) {
  84. $zf_sql = $zf_sql . ' LIMIT ' . $zf_limit;
  85. }
  86. if ( $zf_cache AND $zc_cache->sql_cache_exists($zf_sql) AND !$zc_cache->sql_cache_is_expired($zf_sql, $zf_cachetime) ) {
  87. $obj = new queryFactoryResult;
  88. $obj->cursor = 0;
  89. $obj->is_cached = true;
  90. $obj->sql_query = $zf_sql;
  91. $zp_result_array = $zc_cache->sql_cache_read($zf_sql);
  92. $obj->result = $zp_result_array;
  93. if (sizeof($zp_result_array) > 0 ) {
  94. $obj->EOF = false;
  95. while (list($key, $value) = each($zp_result_array[0])) {
  96. $obj->fields[$key] = $value;
  97. }
  98. return $obj;
  99. } else {
  100. $obj->EOF = true;
  101. }
  102. } elseif ($zf_cache) {
  103. $zc_cache->sql_cache_expire_now($zf_sql);
  104. $time_start = explode(' ', microtime());
  105. $obj = new queryFactoryResult;
  106. $obj->sql_query = $zf_sql;
  107. if (!$this->db_connected) $this->set_error('0', DB_ERROR_NOT_CONNECTED);
  108. $zp_db_resource = @mysql_query($zf_sql, $this->link);
  109. if (!$zp_db_resource) $this->set_error(@mysql_errno(),@mysql_error());
  110. $obj->resource = $zp_db_resource;
  111. $obj->cursor = 0;
  112. $obj->is_cached = true;
  113. if ($obj->RecordCount() > 0) {
  114. $obj->EOF = false;
  115. $zp_ii = 0;
  116. while (!$obj->EOF) {
  117. $zp_result_array = @mysql_fetch_array($zp_db_resource);
  118. if ($zp_result_array) {
  119. while (list($key, $value) = each($zp_result_array)) {
  120. if (!preg_match('/^[0-9]/', $key)) {
  121. $obj->result[$zp_ii][$key] = $value;
  122. }
  123. }
  124. } else {
  125. $obj->Limit = $zp_ii;
  126. $obj->EOF = true;
  127. }
  128. $zp_ii++;
  129. }
  130. while (list($key, $value) = each($obj->result[$obj->cursor])) {
  131. if (!preg_match('/^[0-9]/', $key)) {
  132. $obj->fields[$key] = $value;
  133. }
  134. }
  135. $obj->EOF = false;
  136. } else {
  137. $obj->EOF = true;
  138. }
  139. $zc_cache->sql_cache_store($zf_sql, $obj->result);
  140. $time_end = explode (' ', microtime());
  141. $query_time = $time_end[1]+$time_end[0]-$time_start[1]-$time_start[0];
  142. $this->total_query_time += $query_time;
  143. $this->count_queries++;
  144. return($obj);
  145. } else {
  146. $time_start = explode(' ', microtime());
  147. $obj = new queryFactoryResult;
  148. if (!$this->db_connected) $this->set_error('0', DB_ERROR_NOT_CONNECTED);
  149. $zp_db_resource = @mysql_query($zf_sql, $this->link);
  150. if (!$zp_db_resource) {
  151. if (method_exists($messageStack, 'debug')) {
  152. $messageStack->debug("\n\nThe failing sql was: " . $zf_sql);
  153. $messageStack->debug("\n\nmySQL returned: " . @mysql_errno($this->link) . ' ' . @mysql_error($this->link));
  154. if (defined('FILENAME_DEFAULT')) {
  155. $messageStack->write_debug();
  156. $messageStack->add_session('The last transaction had a SQL database error.', 'error');
  157. gen_redirect(html_href_link(FILENAME_DEFAULT, 'cat=phreedom&page=main&amp;action=crash', 'SSL'));
  158. } else {
  159. echo str_replace("\n", '<br />', $messageStack->debug_info);
  160. die;
  161. }
  162. }
  163. }
  164. $obj->resource = $zp_db_resource;
  165. $obj->cursor = 0;
  166. if ($obj->RecordCount() > 0) {
  167. $obj->EOF = false;
  168. $zp_result_array = @mysql_fetch_array($zp_db_resource);
  169. if ($zp_result_array) {
  170. while (list($key, $value) = each($zp_result_array)) {
  171. if (!preg_match('/^[0-9]/', $key)) {
  172. $obj->fields[$key] = $value;
  173. }
  174. }
  175. $obj->EOF = false;
  176. } else {
  177. $obj->EOF = true;
  178. }
  179. } else {
  180. $obj->EOF = true;
  181. }
  182. $time_end = explode (' ', microtime());
  183. $query_time = $time_end[1] + $time_end[0] - $time_start[1] - $time_start[0];
  184. $this->total_query_time += $query_time;
  185. $this->count_queries++;
  186. //$messageStack->add('query execution time = ' . $query_time . ' and sql = ' . $zf_sql, 'caution');
  187. return($obj);
  188. }
  189. }
  190. function Execute_return_error($zf_sql) {
  191. $time_start = explode(' ', microtime());
  192. $obj = new queryFactoryResult;
  193. if (!$this->db_connected) $this->set_error('0', DB_ERROR_NOT_CONNECTED);
  194. $zp_db_resource = @mysql_query($zf_sql, $this->link);
  195. if (!$zp_db_resource) {
  196. $this->set_error(@mysql_errno($this->link), @mysql_error($this->link), false);
  197. } else {
  198. $this->set_error(0, '', false); // clear the error
  199. }
  200. $obj->resource = $zp_db_resource;
  201. $obj->cursor = 0;
  202. if ($obj->RecordCount() > 0) {
  203. $obj->EOF = false;
  204. $zp_result_array = @mysql_fetch_array($zp_db_resource);
  205. if ($zp_result_array) {
  206. while (list($key, $value) = each($zp_result_array)) {
  207. if (!preg_match('/^[0-9]/', $key)) $obj->fields[$key] = $value;
  208. }
  209. $obj->EOF = false;
  210. } else {
  211. $obj->EOF = true;
  212. }
  213. } else {
  214. $obj->EOF = true;
  215. }
  216. $time_end = explode (' ', microtime());
  217. $query_time = $time_end[1] + $time_end[0] - $time_start[1] - $time_start[0];
  218. $this->total_query_time += $query_time;
  219. $this->count_queries++;
  220. return($obj);
  221. }
  222. function insert_ID() {
  223. return @mysql_insert_id($this->link);
  224. }
  225. }
  226. class queryFactoryResult {
  227. function __construct() {
  228. $this->is_cached = false;
  229. }
  230. function MoveNext() {
  231. global $zc_cache;
  232. $this->cursor++;
  233. if ($this->is_cached) {
  234. if ($this->cursor >= sizeof($this->result)) {
  235. $this->EOF = true;
  236. } else {
  237. while(list($key, $value) = each($this->result[$this->cursor])) {
  238. $this->fields[$key] = $value;
  239. }
  240. }
  241. } else {
  242. $zp_result_array = @mysql_fetch_array($this->resource);
  243. if (!$zp_result_array) {
  244. $this->EOF = true;
  245. } else {
  246. while (list($key, $value) = each($zp_result_array)) {
  247. if (!preg_match('/^[0-9]/', $key)) $this->fields[$key] = $value;
  248. }
  249. }
  250. }
  251. }
  252. function RecordCount() { // use for SELECT queries
  253. return @mysql_num_rows($this->resource);
  254. }
  255. function AffectedRows() { // use for INSERT, UPDATE or DELETE queries
  256. return @mysql_affected_rows();
  257. }
  258. function Move($zp_row) {
  259. global $db;
  260. if (@mysql_data_seek($this->resource, $zp_row)) {
  261. $zp_result_array = @mysql_fetch_array($this->resource);
  262. while (list($key, $value) = each($zp_result_array)) {
  263. $this->fields[$key] = $value;
  264. }
  265. @mysql_data_seek($this->resource, $zp_row);
  266. $this->EOF = false;
  267. return;
  268. } else {
  269. $this->EOF = true;
  270. }
  271. }
  272. }
  273. ?>