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