PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/DataTypes/MySQL/MySQL2Obj.php

https://github.com/jeffreytgilbert/Db2Obj
PHP | 182 lines | 144 code | 28 blank | 10 comment | 39 complexity | cd1b046cc5318fe585d532e70c4fe3ab MD5 | raw file
  1. <?php
  2. require_once( dirname(__FILE__).'/../../Db2Obj/SQL2Obj.php' );
  3. class MySQL2Obj extends SQL2Obj{
  4. public function convertToStandardType($type){
  5. switch(strtolower($type)){
  6. case 'datetime': return 'datetime';
  7. case 'date': return 'date';
  8. case 'timestamp': return 'timestamp';
  9. case 'bool':
  10. case 'boolean': return 'boolean';
  11. case 'integer':
  12. case 'int':
  13. case 'serial':
  14. case 'bigint':
  15. case 'mediumint':
  16. case 'tinyint':
  17. case 'smallint': return 'integer';
  18. case 'enum':
  19. case 'char':
  20. case 'varchar':
  21. case 'text': return 'string';
  22. case 'blob': // is clob supported? i forget. when i get online i'll make this extensive
  23. case 'binary': return 'binary';
  24. case 'set': return 'array';
  25. case 'decimal':
  26. case 'dec':
  27. case 'float':
  28. case 'double precision':
  29. case 'double': return 'double';
  30. }
  31. }
  32. public function parseToDataObj(){
  33. $file_buffer = strtolower($this->_file_buffer);
  34. $file_buffer = str_replace("\n", ' ', $file_buffer);
  35. $file_buffer = str_replace("\t", ' ', $file_buffer);
  36. $file_buffer = preg_replace('/\s+/si', " ", $file_buffer);
  37. $tables = explode('create table ', $file_buffer);
  38. // first ones throw away since all table definitions start with "create table" and this is exploding by it
  39. array_shift($tables);
  40. $db = array(); // empty table array
  41. foreach($tables as $table){
  42. $TableDef = new Table();
  43. trim($table);
  44. preg_match('/`(.*?)`/', $table, $matches);
  45. $TableDef->name = trim($matches[1]);
  46. unset($matches);
  47. $column_pieces = explode('`'.$TableDef->name.'`', $table);
  48. $table_columns = $column_pieces[1];
  49. $columns = explode(',', $table_columns);
  50. foreach($columns as $column){
  51. $Col = new Column();
  52. $column = ' '.trim($column).' ';
  53. // strip out things before a ` unless it's UNIQUE or PRIMARY, and then parse those.
  54. if(stripos($column,'unique') == 1){
  55. $keys = explode('`',$column);
  56. foreach($keys as $key){
  57. if(isset($TableDef->columns[$key])){
  58. $TableDef->columns[$key]->unique = true;
  59. $TableDef->unique_keys[] = $key;
  60. }
  61. }
  62. } else if(stripos($column,'fulltext') == 1){
  63. $keys = explode('`',$column);
  64. foreach($keys as $key){
  65. if(isset($TableDef->columns[$key])){
  66. $TableDef->columns[$key]->fulltext = true;
  67. }
  68. }
  69. } else if(stripos($column,'primary') == 1){
  70. $keys = explode('`',$column);
  71. foreach($keys as $key){
  72. if(isset($TableDef->columns[$key])){
  73. $TableDef->columns[$key]->primary = true;
  74. $TableDef->primary_keys[] = $key;
  75. }
  76. }
  77. } else if(stripos($column,'index') == 1){
  78. $keys = explode('`',$column);
  79. foreach($keys as $key){
  80. if(isset($TableDef->columns[$key])){
  81. $TableDef->columns[$key]->index = true;
  82. $TableDef->index_keys[] = $key;
  83. }
  84. }
  85. } else {
  86. $original_column = $column;
  87. // do appropriate stuff
  88. $column = preg_replace('/.*?`(.*?)/', '`$1', $column,1);
  89. // thank you mr gregory
  90. preg_match_all('/`([a-zA-Z0-9_]+)`[\s]*([\w]+)[\s]*\(*[\s]*([0-9]*)[\s]*\)*[\s]+([^,]+)./', $column, $matches);
  91. if(!isset($matches[1][0])){
  92. print_r($original_column);
  93. continue;
  94. }
  95. $Col->name = $matches[1][0];
  96. $Col->setType( $this->convertToStandardType($matches[2][0]) );
  97. $size_array = $this->convertToStandardSizeArray($matches[3][0]);
  98. $Col->setSize( $size_array[0], $size_array[1] );
  99. $column = ' '.trim($matches[4][0]).' ';
  100. unset($matches);
  101. if(substr($Col->name,-3) == '_id'){
  102. $TableDef->possible_foreign_keys[] = $Col->name;
  103. }
  104. // match for binary, unsigned, or unsigned zerofill for attributes
  105. if(stripos($column,'unsigned zerofill')){
  106. $Col->setAttributes('unsigned zerofill');
  107. } else if(stripos($column,'')){
  108. $Col->setAttributes('unsigned');
  109. } else if(stripos($column,'binary')){
  110. $Col->setAttributes('binary');
  111. }
  112. // match "null" or "not null" and define the nullable attribute
  113. if(stripos($column,'not null')){
  114. $Col->nullable = false;
  115. } else {
  116. $Col->nullable = true;
  117. }
  118. // match primary, unique, index, or fulltext for index
  119. if(stripos($column,' primary')){
  120. $Col->primary = true;
  121. $TableDef->primary_keys[] = $Col->name;
  122. } else if(stripos($column,' unique')){
  123. $Col->unique = true;
  124. $TableDef->unique_keys[] = $Col->name;
  125. } else if(stripos($column,' fulltext')){
  126. $Col->fulltext = true;
  127. } else if(stripos($column,' index')){
  128. $Col->index = true;
  129. $TableDef->index_keys[] = $Col->name;
  130. }
  131. // match auto_increment for itself
  132. if(stripos($column,' auto_increment')){
  133. $Col->auto_increment = true;
  134. }
  135. // match COMMENT '*' for comments
  136. preg_match('/\'(.*?)\'/', $column, $matches);
  137. $Col->comments = isset($matches[1])?trim($matches[1]):null;
  138. unset($matches);
  139. $Col->extra = $column;
  140. // Look at this part
  141. if(trim($column) == '') echo "Failed: ".$original_column."\n";
  142. $TableDef->columns[$Col->name] = $Col;
  143. }
  144. foreach($TableDef->possible_foreign_keys as $fk_guess){
  145. if(in_array($fk_guess, $TableDef->index_keys) || in_array($fk_guess, $TableDef->primary_keys) || in_array($fk_guess, $TableDef->unique_keys)){
  146. $TableDef->foreign_keys[] = $fk_guess;
  147. }
  148. }
  149. }
  150. $db[] = $TableDef;
  151. }
  152. $this->_db = $db;
  153. }
  154. }