W3C home > Mailing lists > Public > public-html-diffs@w3.org > August 2009

hixie: Add some example code in the intro. (whatwg r3650)

From: poot <cvsmail@w3.org>
Date: Tue, 18 Aug 2009 10:13:53 +0900 (JST)
To: public-html-diffs@w3.org
Message-Id: <20090818011353.EC7092BCC1@toro.w3.mag.keio.ac.jp>
hixie: Add some example code in the intro. (whatwg r3650)

http://dev.w3.org/cvsweb/html5/webdatabase/Overview.html?r1=1.13&r2=1.14&f=h
http://html5.org/tools/web-apps-tracker?from=3649&to=3650

===================================================================
RCS file: /sources/public/html5/webdatabase/Overview.html,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- Overview.html	17 Aug 2009 23:01:59 -0000	1.13
+++ Overview.html	18 Aug 2009 01:13:31 -0000	1.14
@@ -168,7 +168,7 @@
    <h1>Web Database</h1>
    <!--ZZZ:-->
    <!--<h2 class="no-num no-toc">W3C Working Draft 23 April 2009</h2>-->
-   <h2 class="no-num no-toc" id="editor-s-draft-date-1-january-1970">Editor's Draft 17 August 2009</h2>
+   <h2 class="no-num no-toc" id="editor-s-draft-date-1-january-1970">Editor's Draft 18 August 2009</h2>
    <!--:ZZZ-->
    <dl><!-- ZZZ: update the month/day (twice), (un)comment out
     <dt>This Version:</dt>
@@ -235,7 +235,7 @@
   specification's progress along the W3C Recommendation track.
   <!--ZZZ:-->
   <!--This specification is the 23 April 2009 Working Draft.-->
-  This specification is the 17 August 2009 Editor's Draft.
+  This specification is the 18 August 2009 Editor's Draft.
   <!--:ZZZ-->
   </p><!-- required patent boilerplate --><p>This document was produced by a group operating under the <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5
   February 2004 W3C Patent Policy</a>. W3C maintains a <a href="http://www.w3.org/2004/01/pp-impl/42538/status" rel="disclosure">public list of
@@ -283,7 +283,73 @@
         * deleting databases
         * determining how much storage room is left
         * handling the database getting corrupted
-  --><h2 id="introduction"><span class="secno">1 </span>Introduction</h2><p><i>This section is non-normative.</i><p class="XXX">...</p><!-- include an example that does something like the following to show
+  --><h2 id="introduction"><span class="secno">1 </span>Introduction</h2><p><i>This section is non-normative.</i><p>This specification introduces a set of APIs to manipulate
+  client-side databases using SQL.<p>The API is asynchronous, so authors are likely to find anonymous
+  functions (lambdas) very useful in using this API.<p>Here is an example of a script using this API. First, a function
+  <code title="">prepareDatabase()</code> is defined. This function
+  tries to create the database if necessary, giving it one table
+  called "docids" with two columns ("id" and "name"). If it is
+  successful, or if the table doesn't need creating, it calls a
+  section function, <code title="">getDatabase()</code>, which obtains
+  a handle to the database, and then calls the function to do the
+  actual work, in this case <code title="">showDocCount()</code>.<pre>function prepareDatabase(ready, error) {
+  // first open the database with no version to see if it exists
+  var db = openDatabase('documents', '', 'Offline document storage', 5*1024*1024);
+  if (db.version == '') {
+    // database didn't exist
+    db.changeVersion('', '1.0', function (t) {
+      // create the tables
+      t.executeSql('CREATE TABLE docids (id, name)');
+    }, function (e) {
+      // in case of error:
+      if (db.version == '1.0') {
+        // the database got upgraded while we were trying to do it.
+        // (there's a race condition between us checking db.version and
+        // calling changeVersion(), so this is possible if the user opened this
+        // page twice at the same time)
+        // let's try reopening it
+        getDatabase(ready, error);
+      } else {
+        // some other error occurred
+        error(e);
+      }
+    }, function () {
+      // in case of success:
+      getDatabase(ready, error);
+    });
+  } else {
+    getDatabase(ready, error);
+  }
+}
+
+function getDatabase(ready, error) {
+  try {
+    ready(openDatabase('documents', '1.0', 'Offline document storage', 5*1024*1024);
+  } catch (e) {
+    error(e);
+  }
+}
+
+function showDocCount(db, span) {
+  db.readTransaction(function (t) {
+    t.executeSql('SELECT COUNT(*) FROM docids', [], function (t, r) {
+      span.textContent = rows.count;
+    }, function (t, e) {
+      // couldn't read database
+      span.textContent = '(unknown: ' + e.message + ')';
+    });
+  });
+}
+
+prepareDatabase(function(db) {
+  // got database
+  var span = document.getElementById('doc-count');
+  showDocCount(db, span);
+}, function (e) {
+  // error getting database
+  alert(e.message);
+});</pre><!-- XXX
+include an example that does something like the following to show
 you should never embed strings straight into the statement, even when you
 have a variable and unknowable number of literals coming:
    var q = "";
Received on Tuesday, 18 August 2009 01:14:30 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Saturday, 18 December 2010 06:14:09 GMT