1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
4 Copyright 2007, Google Inc.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 3. Neither the name of Google Inc. nor the names of its contributors may be
15 used to endorse or promote products derived from this software without
16 specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 <title>Database Module API
</title>
33 <link rel=
"stylesheet" type=
"text/css" href=
"gears.css" />
38 <h1>Database Module API
</h1>
40 <div id=
"pagecontent">
42 <p>The Database module provides browser-local relational data storage to
43 your JavaScript web application. Google Gears uses the open source
44 <a href=
"http://www.google.com/url?sa=D&q=http://www.sqlite.org/">SQLite
</a>
49 <li><a href=
"#overview">Overview
</a></li>
50 <li><a href=
"#example">Example
</a></li>
51 <li><a href=
"#classes">Classes
54 <li><a href=
"#Database" >Database
</a></li>
55 <li><a href=
"#ResultSet">ResultSet
</a></li>
57 <li><a href=
"#directories">Location of Database Files
</a> </li>
58 <li><a href=
"#sqlite_changes">Local Modifications to SQLite
</a> </li>
59 <li><a href=
"#sqlite_fts">Full-Text Search
</a> </li>
63 <h2 id=
"overview">Overview
</h2>
64 <p>The Database module is used to persistently store an application user's data on the user's computer. Data is stored using the same-origin security policy, meaning that a web application cannot access data outside of its domain (see
<a href=
"security.html#model">Security
</a>).
</p>
65 <p>Data is stored and retrieved by executing SQL statements. For information on the SQL syntax supported, see the SQLite document
"<a href=
"http://www.google.com/url?sa=D&q=http://www.sqlite.org/lang.html">SQL as Understood By SQLite
</a>", and also
<a href=
"#sqlite_changes">local modifications to SQLite
</a>, below. Google Gears includes SQLite's
<a href=
"#sqlite_fts">full-text search
</a> extension fts2.
</p>
66 <h4>Security considerations
</h4>
67 <p>SQL statements passed to
<code>execute()
</code> can and should use bind parameters (
<code>?
</code>) to prevent SQL injection attacks. Read about
<a href=
"security.html#database">database security
</a> best practices on the Security page.
</p>
68 <h4>Permission from user
</h4>
69 <p>The database module stores persistent data on the user's disk. It is important that your application request the user's explicit permission to do so, to prevent inadvertently storing data on an untrusted computer.
</p>
72 <h2 id=
"example">Example
</h2>
74 <pre><code><script
type=
"text/javascript" src=
"<a href='tools.html#gears_init'>gears_init.js</a>"></script
>
75 <script
type=
"text/javascript">
76 var db = google.gears.factory.create('beta.database');
77 db.open('database-test');
78 db.execute('create table if not exists Test' +
79 ' (Phrase text, Timestamp int)');
80 db.execute('insert into Test values (?, ?)', ['Monkey!', new Date().getTime()]);
81 var rs = db.execute('select * from Test order by Timestamp desc');
83 while (rs.isValidRow()) {
84 alert(rs.field(
0) + '@' + rs.field(
1));
88 </script
></code></pre>
91 <h2 id=
"classes">Classes
</h2>
93 <pre><code><a href=
"#Database">Database class
</a>
94 void
<b>open
</b>([name])
95 ResultSet
<b>execute
</b>(sqlStatement, [argArray])
97 readonly attribute int
<b>lastInsertRowId
</b>
100 <pre><code><a href=
"#ResultSet">ResultSet class
</a>
101 boolean
<b>isValidRow
</b>()
104 int
<b>fieldCount
</b>()
105 string
<b>fieldName
</b>(int fieldIndex)
106 variant
<b>field
</b>(int fieldIndex)
107 variant
<b>fieldByName
</b>(string fieldName)
112 <!------------------------------------------------------------->
114 <!------------------------------------------------------------->
116 <a name=
"Database"></a>
117 <h2>Database class
</h2>
122 <th colspan=
"2"><a href=
"#Database-open" name=
"Database-open" class=
"code">open([name])
</a></th>
125 <td width=
"113">Return value:
</td>
126 <td width=
"489" class=
"code">void
</td>
131 <code>name
</code> - optional.
136 <td class=
"" >Throws an exception if an error occurs.
<br><br>
137 <i>Currently the name, if supplied and of length greater than zero, must consist only of visible ASCII characters excluding the following
140 </i><kbd>/ \ : * ?
" < > | ; ,
</kbd><br />
142 <i>Otherwise, open() will throw an exception. Before finalization of the API we
143 expect to remove this restriction.
</i></td>
146 <td>Description:
</td>
148 Opens the database
<code>name
</code>, or an unnamed database if
<code>name
</code> is omitted.
<code>name
</code> is local to the application's origin (see
<a href=
"security.html#model">Security
</a>).
154 <th colspan=
"2"><a href=
"#Database-execute" name=
"Database-execute" class=
"code">execute(sqlStatement, [argArray])
</a></th>
157 <td width=
"113">Return Value
</td>
158 <td width=
"489" class=
"code"><a href=
"#ResultSet">ResultSet
</a></td>
162 <td class=
"odd"><code>sqlStatement
</code> is a string containing a SQL statement, with
<code>?
</code> as a placeholder for bind parameters.
<br />
164 <code>argArray
</code> is an optional array of bind parameters to be substituted for the placeholders.
</td>
169 Throws an exception if the SQL statement fails to execute. See the exception object's
<code>message
</code> attribute for details.
171 <p class=
"note">Note: If multiple processes (including
<a href=
"api_workerpool.html">Workers
</a>) attempt to write to the
172 database at the same time, one can fail. It is up to the application to retry in these situations.
</p>
177 Substitute zero or more bind parameters from
<code>argArray
</code> into
<code>sqlStatement
</code> and execute the resulting SQL statement. There must be exactly as many items in
<code>argArray
</code> as their are
<code>?
</code> placeholders in
<code>sqlStatement
</code>.
<code>argArray
</code> can be omitted if there are no placeholders. The results of executing the statement are returned in a
<a href=
"#ResultSet">ResultSet
</a>.
<br/>
179 <code>open()
</code> must be called and must return successfully before calling
<code>execute().
</code><br/>
180 <h4>Example:
</h4><br/>
181 <code>resultSet = db.execute (
<br/>
182 'INSERT INTO MYTABLE VALUES (?, ?, ?) WHERE id=?',
<br/>
183 [
1,
2, 'three four',
5]
<br/>
186 SQLite automatically quote-escapes bind parameters, so in execution the statement expands to:
<br />
187 <code class=
"code">'INSERT INTO MYTABLE VALUES (
1,
2,
"three four") WHERE id=
5'
<br />
190 For information on SQL syntax, see the SQLite documentation
"<a href=
"http://www.google.com/url?sa=D&q=http://www.sqlite.org/lang.html">SQL as Understood By SQLite
</a>"
197 <th colspan=
"2"><a href=
"#Database-close" name=
"Database-close" class=
"code">close()
</a></th>
200 <td width=
"113">Return value:
</td>
201 <td width=
"489" class=
"code">void
</td>
206 <td class=
"" >Throws an exception if an error occurs.
</td>
209 <td>Description:
</td>
210 <td class=
"odd">Closes the database connection, if any, currently associated with this Database
211 instance. Calling Database.close() is not required.
<br />
217 <th colspan=
"2"><a href=
"#Database-lastInsertRowId" name=
"Database-lastInsertRowId" class=
"code">lastInsertRowId
</a></th>
220 <td width=
"113">Return value:
</td>
221 <td width=
"489" class=
"code">readonly attribute int
</td>
225 <td>Description:
</td>
226 <td class=
"odd">Represents the
<span class=
"code">rowid
</span> of the most recent insert on this Database instance.
<br />
228 inserts have ever occurred on this instance.
<br />
231 all rows in every table have a
<span class=
"code">rowid
</span>, even rows in tables that do not have
232 integer-type primary keys. Thus, every successful insert updates this
240 <a name=
"ResultSet"></a>
241 <h2>ResultSet class
</h2>
243 <p class=
"backtotop"><a href=
"#top">Back to top
</a></p>
245 A
<code>ResultSet
</code> is returned from a successful call to
<code>Database.execute()
</code>. It contains the results of executing the SQL statement.
</p>
246 <p>A
<code>ResultSet
</code> is immutable, subsequent changes to the
247 underlying database do not affect the contents.
</p>
248 <p>Iterate over the rows of the result set using
<code>isValidRow()
</code>,
<code>next()
</code>, and
<code>close()
</code>, calling data extraction methods for valid rows. For example:
250 <pre><code>while (rs.isValidRow()) {
251 console.log(rs.fieldName(
0) +
" == " + rs.field(
0));
254 rs.close();
</code></pre>
260 <th colspan=
"2"><a href=
"#ResultSet-isValidRow" name=
"ResultSet-isValidRow" class=
"code">isValidRow()
</a></th>
263 <td width=
"113">Return value:
</td>
264 <td width=
"489" class=
"code">boolean
</td>
268 <td>Description:
</td>
269 <td class=
"odd">Returns true if you can call
<a href=
"#extraction">data extraction methods
</a>.
</td>
275 <th colspan=
"2"><a href=
"#ResultSet-next" name=
"ResultSet-next" class=
"code">next()
</a></th>
278 <td width=
"113">Return value:
</td>
279 <td width=
"489" class=
"code">void
</td>
282 <td>Description:
</td>
283 <td class=
"odd">Advances to the next row of the results.
</td>
289 <th colspan=
"2"><a href=
"#ResultSet-close" name=
"ResultSet-close" class=
"code">close()
</a></th>
292 <td width=
"113">Return value:
</td>
293 <td width=
"489" class=
"code">void
</td>
297 <td class=
"odd">Throws an exception if an error occurs.
</td>
300 <td>Description:
</td>
301 <td class=
"odd">Releases the state associated with this result set
<br/>
303 You are required to call close() when you are finished with any result set.
<br/>
305 <em>Note: there is currently a feature request to have close called automatically when the result set goes out of scope.
</em>
312 <a name=
"extraction" id=
"extraction"></a>
313 <h3>Methods for extracting data
</h3>
317 <th colspan=
"2"><a href=
"#ResultSet-fieldCount" name=
"ResultSet-fieldCount" class=
"code">fieldCount()
</a></th>
320 <td width=
"113">Return value:
</td>
321 <td width=
"489" class=
"code">int
</td>
325 <td>Description:
</td>
326 <td class=
"odd">Returns the
<i>number
</i> of fields in this result set.
</td>
332 <th colspan=
"2"><a href=
"#ResultSet-fieldName" name=
"ResultSet-fieldName" class=
"code">fieldName(int fieldIndex)
</a></th>
335 <td width=
"113">Return value:
</td>
336 <td width=
"489" class=
"code">string
</td>
340 <td class=
"" >Throws an exception if
<code>fieldIndex
</code> is out of range.
</td>
344 <td class=
"odd"><code>fieldIndex
</code> : the zero-based index of the desired field
</td>
347 <td>Description:
</td>
348 <td class=
"odd">Returns the
<i>name
</i> of the specified field in the
349 current result set. This name is derived from the SQL statement which was executed.
</td>
355 <th colspan=
"2"><a href=
"#ResultSet-field" name=
"ResultSet-field" class=
"code">field(int fieldIndex)
</a></th>
358 <td width=
"113">Return value:
</td>
359 <td width=
"489" class=
"code">variant
</td>
363 <td class=
"" >Throws an exception if
<code>fieldIndex
</code> is out of range.
</td>
367 <td class=
"odd"><code>fieldIndex
</code> : the zero-based index of the desired field
</td>
370 <td>Description:
</td>
371 <td class=
"odd">Returns the
<i>contents
</i> of the specified field in the
377 <th colspan=
"2"><a href=
"#ResultSet-fieldByName" name=
"ResultSet-fieldByName" class=
"code">fieldByName(string fieldName)
</a></th>
380 <td width=
"113">Return value:
</td>
381 <td width=
"489" class=
"code">variant
</td>
385 <td class=
"" >Throws an exception if
<code>fieldName
</code> names a nonexistent field.
</td>
389 <td class=
"odd"><code>fieldName
</code>: the name of the desired field
</td>
392 <td>Description:
</td>
393 <td class=
"odd">Returns the
<i>contents
</i> of the specified field in the
397 <a name=
"directories" id=
"directories"></a><br />
400 <h2>Location of Database File
</h2>
401 <p>Database files that your application creates are stored on the user's computer in a location that is determined by the browser being used and the platform.
</p>
402 <p><strong>Windows Vista - Internet Explorer
</strong></p>
403 <p> Location: {FOLDERID_LocalAppDataLow}\Google\Google Gears for Internet Explorer
<br />
404 Example: C:\Users\Bob\AppData\LocalLow\Google\Google Gears for Internet Explorer
</p>
405 <p><strong>Windows Vista - Firefox
</strong>- Database files are stored in the user local profile directory.
</p>
406 <p>Location: C:\Users\
<username
>\AppData\Local\Mozilla\Firefox\Profiles\{profile}.default\Google Gears for Firefox
<br />
407 Example: C:\Users\Bob\AppData\Local\Mozilla\Firefox\Profiles\uelib44s.default\Google Gears for Firefox
</p>
408 <p><strong>Windows XP - Internet Explorer
</strong>- Database files are stored in the user local profile directory.
</p>
409 <p> Location: C:\Documents and Settings\
<username
>\Local Settings\Application Data\Google\Google Gears for Internet Explorer
<br />
410 Example: C:\Documents and Settings\Bob\Local Settings\Application Data\Google\Google Gears for Internet Explorer
</p>
411 <p><strong> Windows XP - Firefox
</strong>- Database files are stored in the user local profile directory.
</p>
412 <p>Location: C:\Documents and Settings\
<username
>\Local Settings\Application Data\Mozilla\Firefox\Profiles\{profile}\Google Gears for Firefox
<br />
413 Example: C:\Documents and Settings\Bob\Local Settings\Application Data\Mozilla\Firefox\Profiles\uelib44s.default\Google Gears for Firefox
</p>
414 <p><strong>Mac OS/X - Firefox
</strong>- Database files are stored in the user local profile directory.
</p>
415 <p>Location: Users/
<username
>/Library/Caches/Firefox/Profiles/{profile}.default/Google Gears for Firefox
<br />
416 Example: Users/Bob/Library/Caches/Firefox/Profiles/
08ywpi3q.default/Google Gears for Firefox
</p>
417 <p><strong>Linux - Firefox
</strong>- Database files are stored in the user home directory.
</p>
418 <p>Location: ~
<em>bob
</em>/.mozilla/firefox/
<firefox's profile id
>/Google Gears for Firefox
<br />
419 Example: ~bob/.mozilla/firefox/
08ywpi3q.default/Google Gears for Firefox
</p>
421 <a name=
"sqlite_changes" id=
"sqlite_changes"></a><br />
424 <h2>Local Modifications to SQLite
</h2>
425 <p>This section describes modifications that Google Gears makes to SQLite.
</p>
426 <h4>Attached databases
</h4>
427 <p>The SQLite
<a href=
"http://www.sqlite.org/lang_attach.html">ATTACH
428 and DETACH
</a> commands can be used to open an arbitrary SQLite
429 database on the user's disk. For this reason, these commands have
430 been disabled in Google Gears. In the future this functionality may
431 be exposed in a way which respects the same-origin security
434 <h4>PRAGMA settings
</h4>
436 <p>The SQLite
<a href=
"http://www.sqlite.org/pragma.html">PRAGMA
</a>
437 command allows setting and inspection of various platform settings,
438 including certain settings which could potentially be used to
439 compromise security. At this time, Google Gears disables PRAGMA,
440 though it is possible that specific PRAGMA uses may be re-enabled or
441 exposed in the Database interface.
</p>
443 <p>The default PRAGMA settings for Google Gears which differ from the
446 <li>PRAGMA encoding = 'UTF-
8';
<br/>
447 This controls the encoding used to store textual data on disk.
448 UTF-
16 encoding on disk is almost never a win on desktop
449 machines, it is generally faster to read the smaller amount of
450 UTF-
8 data and decode it on the fly.
452 <li>PRAGMA auto_vacuum =
1;
<br/>
453 Over time database files can become filled with gaps where data
454 has been deleted. These gaps can be recovered with the VACUUM
455 command, but VACUUM can lock the database for extended periods
456 of time, making it a challenge to integrate into interactive
457 applications. auto_vacuum mode recovers these gaps
458 incrementally as they are generated.
460 <li>PRAGMA page_size =
4096;
<br/>
461 Desktop operating systems mostly have default virtual memory and
462 disk block sizes of
4k and higher.
464 <li>PRAGMA cache_size =
2048;
<br/>
465 Provides
8M of page cache.
467 <li>PRAGMA synchronous = NORMAL;
<br/>
468 Synchronous controls whether data is synchronized to disk before
469 COMMIT commands return (commands not in transactions are
470 implicitly wrapped in one). Setting synchronous to OFF can
471 provide a significant performance boost, at the expense of
472 potential data corruption. Much of the benefit of turning
473 synchronous off can generally be achieved by using a combination
474 of large transactions and WorkerPool.
478 <a name=
"sqlite_fts" id=
"sqlite_fts"></a><br />
481 <h2>Full-Text Search
</h2>
483 <p>Google Gears includes an SQLite extension called
<a
484 href=
"http://www.sqlite.org/cvstrac/wiki?p=FtsTwo">fts2
</a>, for
485 "Full-Text Search". fts2 allows you to create a table and search for words in TEXT data.
486 An fts2 table is created as follows:
</p>
488 <pre><code>db.execute('CREATE VIRTUAL TABLE recipe USING fts2(dish, ingredients)');
</code></pre>
490 <p>This creates an fts2 table named 'recipe', with fields 'dish' and
491 'ingredients'. All fts2 fields are of type TEXT. Data in the table
492 is manipulated using standard SQL commands such as INSERT, UPDATE, and
493 DELETE. Like other SQLite tables, the fts2 table has an implicit
494 unique rowid field, which acts as a unique index.
</p>
495 <p>fts2 tables have some unique properties:
</p>
497 <li> No indices other
498 than the rowid and full-text indices are allowed.
</li>
499 <li> fts2 tables contain a special field with the same
500 name as the table. This field is used in search queries, as described below.
</li>
501 <li>Because the special field exists, is important to always list the specific fields being
502 inserted in INSERT statements, and list specific result fields in
503 SELECT statements.
<em>SELECT * will throw exceptions when run on an fts2
506 <h3>Full-text Query Syntax
</h3>
507 <p>To query using the full-text index, use the MATCH operator as
510 <pre><code><table_or_field_name
> MATCH
<query
></code></pre>
513 <li>If
<code><table_or_field_name
></code> is the name of the
514 table, then the match is done against all fields of the table.
</li>
516 <code><table_or_field_name
></code> is the name of a field, then
517 the match is done against that field.
</li>
522 returns the names of all recipes which include 'tomatoes' in any
526 <pre><code>var rs = db.execute('SELECT dish FROM recipe WHERE recipe MATCH ?', ['tomatoes']);
</code></pre>
528 <p>Examples of fts2 query syntax:
</p>
530 <dt>cheese tomatoes
</td>
531 <dd>Find rows containing 'cheese' and 'tomatoes' in any field.
</dd>
532 <dt>dish:stew tomatoes
</td>
533 <dd>Find rows with 'stew' in the
<code>dish
</code> field, and 'tomatoes' in any field.
</dd>
534 <dt>cheese OR tomatoes
</td>
535 <dd>Find rows containing either 'cheese' or 'tomatoes' in any field. Note that OR operator must be capitalized.
</dd>
536 <dt>"green tomatoes"</td>
537 <dd>Find rows where 'green' is immediately followed by 'tomatoes', in any single field.
</dd>
538 <dt>cheese -tomatoes
</td>
539 <dd>Find rows containing 'cheese' in any field, and not containing 'tomatoes' in any field.
</dd>
541 <dd>Find rows containing words which start with 'ch', including rows containing 'cheese' or 'chowder'. The '*' must come at the end of the word.
</dd>
544 <h3>Using Indices with fts2
</h3>
545 <p>fts2 tables are restricted to contain only TEXT fields and the
546 full-text index. To simulate additional indices or non-TEXT fields,
547 an auxiliary table can be used:
</p>
549 <pre><code>db.execute('CREATE TABLE recipe_aux (dish TEXT PRIMARY KEY, rating INTEGER)');
550 db.execute('CREATE VIRTUAL TABLE recipe USING fts2(dish, ingredients)');
</code></pre>
552 <p>A logical row is split across the
<code>recipe
</code> and
553 <code>recipe_aux
</code> tables, joined on the rowid. The
554 <code>recipe_aux
</code> table constrains
<code>dish
</code> to be
555 unique, and adds field
<code>rating
</code>, which will not be included
556 in the full-text index. For example, to search for recipes with
557 'cheese' that have
<code>rating
</code> higher than
3:
</p>
559 <pre><code>var rs = db.execute('SELECT recipe.rowid FROM recipe, recipe_aux ' +
560 ' WHERE recipe.rowid = recipe_aux.rowid AND recipe_aux.rating
> ? AND recipe MATCH ?',
561 [
3, 'cheese']);
</code></pre>
563 <p>Insertions, deletions, and updates should be done within
564 transactions to keep the tables consistent:
</p>
566 <pre><code>db.execute('BEGIN');
567 db.execute('INSERT INTO recipe_aux (dish, rating) VALUES (?, ?)', ['soup',
3]);
568 db.execute('INSERT INTO recipe (rowid, dish, ingredients) ' +
569 'VALUES (last_insert_rowid(), ?, ?)',
570 ['soup', 'meat carrots celery noodles']);
571 db.execute('COMMIT');
</code></pre>