37
37
< uib-tab index ="0 " data-cy ="sqltab ">
38
38
< uib-tab-heading > < span style ="color: #555; "> SQL</ span > </ uib-tab-heading >
39
39
< textarea id ="usersql " rows ="8 " onchange ="updateSQL() " ng-attr-placeholder ="Your SQL statement here... " data-cy ="usersqltext "> [[ .SQL ]]</ textarea >
40
+
41
+ < button type ="button " class ="btn btn-default " ng-if ="db.Records.length > 0 " ng-click ="toggleResultTable() " data-cy ="resultsbtn "> {{ toggleLabel }}</ button >
42
+ < div uib-collapse ="isCollapsed ">
43
+ < div style ="max-width: 100%; overflow: auto; border: 1px solid #DDD; border-radius: 7px 7px 0 0; ">
44
+ < table class ="table table-bordered table-striped table-responsive " style ="margin-bottom: 0; padding-bottom: 0; " data-cy ="resultstbl ">
45
+ < thead >
46
+ < tr >
47
+ < th ng-repeat ="col in db.ColNames " style ="padding: 7px 0 6px 6px; "> {{ col }}</ th >
48
+ </ tr >
49
+ </ thead >
50
+ < tbody >
51
+ < tr ng-repeat ="row in db.Records ">
52
+ < td ng-repeat ="val in row " dir ="auto "> < pre style ="background-color: transparent; border: none; padding: 0px; margin: 0px; " ng-bind-html ="val.Value | fixSpaces "> </ pre > </ td >
53
+ </ tr >
54
+ </ table >
55
+ </ div >
56
+ </ div >
40
57
</ uib-tab >
41
58
</ uib-tabset >
42
59
</ div >
@@ -115,6 +132,24 @@ <h4 style="color: {{ statusMessageColour }};" data-cy="statusmsg"> {{ statu
115
132
// Initialise things with safe defaults (these are potentially filled out further down the page)
116
133
$scope . selectedName = "[[ .SelectedName ]]" ;
117
134
$scope . execNames = [ [ . ExecNames ] ] ;
135
+ $scope . toggleLabel = "Show result table"
136
+ $scope . isCollapsed = true ;
137
+
138
+ // Create empty stub to hold returned query data
139
+ $scope . db = { "ColCount" :0 , "ColNames" :[ ] , "Offset" :0 , "Records" :[ ] , "RowCount" :0 , "SortCol" :"" , "SortDir" :"" , "Tablename" :"" , "TotalRows" :0 } ;
140
+
141
+ // Functions for toggling on/off various display elements
142
+ $scope . toggleResultTable = function ( ) {
143
+ $scope . isCollapsed = ! $scope . isCollapsed ;
144
+ if ( $scope . isCollapsed ) {
145
+ $scope . toggleLabel = "Show result table"
146
+ } else {
147
+ $scope . toggleLabel = "Hide result table"
148
+ }
149
+
150
+ // Clear any existing status message
151
+ $scope . statusMessage = "" ;
152
+ }
118
153
119
154
// Ensure the SQL statement name field isn't blank
120
155
// Returns true if a name is present, false if it's missing
@@ -178,6 +213,12 @@ <h4 style="color: {{ statusMessageColour }};" data-cy="statusmsg"> {{ statu
178
213
)
179
214
}
180
215
216
+ // Clear out any existing table data
217
+ $scope . clearData = function ( ) {
218
+ $scope . db . ColNames = [ ] ;
219
+ $scope . db . Records = [ ] ;
220
+ }
221
+
181
222
// If there are saved SQL statements but none of them is named "default", then change the Saved SQL drop down
182
223
// selector to use the first one
183
224
if ( ( $scope . execNames . length > 0 ) && ( ! $scope . execNames . includes ( "default" ) ) ) {
@@ -218,15 +259,24 @@ <h4 style="color: {{ statusMessageColour }};" data-cy="statusmsg"> {{ statu
218
259
// Display a success message
219
260
$scope . statusMessageColour = "green" ;
220
261
$scope . statusMessage = nowString ( ) + "SQL statement '" + name + "' deleted" ;
262
+
263
+ // Clear out any existing table data
264
+ $scope . clearData ( ) ;
221
265
} , function failure ( response ) {
222
266
// The deletion failed, so display the returned error message
223
267
$scope . statusMessageColour = "red" ;
224
268
$scope . statusMessage = nowString ( ) + "Deleting '" + name + "' failed: " + response . data ;
269
+
270
+ // Clear out any existing table data
271
+ $scope . clearData ( ) ;
225
272
}
226
273
)
227
274
} else {
228
275
$scope . statusMessageColour = "red" ;
229
276
$scope . statusMessage = nowString ( ) + "Unknown SQL statement" ;
277
+
278
+ // Clear out any existing table data
279
+ $scope . clearData ( ) ;
230
280
}
231
281
} ;
232
282
@@ -239,18 +289,38 @@ <h4 style="color: {{ statusMessageColour }};" data-cy="statusmsg"> {{ statu
239
289
let encoded = base64url ( userSQL ) ;
240
290
$http . get ( "/x/execlivesql/[[ .DB.Info.Owner ]]/[[ .DB.Info.Database ]]?commit=[[ .DB.Info.CommitID ]]&sql=" + encoded ) . then (
241
291
function success ( response ) {
242
- $scope . statusMessageColour = "green" ;
243
- if ( response . data [ "rows_changed" ] === 0 ) {
244
- $scope . statusMessage = nowString ( ) + "Execution succeeded" ;
245
- } else if ( response . data [ "rows_changed" ] === 1 ) {
246
- $scope . statusMessage = nowString ( ) + "Execution success: " + response . data [ "rows_changed" ] + " row changed" ;
292
+ // We can potentially receive either "Execute SQL" or Query responses here
293
+ if ( "rows_changed" in response . data ) {
294
+ // Handle the response as for Execute SQL
295
+ $scope . statusMessageColour = "green" ;
296
+ if ( response . data [ "rows_changed" ] === 0 ) {
297
+ $scope . statusMessage = nowString ( ) + "Execution succeeded" ;
298
+ } else if ( response . data [ "rows_changed" ] === 1 ) {
299
+ $scope . statusMessage = nowString ( ) + "Execution success: " + response . data [ "rows_changed" ] + " row changed" ;
300
+ } else {
301
+ $scope . statusMessage = nowString ( ) + "Execution success: " + response . data [ "rows_changed" ] + " rows changed" ;
302
+ }
303
+
304
+ // Clear out any existing table data
305
+ $scope . clearData ( ) ;
247
306
} else {
248
- $scope . statusMessage = nowString ( ) + "Execution success: " + response . data [ "rows_changed" ] + " rows changed" ;
307
+ // Give a useful "success" status message
308
+ $scope . statusMessageColour = "green" ;
309
+ if ( response . data [ "RowCount" ] === 0 ) {
310
+ $scope . statusMessage = nowString ( ) + "SQL query ran without error, but returned no records" ;
311
+ $scope . clearData ( ) ;
312
+ } else {
313
+ $scope . statusMessage = nowString ( ) + "SQL query ran successfully" ;
314
+ $scope . db = response . data ;
315
+ }
249
316
}
250
317
} , function failure ( response ) {
251
318
// Retrieving data failed, so display the returned error message and hide the graph
252
319
$scope . statusMessageColour = "red" ;
253
320
$scope . statusMessage = nowString ( ) + "Executing SQL statement failed: " + response . data ;
321
+
322
+ // Clear out any existing table data
323
+ $scope . clearData ( ) ;
254
324
}
255
325
)
256
326
} ;
0 commit comments