PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/database_connection_class.php

https://github.com/sauger/forbes_old
PHP | 344 lines | 304 code | 32 blank | 8 comment | 46 complexity | 498284e913f0a80c0b16521545e6d2e3 MD5 | raw file
  1. <?php
  2. include_once dirname(__FILE__) . '/database_row_item_class.php';
  3. class database_connection_class
  4. {
  5. var $_db=NULL;//the link resource
  6. var $_qresult = NULL;//the query result
  7. var $_aresult = array();
  8. var $servername='localhost';
  9. var $databasename = 'mysql';
  10. var $username = 'root';
  11. var $password = 'xunao';
  12. var $code = 'utf8';
  13. var $connected = false;
  14. public $echo_sql = false;
  15. public $record_count = 0;
  16. private $data_set = array();
  17. private $data_set_pointer = 0;
  18. public function connect()
  19. {
  20. //$args = func_get_args();
  21. $args_num = func_num_args();
  22. if ($args_num == 1 && is_array(func_get_arg(0))){
  23. //the parameter is a hash array
  24. $params = func_get_arg(0);
  25. foreach ($params as $k => $v){
  26. if (isset($this->$k)) {
  27. $this->$k = $v;
  28. }
  29. }
  30. }elseif ($args_num == 0){
  31. //use default values
  32. }elseif ($args_num >= 1) {
  33. //more than one parameter.the parameters should be pass in order.
  34. $this->servername = func_get_arg(0);
  35. if($args_num >=2){
  36. $this->databasename = func_get_arg(1);
  37. }
  38. if($args_num >=3){
  39. $this->username = func_get_arg(2);
  40. }
  41. if($args_num >=4){
  42. $this->password = func_get_arg(3);
  43. }
  44. if($args_num >=5){
  45. $this->code = func_get_arg(4);
  46. }
  47. }
  48. $this->connected = false;
  49. $resutl = $this->_connect();
  50. if ($resutl !== false) {
  51. $this->connected = true;
  52. }
  53. return $resutl;
  54. }
  55. function paginate($sql, $per_page=10,$page_var='page') {
  56. $page_count_var = $page_var ."_page_count";
  57. $record_count_var = $page_var ."_record_count";
  58. global $$page_count_var;
  59. global $$record_count_var;
  60. $page = isset($_REQUEST[$page_var]) ? $_REQUEST[$page_var] : 1;
  61. $select = substr($sql,0,6);
  62. if(strtoupper($select) != 'SELECT'){
  63. $this->_debug_info('sql in function painate must be started with SELECT; sql=' .$sql);
  64. return false;
  65. }
  66. $sql = substr_replace($sql," SQL_CALC_FOUND_ROWS ",6, 0) ." limit " .($per_page * ($page - 1)) . "," .$per_page;
  67. $ret = $this->query($sql);
  68. if($ret === false){
  69. return false;
  70. }
  71. $ret = mysql_query('select FOUND_ROWS()');
  72. mysql_data_seek($ret,0);
  73. $ret = mysql_fetch_array($ret);
  74. $$page_count_var = ceil($ret[0] / $per_page);
  75. $$record_count_var = $ret[0];
  76. return $this->data_set;
  77. }
  78. public function close(){
  79. mysql_close();
  80. $this->reset_vars();
  81. $this->connected = false;
  82. }
  83. public function &query($sql)
  84. {
  85. if($this->echo_sql) echo $sql;
  86. $this->reset_vars();
  87. if ($this->connected === false) {
  88. $this->_debug_info('database connection has not been established!');
  89. return false;
  90. }
  91. global $g_db_log_file;
  92. if($g_db_log_file){
  93. $f_start = get_microtime();
  94. }
  95. $this->_qresult = mysql_query($sql, $this->_db);
  96. if($g_db_log_file){
  97. $f_length = get_microtime() - $f_start;
  98. $str = chr(13).chr(10). now() ." ".$_SERVER['SCRIPT_NAME'] ." execute sql: ({$f_length}ms)" .$sql;
  99. write_to_file($g_db_log_file,$str);
  100. }
  101. if ($this->_qresult===FALSE)
  102. {
  103. $this->_debug_info('fail to query db!' . $this->get_error() .";query string = " .$sql);
  104. write_log('fail to execute sql!' . $this->get_error() .";query string = " .$sql);
  105. return FALSE;
  106. }
  107. else
  108. {
  109. //get the recrod count
  110. $this->record_count = mysql_num_rows($this->_qresult);
  111. if($this->_move_first() === false) return $data_set;
  112. do{
  113. $item = new database_row_item_class($this);
  114. $item->load_from_dataset();
  115. $this->data_set[] = $item;
  116. }while($this->_move_next());
  117. return $this->data_set;
  118. }
  119. }
  120. function execute($sqlstr){
  121. if($this->echo_sql) echo $sqlstr;
  122. if ($this->connected === false) {
  123. $this->_debug_info('database connection has not been established!');
  124. return false;
  125. }
  126. global $g_db_log_file;
  127. if($g_db_log_file){
  128. $f_start = get_microtime();
  129. }
  130. $this->_qresult = mysql_query($sqlstr, $this->_db);
  131. if($g_db_log_file){
  132. $f_length = get_microtime() - $f_start;
  133. $str = chr(13).chr(10). now()." ".$_SERVER['SCRIPT_NAME'] ." execute sql: ({$f_length}ms)" .$sqlstr;
  134. write_to_file($g_db_log_file,$str);
  135. }
  136. if ($this->_qresult===FALSE)
  137. {
  138. $this->_debug_info('fail to execute sql!' . $this->get_error() .";query string = " .$sqlstr);
  139. write_log('fail to execute sql!' . $this->get_error() .";query string = " .$sqlstr);
  140. return FALSE;
  141. }
  142. else
  143. {
  144. return true;
  145. }
  146. }
  147. function insert($table,$f,$v=null){
  148. if(is_null($v) && is_array($f)){
  149. foreach ($f as $field => $value) {
  150. $fields[]=$field;
  151. $values[] = $value;
  152. }
  153. return $this->insert($table,$fields,$values);
  154. }
  155. $sql = "insert into $table (";
  156. $f = is_array($f) ? $f : array($f);
  157. $v = is_array($v) ? $v : array($v);
  158. if(count($f) != count($v)){
  159. debug_info("插入表{$table}失败,字段数目与值个数不匹配");
  160. return false;
  161. }
  162. $sql .= implode(",",$f) . ') values(';
  163. $len = count($v);
  164. for($i=0;$i<$len;$i++){
  165. $v[$i] = "'" . str_replace("'","''",$v[$i]) ."'";
  166. }
  167. $v = implode(",",$v);
  168. $sql .= "{$v})";
  169. return $this->execute($sql);
  170. /*
  171. $a = array_combine($f, $v);
  172. $sql .= implode($a,)
  173. */
  174. }
  175. function update($table,$f,$v=null,$c=null){
  176. if(is_null($v) && is_array($f)){
  177. foreach ($f as $field => $value) {
  178. $fields[]=$field;
  179. $values[] = $value;
  180. }
  181. return $this->update($table,$fields,$values,$c);
  182. }
  183. $sql = "update $table set ";
  184. $f = is_array($f) ? $f : array($f);
  185. $v = is_array($v) ? $v : array($v);
  186. if(count($f) != count($v)){
  187. debug_info("修改表{$table}失败,字段数目与值个数不匹配");
  188. return false;
  189. }
  190. $len = count($v);
  191. for($i=0;$i<$len;$i++){
  192. $v[$i] = "'" . str_replace("'","''",$v[$i]) ."'";
  193. $a[] = "{$f[$i]}={$v[$i]}";
  194. }
  195. $value = implode(',',$a);
  196. $sql .= "{$value}";
  197. if(!empty($c)){
  198. $sql .= " where {$c}";
  199. }
  200. return $this->execute($sql);
  201. }
  202. public function move_first(){
  203. if($this->record_count <=0 ){
  204. return false;
  205. }
  206. $this->data_set_pointer = 0;
  207. return true;
  208. }
  209. public function move_next(){
  210. if($this->data_set_pointer + 1 >= $this->record_count) return false;
  211. $this->data_set_pointer += 1;
  212. return true;
  213. }
  214. public function field_by_index($index)
  215. {
  216. return $this->data_set[$this->data_set_pointer]->field_by_index($index);
  217. }
  218. public function field_by_name($name)
  219. {
  220. return $this->data_set[$this->data_set_pointer]->$name;
  221. }
  222. public function get_field_name($index)
  223. {
  224. return mysql_field_name($this->_qresult, $index);
  225. }
  226. public function get_error()
  227. {
  228. return mysql_error();
  229. }
  230. /*
  231. * private functions defination
  232. */
  233. private function _connect()
  234. {
  235. $this->_db = mysql_connect($this->servername, $this->username, $this->password);
  236. if ($this->_db === FALSE)
  237. {
  238. $this->_debug_info('fail to establish db connection,' .$this->get_error());
  239. return FALSE;
  240. }
  241. else
  242. {
  243. mysql_query('SET NAMES ' .$this->code);
  244. mysql_select_db($this->databasename, $this->_db);
  245. return $this->_db;
  246. }
  247. }
  248. private function _debug_info($msg){
  249. global $debug_tag;
  250. if ($debug_tag === true) {
  251. if(function_exists('debug_info')){
  252. debug_info($msg);
  253. }
  254. }
  255. }
  256. private function reset_vars()
  257. {
  258. unset($this->_aresult);
  259. unset($this->_qresult);
  260. $this->data_set = array();
  261. $this->data_set_pointer = 0;
  262. $this->record_count = 0;
  263. }
  264. private function _move_first()
  265. {
  266. $this->_aresult = array();
  267. if (!is_resource($this->_qresult) || mysql_num_rows($this->_qresult) <=0 )
  268. {
  269. return FALSE;
  270. }
  271. mysql_data_seek($this->_qresult,0);
  272. $this->_aresult = mysql_fetch_array($this->_qresult);
  273. return TRUE;
  274. }
  275. private function _move_next()
  276. {
  277. $this->_aresult = array();
  278. if (!is_resource($this->_qresult)){
  279. return FALSE;
  280. }
  281. $rtemp = mysql_fetch_array($this->_qresult);
  282. if ($rtemp===FALSE) {
  283. return FALSE;
  284. }
  285. $this->_aresult = $rtemp;
  286. return TRUE;
  287. }
  288. private function _move_to($pos)
  289. {
  290. $this->_aresult = array();
  291. if(!is_int($pos) || $pos < 0){
  292. $this->_debug_info('fail to call _move_to,out of range!');
  293. return false;
  294. }
  295. $result = mysql_data_seek($this->_qresult,$pos);
  296. if ($result) {
  297. $this->_aresult = mysql_fetch_array($this->_qresult);
  298. }
  299. return $result;
  300. }
  301. public function __get($var){
  302. if (strtolower($var) == "affect_count"){
  303. return mysql_affected_rows($this->_db);
  304. }
  305. if (strtolower($var) == "last_insert_id"){
  306. return mysql_insert_id($this->_db);
  307. }
  308. }
  309. }
  310. ?>