This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
sql-control.component.ts
256 lines (223 loc) · 7.53 KB
/
sql-control.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* @license
* Copyright 2017 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Input, ViewEncapsulation} from "@angular/core";
import { Component, OnInit } from "@angular/core";
import { LoggerService } from "@core/logger.service";
import { ColumnData } from "@dataservices/shared/column-data.model";
import { DataserviceService } from "@dataservices/shared/dataservice.service";
import { QueryResults } from "@dataservices/shared/query-results.model";
import { RowData } from "@dataservices/shared/row-data.model";
import { LoadingState } from "@shared/loading-state.enum";
import "codemirror/addon/display/placeholder.js";
import "codemirror/addon/selection/active-line.js";
import "codemirror/mode/sql/sql.js";
@Component({
encapsulation: ViewEncapsulation.None,
selector: "app-sql-control",
templateUrl: "./sql-control.component.html",
styleUrls: ["./sql-control.component.css"]
})
export class SqlControlComponent implements OnInit {
@Input() public editorVisible = true;
public columns: any[] = [];
public rows: any[] = [];
public config = {
lineNumbers: true,
mode: "text/x-sql",
placeholder: "Enter the SQL, then click Submit",
styleActiveLine: true,
tabSize: 4,
theme: "mdn-like" };
public customClasses = {
sortAscending: "fa fa-sort-asc",
sortDescending: "fa fa-sort-desc",
pagerLeftArrow: "fa fa-chevron-left",
pagerRightArrow: "fa fa-chevron-right",
pagerPrevious: "fa fa-step-backward",
pagerNext: "fa fa-step-forward"
};
private dataserviceService: DataserviceService;
private logger: LoggerService;
private showResults = false;
private queryResultsLoading: LoadingState;
private queryTxt: string;
private queryResults: QueryResults;
constructor( dataserviceService: DataserviceService, logger: LoggerService ) {
this.dataserviceService = dataserviceService;
this.logger = logger;
}
public ngOnInit(): void {
this.initQueryText();
this.submitCurrentQuery();
}
/*
* Initialize the query text based on the selected dataservice
*/
public initQueryText( ): void {
this.queryTxt = this.getDataserviceInitialQueryText();
}
/*
* Submit the currently entered SQL
*/
public submitCurrentQuery( ): void {
this.submitQuery(this.queryText, this.dataserviceService.getSelectedDataservice().getId(), 15, 0);
}
/*
* Determines if the results table should be shown
*/
public get showResultsTable( ): boolean {
return this.showResults;
}
/**
* Get the SQL text
*/
public get queryText( ): string {
return this.queryTxt;
}
/**
* Set the SQL text
*/
public set queryText( sql: string ) {
this.queryTxt = sql;
}
/**
* Determine if query is running
*/
public get queryRunning( ): boolean {
return ( this.queryResultsLoading != null && (this.queryResultsLoading === LoadingState.LOADING) );
}
/**
* Determine if query has run successfully
*/
public get queryRanValid( ): boolean {
return ( this.queryResultsLoading != null && (this.queryResultsLoading === LoadingState.LOADED_VALID) );
}
/**
* Determine if query has run but was not successful
*/
public get queryRanInvalid( ): boolean {
return ( this.queryResultsLoading != null && (this.queryResultsLoading === LoadingState.LOADED_INVALID) );
}
/*
* the selected data service query text
*/
private getDataserviceInitialQueryText(): string {
const dataservice = this.dataserviceService.getSelectedDataservice();
const modelName = dataservice.getServiceViewModel();
const serviceView = dataservice.getServiceViewName();
if ( !modelName || !serviceView ) {
return "SELECT * FROM ";
}
return "SELECT * FROM " + modelName + "." + serviceView + ";";
}
/*
* Submits the query
*/
private submitQuery(sqlQuery: string, dataserviceName: string, limit: number, offset: number): void {
this.queryResultsLoading = LoadingState.LOADING;
this.queryResults = null;
const self = this;
this.dataserviceService
.queryDataservice(sqlQuery, dataserviceName, limit, offset)
.subscribe(
(queryResult) => {
self.refreshData(queryResult);
self.queryResultsLoading = LoadingState.LOADED_VALID;
},
(error) => {
self.logger.error("[SqlControlComponent] Error getting query results: %o", error);
self.queryResultsLoading = LoadingState.LOADED_INVALID;
}
);
}
/*
* Refresh the Query results
* @param {QueryResults} results the results for a query
*/
private refreshData(results: QueryResults): void {
this.queryResults = results;
if (!results) {
return;
}
this.columns.length = 0;
this.rows.length = 0;
const columnData: ColumnData[] = results.getColumns();
const rowData: RowData[] = results.getRows();
//
// Define the row data
//
let firstTime = true;
const rowNumHeader = "ROW #";
// const widths: Map< string, number > = new Map< string, number >();
for ( let rowIndex = 0; rowIndex < rowData.length; rowIndex++ ) {
const row = rowData[ rowIndex ];
const data = row.getData();
const dataRow = {};
dataRow[ rowNumHeader ] = rowIndex + 1;
for (let colIndex = 0; colIndex < data.length; colIndex++) {
const label = columnData[ colIndex ].getLabel();
dataRow[ label ] = data[ colIndex ];
// size column to largest width data of first 50 rows
// if ( rowIndex < 50 ) {
// const value = "" + dataRow[ label ];
// const width = value.length;
// const labelWidth = label.length
//
// if ( firstTime ) {
// const max = Math.max( width, labelWidth );
// widths.set( label, max );
// } else {
// const currVal: number = widths.get( label );
// const max = Math.max( width, currVal, labelWidth );
// widths.set( label, max );
// }
// }
}
this.rows.push( dataRow );
firstTime = false;
}
// setup row number column
const column = { canAutoResize: true,
draggable: false,
// flexGrow: rowNumHeader.length,
maxWidth: 60,
minWidth: 60,
name: rowNumHeader,
prop: rowNumHeader,
resizable: true,
sortable: true,
width: 60,
cellClass: "row-number-column"};
this.columns.push( column );
//
// Setup columns
//
for ( let i = 0; i < columnData.length; i++) {
const colData = columnData[ i ];
const label = colData.getLabel();
// const width = widths.get( label );
const column = { canAutoResize: true,
draggable: false,
// flexGrow: width,
name: label.toUpperCase(),
prop: label,
resizable: true,
sortable: true };
this.columns.push( column );
}
}
}