2022-06-17 09:01:34 -04:00
|
|
|
import Component from "@ember/component";
|
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
|
|
|
import discourseDebounce from "discourse-common/lib/debounce";
|
|
|
|
import { isBlank, isEmpty } from "@ember/utils";
|
2016-02-17 17:17:43 -05:00
|
|
|
|
2022-06-17 09:01:34 -04:00
|
|
|
export default Component.extend({
|
2015-07-14 12:44:42 -04:00
|
|
|
actions: {
|
|
|
|
collapseSchema() {
|
2018-10-10 07:56:23 -04:00
|
|
|
this.set("hideSchema", true);
|
2020-09-04 07:23:11 -04:00
|
|
|
},
|
2015-07-14 12:44:42 -04:00
|
|
|
},
|
|
|
|
|
2022-06-17 09:01:34 -04:00
|
|
|
@discourseComputed("schema")
|
2019-07-16 06:46:32 -04:00
|
|
|
transformedSchema(schema) {
|
2015-07-08 16:45:13 -04:00
|
|
|
for (let key in schema) {
|
|
|
|
if (!schema.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:23:11 -04:00
|
|
|
schema[key].forEach((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;
|
2019-07-16 06:46:32 -04:00
|
|
|
},
|
2015-07-08 16:45:13 -04:00
|
|
|
|
2022-06-17 09:01:34 -04:00
|
|
|
@discourseComputed("filter")
|
2019-07-16 06:46:32 -04:00
|
|
|
rfilter(filter) {
|
2022-06-17 09:01:34 -04:00
|
|
|
if (!isBlank(filter)) {
|
2019-07-16 06:46:32 -04:00
|
|
|
return new RegExp(filter);
|
2015-07-08 16:45:13 -04:00
|
|
|
}
|
2019-07-16 06:46:32 -04:00
|
|
|
},
|
2015-07-08 16:45:13 -04:00
|
|
|
|
2019-07-16 06:46:32 -04:00
|
|
|
filterTables(schema) {
|
2015-07-08 16:45:13 -04:00
|
|
|
let tables = [];
|
2019-07-16 06:46:32 -04:00
|
|
|
const filter = this.rfilter,
|
2015-07-08 16:45:13 -04:00
|
|
|
haveFilter = !!filter;
|
|
|
|
|
|
|
|
for (let key in schema) {
|
|
|
|
if (!schema.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!haveFilter) {
|
|
|
|
tables.push({
|
|
|
|
name: key,
|
|
|
|
columns: schema[key],
|
2020-09-04 07:23:11 -04:00
|
|
|
open: false,
|
2015-07-08 16:45:13 -04:00
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the table name vs the filter
|
2017-08-02 04:19:38 -04:00
|
|
|
if (filter.source === key || filter.source + "s" === key) {
|
2015-07-08 16:45:13 -04:00
|
|
|
tables.unshift({
|
|
|
|
name: key,
|
|
|
|
columns: schema[key],
|
2020-09-04 07:23:11 -04:00
|
|
|
open: haveFilter,
|
2015-07-08 16:45:13 -04:00
|
|
|
});
|
|
|
|
} else if (filter.test(key)) {
|
|
|
|
// whole table matches
|
|
|
|
tables.push({
|
|
|
|
name: key,
|
|
|
|
columns: schema[key],
|
2020-09-04 07:23:11 -04:00
|
|
|
open: haveFilter,
|
2015-07-08 16:45:13 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// filter the columns
|
|
|
|
let filterCols = [];
|
2020-09-04 07:23:11 -04:00
|
|
|
schema[key].forEach((col) => {
|
2017-08-02 04:19:38 -04:00
|
|
|
if (filter.source === col.column_name) {
|
2015-07-08 16:45:13 -04:00
|
|
|
filterCols.unshift(col);
|
|
|
|
} else if (filter.test(col.column_name)) {
|
|
|
|
filterCols.push(col);
|
|
|
|
}
|
|
|
|
});
|
2022-06-17 09:01:34 -04:00
|
|
|
if (!isEmpty(filterCols)) {
|
2015-07-08 16:45:13 -04:00
|
|
|
tables.push({
|
|
|
|
name: key,
|
|
|
|
columns: filterCols,
|
2020-09-04 07:23:11 -04:00
|
|
|
open: haveFilter,
|
2015-07-08 16:45:13 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tables;
|
|
|
|
},
|
|
|
|
|
2019-07-16 06:46:32 -04:00
|
|
|
@observes("filter")
|
2021-01-05 11:09:21 -05:00
|
|
|
triggerFilter() {
|
2022-06-17 09:01:34 -04:00
|
|
|
discourseDebounce(
|
2021-01-05 11:09:21 -05:00
|
|
|
this,
|
|
|
|
function () {
|
|
|
|
this.set("filteredTables", this.filterTables(this.transformedSchema));
|
|
|
|
this.set("loading", false);
|
|
|
|
},
|
|
|
|
500
|
|
|
|
);
|
|
|
|
},
|
2015-07-08 16:45:13 -04:00
|
|
|
|
2019-07-16 06:46:32 -04:00
|
|
|
@observes("filter")
|
|
|
|
setLoading() {
|
2018-10-10 07:56:23 -04:00
|
|
|
this.set("loading", true);
|
2019-07-16 06:46:32 -04:00
|
|
|
},
|
2015-07-08 16:45:13 -04:00
|
|
|
|
2016-11-24 15:31:05 -05:00
|
|
|
init() {
|
2019-07-16 06:46:32 -04:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2018-10-10 07:56:23 -04:00
|
|
|
this.set("loading", true);
|
2016-11-24 15:31:05 -05:00
|
|
|
this.triggerFilter();
|
2020-09-04 07:23:11 -04:00
|
|
|
},
|
2015-07-08 16:45:13 -04:00
|
|
|
});
|