Skip to content

Commit b25e073

Browse files
authored
Merge pull request #7 from zhimin-dev/vite
fix sql to model bug
2 parents 4d7c26e + d4e5500 commit b25e073

4 files changed

Lines changed: 76 additions & 40 deletions

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "dev-tool",
33
"description": "a dev-tool for chrome extension",
4-
"version": "2.0.1",
4+
"version": "2.0.2",
55
"manifest_version": 3,
66
"permissions": [
77
"alarms",

src/components/SqlToModel.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="show-go" v-if="goStruct !== ''">
1414
<el-input
1515
type="textarea"
16-
autosize="true"
16+
:autosize="true"
1717
placeholder="显示的GoStruct"
1818
v-model="goStruct"
1919
></el-input>

src/components/Time.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ export default {
5555
},
5656
created() {
5757
setInterval(() => {
58-
navigator.clipboard
59-
.readText()
60-
.then((res) => {
58+
if (document.hasFocus()) {
59+
navigator.clipboard.readText().then((res) => {
6160
if (!this.checkClipboardExist(res)) {
6261
if (parseInt(res) == res) {
6362
this.nowTime = res;
@@ -66,10 +65,8 @@ export default {
6665
}
6766
localStorage.setItem(copyFromClipboard, res);
6867
}
69-
})
70-
.catch((e) => {
71-
console.log(e);
7268
});
69+
}
7370
}, 1000);
7471
7572
let item = localStorage.getItem(lastTimStr);

src/utils/go/common.js

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ function ucFirst(val) {
44

55
function dbNameToCamelCase(name) {
66
if (name === undefined) {
7-
return '';
7+
return "";
88
}
99
const row = [];
10-
const re = name.split('_');
10+
const re = name.split("_");
1111
re.map((val) => {
1212
row.push(ucFirst(val));
1313
});
14-
return row.join('');
14+
return row.join("");
1515
}
1616

1717
function containStr(name, val) {
@@ -22,61 +22,76 @@ function containStr(name, val) {
2222
}
2323

2424
function isTimeType(type) {
25-
if (containStr(type, 'date') || containStr(type, 'datetime') || containStr(type, 'timestamp')) {
25+
if (
26+
containStr(type, "date") ||
27+
containStr(type, "datetime") ||
28+
containStr(type, "timestamp")
29+
) {
2630
return true;
2731
}
2832
return false;
2933
}
3034

3135
function sqlTypeToGoType(type) {
3236
let isContainUsign = false;
33-
if (containStr(type, 'unsigned')) {
37+
if (containStr(type, "unsigned")) {
3438
isContainUsign = true;
3539
}
36-
if (containStr(type, 'varchar') || containStr(type, 'text')) {
37-
return 'string';
40+
if (containStr(type, "varchar") || containStr(type, "text")) {
41+
return "string";
3842
}
39-
if (containStr(type, 'int')) {
43+
if (containStr(type, "int")) {
4044
if (!isContainUsign) {
41-
return 'int64';
45+
return "int64";
4246
}
43-
return 'uint64';
47+
return "uint64";
4448
}
45-
if (containStr(type, 'float')) {
46-
return 'float64';
49+
if (containStr(type, "float")) {
50+
return "float64";
4751
}
48-
if (containStr(type, 'decimal')) {
49-
return 'float64';
52+
if (containStr(type, "decimal")) {
53+
return "float64";
5054
}
5155
if (isTimeType(type)) {
52-
return 'time.Time';
56+
return "time.Time";
5357
}
54-
return 'unknown';
58+
return "unknown";
5559
}
5660

5761
function sqlToModel(data) {
5862
const dbName = data.tableName;
5963
const dbNameStr = dbNameToCamelCase(dbName);
60-
const packageName = 'db';
64+
const dbComment = data.comment;
65+
const packageName = "db";
6166
const importPackages = [];
62-
let importPackageStr = '';
63-
let columsStr = '';
67+
let importPackageStr = "";
68+
let columsStr = "";
6469
let isTime = false;
6570
data.columns.map((val) => {
66-
let defaultStr = '';
67-
if (val.default !== undefined && val.default !== 'NULL' && val.default !== '') {
71+
let defaultStr = "";
72+
if (
73+
val.default !== undefined &&
74+
val.default !== "NULL" &&
75+
val.default !== ""
76+
) {
6877
defaultStr = `;default:${val.default}`;
6978
}
7079
const goType = sqlTypeToGoType(val.type);
7180
if (!isTime && isTimeType(val.type)) {
7281
isTime = true;
7382
}
74-
columsStr += ` ${dbNameToCamelCase(val.name)} ${goType} \`gorm:"column:${val.name}${defaultStr}" json:"${val.name}"\`\n`;
83+
columsStr += ` ${dbNameToCamelCase(val.name)} ${goType} \`gorm:"column:${
84+
val.name
85+
}${defaultStr}" json:"${val.name}"\``;
86+
if (val.comment !== "") {
87+
columsStr += `//${val.comment}`;
88+
}
89+
columsStr += "\n";
7590
});
7691
if (isTime) {
77-
importPackages.push('time');
92+
importPackages.push("time");
7893
}
79-
let importStr = '';
94+
let importStr = "";
8095
if (importPackages.length > 0) {
8196
importPackages.map((val) => {
8297
importPackageStr += `"${val}"\n`;
@@ -93,16 +108,17 @@ type ${dbNameStr}Model struct {
93108
${columsStr}
94109
}
95110
func (*${dbNameStr}Model) TableName() string {
96-
return "${dbName}"
111+
return "${dbName}"//${dbComment}
97112
}
98113
`;
99114
return resultStr;
100115
}
101116

102117
// 解析sql表明等基础信息
103118
function sqlStrToGoTemplate(str) {
104-
// CREATE TABLE `(.*)`\s\(\s((.*\s)*)\)\s(.*)COMMENT='(.*)'
105-
const regex = /CREATE TABLE `(.*)`\s\(\s((.*\s)*)\)\s(.*)COMMENT='(.*)'/gm;
119+
// CREATE TABLE `(.*)`\s\(\s((.*\s)*)\)\s(ENGINE=\w*\s)?(AUTO_INCREMENT=\d*\s)?(DEFAULT\s)?(CHARSET=\w+\s)?(COLLATE=\w*\s)?(ROW_FORMAT\=DYNAMIC\s)?(COMMENT='(.*)')?
120+
const regex =
121+
/CREATE TABLE `(.*)`\s\(\s((.*\s)*)\)\s(ENGINE=\w*\s)?(AUTO_INCREMENT=\d*\s)?(DEFAULT\s)?(CHARSET=\w+\s)?(COLLATE=\w*\s)?(ROW_FORMAT\=DYNAMIC\s)?(COMMENT='(.*)')?/gm;
106122
// `(.*)`\s(\w+[\((\d+)\)]*[\sunsigned\s]*)([NOT\s[NULL\s]*]*)(DEFAULT\s([\'](.*?)[\']|NULL)\s)*(AUTO_INCREMENT\s)*(COMMENT\s[\'](.*)[\'])*
107123
// const columnRegex = /`(.*)`\s(\w+[\((\d+)\)]*[\sunsigned\s]*)([NOT\s[NULL\s]*]*)(DEFAULT\s([\'](.*?)[\']|NULL)\s)*(AUTO_INCREMENT\s)*(COMMENT\s[\'](.*)[\'])*/gm;
108124
let m;
@@ -121,14 +137,25 @@ function sqlStrToGoTemplate(str) {
121137
return sqlStrColumnToEachColumn(data);
122138
}
123139

140+
function getExpValue(str) {
141+
let exp = str.split("=");
142+
if (exp.length >= 2) {
143+
return exp[1].trim();
144+
}
145+
return "";
146+
}
147+
124148
// 解析sql每个字段
125149
function sqlStrColumnToEachColumn(data) {
126-
const regex = /`(.*)`\s(\w+[\((\d+)\)]*[\sunsigned\s]*)([NOT\s[NULL\s]*]*)(DEFAULT\s([\'](.*?)[\']|NULL)\s)*(AUTO_INCREMENT\s)*(COMMENT\s[\'](.*)[\'])*/gm;
150+
const regex =
151+
/`(.*)`\s(\w+[\((\d+)\)]*[\sunsigned\s]*)([NOT\s[NULL\s]*]*)(DEFAULT\s([\'](.*?)[\']|NULL)\s)*(AUTO_INCREMENT\s)*(COMMENT\s[\'](.*)[\'])*/gm;
127152
const tableInfo = {
128153
tableName: data[1],
129-
engine: data[3],
130-
increasement: data[4],
131-
dafaultCharset: data[6],
154+
engine: getExpValue(data[4]),
155+
increasement: getExpValue(data[5]),
156+
dafaultCharset: getExpValue(data[7]),
157+
comment: data[11],
158+
rowFormat: getExpValue(data[9]),
132159
};
133160
const str = data[2];
134161
const columns = [];
@@ -147,11 +174,23 @@ function sqlStrColumnToEachColumn(data) {
147174
}
148175
const columnData = [];
149176
columns.map((column) => {
177+
let _comment = column[9];
178+
if (
179+
_comment === undefined ||
180+
_comment === null ||
181+
_comment === "undefined"
182+
) {
183+
_comment = "";
184+
}
150185
columnData.push({
151-
name: column[1], type: column[2], default: column[6], comment: column[9],
186+
name: column[1],
187+
type: column[2],
188+
default: column[6],
189+
comment: _comment,
152190
});
153191
});
154192
tableInfo.columns = columnData;
193+
console.log("tableInfo---", tableInfo);
155194
return sqlToModel(tableInfo);
156195
}
157196

0 commit comments

Comments
 (0)