|
| 1 | +function sqlStrToGoTemplate(str) { |
| 2 | + // CREATE TABLE `(.*)`\s\(([\d\D]*)\)\sENGINE=(.*)\sAUTO_INCREMENT=(\d+)\s(\w+)\sCHARSET=(\w+)\s(COLLATE=\w+)* |
| 3 | + const regex = /CREATE TABLE `(.*)`\s\(([\d\D]*)\)\sENGINE=(.*)\sAUTO_INCREMENT=(\d+)\s(\w+)\sCHARSET=(\w+)\s(COLLATE=\w+)*/gm; |
| 4 | + // `(.*)`\s(\w+[\((\d+)\)]*[\sunsigned\s]*)([NOT\s[NULL\s]*]*)(DEFAULT\s([\'](.*?)[\']|NULL)\s)*(AUTO_INCREMENT\s)*(COMMENT\s[\'](.*)[\'])* |
| 5 | + // const columnRegex = /`(.*)`\s(\w+[\((\d+)\)]*[\sunsigned\s]*)([NOT\s[NULL\s]*]*)(DEFAULT\s([\'](.*?)[\']|NULL)\s)*(AUTO_INCREMENT\s)*(COMMENT\s[\'](.*)[\'])*/gm; |
| 6 | + let m; |
| 7 | + const data = {}; |
| 8 | + while ((m = regex.exec(str)) !== null) { |
| 9 | + // This is necessary to avoid infinite loops with zero-width matches |
| 10 | + if (m.index === regex.lastIndex) { |
| 11 | + regex.lastIndex++; |
| 12 | + } |
| 13 | + |
| 14 | + // The result can be accessed through the `m`-variable. |
| 15 | + m.forEach((match, groupIndex) => { |
| 16 | + data[groupIndex] = match; |
| 17 | + }); |
| 18 | + } |
| 19 | + return sqlStrColumnToEachColumn(data); |
| 20 | +} |
| 21 | + |
| 22 | +function sqlStrColumnToEachColumn(data) { |
| 23 | + const regex = /`(.*)`\s(\w+[\((\d+)\)]*[\sunsigned\s]*)([NOT\s[NULL\s]*]*)(DEFAULT\s([\'](.*?)[\']|NULL)\s)*(AUTO_INCREMENT\s)*(COMMENT\s[\'](.*)[\'])*/gm; |
| 24 | + console.log(data); |
| 25 | + const tableName = data[1]; |
| 26 | + const str = data[2]; |
| 27 | + const engine = data[3]; |
| 28 | + const incrasement = data[4]; |
| 29 | + const dafaultCharset = data[6]; |
| 30 | + const columns = []; |
| 31 | + let m; |
| 32 | + while ((m = regex.exec(str)) !== null) { |
| 33 | + // This is necessary to avoid infinite loops with zero-width matches |
| 34 | + if (m.index === regex.lastIndex) { |
| 35 | + regex.lastIndex++; |
| 36 | + } |
| 37 | + const one = {}; |
| 38 | + // The result can be accessed through the `m`-variable. |
| 39 | + m.forEach((match, groupIndex, arr) => { |
| 40 | + one[groupIndex] = match; |
| 41 | + }); |
| 42 | + columns.push(one); |
| 43 | + } |
| 44 | + console.log(columns); |
| 45 | +} |
| 46 | + |
| 47 | +export { |
| 48 | + sqlStrToGoTemplate, |
| 49 | + sqlStrColumnToEachColumn, |
| 50 | +}; |
0 commit comments