-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcolumnDefinitionHelper.js
More file actions
131 lines (108 loc) · 3.55 KB
/
columnDefinitionHelper.js
File metadata and controls
131 lines (108 loc) · 3.55 KB
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
const _ = require('lodash');
module.exports = wrap => {
const addLength = (type, length) => {
return `${type}(${length})`;
};
const addScalePrecision = (type, precision, scale) => {
if (_.isNumber(scale)) {
return `${type}(${precision},${scale})`;
} else {
return `${type}(${precision})`;
}
};
const addPrecision = (type, precision) => {
return `${type}(${precision})`;
};
const canHaveLength = type => ['CHAR', 'VARCHAR', 'BINARY', 'CHAR BYTE', 'VARBINARY', 'BLOB'].includes(type);
const isNumeric = type =>
[
'TINYINT',
'SMALLINT',
'MEDIUMINT',
'INT',
'INTEGER',
'BIGINT',
'INT1',
'INT2',
'INT3',
'INT4',
'INT8',
'FLOAT',
'DOUBLE',
'REAL',
'DECIMAL',
'DEC',
'NUMERIC',
'FIXED',
'NUMBER',
'DOUBLE PRECISION',
'BIT',
].includes(type);
const canHavePrecision = type => isNumeric(type);
const canHaveMicrosecondPrecision = type => ['TIME', 'DATETIME', 'TIMESTAMP'].includes(type);
const canHaveScale = type =>
['DECIMAL', 'FLOAT', 'DOUBLE', 'DEC', 'FIXED', 'NUMERIC', 'NUMBER', 'DOUBLE PRECISION', 'REAL'].includes(type);
const decorateType = (type, columnDefinition) => {
if (canHaveLength(type) && _.isNumber(columnDefinition.length)) {
return addLength(type, columnDefinition.length);
} else if (canHavePrecision(type) && canHaveScale(type) && _.isNumber(columnDefinition.precision)) {
return addScalePrecision(type, columnDefinition.precision, columnDefinition.scale);
} else if (canHavePrecision(type) && _.isNumber(columnDefinition.precision)) {
return addPrecision(type, columnDefinition.precision);
} else if (canHaveMicrosecondPrecision(type) && _.isNumber(columnDefinition.microSecPrecision)) {
return addPrecision(type, columnDefinition.microSecPrecision);
} else if (['ENUM', 'SET'].includes(type) && !_.isEmpty(columnDefinition.enum)) {
return `${type}('${columnDefinition.enum.join("', '")}')`;
}
return type;
};
const isString = type =>
['CHAR', 'VARCHAR', 'TEXT', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT'].includes(_.toUpper(type));
const isDateTime = type => ['TIME', 'DATE', 'DATETIME', 'TIMESTAMP'].includes(type);
const escapeQuotes = str => _.trim(str).replace(/(\')+/g, "'$1");
const decorateDefault = (type, defaultValue) => {
const defaultValuesRegExp =
/^(null|current_timestamp(\(\d+\))?)(\s+on\s+update\s+current_timestamp(\(\d+\))?)?$/i;
if ((isString(type) || isDateTime(type)) && !defaultValuesRegExp.test(_.trim(defaultValue))) {
return wrap(escapeQuotes(defaultValue));
} else {
return defaultValue;
}
};
const canBeNational = type => {
return ['CHAR', 'VARCHAR'].includes(type);
};
const getSign = (type, signed) => {
if (!isNumeric(type)) {
return '';
}
if (signed === false) {
return ' UNSIGNED';
}
return '';
};
const createGeneratedColumn = generatedDefaultValue => {
if (!generatedDefaultValue) {
return '';
}
if (!generatedDefaultValue.expression) {
return '';
}
const storage = _.toUpper(generatedDefaultValue.generated_storage || '');
const generatedType = generatedDefaultValue.generatedType === 'always' ? 'GENERATED ALWAYS' : '';
return ' ' + [generatedType, `AS (${generatedDefaultValue.expression})`, storage].filter(Boolean).join(' ');
};
const canHaveAutoIncrement = type => {
const typesAllowedToHaveAutoIncrement = ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'];
return typesAllowedToHaveAutoIncrement.includes(type);
};
return {
decorateType,
decorateDefault,
canBeNational,
isNumeric,
getSign,
createGeneratedColumn,
canHaveAutoIncrement,
};
};