• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<script>
3
4// Open a Web SQL database.
5var g_db = null;
6if (typeof window.openDatabase == "undefined") {
7  document.write("Error: Web SQL databases are not supported.");
8}
9try {
10  g_db = openDatabase("test", "1.0", "test database", 1024 * 1024);
11} catch(err) {
12  document.write("Error: cannot open database.");
13}
14
15// Creates a table named "table1" with one text column named "data".
16function createTable() {
17  if (!g_db)
18    return;
19  g_db.transaction(
20      function(tx) {
21        tx.executeSql("CREATE TABLE table1 (data TEXT)");
22      },
23      function(error) {
24        sendValueToTest(error);
25      },
26      function() {
27        sendValueToTest("done");
28      });
29}
30
31// Inserts a record into the database.
32function insertRecord(text) {
33  g_db.transaction(
34      function(tx) {
35        tx.executeSql("INSERT INTO table1 VALUES (?)", [text]);
36      },
37      function(error) {
38        sendValueToTest(error);
39      },
40      function() {
41        sendValueToTest("done");
42      });
43}
44
45
46// Updates a record at the given index with the given text. The indices are
47// 0-based and are ordered from oldest record, to newest record.
48function updateRecord(index, text) {
49  var didUpdate = false;
50  findId(index, function(rowId) {
51    g_db.transaction(
52        function(tx) {
53          tx.executeSql(
54              "UPDATE table1 SET data=? WHERE ROWID=?",
55              [text, rowId],
56              function(tx, result) {
57                if (result.rowsAffected == 1)
58                  didUpdate = true;
59                else if (result.rowsAffected == 0)
60                  sendValueToTest("could not update index: " + index);
61                else
62                  sendValueToTest("multiple rows with index: " + index);
63              });
64        },
65        function(error) {
66          sendValueToTest("update error: " + error);
67        },
68        function() {
69          if (didUpdate)
70            sendValueToTest("done");
71        });
72  });
73}
74
75// Deletes a record at the given index.
76function deleteRecord(index) {
77  findId(index, function(rowId) {
78    g_db.transaction(
79        function(tx) {
80          tx.executeSql("DELETE FROM table1 WHERE ROWID=?", [rowId]);
81        },
82        function(error) {
83          sendValueToTest("delete error: " + error);
84        },
85        function() {
86          sendValueToTest("done");
87        });
88  });
89}
90
91// Gets all the records in the database, ordered by their age.
92function getRecords() {
93  g_db.readTransaction(function(tx) {
94    tx.executeSql(
95        "SELECT data FROM table1 ORDER BY ROWID",
96        [],
97        function(tx, result) {
98          items = "";
99          for (var i = 0; i < result.rows.length; i++) {
100            if (items != "")
101              items += ", ";
102            items += result.rows.item(i).data;
103          }
104          sendValueToTest(items);
105        },
106        function(tx, error) {
107          sendValueToTest("getRecords error: " + error);
108        });
109  });
110}
111
112// Helper function that finds the ID for a record based on a given index.
113function findId(index, callback) {
114  g_db.readTransaction(function(tx) {
115    // |ROWID| is a special sqlite column. It is unique and is incremented
116    // automatically when a new record is created.
117    // |LIMIT| is a nonstandard clause supported by sqlite that lets us pick
118    // rows from the database by index. E.g., LIMIT 2,10 will give us 10 records
119    // starting at offset 2.
120    tx.executeSql(
121        "SELECT ROWID AS id FROM table1 ORDER BY ROWID LIMIT ?,1",
122        [index],
123        function(tx, result) {
124          if (result.rows.length >= 1)
125            callback(result.rows.item(0).id);
126          else
127            sendValueToTest("could not find row with index: " + index);
128        },
129        function(tx, error) {
130        sendValueToTest("findId error: " + error);
131        });
132  });
133}
134
135function sendValueToTest(value) {
136  //alert(value);
137  window.domAutomationController.setAutomationId(0);
138  window.domAutomationController.send(value);
139}
140
141</script>
142
143<body>
144This page is used for testing Web SQL databases.
145</body>
146</html>
147