2016-02-17 17:17:43 -05:00
|
|
|
import debounce from 'discourse/lib/debounce';
|
|
|
|
|
2015-07-08 16:45:13 -04:00
|
|
|
export default Ember.Component.extend({
|
|
|
|
|
2015-07-14 12:44:42 -04:00
|
|
|
actions: {
|
|
|
|
expandSchema() {
|
|
|
|
this.set('hideSchema', false);
|
|
|
|
},
|
|
|
|
collapseSchema() {
|
|
|
|
this.set('hideSchema', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-08 16:45:13 -04:00
|
|
|
transformedSchema: function() {
|
|
|
|
const schema = this.get('schema');
|
|
|
|
|
|
|
|
for (let key in schema) {
|
|
|
|
if (!schema.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
schema[key].forEach(function(col) {
|
2015-07-28 14:18:22 -04:00
|
|
|
const notes_components = [];
|
|
|
|
if (col.primary) {
|
|
|
|
notes_components.push("primary key");
|
|
|
|
}
|
2015-07-08 16:45:13 -04:00
|
|
|
if (col.is_nullable) {
|
2015-07-28 14:18:22 -04:00
|
|
|
notes_components.push("null");
|
2015-07-08 16:45:13 -04:00
|
|
|
}
|
|
|
|
if (col.column_default) {
|
2015-07-28 14:18:22 -04:00
|
|
|
notes_components.push("default " + col.column_default);
|
2015-07-08 16:45:13 -04:00
|
|
|
}
|
2015-07-28 12:59:26 -04:00
|
|
|
if (col.fkey_info) {
|
2015-07-28 14:18:22 -04:00
|
|
|
notes_components.push("fkey " + col.fkey_info);
|
|
|
|
}
|
|
|
|
if (col.denormal) {
|
|
|
|
notes_components.push("denormal " + col.denormal);
|
2015-07-28 12:59:26 -04:00
|
|
|
}
|
2015-07-28 14:18:22 -04:00
|
|
|
const notes = notes_components.join(", ");
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2015-07-08 16:45:13 -04:00
|
|
|
if (notes) {
|
|
|
|
col.notes = notes;
|
|
|
|
}
|
2015-07-09 18:46:19 -04:00
|
|
|
|
2015-07-28 12:59:26 -04:00
|
|
|
if (col.enum || col.column_desc) {
|
|
|
|
col.havepopup = true;
|
2015-07-09 18:46:19 -04:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:59:26 -04:00
|
|
|
col.havetypeinfo = !!(col.notes || col.enum || col.column_desc);
|
|
|
|
|
2015-07-08 16:45:13 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return schema;
|
|
|
|
}.property('schema'),
|
|
|
|
|
|
|
|
rfilter: function() {
|
|
|
|
if (!Em.isBlank(this.get('filter'))) {
|
|
|
|
return new RegExp(this.get('filter'));
|
|
|
|
}
|
|
|
|
}.property('filter'),
|
|
|
|
|
|
|
|
filterTables: function(schema) {
|
|
|
|
let tables = [];
|
|
|
|
const filter = this.get('rfilter'),
|
|
|
|
haveFilter = !!filter;
|
|
|
|
|
|
|
|
for (let key in schema) {
|
|
|
|
if (!schema.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!haveFilter) {
|
|
|
|
tables.push({
|
|
|
|
name: key,
|
|
|
|
columns: schema[key],
|
2015-07-08 19:46:36 -04:00
|
|
|
open: false
|
2015-07-08 16:45:13 -04:00
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the table name vs the filter
|
|
|
|
if (filter.source == key || filter.source + "s" == key) {
|
|
|
|
tables.unshift({
|
|
|
|
name: key,
|
|
|
|
columns: schema[key],
|
|
|
|
open: haveFilter
|
|
|
|
});
|
|
|
|
} else if (filter.test(key)) {
|
|
|
|
// whole table matches
|
|
|
|
tables.push({
|
|
|
|
name: key,
|
|
|
|
columns: schema[key],
|
|
|
|
open: haveFilter
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// filter the columns
|
|
|
|
let filterCols = [];
|
|
|
|
schema[key].forEach(function(col) {
|
|
|
|
if (filter.source == col.column_name) {
|
|
|
|
filterCols.unshift(col);
|
|
|
|
} else if (filter.test(col.column_name)) {
|
|
|
|
filterCols.push(col);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!Em.isEmpty(filterCols)) {
|
|
|
|
tables.push({
|
|
|
|
name: key,
|
|
|
|
columns: filterCols,
|
|
|
|
open: haveFilter
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tables;
|
|
|
|
},
|
|
|
|
|
2016-02-17 17:17:43 -05:00
|
|
|
triggerFilter: debounce(function() {
|
2015-07-08 16:45:13 -04:00
|
|
|
this.set('filteredTables', this.filterTables(this.get('transformedSchema')));
|
|
|
|
this.set('loading', false);
|
|
|
|
}, 500).observes('filter'),
|
|
|
|
|
|
|
|
setLoading: function() {
|
|
|
|
this.set('loading', true);
|
|
|
|
}.observes('filter'),
|
|
|
|
|
2016-11-24 15:31:05 -05:00
|
|
|
init() {
|
|
|
|
this._super();
|
|
|
|
this.set('loading', true);
|
|
|
|
this.triggerFilter();
|
|
|
|
}
|
2015-07-08 16:45:13 -04:00
|
|
|
});
|