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