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

/code/classes/pinetd/SQL/MySQL.class.php

https://github.com/blekkzor/pinetd2
PHP | 149 lines | 129 code | 18 blank | 2 comment | 19 complexity | cc9253dd6b21f604cbfb260367ac72c7 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace pinetd\SQL;
  3. use \Exception;
  4. class MySQL {
  5. private $settings;
  6. private $mysqli;
  7. private $unique;
  8. private $DAO = array();
  9. public function __construct($settings) {
  10. if (!extension_loaded('mysqli')) throw new Exception('This class requires MySQLi');
  11. $this->settings = $settings;
  12. $this->mysqli = mysqli_init();
  13. $this->mysqli->connect($settings['Host'], $settings['Login'], $settings['Password'], $settings['Database']);
  14. if (mysqli_connect_errno()) {
  15. throw new Exception(mysqli_connect_error());
  16. }
  17. $this->unique = sha1($settings['Host'].$settings['Login'].$settings['Password'].$settings['Database']);
  18. }
  19. public function __call($func, $args) {
  20. return call_user_func_array(array($this->mysqli, $func), $args);
  21. }
  22. public function __get($var) {
  23. return $this->mysqli->$var;
  24. }
  25. public function unique() {
  26. return $this->unique;
  27. }
  28. public function now() {
  29. return date('Y-m-d H:i:s');
  30. }
  31. public function timeStamp($when) {
  32. return date('Y-m-d H:i:s', $when);
  33. }
  34. public function DAO($table, $key) {
  35. if (!isset($this->DAO[$table])) $this->DAO[$table] = new \DAO\MySQL($this, $table, $key);
  36. return $this->DAO[$table];
  37. }
  38. // when we fork, this is required
  39. public function reconnect() {
  40. $this->mysqli->close();
  41. $this->mysqli->connect($this->settings['Host'], $this->settings['Login'], $this->settings['Password'], $this->settings['Database']);
  42. if (mysqli_connect_errno()) {
  43. throw new Exception(mysqli_connect_error());
  44. }
  45. }
  46. public function quote_escape($string) {
  47. if (is_null($string)) return 'NULL';
  48. if (is_array($string)) {
  49. $res = '';
  50. foreach($string as $elem) $res .= ($res == ''?'':',') . $this->quote_escape($elem);
  51. return $res;
  52. }
  53. return '\''.$this->mysqli->escape_string($string).'\'';
  54. }
  55. function col_gen_type($col) {
  56. $res = strtolower($col['type']);
  57. switch($res) {
  58. case 'set': case 'enum':
  59. $res.='('.$this->quote_escape($col['values']).')';
  60. break;
  61. case 'text': case 'blob': case 'datetime':
  62. break;
  63. default:
  64. if (isset($col['size'])) $res.='('.$col['size'].')';
  65. break;
  66. }
  67. if ($col['unsigned']) $res.=' unsigned';
  68. return $res;
  69. }
  70. function gen_field_info($cname, $col) {
  71. $tmp = '`'.$cname.'` '.$this->col_gen_type($col);
  72. if (!$col['null']) $tmp.=' NOT NULL';
  73. if (isset($col['auto_increment'])) $tmp.=' auto_increment';
  74. if (array_key_exists('default',$col)) $tmp.=' DEFAULT '.$this->quote_escape($col['default']);
  75. return $tmp;
  76. }
  77. function gen_create_query($name, $struct) {
  78. $req = '';
  79. $keys = array();
  80. foreach($struct as $cname=>$col) {
  81. $req.=($req==''?'':', ').$this->gen_field_info($cname, $col);
  82. if (isset($col['key'])) $keys[$col['key']][]=$cname;
  83. }
  84. foreach($keys as $kname=>$cols) {
  85. $tmp = '';
  86. foreach($cols as $c) $tmp.=($tmp==''?'':',').'`'.$c.'`';
  87. $tmp='('.$tmp.')';
  88. if ($kname == 'PRIMARY') {
  89. $tmp = 'PRIMARY KEY '.$tmp;
  90. } elseif (substr($kname, 0, 7)=='UNIQUE:') {
  91. $kname = substr($kname, 7);
  92. $tmp = 'UNIQUE KEY `'.$kname.'` '.$tmp;
  93. } elseif (substr($kname, 0, 9)=='FULLTEXT:') {
  94. $kname = substr($kname, 9);
  95. $tmp = 'FULLTEXT KEY `'.$kname.'` '.$tmp;
  96. } else {
  97. $tmp = 'KEY `'.$kname.'` '.$tmp;
  98. }
  99. $req.=($req==''?'':',').$tmp;
  100. }
  101. $req = 'CREATE TABLE `'.$name.'` ('.$req.') ENGINE=MyISAM DEFAULT CHARSET=utf8';
  102. return $req;
  103. }
  104. public function validateStruct($table_name, $struct) {
  105. $f = array_flip(array_keys($struct)); // field list
  106. $req = 'SHOW FIELDS FROM `'.$table_name.'`';
  107. $res = @$this->mysqli->query($req);
  108. if (!$res) {
  109. $req = $this->gen_create_query($table_name, $struct);
  110. return @$this->mysqli->query($req);
  111. }
  112. while($row = $res->fetch_assoc()) {
  113. if (!isset($f[$row['Field']])) {
  114. // we got a field we don't know about
  115. $req = 'ALTER TABLE `'.$table_name.'` DROP `'.$row['Field'].'`';
  116. @$this->mysqli->query($req);
  117. continue;
  118. }
  119. unset($f[$row['Field']]);
  120. $col = $struct[$row['Field']];
  121. if ($row['Type']!=$this->col_gen_type($col)) {
  122. $req = 'ALTER TABLE `'.$table_name.'` CHANGE `'.$row['Field'].'` '.$this->gen_field_info($row['Field'], $col);
  123. @$this->mysqli->query($req);
  124. }
  125. }
  126. foreach($f as $k=>$ign) {
  127. $req = 'ALTER TABLE `'.$table_name.'` ADD '.$this->gen_field_info($k, $struct[$k]);
  128. @$this->mysqli->query($req);
  129. }
  130. }
  131. }