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

/sys/plugins/classes/table_structure.class.php

https://bitbucket.org/DESURE/dcms
PHP | 198 lines | 152 code | 15 blank | 31 comment | 49 complexity | 3ea692c90ea7de507eef7cad9aa9f1dd MD5 | raw file
  1. <?php
  2. class table_structure {
  3. var $_structure = array(); // ???? ? ??????? ???????
  4. var $_properties = array();
  5. function __construct($path = false)
  6. {
  7. if ($path) $this->loadFromIniFile($path);
  8. }
  9. public function clear()
  10. {
  11. $this->_structure = array();
  12. $this->_properties = array();
  13. }
  14. // ???????? ????????? ?? INI ?????
  15. function loadFromIniFile($path)
  16. {
  17. $this->clear();
  18. $ini = ini::read($path, 1);
  19. foreach ($ini as $key => $value) {
  20. if ($key == '~TABLE~PROPERTIES~')$this->_properties = $value;
  21. else $this->_structure[$key] = $value;
  22. }
  23. }
  24. // ????????? ????????? ??????? ?? ???????????? ????
  25. function loadFromBase($table)
  26. {
  27. $this->clear();
  28. $table = mysql_real_escape_string($table);
  29. // ????????? ????? ???????
  30. $q = mysql_query("SHOW FULL COLUMNS FROM `$table`");
  31. while ($result = mysql_fetch_assoc($q)) {
  32. $structure = array();
  33. $structure['type'] = $result['Type'];
  34. if ($result['Default'] == '' && $result['Null'] == 'YES')$structure['default_and_null'] = 'DEFAULT NULL';
  35. elseif ($result['Default'] != '' && $result['Null'] == 'YES')$structure['default_and_null'] = "NULL DEFAULT '" . my_esc($result['Default']) . "'";
  36. elseif ($result['Default'] != '' && $result['Null'] == 'NO')$structure['default_and_null'] = "NOT NULL DEFAULT '" . my_esc($result['Default']) . "'";
  37. else $structure['default_and_null'] = 'NOT NULL';
  38. $structure['ai'] = $result['Extra'] == 'auto_increment'?'AUTO_INCREMENT':'';
  39. $structure['comment'] = $result['Comment']?"COMMENT '" . my_esc($result['Comment']) . "'":'';
  40. $this->_structure['`' . $result['Field'] . '`'] = $structure;
  41. }
  42. // ????????? ?????? ???????
  43. $q = mysql_query("SHOW KEYS FROM `$table`");
  44. while ($result = mysql_fetch_assoc($q)) {
  45. $structure = array();
  46. if ($result['Key_name'] == 'PRIMARY')$structure['type'] = 'PRIMARY KEY';
  47. elseif ($result['Index_type'] == 'FULLTEXT')$structure['type'] = 'FULLTEXT KEY `' . my_esc($result['Key_name']) . '`';
  48. elseif (!$result['Non_unique'])$structure['type'] = 'UNIQUE KEY `' . my_esc($result['Key_name']) . '`';
  49. else $structure['type'] = 'KEY `' . my_esc($result['Key_name']) . '`';
  50. if (isset($this->_structure[$structure['type']]['fields']))
  51. $this->_structure[$structure['type']]['fields'] .= ", `" . my_esc($result['Column_name']) . "`";
  52. else
  53. $this->_structure[$structure['type']]['fields'] = "`" . my_esc($result['Column_name']) . "`";
  54. }
  55. // ????????? ??????? ???????
  56. $q = mysql_query("SHOW TABLE STATUS LIKE '$table'");
  57. $properties = mysql_fetch_assoc($q);
  58. $this->_properties['name'] = $properties['Name'];
  59. $this->_properties['engine'] = 'ENGINE=' . $properties['Engine'];
  60. $this->_properties['auto_increment'] = 'AUTO_INCREMENT=' . $properties['Auto_increment'];
  61. $this->_properties['comment'] = "COMMENT='" . my_esc($properties['Comment']) . "'";
  62. }
  63. // ?????????? ????????? ? INI ????
  64. function saveToIniFile($path)
  65. {
  66. return ini::save($path, array_merge($this->_structure, array('~TABLE~PROPERTIES~' => $this->_properties)), true);
  67. }
  68. // ????????? SQL ??????? ?? ???????? ???????
  69. function getSQLQueryCreate()
  70. {
  71. $sql = "CREATE TABLE `" . my_esc($this->_properties['name']) . "` (\r\n";
  72. $structure = array();
  73. foreach ($this->_structure as $name => $struct) {
  74. $struct_tmp = array();
  75. $struct_tmp[] = $name ;
  76. if (isset($struct['fields'])) {
  77. $struct_tmp[] = '(' . $struct['fields'] . ')';
  78. } else {
  79. if ($struct['type'])$struct_tmp[] = $struct['type'];
  80. if ($struct['default_and_null'])$struct_tmp[] = $struct['default_and_null'];
  81. if ($struct['ai'])$struct_tmp[] = $struct['ai'];
  82. if ($struct['comment'])$struct_tmp[] = $struct['comment'];
  83. }
  84. $structure[] = implode(' ', $struct_tmp);
  85. }
  86. $sql .= implode(",\r\n", $structure);
  87. $sql .= "\r\n) ";
  88. $prop_tmp = array();
  89. if ($this->_properties['engine'])$prop_tmp[] = $this->_properties['engine'];
  90. $prop_tmp[] = 'DEFAULT CHARSET=utf8';
  91. // $prop_tmp[] = $this -> _properties['auto_increment'];
  92. if ($this->_properties['comment'])$prop_tmp[] = $this->_properties['comment'];
  93. $sql .= implode(' ', $prop_tmp);
  94. return $sql;
  95. }
  96. // ????????? SQL ??????? ?? ????????? ???????
  97. function getSQLQueryChange($tStruct_obj)
  98. {
  99. $to_add = array(); // ????????? ???? (???????)
  100. $to_delete = array(); // ??????? ???? (???????)
  101. $to_edit = array(); // ???????? ???? (???????)
  102. foreach ($tStruct_obj->_structure as $key => $value) {
  103. if (!isset($this->_structure[$key])) { // ???? ? ?????? ??????? ?????? ???? ?? ??????????, ?? ???????? ? ???????????
  104. $to_add[$key] = $value;
  105. continue;
  106. }
  107. }
  108. foreach ($this->_structure as $key => $value) {
  109. if (!isset($tStruct_obj->_structure[$key])) { // ???? ? ????? ??????? ?????? ???? ?? ??????????, ?? ???????? ? ?????????
  110. $to_delete[$key] = $value;
  111. continue;
  112. }
  113. $new_value = $tStruct_obj->_structure[$key];
  114. foreach ($value as $key2 => $value2) {
  115. if ($new_value[$key2] != $value2) {
  116. // ???? ???? ???? ?? ?????????? ??????????, ?? ????????? ? ??????????
  117. $to_edit[$key] = $new_value;
  118. continue(2);
  119. }
  120. }
  121. }
  122. // ???? ??? ?????????
  123. if (!$to_add /* && !$to_delete*/ && !$to_edit)return false;
  124. $sql = "ALTER TABLE `" . my_esc($this->_properties['name']) . "` \r\n";
  125. $structure = array();
  126. /*
  127. // ????????? ????????? ????? (????????)
  128. ksort($to_delete);
  129. foreach ($to_delete as $name => $struct) {
  130. if (isset($struct['fields'])) {
  131. if (strpos($name, 'PRIMARY') === 0)
  132. $structure[] = 'DROP PRIMARY KEY';
  133. elseif (preg_match('#`(.+?)`#', $name, $m))
  134. $structure[] = 'DROP INDEX ' . $m[1];
  135. continue;
  136. }
  137. $struct_tmp = array();
  138. $struct_tmp[] = 'DROP';
  139. $struct_tmp[] = $name;
  140. $structure[] = implode(' ', $struct_tmp);
  141. }
  142. */
  143. // ????????? ?????????? ????? (????????)
  144. foreach ($to_edit as $name => $struct) {
  145. $struct_tmp = array();
  146. $struct_tmp[] = 'CHANGE';
  147. $struct_tmp[] = $name ;
  148. $struct_tmp[] = $name ;
  149. // ??????? ?? ?????? ?? ?????, ?? ???? ?? ????? ??????? ? ??????? ??????
  150. if (isset($struct['fields'])) {
  151. if (strpos($name, 'PRIMARY') === 0)
  152. $structure[] = 'DROP PRIMARY KEY';
  153. elseif (preg_match('#`(.+?)`#', $name, $m))
  154. $structure[] = 'DROP INDEX ' . $m[1];
  155. $structure[] = 'ADD ' . $name . ' (' . $struct['fields'] . ')';
  156. continue;
  157. }
  158. if ($struct['type'])$struct_tmp[] = $struct['type'];
  159. if ($struct['default_and_null'])$struct_tmp[] = $struct['default_and_null'];
  160. if ($struct['ai'])$struct_tmp[] = $struct['ai'];
  161. if ($struct['comment'])$struct_tmp[] = $struct['comment'];
  162. $structure[] = implode(' ', $struct_tmp);
  163. }
  164. // ????????? ??????????? ????? (????????)
  165. foreach ($to_add as $name => $struct) {
  166. $struct_tmp = array();
  167. $struct_tmp[] = 'ADD';
  168. $struct_tmp[] = $name ;
  169. if (isset($struct['fields'])) {
  170. $struct_tmp[] = '(' . $struct['fields'] . ')';
  171. } else {
  172. if ($struct['type'])$struct_tmp[] = $struct['type'];
  173. if ($struct['default_and_null'])$struct_tmp[] = $struct['default_and_null'];
  174. if ($struct['ai'])$struct_tmp[] = $struct['ai'];
  175. if ($struct['comment'])$struct_tmp[] = $struct['comment'];
  176. }
  177. $structure[] = implode(' ', $struct_tmp);
  178. }
  179. $sql .= implode(",\r\n", $structure);
  180. return $sql;
  181. }
  182. }
  183. ?>