-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsql.js
191 lines (167 loc) · 5.33 KB
/
sql.js
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
cdbmanager.controller('sqlSelectorCtrl', ["$scope", "nav", function ($scope, nav) {
$scope.nav = nav;
}]);
cdbmanager.controller('sqlCtrl', ["$scope", "SQLClient", "endpoints", "map", "nav", "tables", "$localStorage", "settings", "$timeout", function ($scope, SQLClient, endpoints, map, nav, tables, $localStorage, settings, $timeout) {
let self = this;
let editor = null;
this.api = new SQLClient();
$localStorage.history = $localStorage.history || [];
this.historyCurrent = null; // Idx to the current position in the history array. If greater than its size, we're not browsing the history.
this.historyBuffer = null; // Temporary buffer for the current input when browsing the history.
$scope.sql = {};
$scope.isQueryPlan = false;
$scope.nav = nav;
$scope.running = this.api.running;
$scope.historyNotFound = false;
$scope.cdbrt = { // Settings for the result table
id: "sql_console",
rowsPerPage: settings.sqlConsoleRowsPerPage
};
// codemirror configuration
let mime = 'text/x-mariadb';
if (window.location.href.indexOf('mime=') > -1) {
mime = window.location.href.substr(window.location.href.indexOf('mime=') + 5);
}
$scope.editorOptions = {
mode: 'text/x-sql',
indentWithTabs: false,
smartIndent: true,
lineNumbers: true,
matchBrackets: true,
autofocus: true,
extraKeys: {
"Ctrl-Space": "autocomplete"
}
};
this.setHistoryCurrent = function (idx) {
self.historyCurrent = idx;
$scope.historyNotFound = false;
if (idx < $localStorage.history.length) {
$scope.sql.query = $localStorage.history[self.historyCurrent];
} else {
$scope.sql.query = self.historyBuffer;
}
};
this.resetEditor = function () {
$scope.sql.result = null;
$scope.historyNotFound = false;
$scope.sql.historyNeedle = null;
self.historyBuffer = "";
self.setHistoryCurrent($localStorage.history.length);
};
$scope.resetConsole = function () {
self.resetEditor();
};
$scope.execSQL = function (query) {
self.historyCurrent = $localStorage.history.length;
if ($localStorage.history[self.historyCurrent - 1] != query) {
$localStorage.history.push(query);
}
self.historyBuffer = "";
$scope.historyNotFound = false;
self.api.send(query);
map.update(query);
};
$scope.cleanHistory = function () {
$localStorage.history = [];
// Now we don't call resetEditor directly because we don't want to lose the current input.
self.historyCurrent = 0;
self.historyBuffer = "";
};
$scope.searchHistory = function (needle) {
for (let i = self.historyCurrent - 1; i >= 0; i--) {
let historyIdx = $localStorage.history[i].search(new RegExp(needle, "i"));
if (historyIdx >= 0) {
self.setHistoryCurrent(i);
return;
}
}
$scope.historyNotFound = true;
};
// clean console if current endpoint changes
$scope.$watch(function () {
return endpoints.current;
}, function () {
self.resetEditor();
}, true);
// update number of rows per page when settings changes
$scope.$watch(function () {
return settings.sqlConsoleRowsPerPage;
}, function () {
$scope.cdbrt.rowsPerPage = settings.sqlConsoleRowsPerPage;
});
// query executed successfully
$scope.$watch(function () {
return self.api.raw;
}, function () {
$scope.sql.result = self.api;
if (self.api.items && self.api.items.length > 0 && self.api.items[0].hasOwnProperty("QUERY PLAN")) {
$scope.isQueryPlan = true;
} else {
$scope.isQueryPlan = false;
}
});
// codemirror must be refreshed when not hidden anymore, otherwise the text won't show until you click on the editor
$scope.$watch(function () {
return nav.current;
}, function () {
if (nav.isCurrentView("sql")) {
// Need to refresh after digest cycle is over
$timeout(function () {
editor.refresh();
});
}
});
$scope.$watch(function () {
return tables.api.items;
}, function () {
let hints = {};
let table = null;
if (tables.api.items) {
for (let i = 0; i < tables.api.items.length; i++) {
table = tables.api.items[i];
hints[table.relname] = [];
}
editor.setOption("hintOptions", {
"tables": hints
});
}
}, true);
// Key bindings for history
$scope.codemirrorLoaded = function (_editor) {
editor = _editor;
let ctrlUp = {
"Ctrl-Up": function () {
if (self.historyCurrent == $localStorage.history.length) {
self.historyBuffer = $scope.sql.query;
}
if (self.historyCurrent > 0) {
$scope.sql.query = $localStorage.history[--self.historyCurrent];
$scope.$apply();
}
}
};
editor.addKeyMap(ctrlUp);
let ctrlDown = {
"Ctrl-Down": function () {
if (self.historyCurrent < $localStorage.history.length) {
++self.historyCurrent;
if (self.historyCurrent < $localStorage.history.length) {
$scope.sql.query = $localStorage.history[self.historyCurrent];
} else {
$scope.sql.query = self.historyBuffer;
}
$scope.$apply();
}
}
};
editor.addKeyMap(ctrlDown);
let ctrlEnter = {
"Ctrl-Enter": function () {
$scope.execSQL($scope.sql.query);
}
};
editor.addKeyMap(ctrlEnter);
};
this.resetEditor();
}]);