summaryrefslogtreecommitdiffstats
path: root/third_party/webdriver/test_data/html5/database.js
blob: c6333be8cd0066c3cb9031b287b321ab7ee988d1 (plain)
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
var database={};
database.db={};

database.onError = function(tx, e) {
  var log = document.createElement('div');
  log.setAttribute('name','error');
  log.setAttribute('style','background-color:red');
  log.innerText = e.message;
  document.getElementById('logs').appendChild(log);
}

database.onSuccess = function(tx, r) {
  if (r.rows.length) {
    var ol;
    for (var i = 0; i < r.rows.length; i++) {
      ol = document.createElement('ol');
      ol.innerHTML = r.rows.item(i).ID + ": " + r.rows.item(i).docname + " (" + r.rows.item(i).created + ")";
      document.getElementById('logs').appendChild(ol);
    }

  }
}

database.open=function(){
  database.db=openDatabase('HTML5', '1.0', 'Offline document storage', 100*1024);
}

database.create=function(){
  database.db.transaction(function(tx) {
    tx.executeSql("CREATE TABLE IF NOT EXISTS docs(ID INTEGER PRIMARY KEY ASC, docname TEXT, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP)",
      [],
      database.onSuccess,
      database.onError);
  });}

database.add = function(message) {
  database.db.transaction(function(tx){
    tx.executeSql("INSERT INTO docs(docname) VALUES (?)",
        [message], database.onSuccess, database.onError);
    });
}

database.selectAll = function() {
  database.db.transaction(function(tx) {
    tx.executeSql("SELECT * FROM docs", [], database.onSuccess,
        database.onError);
 });
}

database.onDeleteAllSuccess = function(tx, r) {
  var doc = document.documentElement;
  var db_completed = document.createElement("div");
  db_completed.setAttribute("id", "db_completed");
  db_completed.innerText = "db operation completed";
  doc.appendChild(db_completed);
}

database.deleteAll = function() {
  database.db.transaction(function(tx) {
    tx.executeSql("delete from docs", [], database.onDeleteAllSuccess,
        database.onError);
  });
}

var log = document.createElement('div');
log.setAttribute('name','notice');
log.setAttribute('style','background-color:yellow');
log.innerText = typeof window.openDatabase == "function" ? "Web Database is supported." : "Web Database is not supported.";
document.getElementById('logs').appendChild(log);

try {
  database.open();
  database.create();
  database.add('Doc 1');
  database.add('Doc 2');
  database.selectAll();
  database.deleteAll();
} catch(error) {
  var log = document.createElement('div');
  log.setAttribute('name','critical');
  log.setAttribute('style','background-color:pink');
  log.innerText =  error;
  document.getElementById('logs').appendChild(log);
}