-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
201 lines (193 loc) · 8.25 KB
/
index.html
File metadata and controls
201 lines (193 loc) · 8.25 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SQL Table Custom Element</title>
<link rel="stylesheet" href="style.css" />
<!-- favicon from https://icons8.com/icon/1476/database" -->
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<script src="sql-element.js" type="module" defer></script>
<script src="examples.js" type="module" defer></script>
</head>
<body>
<main class="container">
<header>
<h1>SQL Table Custom Element</h1>
<div class="warning-box">
<svg
class="warning-icon"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:serif="http://www.serif.com/"
fill="#a8071a"
stroke="#a8071a"
width="60px"
height="60px"
viewBox="0 0 64 64"
version="1.1"
xml:space="preserve"
style="
fill-rule: evenodd;
clip-rule: evenodd;
stroke-linejoin: round;
stroke-miterlimit: 2;
"
>
<g id="warning">
<path
d="M32.427,7.987c2.183,0.124 4,1.165 5.096,3.281l17.936,36.208c1.739,3.66 -0.954,8.585 -5.373,8.656l-36.119,0c-4.022,-0.064 -7.322,-4.631 -5.352,-8.696l18.271,-36.207c0.342,-0.65 0.498,-0.838 0.793,-1.179c1.186,-1.375 2.483,-2.111 4.748,-2.063Zm-0.295,3.997c-0.687,0.034 -1.316,0.419 -1.659,1.017c-6.312,11.979 -12.397,24.081 -18.301,36.267c-0.546,1.225 0.391,2.797 1.762,2.863c12.06,0.195 24.125,0.195 36.185,0c1.325,-0.064 2.321,-1.584 1.769,-2.85c-5.793,-12.184 -11.765,-24.286 -17.966,-36.267c-0.366,-0.651 -0.903,-1.042 -1.79,-1.03Z"
style="fill-rule: nonzero"
/>
<path
d="M33.631,40.581l-3.348,0l-0.368,-16.449l4.1,0l-0.384,16.449Zm-3.828,5.03c0,-0.609 0.197,-1.113 0.592,-1.514c0.396,-0.4 0.935,-0.601 1.618,-0.601c0.684,0 1.223,0.201 1.618,0.601c0.395,0.401 0.593,0.905 0.593,1.514c0,0.587 -0.193,1.078 -0.577,1.473c-0.385,0.395 -0.929,0.593 -1.634,0.593c-0.705,0 -1.249,-0.198 -1.634,-0.593c-0.384,-0.395 -0.576,-0.886 -0.576,-1.473Z"
style="fill-rule: nonzero"
/>
</g>
</svg>
<pre><strong>Please do not use this</strong>, it's a really awful idea that I threw together as a joke after a friend suggested that html should be able to directly interact with a database.
I take no responsibility for anything that happens if you choose to ignore this warning.</pre>
</div>
</header>
<section>
<h2>So what is this?</h2>
<p>
Inspired by a discord conversation that started a little like this:
</p>
<blockquote>
<p>
Just started wondering if there is a way to read from a database in
HTML 🤔
</p>
<cite>- gusgo99</cite>
</blockquote>
<p>and quickly led to:</p>
<blockquote>
<p><sql query="SELECT * FROM Foo" /></p>
<cite>- moon_president</cite>
</blockquote>
<p>
This is a collection of custom HTML elements that allow running
arbitrary sql queries on an SQLite database fetched from the server.
From a practical point of view this is entirely useless and should
never be used, but it was a fun introduction to web components and I
did learn a couple of things along the way.
</p>
<p>
The code can be browsed on Github
<a
href="https://github.com/iCiaran/html-sql-table/blob/main/sql-element.js"
>here</a
>, or just open your browser's dev tools and take a look at the source
for the examples below.
</p>
</section>
<section>
<h2>Limitations</h2>
<p>
This is a haphazard implementation of an idea that should have
remained just that, it is not remotely production ready (and never
will be). I could list limitations all day, here are the first few
that come to mind:
</p>
<ul>
<li>
<strong>Security</strong> What security? The entire SQLite database
is sent to the front-end, nothing is private.
</li>
<li>
<strong>Performance</strong> No effort has been made to make this
performant, the UI thread is used even though SQLite explicitly
discourages this (use a worker thread instead for long running
JavaScript operations), if multiple tables use the same database
file it's loaded each time, the list goes on...
</li>
<li>
<strong>Compatibility</strong> Only SQLite, I did look to see if any
browser -> database clients existed (even more horrifying), but the
(lack of) raw TCP socket support in browsers currently makes those
not possible.
</li>
<li><strong>Error handling</strong> There is none!</li>
<li>
<strong>Custom element callbacks</strong> Only
<code>connectedCallback</code> has been implemented, any time the
position of these elements in the DOM is manipulated weird things
might happen.
</li>
<li>
<strong>More</strong> Likely many more, I really wasn't kidding in
the warning at the top of this page, don't use this!
</li>
</ul>
</section>
<section>
<h2>Examples</h2>
A few examples of the elements in action, the SQLite database used can
be downloaded <a href="pets.db" download>here</a> to browse in a more
sane way.
<h3>Single value selection</h3>
<p>
Single values can be selected from the database using the
<code><sql-value></code> element.
</p>
<p>
For a single value the field returned <strong>must</strong> be named
<code>value</code> in the SQL statement.
</p>
<p>
If multiple rows are returned then the value from the first row is
used.
</p>
<pre class="example-placeholder"></pre>
<p>
The pet with the name 'Rex' is a
<sql-value
database="pets.db"
query="select type value from pets where name='Rex'"
></sql-value>
.
</p>
<h3>Simple select all</h3>
<p>List of users with their IDs, names, and ages.</p>
<pre class="example-placeholder"></pre>
<sql-table database="pets.db" query="select * from users"></sql-table>
<h3>Choosing specific columns and filtering</h3>
<p>Table headings will reflect the column names used in the query.</p>
<pre class="example-placeholder"></pre>
<sql-table
database="pets.db"
query="select name, type as animal from pets where owner = 1"
></sql-table>
<h3>Joins e.t.c.</h3>
<p>
More advanced queries are possible, even modifications though they
won't be persisted and each table has it's own instance of the
database so they're very pointless.
</p>
<pre class="example-placeholder"></pre>
<sql-table
database="pets.db"
query="select distinct u.name, count(1) as 'pet count' from pets p join users u on p.owner = u.id group by p.owner"
></sql-table>
<h3>Empty results</h3>
<p>
Well, what did you expect? If there are no results from the query the
table will still be displayed, it just won't have any rows.
</p>
<pre class="example-placeholder"></pre>
<sql-table
database="pets.db"
query="select * from pets where owner = 1000"
></sql-table>
<h3>Syntax Errors</h3>
<p>
Did I mention there's no error handling? If the database file doesn't
exist or the query has invalid syntax things get a bit messy...
</p>
<pre class="example-placeholder"></pre>
<sql-table database="pets.db" query="select ???"></sql-table>
</section>
</main>
</body>
</html>