Encrypted SQL example


The index.html file containing JavaScript that makes use of the Encrypted SQL API functionality is listed below.

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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<!DOCTYPE html>
<html manifest="manifest.appcache">
  <head>
    <title>
      Encrypted SQL API usage example
    </title>
    <link rel="x-antenna-managed-webapp-descriptor"
    href="webapp-descriptor.xml"/>
 
    <script type="text/javascript">
      var db;
 
      function openDB() {
        if (db == null) {
          try {
            var onCreated = function() {
              printText('db Created/opened');
            }
            printText("Opening database...");
            //
            var radios = document.getElementsByName('sharing');
            var sharing;
            for (var i = 0, length = radios.length; i < length; i++) {
              if (radios[i].checked) {
                sharing = radios[i].value;
                break;
              }
            }
 
            //
            db = openDatabase('testdb', '1.0',
              'my test database', 2 * 1024 * 1024, sharing);
          }
          catch (e) {
            printText("Error occured in openDB: " + e);
          }
        }
      }
      function createDBandPopulateSampleData() {
        try {
          openDB();
          dbObj = db;
          sharing = null;
          // create schema (using object callbacks)
          printText("Creating schema...");
          dbObj.transaction(
            function (tx) {
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (1, "apple")');
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [2, 'banana']);
              tx.executeSql('INSERT INTO testtable (id, text) VALUES (?, ?)', [3, 'kiwi'],
                function(tx, result) {
                  printText('kiwi record insert succeeded.')
                },
                function(tx, error) {
                  printText('kiwi record insert failed.' + error.message)
                }
              );
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [4, 'orange'],
                null,
                function(tx, error) {
                  printText('orange record insert failed.')
                }
              );
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [5, 'plum'],
                function(tx, result) {
                  printText('plum record insert succeeded.')
                },
                null);
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [6, "an'a'nas"],
                null,
                null);
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [7, 'gr"a"pe'],
                undefined,
                undefined);
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [8, "man'go"],
                function(tx, result) { printText(
                  'mango record insert succeeded.')
                },
                undefined);
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, null)', [9]);
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [10, null]);
            },
            function (err) {
              printText("Something went wrong when inserting data: " + JSON.stringify(err.message));
            },
            function () {
              printText("Inserting data transaction succeeded");
            }
          );
          printText("Data inserted.");
        } catch (e) {
          printText("Error occured in createDBandPopulateSampleData: " + e);
        }
      }
 
      function createDBandPopulateSampleDataWithInnerTransaction() {
        try {
          openDB();
          dbObj = db;
 
          // create schema (using object callbacks)
          printText("Creating schema...");
          dbObj.transaction(
            function(tx) {
              tx.executeSql(
                'CREATE TABLE IF NOT EXISTS testtable (id unique, text)');
            },
            function(err) {
              printText("Something went wrong when creating schema: " + err);
            }
          );
 
          printText("Schema created");
 
          // populate date (using function callbacks)
          printText("Inserting data...");
          dbObj.transaction(
            function(tx) {
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (1, "apple")');
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [2, 'banana'],
                function(parentTx, results) {
                  console.log(
                    "inside banana executeSql callback, parentTx: " +
                    JSON.stringify(parentTx) + ", results: " + results);
 
                  parentTx.executeSql(
                    'INSERT INTO testtable (id, text) VALUES (?, ?)', [11, 'bananaInner11']);
                  parentTx.executeSql(
                    'INSERT INTO testtable (id, text) VALUES (?, ?)', [12, 'bananaInner12']);
                },
                function(err) {
                  console.log("outer banana tx error: " + err);
                }
              );
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [3, 'kiwi'],
                function() { printText('kiwi record insert succeeded.'); },
                function(parentTx, error) { printText(
                  'kiwi record insert failed.' + error.message); });
              tx.executeSql(
                'INSERT INTO testtable (id, text) VALUES (?, ?)', [4, 'orange'],
                null,
                function() { printText('orange record insert failed.');
                }
              );
                 
              console.log("started long loop");
              for (var i = 0; i < 49999; i++) {
                var t = new Date().getTime();
              }
              console.log("finished long loop");
            },
            function(err) {
              printText("Something went wrong when inserting data: " + JSON.stringify(err.message));
            },
            function() {
              printText("Inserting data transaction succeeded");
            }
          );
          printText("Data inserted.");
        } catch(e) {
            printText("Error occured in createDBandPopulateSampleData: " + e);
        }
      }
       
      function selectAndPrintAllDataWithCallbackFunction() {
        try {
          openDB();
          dbObj = db;
           
          printText("Selecting all data (with callback function)..."); 
          dbObj.transaction(
            function (tx) {
              tx.executeSql('SELECT * FROM testtable', [],
                function (tx, results) {
                  console.log(results);
                  var len = results.rows.length;
                  for (var i = 0; len > i; i++) {
                    printText("row " + i + ": id=[" +
                      results.rows.item(i).id + "] text=[" +
                      results.rows.item(i).text + "]");
                  }
                },
                function(tx, result) { printText('Reading data failed: ' +
                  result.message)}
              );
            },
            function (err) {
              printText(
                "Something went wrong when selecting all data (with callback function): " +
                err);
            },
            function () {
              printText("Selecting data transaction succeeded");
            }
          );
        } catch (e) {
        printText("Error occured in selectAndPrintAllData: " + e);
        }
      }
 
      function selectAndPrintAllDataWithCallbackObject() {
        try {
          openDB();
          dbObj = db;
          printText("Selecting all data (with callback object)..."); 
          dbObj.transaction({
            handleEvent:
            function (tx) {
              tx.executeSql('SELECT * FROM testtable', [], {
                handleEvent:
                function (tx, results) {
                  var len = results.rows.length, i;
                  for (i = 0; len > i; i++) {
                    printText("row " + i + ": id=[" +
                    results.rows.item(i).id + "] text=[" +
                    results.rows.item(i).text + "]");
                  }
                }
              });
            }
          },
          {
            handleEvent:
            function (err) {
              printText(
                "Something went wrong when selecting all data (with callback object): " +
                err);
            }
          });
        } catch (e) {
          printText("Error occured in selectAndPrintAllData: " + e);
        }
      }
 
      function deleteDataWithCallbackFunction() {
        try {
          openDB();
          dbObj = db;
 
          printText("Deleting all data (with callback function)..."); 
          dbObj.transaction(function (tx) {
            printText("Deleting in sequence...");
            tx.executeSql("DELETE FROM testtable WHERE id=1");
            tx.executeSql("DELETE FROM testtable WHERE id=?", [2]);
            tx.executeSql("DELETE FROM testtable WHERE id=?", [3], null);
            tx.executeSql("DELETE FROM testtable WHERE id=?", [4],
              null, null);
            tx.executeSql("DELETE FROM testtable WHERE id=?", [5],
              undefined);
            tx.executeSql("DELETE FROM testtable WHERE id=?", [6],
              undefined, undefined);
            tx.executeSql("DELETE FROM testtable WHERE id=?", [7],
              function(tx, result) { printText('Record with id=7 deleted.')});
            tx.executeSql("DELETE FROM testtable WHERE id=?", [8], null,
              function(tx, error) { printText(
                'Got an error when deleting record with id=8.')});
          },
            function (err) {
              printText(
                "Something went wrong while deleting all data (with callback function): " +
                err);
            },
            function () {
              printText("Deleting data transaction succeeded");
            }
         );
        } catch (e) {
          printText("Error occured in selectAndPrintAllData: " + e);
        }
      }
 
 
      function deleteAllDataWithCallbackFunction() {
        try {
          openDB();
          dbObj = db;
 
          printText("Deleting all data (with callback function)..."); 
          dbObj.transaction(function (tx) {
            printText("Deleting all data...");
            tx.executeSql("DELETE FROM testtable", [],
              function(tx, result) { printText('Deleted data.')},
              function(tx, error) { printText(
                'Error occured when deleting data.')}
            );
          },
          function (err) {
            printText(
              "Something went wrong while deleting all data (with callback function): " +
              err);
          },
          function () {
            printText("Deleting data transaction succeeded");
          });
        } catch (e) {
          printText("Error occured in selectAndPrintAllData: " + e);
        }
      }
 
      function selectAndPrintDataWithNullText() {
        try {
          openDB();
          dbObj = db;
 
          printText("Selecting data with null text..."); 
          dbObj.transaction(
            function (tx) {
              tx.executeSql('SELECT * FROM testtable WHERE text IS NULL', [],
                function (tx, results) {
                  var len = results.rows.length, i;
                  for (i = 0; len > i; i++) {
                  printText("row " + i + ": id=[" + results.rows.item(i).id +
                  "], text=[" + results.rows.item(i).text +
                  "], text === null: " +
                  (results.rows.item(i).text === null));
                  }
                }
              );
            },
            function (err) {
              printText("Something went wrong when selecting data with null text): " +
                        err);
            }
          );
        } catch (e) {
          printText("Error occured in selectAndPrintDataWithNullText: " + e);
        }
      }
 
      function printText(str) {
        var d = document.getElementById('results');
        d.innerHTML += "<br/>" + str;
        d.scrollTop = d.scrollHeight;
        console.log(str);
      }
 
      function clearText() {
        var d = document.getElementById('results');
        d.innerHTML = "";
      }    
 
      function changeDBVersion() {
        openDB();
        dbObj = db;
        dbObj.changeVersion("1.0", "1.1", function (tx) {
          tx.executeSql(
            'INSERT INTO testtable (id, text) VALUES (11, "apple")');
        });
      }
 
      </script>
 
  </head>
  <body>
    <header>
      <h3>
        <span>
          Encrypted SQL API Usage Example
        </span>
      </h3>
    </header>
    <div style="padding: 10px;">
      <div class="info">
        This Application demonstrates the use of the SQL JavaScript API.
      </div>
    </div>
    <div>
      <form action="#n" name="sharingForm">
 
        <label for="gender">
          Sharing:
        </label>
        <input type="radio" name="sharing" value="" checked="">
        None
      </input>
      <br>
      <br>
      <input type="radio" name="sharing" value="window.launchbox.SQLStorage.SHARED_AMONG_APPS" >
      Among apps
    </input>
    <br>
    <br>
    <input type="radio" name="sharing" value="window.launchbox.SQLStorage.SHARED_AMONG_USERS" >
    Among users
  </input>
  <br>
  <br>
</form>
</div>
<div id="simple-buttons" style="display: block;padding: 10px;">
  <input type="button" onclick="createDBandPopulateSampleData();"
  value="Create DB and populate sample data"/>
  <input type="button"
  onclick="createDBandPopulateSampleDataWithInnerTransaction();"
  value="Create db, select and print all data with inner transaction"/>
  <input type="button"
  onclick="selectAndPrintAllDataWithCallbackFunction();"
  value="Select and print all data (callback function)"/>
  <input type="button"
  onclick="selectAndPrintAllDataWithCallbackObject();"
  value="Select and print all data (callback object)"/>
  <input type="button" onclick="selectAndPrintDataWithNullText();"
  value="Print data with null text"/>
  <input type="button" onclick="deleteAllDataWithCallbackFunction();"
  value="Delete all data"/>
  <input type="button" onclick="changeDBVersion();"
  value="Change DB version"/>
</div>
<div style="padding: 10px;margin-top:-20px;">
  <input type="button" onclick="clearText();"
  value="Clear all text"/>
</div>
<div id="results">
</div>
 
<script type="text/javascript">
  function openTabButtons(openShared) {
    var toOpen = document.getElementById("simple-buttons");
    var toClose = document.getElementById("shared-buttons");
    var toBold = document.getElementById("simple-activate");
    var toUnbold = document.getElementById("shared-activate");
 
    if(openShared) {
      toOpen = document.getElementById("shared-buttons");
      toClose = document.getElementById("simple-buttons");
      toBold = document.getElementById("shared-activate");
      toUnbold = document.getElementById("simple-activate");
    }
 
    toOpen.style.display = "block";
    toClose.style.display = "none";
    toBold.style.fontWeight = "bold";
    toUnbold.style.fontWeight = "normal";
  }
 
</script>
</body>
</html>

The contents of the cache manifest file called manifest.appcache for this application are listed below:

1
2
3
4
5
6
7
CACHE MANIFEST
 
CACHE:
index.html
 
NETWORK:
*

The webapp-descriptor.xml file for this application is defined in the following way:

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<webapp-descriptor xmlns="http://www.pega.com/application-hosting/web-app-descriptor/2.0">
   <id>com.pega.sample.encryptedsql</id>
   <version>1.0.0</version>
   <name>Encrypted SQL API usage example</name>
</webapp-descriptor>

Related topics

Public API reference
Overview
Legal notice | Copyright © 2015 and Confidential to Pegasystems Inc. All rights reserved. | Feedback
Advanced...