-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·142 lines (134 loc) · 4.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* code here... */
function getModelName(tableName){
return tableName.charAt(0).toUpperCase()
+tableName.substring(1).replace(/_([a-z])/g, function (g) {
return g[1].toUpperCase(); });
}
function castDBType(type){
if(type.indexOf('int')>-1){
return "int";
}else if(type=='decimal' || type=='float'){
return 'float'
}
return "string"
}
var defaultDDL="CREATE TABLE `slow_log` (\n" +
" `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),\n" +
" `user_host` mediumtext NOT NULL,\n" +
" `query_time` time(6) NOT NULL,\n" +
" `lock_time` time(6) NOT NULL,\n" +
" `rows_sent` int(11) NOT NULL,\n" +
" `rows_examined` int(11) NOT NULL,\n" +
" `db` varchar(512) NOT NULL,\n" +
" `last_insert_id` int(11) NOT NULL,\n" +
" `insert_id` int(11) NOT NULL,\n" +
" `server_id` int(10) unsigned NOT NULL,\n" +
" `sql_text` mediumtext NOT NULL,\n" +
" `thread_id` bigint(21) unsigned NOT NULL\n" +
") ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';"
document.getElementById('run').addEventListener('click',function (){
let ddl=document.getElementById('coding').value || defaultDDL;
let dateFormat="Y-m-d H:i:s";
let tableComment="";
let tableName=/TABLE `(.*)`/.exec(ddl)[1]
let modelName=getModelName(tableName)
let primaryKey=""
let primaryKeyMatch=/PRIMARY KEY \(`(.*)`\)/.exec(ddl)
if(primaryKeyMatch){
primaryKey=primaryKeyMatch[1]
}
let fillable=[]
let casts=[]
let lines=ddl.split("\n")
for (let i = 0; i < lines.length; i++) {
let line=lines[i]
if(/CREATE\sTABLE/.test(line)){
// console.log("表头",line)
}else if(/ENGINE=/.test(line)){
let tableCommentMatch=/COMMENT='(.*)'/.exec(line)
if(tableCommentMatch){
tableComment=`// ${tableCommentMatch[1]}`
}
// console.log("表尾",line,tableComment)
}else if(/PRIMARY\sKEY/.test(line)){
// console.log("主键")
}else {
if(/created_at|updated_at/.test(line)){
let items=/`\s(.*?)\s/.exec(line);
if(items[1]=='timestamp'){
dateFormat="U";
}
}else {
let items=/`(.*)`\s(.*)(\s|\()/.exec(line)
let field_name=items[1]
// 主键不需要处理
if(field_name==primaryKey){
continue
}
// cast
let field_type=castDBType(items[2])
if(field_type!='string'){
casts.push(`"${field_name}"=>"${field_type}",`)
}
/**
* fillable
*/
//comment
let remark=""
let field_comment_match=/COMMENT\s'(.*)'/.exec(line)
if(field_comment_match){
remark+=`${field_comment_match[1]};`
}
// default value
let field_default_value_match=/DEFAULT\s(.*?)(\s|\,)/.exec(line);
if(field_default_value_match){
remark+=`default ${field_default_value_match[1]}`
}
if(remark){
remark=`, // `+remark
}
fillable.push(`'${field_name}'${remark}`)
}
}
}
fillable=fillable.join("\n\t\t")
casts=casts.join("\n\t\t")
var result=`
<?php
namespace App\\Model;
use Illuminate\\Database\\Eloquent\\Model;
${tableComment}
class ${modelName} extends Model{
\tprotected $connection='';
\tprotected $table='${tableName}';
\tprotected $primaryKey='${primaryKey}';
\tpublic $dateFormat='U';
\tprotected $fillable=[
\t\t${fillable}
\t];
\tprotected $casts=[
\t\t${casts}
\t];
}`
result=result.replace(/>/g,'>')
result=result.replace(/</g,'<')
document.getElementById('result').innerHTML=result
hljs.configure({tabReplace: ''});
hljs.highlightAll();
})
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById('coding').setAttribute('placeholder',defaultDDL)
let clipboard=new ClipboardJS('.btn-copy');
clipboard.on('success', function(e) {
document.getElementById('toast').style.animation="spin1 0.6s linear";
e.clearSelection();
setTimeout(function (){
document.getElementById('toast').style.animation=""
},600)
});
clipboard.on('error', function(e) {
console.log(e)
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});
});