feat(MySQL): ENUM and SET fields support, closes #61

This commit is contained in:
Fabio Di Stasio 2021-05-04 21:50:41 +02:00
parent 9dfe7cca22
commit bebba64d06
4 changed files with 55 additions and 15 deletions

View File

@ -277,13 +277,6 @@ module.exports = [
{ {
group: 'other', group: 'other',
types: [ types: [
{
name: 'UNKNOWN',
length: false,
collation: false,
unsigned: false,
zerofill: false
},
{ {
name: 'ENUM', name: 'ENUM',
length: true, length: true,
@ -299,5 +292,17 @@ module.exports = [
zerofill: false zerofill: false
} }
] ]
},
{
group: 'unknown',
types: [
{
name: 'UNKNOWN',
length: false,
collation: false,
unsigned: false,
zerofill: false
}
]
} }
]; ];

View File

@ -310,6 +310,9 @@ export class MySQLClient extends AntaresCore {
return rows.map(field => { return rows.map(field => {
let numLength = field.COLUMN_TYPE.match(/int\(([^)]+)\)/); let numLength = field.COLUMN_TYPE.match(/int\(([^)]+)\)/);
numLength = numLength ? +numLength.pop() : null; numLength = numLength ? +numLength.pop() : null;
const enumValues = /(enum|set)/.test(field.COLUMN_TYPE)
? field.COLUMN_TYPE.match(/\(([^)]+)\)/)[0].slice(1, -1)
: null;
return { return {
name: field.COLUMN_NAME, name: field.COLUMN_NAME,
@ -319,6 +322,7 @@ export class MySQLClient extends AntaresCore {
table: field.TABLE_NAME, table: field.TABLE_NAME,
numPrecision: field.NUMERIC_PRECISION, numPrecision: field.NUMERIC_PRECISION,
numLength, numLength,
enumValues,
datePrecision: field.DATETIME_PRECISION, datePrecision: field.DATETIME_PRECISION,
charLength: field.CHARACTER_MAXIMUM_LENGTH, charLength: field.CHARACTER_MAXIMUM_LENGTH,
nullable: field.IS_NULLABLE.includes('YES'), nullable: field.IS_NULLABLE.includes('YES'),
@ -1116,7 +1120,7 @@ export class MySQLClient extends AntaresCore {
// CHANGE FIELDS // CHANGE FIELDS
changes.forEach(change => { changes.forEach(change => {
const typeInfo = this._getTypeInfo(change.type); const typeInfo = this._getTypeInfo(change.type);
const length = typeInfo.length ? change.numLength || change.charLength || change.datePrecision : false; const length = typeInfo.length ? change.enumValues || change.numLength || change.charLength || change.datePrecision : false;
alterColumns.push(`CHANGE COLUMN \`${change.orgName}\` \`${change.name}\` alterColumns.push(`CHANGE COLUMN \`${change.orgName}\` \`${change.name}\`
${change.type.toUpperCase()}${length ? `(${length})` : ''} ${change.type.toUpperCase()}${length ? `(${length})` : ''}
@ -1190,7 +1194,7 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async truncateTable (params) { async truncateTable (params) {
const sql = `TRUNCATE TABLE \`${params.table}\``; const sql = `TRUNCATE TABLE \`${this._schema}\`.\`${params.table}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -1201,7 +1205,7 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async dropTable (params) { async dropTable (params) {
const sql = `DROP TABLE \`${params.table}\``; const sql = `DROP TABLE \`${this._schema}\`.\`${params.table}\``;
return await this.raw(sql); return await this.raw(sql);
} }

View File

@ -98,6 +98,15 @@
> >
{{ localLength }} {{ localLength }}
</span> </span>
<input
v-else-if="localRow.enumValues"
ref="editField"
v-model="editingContent"
type="text"
autofocus
class="editable-field px-2"
@blur="editOFF"
>
<input <input
v-else v-else
ref="editField" ref="editField"
@ -352,7 +361,7 @@ export default {
getWorkspace: 'workspaces/getWorkspace' getWorkspace: 'workspaces/getWorkspace'
}), }),
localLength () { localLength () {
return this.localRow.numLength || this.localRow.charLength || this.localRow.datePrecision || this.localRow.numPrecision || 0; return this.localRow.enumValues || this.localRow.numLength || this.localRow.charLength || this.localRow.datePrecision || this.localRow.numPrecision || 0;
}, },
fieldType () { fieldType () {
const fieldType = this.dataTypes.reduce((acc, group) => [...acc, ...group.types], []).filter(type => const fieldType = this.dataTypes.reduce((acc, group) => [...acc, ...group.types], []).filter(type =>
@ -458,14 +467,21 @@ export default {
editON (event, content, field) { editON (event, content, field) {
if (field === 'length') { if (field === 'length') {
if (['integer', 'float', 'binary', 'spatial'].includes(this.fieldType.group)) this.editingField = 'numLength'; if (['integer', 'float', 'binary', 'spatial'].includes(this.fieldType.group)) this.editingField = 'numLength';
if (['string', 'other'].includes(this.fieldType.group)) this.editingField = 'charLength'; else if (['string', 'unknown'].includes(this.fieldType.group)) this.editingField = 'charLength';
if (['time'].includes(this.fieldType.group)) this.editingField = 'datePrecision'; else if (['other'].includes(this.fieldType.group)) this.editingField = 'enumValues';
else if (['time'].includes(this.fieldType.group)) this.editingField = 'datePrecision';
} }
else else
this.editingField = field; this.editingField = field;
if (this.localRow.enumValues && field === 'length') {
this.editingContent = this.localRow.enumValues;
this.originalContent = this.localRow.enumValues;
}
else {
this.editingContent = content; this.editingContent = content;
this.originalContent = content; this.originalContent = content;
}
const obj = { [field]: true }; const obj = { [field]: true };
this.isInlineEditor = { ...this.isInlineEditor, ...obj }; this.isInlineEditor = { ...this.isInlineEditor, ...obj };

View File

@ -43,6 +43,16 @@
<option>true</option> <option>true</option>
<option>false</option> <option>false</option>
</select> </select>
<select
v-else-if="enumArray"
v-model="editingContent"
class="form-select small-select editable-field"
@blur="editOFF"
>
<option v-for="value in enumArray" :key="value">
{{ value }}
</option>
</select>
<input <input
v-else v-else
ref="editField" ref="editField"
@ -336,6 +346,11 @@ export default {
} }
return false; return false;
},
enumArray () {
if (this.fields[this.editingField].enumValues)
return this.fields[this.editingField].enumValues.replaceAll('\'', '').split(',');
return false;
} }
}, },
watch: { watch: {