PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/db/mysql.class.php

https://bitbucket.org/crxproject/cutenews-ru
PHP | 230 lines | 189 code | 35 blank | 6 comment | 40 complexity | 1b7c03c8c36949f9cdc7c2b1812351f3 MD5 | raw file
  1. <?php
  2. /*
  3. example:
  4. $sql = new MySQL();
  5. $sql->connect('root', '', 'localhost');
  6. $sql->selectdb('cutenews');
  7. */
  8. class MySQL {
  9. function connect($username = 'root', $password = '', $server = 'localhost'){
  10. $this->username = $username;
  11. $this->password = $password;
  12. $this->server = $server;
  13. $this->link = mysql_connect($this->server, $this->username, $this->password);
  14. }
  15. function disconnect(){
  16. mysql_close($this->link);
  17. }
  18. function strict($error = false){
  19. }
  20. function selectdb($database, $prefix = ''){
  21. $this->database = $database;
  22. $this->prefix = $prefix;
  23. mysql_select_db($this->database, $this->link);
  24. }
  25. function altertable($arg){
  26. $query .= 'alter table '.$this->prefix.$arg['table']."\n";
  27. if ($arg['action'] == 'insert'){
  28. $query .= 'add '.$arg['name'].' '.$this->_values($arg);
  29. } elseif ($arg['action'] == 'rename table'){
  30. $query .= 'rename '.$arg['name'];
  31. } elseif ($arg['action'] == 'rename col'){
  32. $result = array();
  33. $list = mysql_query('select * from '.$this->prefix.$arg['table'], $this->link);
  34. for ($i = 0; $i < mysql_num_fields($list); $i++){
  35. if (mysql_field_name($list, $i) == $arg['name']){
  36. $result[] = mysql_field_type($list, $i).' '.mysql_field_flags($list, $i);
  37. }
  38. }
  39. $query .= 'change '.$arg['name'].' '.$arg['values']['name'].' '.join('', $result)."\n";
  40. } elseif ($arg['action'] == 'modify'){
  41. $result = array();
  42. $list = mysql_query('select * from '.$this->prefix.$arg['table'], $this->link);
  43. for ($i = 0; $i < mysql_num_fields($list); $i++){
  44. if (mysql_field_name($list, $i) == $arg['name']){
  45. $result[] = mysql_field_type($list, $i).' '.mysql_field_flags($list, $i);
  46. }
  47. }
  48. $query .= 'change '.$arg['name'].' '.$arg['name'].' '.join('', $result).' '.$this->_values($arg).' not null'."\n";
  49. } elseif ($arg['action'] == 'addkey'){
  50. $query .= 'add primary key('.$arg['name'].')'."\n";
  51. } elseif ($arg[ 'action'] == 'drop'){
  52. $query = 'drop table '.$this->prefix.$arg['table']."\n";
  53. }
  54. $query = str_replace('string', 'varchar(255)', $query);
  55. $query = str_replace('bool', 'tinyint(1)', $query);
  56. $query = str_replace('primary key', 'primary', $query);
  57. $query = str_replace('primary', 'primary key', $query);
  58. return mysql_query($query, $this->link);
  59. }
  60. function showdbs($arg){
  61. $list = mysql_list_dbs($this->link);
  62. for ($i = 0; $i < mysql_num_rows($list); $i++){
  63. $result[] = mysql_db_name($list, $i);
  64. }
  65. return $result;
  66. }
  67. function createdb($arg){
  68. mysql_create_db($arg['db'], $this->link);
  69. }
  70. function dropdb($arg){
  71. mysql_drop_db($arg['db'], $this->link);
  72. }
  73. function createtable($arg){
  74. foreach ($arg['columns'] as $column => $arg['values']){
  75. $query[] = $column.' '.$this->_values($arg);
  76. foreach($arg['values'] as $k => $v){
  77. if ($k == 'primary'){
  78. $primary = ','."\n".'primary key ('.$column.')';
  79. }
  80. }
  81. }
  82. $query = join(', ', $query).$primary;
  83. $query = str_replace('string', 'varchar(255)', $query);
  84. $query = str_replace('bool', 'tinyint(1)', $query);
  85. $query = 'create table '.$this->prefix.$arg['table']." (\n".$query."\n)\n";
  86. return mysql_query($query, $this->link);
  87. }
  88. function table_exists($table, $database = ''){
  89. $list = mysql_query("SHOW TABLES FROM ".$database."", $this->link);
  90. for ($i = 0; $i < mysql_num_rows($list); $i++){
  91. if (mysql_query("SHOW TABLES FROM ".$list."", $i) == $this->prefix.$table){
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. function query_count(){
  98. return $this->query;
  99. }
  100. function table_count($table, $database = ''){
  101. $query = 'show table status like \''.$this->prefix.$table.'\'';
  102. $query = mysql_query($query, $this->link);
  103. $row = mysql_fetch_array($query);
  104. return $row['Rows'];
  105. }
  106. function last_insert_id($table = '', $database = '', $select = ''){
  107. $query = 'show table status like \''.$this->prefix.$table.'\'';
  108. $query = mysql_query($query, $this->link);
  109. $row = mysql_fetch_array($query);
  110. return ($row['Auto_increment'] - 1);
  111. }
  112. function insert($arg){
  113. foreach ($arg['values'] as $k => $v){
  114. $insert[] = $k;
  115. $values[] = '\''.mysql_real_escape_string($v).'\'';
  116. }
  117. $query .= 'insert into '.$this->prefix.$arg['table']."\n";
  118. $query .= '('.join(', ', $insert).')'."\n";
  119. $query .= ' values ('.join(', ', $values).')'."\n";
  120. $debug_return = false;
  121. if (mysql_query($query, $this->link)) {
  122. $debug_return = mysql_affected_rows($this->link) != 0;
  123. }
  124. return $debug_return;
  125. }
  126. function delete($arg){
  127. $query .= 'delete from '.$this->prefix.$arg['table']."\n";
  128. $query .= $this->_where($arg)."\n";
  129. $query .= ($arg['limit'] ? 'limit '.join(', ', $arg['limit']) : '')."\n";
  130. $debug_return = false;
  131. if (mysql_query($query, $this->link)) {
  132. $debug_return = mysql_affected_rows($this->link) != 0;
  133. }
  134. return $debug_return;
  135. }
  136. function update($arg){
  137. $query .= 'update '.$this->prefix.$arg['table']."\n";
  138. $query .= 'set '.$this->_values($arg, ' = ', ', ')."\n";
  139. $query .= $this->_where($arg)."\n";
  140. $query .= ($arg['limit'] ? 'limit '.join(', ', $arg['limit']) : '')."\n";
  141. $debug_return = false;
  142. if (mysql_query($query, $this->link)) {
  143. $debug_return = mysql_affected_rows($this->link) != 0;
  144. }
  145. return $debug_return;
  146. }
  147. function select($arg){
  148. $this->query++;
  149. $query .= 'select '.(!$arg['select'] ? '*' : join(', ', $arg['select']))."\n";
  150. $query .= 'from '.$this->prefix.$arg['table']."\n";
  151. $query .= ($arg['join'] ? 'inner join '.$this->prefix.$arg['join']['table'].' on '.$arg['join']['where'] : '')."\n";
  152. $query .= $this->_where($arg)."\n";
  153. $query .= ($arg['orderby'] ? 'order by '.join(' ', $arg['orderby']) : '')."\n";
  154. $query .= ($arg['limit'] ? 'limit '.join(', ', $arg['limit']) : '')."\n";
  155. $query = mysql_query($query, $this->link);
  156. $result = array();
  157. while ($row = @mysql_fetch_assoc($query)){
  158. $result[] = $row;
  159. }
  160. return $result;
  161. }
  162. function _values($arg, $separator_in = ' ', $separator_out = ' '){
  163. foreach ($arg['values'] as $k => $v){
  164. if ($k != 'primary'){
  165. if ($k == 'type' or ($k == 'name' and $arg['action'])){
  166. $result[] = $v.' not null';
  167. } elseif ($k == 'auto_increment' or $k == 'permanent'){
  168. $result[] = $k;
  169. } else {
  170. $result[] = $k.$separator_in.'\''.mysql_real_escape_string($v).'\'';
  171. }
  172. }
  173. }
  174. return join($separator_out, $result);
  175. }
  176. function _where($arg, $separator = ' '){
  177. if ($arg['where']){
  178. $operators = '( |=|!=|<>|<|<=|>|>=|=~|!~|\?|\?!)';
  179. foreach ($arg['where'] as $k => $v){
  180. if (preg_match('/\[(.*)\]/i', $v, $match)){
  181. foreach (explode('|', $match[1]) as $or){
  182. $where[] = preg_replace('/(.*)'.$operators.'(.*)/i', '\\1\\2\'\\3\'', preg_replace('/\['.str_replace('|', '\|', $match[1]).'\]/i', $or, $v));
  183. }
  184. $result[] = '('.join(' or ', $where).')'."\r\n";
  185. } elseif ($v != 'and' and $v != 'or' and $v != 'xor'){
  186. $result[] = preg_replace('/(.*)'.$operators.'(.*)/i', '\\1\\2\'\\3\'', $v)."\r\n";
  187. } else {
  188. $result[] = $v."\r\n";
  189. }
  190. }
  191. return 'where '.str_replace(array('!~', '=~'), array('not like', 'like'), join($separator, $result));
  192. }
  193. }
  194. }
  195. ?>