Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Support the result table to display results of DESCRIBE/SHOW/DELETE queries #37

Merged
merged 2 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions public/components/Main/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,73 @@ export function getQueryResultsForTable(queryResultsRaw: ResponseDetail<string>[
} else {
let databaseRecords: { [key: string]: any }[] = [];
const responseObj = queryResultResponseDetail.data ? JSON.parse(queryResultResponseDetail.data) : '';
const hits = _.get(responseObj, "hits[hits]", []);
let databaseFields: string[] = [];
let fields: string[] = [];

// the following block is to deal with describe/show/delete query from jdbc results
if (_.has(responseObj, 'schema')) {
const schema: object[] = _.get(responseObj, 'schema');
const datarows: any[][] = _.get(responseObj, 'datarows');
let queryType: string = 'show';

for (const column of schema.values()) {
if (_.isEqual(_.get(column, 'name'), 'deleted_rows')) {
queryType = 'delete';
} else if (_.isEqual(_.get(column, 'name'), 'DATA_TYPE')) {
queryType = 'describe'
}
}
switch (queryType) {
case 'delete':
case 'describe':
for (const [id, field] of schema.entries()) {
fields[id] = _.get(field, 'name');
}
databaseFields = fields;
databaseFields.unshift("id");
for (const [id, datarow] of datarows.entries()) {
let databaseRecord: { [key: string]: any } = {};
databaseRecord['id'] = id;
for (const index of schema.keys()) {
const fieldname = databaseFields[index+1];
databaseRecord[fieldname] = datarow[index];
}
databaseRecords.push(databaseRecord);
}
break;
case 'show':
databaseFields[0] = 'TABLE_NAME';
databaseFields.unshift('id');
let index: number = -1;
for (const [id, field] of schema.entries()) {
if (_.eq(_.get(field, 'name'), 'TABLE_NAME')) {
index = id;
break;
}
}
for (const [id, datarow] of datarows.entries()) {
let databaseRecord: { [key: string]: any } = {};
databaseRecord['id'] = id;
databaseRecord['TABLE_NAME'] = datarow[index];
databaseRecords.push(databaseRecord);
}
break;
default:
let databaseRecord: { [key: string]: any } = {};
databaseRecord['id'] = 'null';
databaseRecords.push(databaseRecord);
}
return {
fulfilled: queryResultResponseDetail.fulfilled,
data: {
fields: databaseFields,
records: databaseRecords,
message: SUCCESS_MESSAGE
}
}
}

// the following block is to deal with aggregation result
if (_.has(responseObj, "aggregations")) {
const aggregations = _.get(responseObj, "aggregations", {});
let databaseRecord: { [key: string]: any } = {};
Expand All @@ -118,6 +182,7 @@ export function getQueryResultsForTable(queryResultsRaw: ResponseDetail<string>[
}
}

const hits = _.get(responseObj, "hits[hits]", []);
if (hits.length > 0) {
databaseFields=(Object.keys(hits[0]["_source"]));
databaseFields.unshift("id");
Expand Down Expand Up @@ -258,6 +323,15 @@ export class Main extends React.Component<MainProps, MainState> {
});
}

isSelectQuery(query: string): boolean {
return _.startsWith(_.trim(query).toLowerCase(), "select");
}

requestedFormat(query: string): string {
if (this.isSelectQuery(query)) return "";
return "jdbc";
}

onRun = (queriesString: string): void => {
const queries: string[] = getQueries(queriesString);

Expand All @@ -266,7 +340,7 @@ export class Main extends React.Component<MainProps, MainState> {
const esRawResponsePromise = Promise.all(
queries.map((query: string) =>
this.httpClient
.post("../api/sql_console/query", {query})
.post("../api/sql_console/query".concat(this.requestedFormat(query)), {query})
.catch((error: any) => {
this.setState({
messages: [
Expand Down
4 changes: 2 additions & 2 deletions server/services/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* permissions and limitations under the License.
*/

export const TRANSLATE_ROUTE = `_opendistro/_sql/_explain`;
export const QUERY_ROUTE = `_opendistro/_sql`;
export const TRANSLATE_ROUTE = `/_opendistro/_sql/_explain`;
export const QUERY_ROUTE = `/_opendistro/_sql`;
export const FORMAT_CSV = `format=csv`;
export const FORMAT_ESRAW = `format=json`;
export const FORMAT_TEXT = `format=raw`;
Expand Down