PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/snsmarketing/DbUtil.php

http://collectgame.googlecode.com/
PHP | 344 lines | 200 code | 50 blank | 94 comment | 38 complexity | f4f77ee821afe79a9104c0d0a3b00baa MD5 | raw file
  1. <?php
  2. /**
  3. * ??????
  4. * Enter description here ...
  5. * @author Administrator
  6. *
  7. */
  8. class DbUtil{
  9. var $dbuser;
  10. var $dbpassword;
  11. var $dbname;
  12. var $dbhost;
  13. var $dbh;
  14. var $charset;
  15. var $collate;
  16. var $rows_affected = 0;
  17. var $insert_id = 0;
  18. var $col_info;
  19. var $last_result;
  20. var $last_error = 0;
  21. var $result;
  22. /**
  23. * ????
  24. * Enter description here ...
  25. * @param ??? $hostname
  26. * @param ??? $username
  27. * @param ?? $password
  28. * @param ???? $dbname
  29. */
  30. function DbUtil($hostname, $username, $password, $dbname){
  31. return $this->__construct($hostname, $username, $password, $dbname);
  32. }
  33. /**
  34. * ????
  35. * Enter description here ...
  36. * @param unknown_type $hostname
  37. * @param unknown_type $username
  38. * @param unknown_type $password
  39. * @param unknown_type $dbname
  40. */
  41. function __construct($hostname, $username, $password, $dbname){
  42. $this->dbhost = $hostname;
  43. $this->dbname = $dbname;
  44. $this->dbpassword = $password;
  45. $this->dbuser = $username;
  46. $this->init_charset();
  47. $this->db_connect();
  48. }
  49. /**
  50. * ?????
  51. * Enter description here ...
  52. */
  53. function init_charset(){
  54. if(defined('DB_CHARSET')){
  55. $this->charset = DB_CHARSET;
  56. }else {
  57. $this->charset = 'utf-8';
  58. }
  59. if(defined('DB_COLLATE'))
  60. $this->collate = DB_COLLATE;
  61. else
  62. $this->collate = 'utf8_general_ci';
  63. }
  64. /**
  65. * ?????
  66. * Enter description here ...
  67. */
  68. function db_connect(){
  69. $this->dbh = mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword);
  70. mysql_set_charset($this->charset, $this->dbh);
  71. mysql_select_db($this->dbname, $this->dbh);
  72. }
  73. /**
  74. * ????????
  75. * Enter description here ...
  76. * @param string $sql
  77. */
  78. function query($sql){
  79. $this->flush();
  80. $return_val = 0;
  81. $this->result = @mysql_query($sql, $this->dbh);
  82. $this->last_error = mysql_errno($this->dbh);
  83. if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $sql ) ) {
  84. $this->rows_affected = mysql_affected_rows( $this->dbh );
  85. if ( preg_match( "/^\\s*(insert|replace) /i", $sql ) ) {
  86. $this->insert_id = mysql_insert_id($this->dbh);
  87. }
  88. $return_val = $this->rows_affected;
  89. }else{
  90. $i = 0;
  91. while ( $i < @mysql_num_fields( $this->result ) ) {
  92. $this->col_info[$i] = @mysql_fetch_field( $this->result );
  93. $i++;
  94. }
  95. $num_rows = 0;
  96. while ( $row = @mysql_fetch_object( $this->result ) ) {
  97. $this->last_result[$num_rows] = $row;
  98. $num_rows++;
  99. }
  100. $this->num_rows = $num_rows;
  101. $return_val = $num_rows;
  102. }
  103. return $return_val;
  104. }
  105. /**
  106. * ????
  107. * @param unknown_type $table ?
  108. * @param unknown_type $data ????????? ??->????
  109. * @param unknown_type $format ?????????????????????? %d
  110. */
  111. function insert($table, $data, $format = null){
  112. return $this->_insert_helper($table, $data, $format, 'INSERT');
  113. }
  114. /**
  115. * ?????
  116. * @param unknown_type $table ?????
  117. * @param unknown_type $data ???????????->????
  118. * @param unknown_type $where ????,????
  119. * @param unknown_type $format ?????
  120. * @param unknown_type $where_format ????????
  121. */
  122. function update( $table, $data, $where, $format = null, $where_format = null ) {
  123. if ( ! is_array( $data ) || ! is_array( $where ) )
  124. return false;
  125. $formats = $format = (array) $format;
  126. $bits = $wheres = array();
  127. foreach ( (array) array_keys( $data ) as $field ) {
  128. if ( !empty( $format ) )
  129. $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
  130. elseif ( isset($this->field_types[$field]) )
  131. $form = $this->field_types[$field];
  132. else
  133. $form = '%s';
  134. $bits[] = "`$field` = {$form}";
  135. }
  136. $where_formats = $where_format = (array) $where_format;
  137. foreach ( (array) array_keys( $where ) as $field ) {
  138. if ( !empty( $where_format ) )
  139. $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
  140. elseif ( isset( $this->field_types[$field] ) )
  141. $form = $this->field_types[$field];
  142. else
  143. $form = '%s';
  144. $wheres[] = "`$field` = {$form}";
  145. }
  146. $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
  147. return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
  148. }
  149. /**
  150. * ?????????????
  151. * Enter description here ...
  152. * @param string $query ????
  153. * @param string $output ?????object???
  154. */
  155. function get_results( $query = null, $output = OBJECT ) {
  156. if ( $query )
  157. $this->query( $query );
  158. else
  159. return null;
  160. $new_array = array();
  161. if ( $output == OBJECT ) {
  162. // Return an integer-keyed array of row objects
  163. return $this->last_result;
  164. } elseif ( $output == OBJECT_K ) {
  165. // Return an array of row objects with keys from column 1
  166. // (Duplicates are discarded)
  167. foreach ( $this->last_result as $row ) {
  168. $key = array_shift( $var_by_ref = get_object_vars( $row ) );
  169. if ( ! isset( $new_array[ $key ] ) )
  170. $new_array[ $key ] = $row;
  171. }
  172. return $new_array;
  173. } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  174. // Return an integer-keyed array of...
  175. if ( $this->last_result ) {
  176. foreach( (array) $this->last_result as $row ) {
  177. if ( $output == ARRAY_N ) {
  178. // ...integer-keyed row arrays
  179. $new_array[] = array_values( get_object_vars( $row ) );
  180. } else {
  181. // ...column name-keyed row arrays
  182. $new_array[] = get_object_vars( $row );
  183. }
  184. }
  185. }
  186. return $new_array;
  187. }
  188. return null;
  189. }
  190. /**
  191. * ??????
  192. * Enter description here ...
  193. * @param unknown_type $query ????
  194. * @param unknown_type $output ?????object???
  195. * @param unknown_type $y ?N?
  196. */
  197. function get_row( $query = null, $output = OBJECT, $y = 0 ) {
  198. if ( $query )
  199. $this->query( $query );
  200. else
  201. return null;
  202. if ( !isset( $this->last_result[$y] ) )
  203. return null;
  204. if ( $output == OBJECT ) {
  205. return $this->last_result[$y] ? $this->last_result[$y] : null;
  206. } elseif ( $output == ARRAY_A ) {
  207. return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
  208. } elseif ( $output == ARRAY_N ) {
  209. return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
  210. } else {
  211. $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/' $db->get_row(string query, output type, int offset) -- ????????????????OBJECT, ARRAY_A, ARRAY_N'/*/WP_I18N_DB_GETROW_ERROR*/);
  212. }
  213. }
  214. /**
  215. * ???????
  216. * Enter description here ...
  217. * @param unknown_type $query ????
  218. * @param unknown_type $x ???
  219. * @param unknown_type $y ???
  220. */
  221. function get_var( $query = null, $x = 0, $y = 0 ) {
  222. if ( $query )
  223. $this->query( $query );
  224. // Extract var out of cached results based x,y vals
  225. if ( !empty( $this->last_result[$y] ) ) {
  226. $values = array_values( get_object_vars( $this->last_result[$y] ) );
  227. }
  228. // If there is a value return it else return null
  229. return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
  230. }
  231. /**
  232. * ???????
  233. * Enter description here ...
  234. * @param unknown_type $table
  235. * @param unknown_type $data
  236. * @param unknown_type $format
  237. * @param unknown_type $type
  238. */
  239. function _insert_helper($table, $data, $format = null, $type = 'INSERT'){
  240. if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
  241. return false;
  242. $formats = $format = (array) $format;
  243. $fields = array_keys( $data );
  244. $formatted_fields = array();
  245. foreach ( $fields as $field ) {
  246. if ( !empty( $format ) )
  247. $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
  248. elseif ( isset( $this->field_types[$field] ) )
  249. $form = $this->field_types[$field];
  250. else
  251. $form = '%s';
  252. $formatted_fields[] = $form;
  253. }
  254. $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
  255. return $this->query( $this->prepare( $sql, $data ) );
  256. }
  257. /**
  258. * ?????????
  259. */
  260. function flush() {
  261. $this->last_result = array();
  262. $this->col_info = null;
  263. $this->last_query = null;
  264. }
  265. /**
  266. * ???sql??
  267. * Enter description here ...
  268. * @param string $query sql????
  269. */
  270. function prepare( $query = null ) {
  271. if ( is_null( $query ) )
  272. return;
  273. $args = func_get_args();
  274. array_shift( $args );
  275. // If args were passed as an array (as in vsprintf), move them up
  276. if ( isset( $args[0] ) && is_array($args[0]) )
  277. $args = $args[0];
  278. $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
  279. $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
  280. $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
  281. array_walk( $args, array( &$this, 'escape_by_ref' ) );
  282. return @vsprintf( $query, $args );
  283. }
  284. function escape_by_ref( &$string ) {
  285. $string = $this->_real_escape( $string );
  286. }
  287. function _real_escape( $string ) {
  288. if ( $this->dbh )
  289. return mysql_real_escape_string( $string, $this->dbh );
  290. else
  291. return addslashes( $string );
  292. }
  293. }
  294. ?>