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

/classes/bors/tools/codegen/sqlt.php

https://bitbucket.org/Balancer/bors-core
PHP | 263 lines | 241 code | 20 blank | 2 comment | 22 complexity | 3ef94a054a88a694f65085560f611f2d MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. class bors_tools_codegen_sqlt extends bors_object
  3. {
  4. var $class_name;
  5. var $view_class_name;
  6. var $admin_class_name;
  7. var $class_title;
  8. var $class_titles;
  9. var $fields;
  10. var $keys = array();
  11. function class_name() { return $this->class_name; }
  12. function class_title() { return $this->class_title; }
  13. function class_title_lower() { return bors_lower($this->class_title); }
  14. function admin_path() { return str_replace('_', '/', $this->class_name); }
  15. function sqlt_parse($text)
  16. {
  17. foreach(explode("\n", $text) as $s)
  18. {
  19. $arg = false;
  20. $skip_class_type = false;
  21. $s = trim($s);
  22. if(!$s)
  23. continue;
  24. if(preg_match('/^#/', $s)) // комментарий
  25. continue;
  26. if(!$this->class_name && preg_match('!^class:\s*(.+?)$!', $s, $m))
  27. {
  28. $this->class_name = $m[1];
  29. continue;
  30. }
  31. if(!$this->admin_class_name && preg_match('!^admin_class:\s*(.+?)$!', $s, $m))
  32. {
  33. $this->admin_class_name = $m[1];
  34. continue;
  35. }
  36. if(!$this->view_class_name && preg_match('!^view_class:\s*(.+?)$!', $s, $m))
  37. {
  38. $this->view_class_name = $m[1];
  39. continue;
  40. }
  41. if(!$this->class_title && preg_match('!^title:\s*(.+?)$!', $s, $m))
  42. {
  43. $this->class_title = $m[1];
  44. continue;
  45. }
  46. if(!$this->class_titles && preg_match('!^titles:\s*(.+?)$!', $s, $m))
  47. {
  48. $this->class_titles = $m[1];
  49. continue;
  50. }
  51. // Комментарии
  52. $field_title = false;
  53. $comment = false;
  54. if(preg_match('!^(.+?)\s*//\s*(.+?)\s* \-\- \s*(.+?)\s*$!', $s, $m))
  55. {
  56. $s = $m[1];
  57. $field_title = $m[2];
  58. $comment = $m[3];
  59. }
  60. elseif(preg_match('!^(.+?)\s*//\s*(.+?)\s*$!', $s, $m))
  61. {
  62. $s = $m[1];
  63. $field_title = $m[2];
  64. }
  65. if(preg_match('/^(\w+):$/', $s, $m))
  66. {
  67. $this->table_name = $m[1];
  68. //TODO: сделать возможность мультитаблиц
  69. continue;
  70. }
  71. if(preg_match('/^(unique)\s+(.+?)$/', $s, $m))
  72. {
  73. $ff = array_map(create_function('$s', 'return trim($s);'), explode(',', $m[2]));
  74. $this->keys[] = 'UNIQUE `'.join('__', $ff).'` (`'.join('`,`', $ff).'`)';
  75. continue;
  76. }
  77. if(preg_match('/^(.+)!$/', $s, $m))
  78. {
  79. $s = trim($m[1]);
  80. $is_index = true;
  81. }
  82. else
  83. $is_index = false;
  84. if(preg_match('/^(.+)latin1$/i', $s, $m))
  85. {
  86. $s = trim($m[1]);
  87. $is_latin1 = true;
  88. }
  89. else
  90. $is_latin1 = false;
  91. if(preg_match('/^(.+)NULL$/', $s, $m))
  92. {
  93. $s = trim($m[1]);
  94. $is_null = true;
  95. }
  96. else
  97. $is_null = false;
  98. if(preg_match('/^(.+)\+\+$/', $s, $m))
  99. {
  100. $s = trim($m[1]);
  101. $is_autoinc = true;
  102. }
  103. else
  104. $is_autoinc = false;
  105. if(preg_match('/^(\w+)\(((\w+)\[\S+?\])\)$/', $s, $m))
  106. {
  107. $class_auto_objects[$m[1]] = $m[3];
  108. $s = $m[2];
  109. }
  110. elseif(preg_match('/^(\w+)\((\w+)\)$/', $s, $m))
  111. {
  112. $class_auto_objects[$m[1]] = $m[2];
  113. $s = $m[2];
  114. }
  115. if(preg_match('/^(\w+)\[(\S+)\]$/', $s, $m))
  116. {
  117. $s = "enum {$m[1]}";
  118. $arg = array();
  119. foreach(explode(',', $m[2]) as $v)
  120. $arg[] = $v[1] == "'" ? $v : "'".addslashes($v)."'";
  121. $arg = join(', ', $arg);
  122. }
  123. if(preg_match('/^\w+_id$/', $s))
  124. $s = $skip_class_type = "int $s";
  125. if(preg_match('/^is_\w+$/', $s))
  126. $s = $skip_class_type = "bool $s";
  127. if(preg_match('/^\w+_date$/', $s))
  128. $s = $skip_class_type = "date $s";
  129. if(preg_match('/^\w+$/', $s))
  130. $s = $skip_class_type = "string $s";
  131. if(preg_match('/^int\s+id$/', $s))
  132. $skip_class_type = true;
  133. if(preg_match('/^(\w+)\s+(\w+)$/', $s, $m))
  134. {
  135. $field_type = strtolower($m[1]);
  136. $field_name = $m[2];
  137. }
  138. else
  139. {
  140. $field_type = NULL;
  141. $field_name = $s;
  142. }
  143. $this->fields[] = array(
  144. 'type' => $field_type,
  145. 'name' => $field_name,
  146. 'field_title' => $field_title,
  147. 'is_null' => $is_null,
  148. 'is_latin1' => $is_latin1,
  149. 'is_autoinc' => $is_autoinc,
  150. 'is_index' => $is_index,
  151. 'arg' => $arg,
  152. 'comment' => $comment,
  153. 'auto_type' => !$skip_class_type,
  154. );
  155. }
  156. }
  157. function make_mysql_create()
  158. {
  159. $map = array(
  160. 'string' => 'VARCHAR(255)',
  161. 'text' => 'TEXT',
  162. 'int' => 'INT',
  163. 'uint' => 'INT UNSIGNED',
  164. 'bool' => 'TINYINT(1) UNSIGNED',
  165. 'float' => 'FLOAT',
  166. 'enum' => 'ENUM(%)',
  167. );
  168. $sql_fields = array();
  169. $keys = $this->keys;
  170. foreach($this->fields as $f)
  171. {
  172. $name = $f['name'];
  173. $sql_type = defval($map, $f['type'], 'VARCHAR(255)');
  174. if($f['arg'])
  175. $sql_type = str_replace("%", $f['arg'], $sql_type);
  176. if($f['is_index'] && $f['is_autoinc'])
  177. $keys[] = 'PRIMARY KEY (`'.$name.'`)';
  178. elseif($f['is_index'])
  179. $keys[] = 'KEY `'.$name.'` (`'.$name.'`)';
  180. $s = '`'.$name.'` '.$sql_type;
  181. if($f['is_latin1'])
  182. $s .= ' CHARACTER SET latin1 COLLATE latin1_general_ci';
  183. if($f['is_null'])
  184. $s .= ' NULL';
  185. else
  186. $s .= ' NOT NULL';
  187. if($f['is_autoinc'])
  188. $s .= ' AUTO_INCREMENT';
  189. $sql_comment = array();
  190. $sql_comment[] = $f['field_title'];
  191. $sql_comment[] = $f['comment'];
  192. if($sql_comment && $sql_comment[0])
  193. $s .= " COMMENT '".addslashes(join('. ', $sql_comment))."'";
  194. $sql_fields[] = $s;
  195. }
  196. $sql = "CREATE TABLE IF NOT EXISTS `".addslashes($this->table_name)."` (\n"
  197. ."\t".join(",\n\t", $sql_fields).",\n"
  198. ."\t".join(",\n\t", $keys)."\n"
  199. .");\n";
  200. return $sql;
  201. }
  202. function make_class()
  203. {
  204. $fields = array();
  205. foreach($this->fields as $f)
  206. {
  207. $fields[] = $f['name']."\n";
  208. }
  209. $this->class_field_names = join("\t\t\t", $fields);
  210. global $self;
  211. $self = $this;
  212. $tpl = file_get_contents('sqlt-class.tpl.nophp');
  213. $tpl = preg_replace_callback('/%(\w+)%/', create_function('$m', 'global $self; return method_exists($self, $m[1]) ? $self->$m[1]() : $self->$m[1];'), $tpl);
  214. return $tpl;
  215. }
  216. }