-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.html
199 lines (199 loc) · 8.6 KB
/
demo.html
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
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>IndexSQL</title>
<meta name="Description" content="Project that simplifies the use of IndexedDB by making it compatible with SQL like sintaxis.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="/IndexSQL/static/IndexSQL.png">
<link rel="stylesheet" href="static/style.css">
</head>
<body>
<div class="header">
<a href="/IndexSQL/" class="logo"><img src="/IndexSQL/static/IndexSQL.png" alt="logo" height="30"> IndexSQL</a>
<div class="header-right">
<a href="/IndexSQL/">Home</a>
<a class="active" href="/IndexSQL/demo.html">Demo</a>
<a href="/IndexSQL/docs/">Docs</a>
<a href="https://github.com/dandimrod/IndexSQL">Git Repository</a>
</div>
</div>
<div class="demo-container">
<div class="console-container">
<div class="execute">
<div class="scrollable">
<textarea id="code"></textarea><br/>
<button onclick="submitCode()" id="submit">Submit ></button><br/>
<button onclick="backup()">Backup DB</button><br/>
<button onclick="restore()">Restore DB from code</button><br/>
<button onclick="if(confirm('ARE YOU SURE YOU WANT TO DROP THE DATABASE?\nYou have to reload the page to continue using the demo')){drop()}">Drop DB</button>
</div>
</div>
<div class="console">
<div class="scrollable" id="result" style="margin: 1%;">
</div>
</div>
</div>
<div class="inspector">
<div class="tables" >
<div class="scrollable" id="tables"></div>
</div>
<div class="table-inspector" >
<div id="table-inspector" class="scrollable" style="margin: 1%;"></div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/codemirror.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/codemirror.min.css">
<script src="src/IndexSQL.js"></script>
<script>
var codeElement=document.getElementById("code");
var editor=CodeMirror.fromTextArea(codeElement, {
mode: 'text/x-mariadb',
indentWithTabs: true,
smartIndent: true,
lineNumbers: true,
matchBrackets : true,
autofocus: true,
});
editor.getDoc().setValue(
`START TRANSACTION;
DROP TABLE Persons;
DROP TABLE Orders;
CREATE TABLE Persons (
PersonID number NOT_NULL AUTO_INCREMENT,
Name string NOT_NULL,
IsMale boolean NOT_NULL,
PRIMARY KEY (PersonID)
);
CREATE TABLE Orders (
OrderID number NOT_NULL AUTO_INCREMENT,
OrderNumber number NOT_NULL,
PersonID number,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
INSERT INTO Persons (Name, IsMale)
VALUES ("pipo", true);
INSERT INTO Persons (Name, IsMale)
VALUES ("pepa", false);
INSERT INTO Orders (OrderNumber, PersonID)
VALUES (1,0);
SELECT Name FROM Persons;
SELECT Name,IsMale FROM Persons ORDER BY Name, IsMale DESC;
SELECT * FROM Persons ORDER BY PersonID DESC;
SELECT * FROM Persons WHERE Name="pipo";
SELECT * FROM Persons WHERE IsMale=false;
SELECT * FROM Persons WHERE PersonID=0;
SELECT * FROM Persons WHERE Name="pipo" OR IsMale=false;
SELECT * FROM Persons WHERE Name="pipo" AND IsMale=false;
SELECT * FROM Persons WHERE NOT( Name="pipo" AND IsMale=false);
UPDATE Persons SET IsMale=false;
SELECT * FROM Persons;
UPDATE Persons SET IsMale=true WHERE Name="pipo";
SELECT * FROM Persons;
DELETE FROM Orders;
SELECT * FROM Orders;
DELETE FROM Persons WHERE IsMale=false;
SELECT * FROM Persons;
END TRANSACTION;`);
var indexSQL=new IndexSQL("demo");
function backup(){
indexSQL.backup(function(backup){
document.getElementById("result").innerHTML=JSON.stringify(backup);
})
}
function restore(){
indexSQL.restore(JSON.parse(editor.getValue()));
}
function drop(){
indexSQL.drop();
document.getElementById("result").innerHTML="<p style='color:red;'>DATABASE HAS BEEN DROPPED</>";
let buttons=document.getElementsByTagName("button");
for (let index = 0; index < buttons.length; index++) {
const element = buttons[index];
element.disabled=true;
}
}
function drawResults(results, time){
let difference=(new Date().getTime()-time)/1000;
let resultString="<p>Queries executed in "+difference+" s</p><br/>";
for (let index = 0; index < results.length; index++) {
const result = results[index];
if(result.error){
resultString=resultString+"<p style='color:red;'>Error in command "+(index+1)+": "+result.error+"</p>";
continue;
}
if(result.message){
resultString=resultString+"<p>Message from command "+(index+1)+": "+result.message+"</p>";
continue;
}
if(result.warn){
resultString=resultString+"<p style='color:orange;'>Warn in command "+(index+1)+": "+result.warn+"</p>";
continue;
}
if(result.result){
resultString=resultString+"<p>Result from command "+(index+1)+": </p>"+tableDrawer(result.result);
continue;
}
}
document.getElementById("result").innerHTML=resultString;
}
function submitCode(){
let time=new Date().getTime();
indexSQL.execute(editor.getValue(),function(time){
return function(result){
drawResults(result,time);
indexSQL.execute("Tables;",function(result){
drawTables(result)});
}
}(time));
}
function drawTables(results){
let tables=results[0].result;
let tablesString="<h2>Table inspector</h2>";
for (let index = 0; index < tables.values.length; index++) {
const element = tables.values[index][0];
tablesString=tablesString+"<button onclick='drawTable(\""+element+"\");' class='tableButton' id='button"+element+"'>"+element+"</button><br/>"
}
document.getElementById("tables").innerHTML=tablesString;
}
function drawTable(table){
let tableButtons=document.getElementsByClassName("tableButton");
for (let index = 0; index < tableButtons.length; index++) {
const element = tableButtons[index];
if(element.id==="button"+table){
element.classList.add("active");
}else{
element.classList.remove("active");
}
}
indexSQL.execute("SELECT * FROM "+table+";",function(tableName){
return function(result){
let table=result[0].result;
let tablesString="<h2>Showing table "+tableName+"</h2>"+tableDrawer(table);
document.getElementById("table-inspector").innerHTML=tablesString;
}
}(table));
}
function tableDrawer(table){
let tableString="<table><tr>";
for (let index = 0; index < table.header.length; index++) {
const element = table.header[index];
tableString=tableString+"<th title='"+element.constraints+"'>"+element.name+"</th>";
}
tableString=tableString+"</tr>";
for (let index = 0; index < table.values.length; index++) {
const values = table.values[index];
tableString=tableString+"<tr>";
for (let index2 = 0; index2 < values.length; index2++) {
const value = values[index2];
tableString=tableString+"<td>"+value+"</td>";
}
tableString=tableString+"</tr>";
}
tableString=tableString+"</table>";
return tableString;
}
indexSQL.execute("Tables;",function(result){drawTables(result)});
</script>
</body>