2018-10-15 09:43:57 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
/**
|
|
|
|
* Returns a set of allowed values defined on table scheme
|
|
|
|
* @param {String} column - Model or Table column name
|
|
|
|
* @return {Array} - Array of set values
|
|
|
|
*/
|
|
|
|
Self.getSetValues = async function(column) {
|
|
|
|
let model = this.app.models[this.modelName].definition;
|
|
|
|
let properties = model.properties;
|
|
|
|
let columnName;
|
2018-12-19 07:42:07 +00:00
|
|
|
let tableName = this.modelName;
|
|
|
|
let schema = null;
|
2018-10-15 09:43:57 +00:00
|
|
|
|
2018-12-19 07:42:07 +00:00
|
|
|
if (model.settings && model.settings.mysql) {
|
|
|
|
let tableSplit = model.settings.mysql.table.split('.');
|
|
|
|
tableName = tableSplit.pop();
|
|
|
|
schema = tableSplit.pop() || null;
|
|
|
|
}
|
2018-10-15 09:43:57 +00:00
|
|
|
|
|
|
|
if (properties[column]) {
|
|
|
|
columnName = column;
|
|
|
|
|
|
|
|
if (properties[column].mysql)
|
|
|
|
columnName = properties[column].mysql.columnName;
|
|
|
|
}
|
|
|
|
|
|
|
|
let findColumn = Object.keys(properties).find(prop => {
|
|
|
|
return properties[prop].mysql && properties[prop].mysql.columnName === column;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (findColumn)
|
|
|
|
columnName = properties[findColumn].mysql.columnName;
|
|
|
|
|
2018-12-19 07:42:07 +00:00
|
|
|
let type = await this.rawSql(
|
|
|
|
`SELECT DISTINCT column_type FROM information_schema.columns
|
2018-10-15 09:43:57 +00:00
|
|
|
WHERE table_name = ?
|
2018-12-19 07:42:07 +00:00
|
|
|
AND table_schema = IFNULL(?, DATABASE())
|
|
|
|
AND column_name = ?`,
|
|
|
|
[tableName, schema, columnName]
|
|
|
|
);
|
2018-10-15 09:43:57 +00:00
|
|
|
|
|
|
|
if (!type) return;
|
|
|
|
|
|
|
|
let setValues;
|
|
|
|
setValues = type[0].column_type;
|
|
|
|
setValues = setValues.replace(/set\((.*)\)/i, '$1');
|
|
|
|
setValues = setValues.replace(/'/g, '');
|
|
|
|
setValues = setValues.match(new RegExp(/(\w+)+/, 'ig'));
|
|
|
|
|
|
|
|
let values = [];
|
|
|
|
setValues.forEach(setValue => {
|
|
|
|
values.push({value: setValue});
|
|
|
|
});
|
|
|
|
|
|
|
|
return values;
|
|
|
|
};
|
|
|
|
};
|