Copyright © 2010 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
This specification defines an API for storing data in databases that can be queried using a variant of SQL.
Beware. This specification is no longer in active maintenance and the Web Applications Working Group does not intend to maintain it further.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the most recently formally published revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document is the 18 November 2010 Working Group Note of Web SQL Database. Publication as a Working Group Note does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress. The W3C Web Applications Working Group is the W3C working group responsible for this document.
This document was on the W3C Recommendation track but specification work has stopped. The specification reached an impasse: all interested implementors have used the same SQL backend (Sqlite), but we need multiple independent implementations to proceed along a standardisation path.
The Web Applications Working Group continues work on two other storage-related specifications: Web Storage and Indexed Database API.
Implementors should be aware that this specification is not stable. Implementors who are not taking part in the discussions are likely to find the specification changing out from under them in incompatible ways. Vendors interested in implementing this specification should join the aforementioned mailing lists and take part in the discussions.
If you wish to make comments regarding this document, please send them to public-webapps@w3.org (subscribe, archives) or whatwg@whatwg.org (subscribe, archives), or submit them using our public bug database. All feedback is welcome.
The latest stable version of the editor's draft of this specification is always available on the W3C CVS server. Change tracking for this document is available at the following location:
This specification is automatically generated from the corresponding section in the HTML5 specification's source document, as hosted in the WHATWG Subversion repository. Detailed change history for all of HTML5, including the parts that form this specification, can be found at the following locations:
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This section is non-normative.
This specification introduces a set of APIs to manipulate client-side databases using SQL.
The API is asynchronous, so authors are likely to find anonymous functions (lambdas) very useful in using this API.
Here is an example of a script using this API. First, a function
prepareDatabase()
is defined. This function
returns a handle to the database, first creating the database if
necessary. The example then calls the function to do the actual
work, in this case showDocCount()
.
function prepareDatabase(ready, error) { return openDatabase('documents', '1.0', 'Offline document storage', 5*1024*1024, function (db) { db.changeVersion('', '1.0', function (t) { t.executeSql('CREATE TABLE docids (id, name)'); }, error); }); } function showDocCount(db, span) { db.readTransaction(function (t) { t.executeSql('SELECT COUNT(*) AS c FROM docids', [], function (t, r) { span.textContent = r.rows[0].c; }, 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); });
The executeSql()
method has
an argument intended to allow variables to be substituted into
statements without risking SQL injection vulnerabilities:
db.readTransaction(function (t) { t.executeSql('SELECT title, author FROM docs WHERE id=?', [id], function (t, data) { report(data.rows[0].title, data.rows[0].author); }); });
Sometimes, there might be an arbitrary number of variables to substitute in. Even in these case, the right solution is to construct the query using only "?" characters, and then to pass the variables in as the second argument:
function findDocs(db, resultCallback) { var q = ""; for each (var i in labels) q += (q == "" ? "" : ", ") + "?"; db.readTransaction(function (t) { t.executeSql('SELECT id FROM docs WHERE label IN (' + q + ')', labels, function (t, data) { resultCallback(data); }); }); }
All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.
The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC2119. For readability, these words do not appear in all uppercase letters in this specification. [RFC2119]
Requirements phrased in the imperative as part of algorithms (such as "strip any leading space characters" or "return false and abort these steps") are to be interpreted with the meaning of the key word ("must", "should", "may", etc) used in introducing the algorithm.
Some conformance requirements are phrased as requirements on attributes, methods or objects. Such requirements are to be interpreted as requirements on user agents.
Conformance requirements phrased as algorithms or specific steps may be implemented in any manner, so long as the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be easy to follow, and not intended to be performant.)
The only conformance class defined by this specification is user agents.
User agents may impose implementation-specific limits on otherwise unconstrained inputs, e.g. to prevent denial of service attacks, to guard against running out of memory, or to work around platform-specific limitations.
When support for a feature is disabled (e.g. as an emergency measure to mitigate a security problem, or to aid in development, or for performance reasons), user agents must act as if they had no support for the feature whatsoever, and as if the feature was not mentioned in this specification. For example, if a particular feature is accessed via an attribute in a Web IDL interface, the attribute itself would be omitted from the objects that implement that interface — leaving the attribute on the object but making it return null or throw an exception is insufficient.
This specification relies on several other underlying specifications.
Many fundamental concepts from HTML are used by this specification. [HTML]
The IDL blocks in this specification use the semantics of the WebIDL specification. [WEBIDL]
The construction "a Foo
object", where
Foo
is actually an interface, is sometimes
used instead of the more accurate "an object implementing the
interface Foo
".
The term DOM is used to refer to the API set made available to
scripts in Web applications, and does not necessarily imply the
existence of an actual Document
object or of any other
Node
objects as defined in the DOM Core
specifications. [DOMCORE]
An IDL attribute is said to be getting when its value is being retrieved (e.g. by author script), and is said to be setting when a new value is assigned to it.
The term "JavaScript" is used to refer to ECMA262, rather than the official term ECMAScript, since the term JavaScript is more widely known. [ECMA262]
Each origin has an associated set of databases. Each database has a name and a current version. There is no way to enumerate or delete the databases available for an origin from this API.
Each database has one version at a time; a database can't exist in multiple versions at once. Versions are intended to allow authors to manage schema changes incrementally and non-destructively, and without running the risk of old code (e.g. in another browser window) trying to write to a database with incorrect assumptions.
[Supplemental, NoInterfaceObject] interface WindowDatabase { Database openDatabase(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize, in optional DatabaseCallback creationCallback); }; Window implements WindowDatabase; [Supplemental, NoInterfaceObject] interface WorkerUtilsDatabase { Database openDatabase(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize, in optional DatabaseCallback creationCallback); DatabaseSync openDatabaseSync(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize, in optional DatabaseCallback creationCallback); }; WorkerUtils implements WorkerUtilsDatabase; [Callback=FunctionOnly, NoInterfaceObject] interface DatabaseCallback { void handleEvent(in Database database); };
The openDatabase()
method on
the Window
and WorkerUtils
interfaces and
the openDatabaseSync()
method on the WorkerUtils
interface take the following
arguments: a database name, a database version, a display name, an
estimated size — in bytes — of the data that will be
stored in the database, and optionally a callback to be invoked if
the database has not yet been created. The callback, if provided, is
intended to be used to call changeVersion()
; the
callback is invoked with the database having the empty string as its
version regardless of the given database version. If the callback is
not provided, the database is created with the given database
version as its version.
When invoked, these methods must run the following steps, with all but the last two steps being run atomically:
The user agent may raise a SECURITY_ERR
exception
instead of returning a Database
object if the request
violates a policy decision (e.g. if the user agent is configured
to not allow the page to open databases).
For the method on the Window
object: let origin be the origin of the
active document of the browsing context
of the Window
object on which the method was
invoked.
For the methods on the WorkerUtils
object: let
origin be the origin of the
scripts in the worker.
If origin is not a scheme/host/port
tuple, then throw a SECURITY_ERR
exception and abort
these steps.
If the database version provided is not the empty string,
and there is already a database with the given name from the origin
origin, but the database has a different
version than the version provided, then throw an
INVALID_STATE_ERR
exception and abort these
steps.
If no database with the given name from the origin origin exists, then create the database and let created be true. If a callback was passed to the method, then set the new database's version to the empty string. Otherwise, set the new database's version to the given database version.
Otherwise, if a database with the given name already exists, let created be false.
For the openDatabase()
methods: let result be a newly constructed
Database
object representing the database with the
given database name from the origin origin.
For the openDatabaseSync()
method:
let result be a newly constructed
DatabaseSync
object representing the database with
the given database name from the origin origin.
If created is false or if no callback was passed to the method, skip this step. Otherwise:
For the openDatabase()
methods: queue a task to to invoke the callback with
result as its only argument.
For the openDatabaseSync()
method:
invoke the callback with result as its only
argument. If the callback throws an exception, rethrow that
exception and abort these steps.
Return result.
All strings including the empty string are valid database names. Database names must be compared in a case-sensitive manner.
Implementations can support this even in environments that only support a subset of all strings as database names by mapping database names (e.g. using a hashing algorithm) to the supported set of names.
The version that the database was opened with is the expected version of
this Database
or DatabaseSync
object. It
can be the empty string, in which case there is no expected version
— any version is fine.
User agents are expected to use the display name and the estimated database size to optimize the user experience. For example, a user agent could use the estimated size to suggest an initial quota to the user. This allows a site that is aware that it will try to use hundreds of megabytes to declare this upfront, instead of the user agent prompting the user for permission to increase the quota every five megabytes.
When the user agent is to preprocess a SQL statement sqlStatement with an array of arguments arguments, it must run the following steps:
Parse sqlStatement as a SQL statement, with the exception that U+003F QUESTION MARK characters (?) can be used in place of SQL literals in the statement. [SQL]
Bind each ?
placeholder with the value of
the argument in the arguments array with the
same position. (So the first ?
placeholder
gets bound to the first value in the arguments
array, and generally the nth ?
placeholder gets bound to the nth value in the arguments
array.)
Binding the ?
placeholders
is done at the literal level, not as string concatenations, so
this provides a way to dynamically insert parameters into a
statement without risk of a SQL injection attack.
The result is the statement.
If the Database
object that the
SQLTransaction
or SQLTransactionSync
object was created from has an expected version
that is neither the empty string nor the actual version of the
database, then mark the statement as
bogus. (Error code
2.)
Otherwise, if the syntax of sqlStatement is
not valid (except for the use of ?
characters in the place of literals), or the statement uses
features that are not supported (e.g. due to security reasons), or
the number of items in the arguments array is
not equal to the number of ?
placeholders in
the statement, or the statement cannot be parsed for some other
reason, then mark the statement as
bogus. (Error code
5.)
User agents must consider statements that use the BEGIN
, COMMIT
, and ROLLBACK
SQL features as being unsupported (and thus
will mark them as bogus), so as to not let these statements
interfere with the explicit transactions managed by the database API
itself.
Otherwise, if the mode that was used to create the
SQLTransaction
or SQLTransactionSync
object is read-only but the statement's main verb can modify the
database, mark the statement as bogus. (Error code 5.)
Only the statement's main verb (e.g. UPDATE
, SELECT
, DROP
) is considered here. Thus, a statement like
"UPDATE test SET id=0 WHERE 0=1
" would be
treated as potentially modifying the database for the purposes
of this step, even though it could never in fact have any
side-effects.
Return the statement.
The user agent must act as if the database was hosted in an otherwise completely empty environment with no resources. For example, attempts to read from or write to the file system will fail.
A future version of this specification will probably define the exact SQL subset required in more detail.
interface Database { void transaction(in SQLTransactionCallback callback, in optional SQLTransactionErrorCallback errorCallback, in optional SQLVoidCallback successCallback); void readTransaction(in SQLTransactionCallback callback, in optional SQLTransactionErrorCallback errorCallback, in optional SQLVoidCallback successCallback); readonly attribute DOMString version; void changeVersion(in DOMString oldVersion, in DOMString newVersion, in optional SQLTransactionCallback callback, in optional SQLTransactionErrorCallback errorCallback, in optional SQLVoidCallback successCallback); }; [Callback=FunctionOnly, NoInterfaceObject] interface SQLVoidCallback { void handleEvent(); }; [Callback=FunctionOnly, NoInterfaceObject] interface SQLTransactionCallback { void handleEvent(in SQLTransaction transaction); }; [Callback=FunctionOnly, NoInterfaceObject] interface SQLTransactionErrorCallback { void handleEvent(in SQLError error); };
The transaction()
and readTransaction()
methods takes one to three arguments. When called, these methods must
immediately return and then asynchronously run the transaction
steps with the transaction callback being the first
argument, the error callback being the second argument, if
any, the success callback being the third argument, if any,
and with no preflight operation or postflight
operation.
For the transaction()
method, the
mode must be read/write. For the readTransaction()
method, the mode must be read-only.
On getting, the version
attribute
must return the current version of the database (as opposed to the
expected
version of the Database
object).
The changeVersion()
method allows scripts to atomically verify the version number and
change it at the same time as doing a schema update. When the method
is invoked, it must immediately return, and then asynchronously run
the transaction steps with the transaction
callback being the third argument, the error callback
being the fourth argument, the success callback being the
fifth argument, the preflight operation being the
following:
Check that the value of the first argument to the changeVersion()
method
exactly matches the database's actual version. If it does not, then
the preflight operation fails.
...the postflight operation being the following:
changeVersion()
method.Database
object's expected version to
the value of the second argument to the changeVersion()
method....and the mode being read/write.
If any of the optional arguments are omitted, then they must be treated as if they were null.
The transaction()
,
readTransaction()
,
and changeVersion()
methods invoke callbacks with SQLTransaction
objects.
typedef sequence<any> ObjectArray; interface SQLTransaction { void executeSql(in DOMString sqlStatement, in optional ObjectArray arguments, in optional SQLStatementCallback callback, in optional SQLStatementErrorCallback errorCallback); }; [Callback=FunctionOnly, NoInterfaceObject] interface SQLStatementCallback { void handleEvent(in SQLTransaction transaction, in SQLResultSet resultSet); }; [Callback=FunctionOnly, NoInterfaceObject] interface SQLStatementErrorCallback { boolean handleEvent(in SQLTransaction transaction, in SQLError error); };
When the executeSql(sqlStatement, arguments, callback, errorCallback)
method is invoked, the
user agent must run the following algorithm. (This algorithm is
relatively simple in that it doesn't actually execute any SQL
— the bulk of the work is actually done as part of the
transaction steps.)
If the method was not invoked during the execution of a
SQLTransactionCallback
,
SQLStatementCallback
, or
SQLStatementErrorCallback
then raise an
INVALID_STATE_ERR
exception. (Calls from inside a
SQLTransactionErrorCallback
thus raise an
exception. The SQLTransactionErrorCallback
handler is
only called once a transaction has failed, and no SQL statements
can be added to a failed transaction.)
Preprocess the SQL statement given as the first argument to the method (sqlStatement), using the second argument to the method as the arguments array, to obtain the statement.
If the second argument is omitted or null, then treat the arguments array as empty.
Queue up the statement in the transaction, along with the third argument (if any) as the statement's result set callback and the fourth argument (if any) as the error callback.
The transaction steps are as follows. These steps must be run asynchronously. These steps are invoked with a transaction callback, optionally an error callback, optionally a success callback, optionally a preflight operation, optionally a postflight operation, and with a mode that is either read/write or read-only.
Open a new SQL transaction to the database, and create a
SQLTransaction
object that represents that
transaction. If the mode is read/write, the transaction must
have an exclusive write lock over the entire database. If the
mode is read-only, the transaction must have a shared read
lock over the entire database. The user agent should wait for an
appropriate lock to be available.
If an error occurred in the opening of the transaction (e.g. if the user agent failed to obtain an appropriate lock after an appropriate delay), jump to the last step.
If a preflight operation was defined for this
instance of the transaction steps, run that. If it fails, then jump
to the last step. (This is basically a hook for the changeVersion()
method.)
If the transaction callback is not null, queue
a task to invoke the transaction callback with the
aforementioned SQLTransaction
object as its only
argument, and wait for that task to be run.
If the callback raised an exception, jump to the last step.
While there are any statements queued up in the transaction, perform the following steps for each queued up statement in the transaction, oldest first. Each statement has a statement, optionally a result set callback, and optionally an error callback.
If the statement is marked as bogus, jump to the "in case of error" steps below.
Execute the statement in the context of the transaction. [SQL]
If the statement failed, jump to the "in case of error" steps below.
Create a SQLResultSet
object that represents
the result of the statement.
If the statement has a result set callback that is not
null, queue a task to invoke it with the
SQLTransaction
object as its first argument and the
new SQLResultSet
object as its second argument, and
wait for that task to be run.
If the callback was invoked and raised an exception, jump to the last step in the overall steps.
Move on to the next statement, if any, or onto the next overall step otherwise.
In case of error (or more specifically, if the above substeps say to jump to the "in case of error" steps), run the following substeps:
If the statement had an associated error callback that is
not null, then queue a task to invoke that error
callback with the SQLTransaction
object and a newly
constructed SQLError
object that represents the
error that caused these substeps to be run as the two arguments,
respectively, and wait for the task to be run.
If the error callback returns false, then move on to the next statement, if any, or onto the next overall step otherwise.
Otherwise, the error callback did not return false, or there was no error callback. Jump to the last step in the overall steps.
If a postflight operation was defined for this instance
of the transaction steps, then: as one atomic operation, commit
the transaction and, if that succeeds, run the postflight
operation. If the commit fails, then instead jump to the last
step. (This is basically a hook for the changeVersion()
method.)
Otherwise: commit the transaction. If an error occurred in the committing of the transaction, jump to the last step.
Queue a task to invoke the success callback, if it is not null.
End these steps. The next step is only used when something goes wrong.
Queue a task to invoke the transaction's
error callback, if it is not null, with a newly constructed
SQLError
object that represents the last error to have
occurred in this transaction. Rollback the transaction. Any
still-pending statements in the transaction are discarded.
The task source for these tasks is the database access task source.
interface DatabaseSync {
void transaction(in SQLTransactionSyncCallback callback);
void readTransaction(in SQLTransactionSyncCallback callback);
readonly attribute DOMString version;
void changeVersion(in DOMString oldVersion, in DOMString newVersion, in optional SQLTransactionSyncCallback callback);
};
[Callback=FunctionOnly, NoInterfaceObject]
interface SQLTransactionSyncCallback {
void handleEvent(in SQLTransactionSync transaction);
};
The transaction()
and readTransaction()
methods must run the following steps:
If the method was the transaction()
method,
create a SQLTransactionSync
object for a
read/write transaction. Otherwise, create a
SQLTransactionSync
object for a read-only
transaction. In either case, if this throws an exception, then
rethrow it and abort these steps. Otherwise, let transaction be the newly created
SQLTransactionSync
object.
If the first argument is null, rollback the transaction,
throw a SQLException
exception, and abort these
steps. (Error code
0.)
Invoke the callback given by the first argument, passing it the transaction object as its only argument.
Mark the SQLTransactionSync
object as stale.
If the callback was terminated by an exception, then rollback the transaction, rethrow that exception, and abort these steps.
Commit the transaction.
If an error occurred in the committing of the transaction,
rollback the transaction, throw a SQLException
exception, and abort these steps.
On getting, the version
attribute must return the current version of the database (as
opposed to the expected version of
the DatabaseSync
object).
The changeVersion()
method allows scripts to atomically verify the version number and
change it at the same time as doing a schema update. When the method
is invoked, it must run the following steps:
Create a SQLTransactionSync
object
for a read/write transaction. If this throws an exception, then
rethrow it and abort these steps. Otherwise, let transaction be the newly created
SQLTransactionSync
object.
Check that the value of the first argument to the changeVersion()
method exactly matches the database's actual version. If it does
not, then throw a SQLException
exception and abort
these steps. (Error code
2.)
If the third argument is not null, invoke the callback given by the third argument, passing it the transaction object as its only argument.
Mark the SQLTransactionSync
object as stale.
If the callback was terminated by an exception, then rollback the transaction, rethrow the exception, and abort these steps.
Commit the transaction.
If an error occurred in the committing of the transaction,
rollback the transaction, throw a SQLException
exception, and abort these steps.
changeVersion()
method.Database
object's expected version to
the value of the second argument to the changeVersion()
method.When the user agent is to create a
SQLTransactionSync
object for a transaction that
is either read/write or read-only, it must run the following
steps:
Open a new SQL transaction to the database, and create a
SQLTransactionSync
object that represents that
transaction. If the mode is read/write, the transaction must
have an exclusive write lock over the entire database. If the
mode is read-only, the transaction must have a shared read
lock over the entire database. The user agent should wait for an
appropriate lock to be available.
If an error occurred in the opening of the transaction
(e.g. if the user agent failed to obtain an appropriate lock after
an appropriate delay), throw a SQLException
exception
and abort these steps.
Return the newly created SQLTransactionSync
object.
The transaction()
, readTransaction()
,
and changeVersion()
methods invoke callbacks that are passed
SQLTransactionSync
objects.
// typedef sequence<any> ObjectArray; interface SQLTransactionSync { SQLResultSet executeSql(in DOMString sqlStatement, in optional ObjectArray arguments); };
A SQLTransactionSync
object is initially fresh, but it will be marked as stale
once it has been committed or rolled back.
When the executeSql(sqlStatement, arguments)
method is invoked, the user
agent must run the following algorithm:
If the SQLTransactionSync
object is stale, then throw an INVALID_STATE_ERR
exception.
Preprocess the SQL statement given as the first argument to the method (sqlStatement), using the second argument to the method as the arguments array, to obtain the statement.
If the second argument is omitted or null, then treat the arguments array as empty.
If the statement is marked as bogus, throw a
SQLException
exception.
Execute the statement in the context of the transaction. [SQL]
If the statement failed, throw a SQLException
exception.
Create a SQLResultSet
object that represents
the result of the statement.
Return the newly created SQLResultSet
object.
The executeSql()
method invokes its callback with a SQLResultSet
object
as an argument.
interface SQLResultSet { readonly attribute long insertId; readonly attribute long rowsAffected; readonly attribute SQLResultSetRowList rows; };
The insertId
attribute must return the row ID of the row that the
SQLResultSet
object's SQL statement inserted into the
database, if the statement inserted a row. If the statement inserted
multiple rows, the ID of the last row must be the one returned. If
the statement did not insert a row, then the attribute must instead
raise an INVALID_ACCESS_ERR
exception.
The rowsAffected
attribute must return the number of rows that were changed by the
SQL statement. If the statement did not affected any rows, then the
attribute must return zero. For "SELECT" statements, this returns
zero (querying the database doesn't affect any rows).
The rows
attribute must return a SQLResultSetRowList
representing the rows returned, in the order returned by the
database. The same object must be returned each time. If no rows
were returned, then the object will be empty (its length
will be
zero).
interface SQLResultSetRowList {
readonly attribute unsigned long length;
getter any item(in unsigned long index);
};
For the asynchronous API, implementors are
encouraged to prefetch all the data for
SQLResultSetRowList
objects when the object is
constructed (before the result set callback is invoked), rather than
on-demand, for better responsiveness. For the synchronous API, an
on-demand lazy evaluation implementation strategy is encouraged
instead, for better performance.
SQLResultSetRowList
objects have a length
attribute that must return the number of rows it represents (the
number of rows returned by the database). This is the length.
Fetching the length
might be
expensive, and authors are thus encouraged to avoid using it (or
enumerating over the object, which implicitly uses it) where
possible.
The object's supported property indices are the numbers in the range zero to length-1, unless the length is zero, in which case there are no supported property indices.
The item(index)
attribute must return the row
with the given index index. If there is no such
row, then the method must return null.
Each row must be represented by a native ordered dictionary data
type. In the JavaScript binding, this must be Object
.
Each row object must have one property (or dictionary entry) per
column, with those properties enumerating in the order that these
columns were returned by the database. Each property must have the
name of the column and the value of the cell, as they were returned
by the database.
Errors in the asynchronous database API are reported using
callbacks that have a SQLError
object as one of their
arguments.
interface SQLError { const unsigned short UNKNOWN_ERR = 0; const unsigned short DATABASE_ERR = 1; const unsigned short VERSION_ERR = 2; const unsigned short TOO_LARGE_ERR = 3; const unsigned short QUOTA_ERR = 4; const unsigned short SYNTAX_ERR = 5; const unsigned short CONSTRAINT_ERR = 6; const unsigned short TIMEOUT_ERR = 7; readonly attribute unsigned short code; readonly attribute DOMString message; };
The code
IDL
attribute must return the most appropriate code from the table
below.
The message
IDL attribute must return an error message describing the error
encountered. The message should be localized to the user's
language.
Errors in the synchronous database API are reported using
SQLException
exceptions:
exception SQLException { const unsigned short UNKNOWN_ERR = 0; const unsigned short DATABASE_ERR = 1; const unsigned short VERSION_ERR = 2; const unsigned short TOO_LARGE_ERR = 3; const unsigned short QUOTA_ERR = 4; const unsigned short SYNTAX_ERR = 5; const unsigned short CONSTRAINT_ERR = 6; const unsigned short TIMEOUT_ERR = 7; unsigned short code; DOMString message; };
The code
IDL attribute must return the most appropriate code from the table
below.
The message
IDL
attribute must return an error message describing the error
encountered. The message should be localized to the user's
language.
The error codes are as follows:
Constant | Code | Situation |
---|---|---|
UNKNOWN_ERR
| 0 | The transaction failed for reasons unrelated to the database itself and not covered by any other error code. |
DATABASE_ERR
| 1 | The statement failed for database reasons not covered by any other error code. |
VERSION_ERR
| 2 | The operation failed because the actual database version was
not what it should be. For example, a statement found that the
actual database version no longer matched the expected version
of the Database or DatabaseSync object,
or the Database.changeVersion()
or DatabaseSync.changeVersion()
methods were passed a version that doesn't match the actual
database version.
|
TOO_LARGE_ERR
| 3 | The statement failed because the data returned from the database was too large. The SQL "LIMIT" modifier might be useful to reduce the size of the result set. |
QUOTA_ERR
| 4 | The statement failed because there was not enough remaining storage space, or the storage quota was reached and the user declined to give more space to the database. |
SYNTAX_ERR
| 5 | The statement failed because of a syntax error, or the number
of arguments did not match the number of ?
placeholders in the statement, or the statement tried to use a
statement that is not allowed, such as BEGIN , COMMIT , or ROLLBACK , or the statement tried to use a verb
that could modify the database but the transaction was read-only.
|
CONSTRAINT_ERR
| 6 | An INSERT , UPDATE , or REPLACE
statement failed due to a constraint failure. For example,
because a row was being inserted and the value given for the
primary key column duplicated the value of an existing row.
|
TIMEOUT_ERR
| 7 | A lock for the transaction could not be obtained in a reasonable time. |
User agents must implement the SQL dialect supported by Sqlite 3.6.19.
When converting bound arguments to SQL data types, the JavaScript ToPrimitive abstract operation must be applied to obtain the raw value to be processed. [ECMA262].
User agents should limit the total amount of space allowed for databases.
User agents should guard against sites storing data under the origins other affiliated sites, e.g. storing up to the limit in a1.example.com, a2.example.com, a3.example.com, etc, circumventing the main example.com storage limit.
User agents may prompt the user when quotas are reached, allowing the user to grant a site more space. This enables sites to store many user-created documents on the user's computer, for instance.
User agents should allow users to see how much space each domain is using.
A mostly arbitrary limit of five megabytes per origin is recommended. Implementation feedback is welcome and will be used to update this suggestion in the future.
A third-party advertiser (or any entity capable of getting content distributed to multiple sites) could use a unique identifier stored in its client-side databases to track a user across multiple sessions, building a profile of the user's interests to allow for highly targeted advertising. In conjunction with a site that is aware of the user's real identity (for example an e-commerce site that requires authenticated credentials), this could allow oppressive groups to target individuals with greater accuracy than in a world with purely anonymous Web usage.
There are a number of techniques that can be used to mitigate the risk of user tracking:
User agents may restrict access to
the database objects
to scripts originating at the domain of the top-level document of
the browsing context, for instance denying access to
the API for pages from other domains running in
iframe
s.
User agents may, if so configured by the user, automatically delete stored data after a period of time.
This can restrict the ability of a site to track a user, as the site would then only be able to track the user across multiple sessions when he authenticates with the site itself (e.g. by making a purchase or logging in to a service).
However, this also reduces the usefulness of the API as a long-term storage mechanism. It can also put the user's data at risk, if the user does not fully understand the implications of data expiration.
If users attempt to protect their privacy by clearing cookies without also clearing data stored in the relevant databases, sites can defeat those attempts by using the two features as redundant backup for each other. User agents should present the interfaces for clearing these in a way that helps users to understand this possibility and enables them to delete data in all persistent storage features simultaneously. [COOKIES]
User agents may require the user to authorize access to databases before a site can use the feature.
User agents may record the origins of sites that contained content from third-party origins that caused data to be stored.
If this information is then used to present the view of data currently in persistent storage, it would allow the user to make informed decisions about which parts of the persistent storage to prune. Combined with a blacklist ("delete this data and prevent this domain from ever storing data again"), the user can restrict the use of persistent storage to sites that he trusts.
User agents may allow users to share their persistent storage domain blacklists.
This would allow communities to act together to protect their privacy.
While these suggestions prevent trivial use of this API for user tracking, they do not block it altogether. Within a single domain, a site can continue to track the user during a session, and can then pass all this information to the third party along with any identifying information (names, credit card numbers, addresses) obtained by the site. If a third party cooperates with multiple sites to obtain such information, a profile can still be created.
However, user tracking is to some extent possible even with no cooperation from the user agent whatsoever, for instance by using session identifiers in URLs, a technique already commonly used for innocuous purposes but easily repurposed for user tracking (even retroactively). This information can then be shared with other sites, using using visitors' IP addresses and other user-specific data (e.g. user-agent headers and configuration settings) to combine separate sessions into coherent user profiles.
User agents should treat persistently stored data as potentially sensitive; it's quite possible for e-mails, calendar appointments, health records, or other confidential documents to be stored in this mechanism.
To this end, user agents should ensure that when deleting data, it is promptly deleted from the underlying storage.
Because of the potential for DNS spoofing attacks, one cannot guarantee that a host claiming to be in a certain domain really is from that domain. To mitigate this, pages can use TLS. Pages using TLS can be sure that only pages using TLS that have certificates identifying them as being from the same domain can access their databases.
Different authors sharing one host name, for example users
hosting content on geocities.com
, all share one
set of databases.
There is no feature to restrict the access by pathname. Authors on
shared hosts are therefore recommended to avoid using these
features, as it would be trivial for other authors to read the data
and overwrite it.
Even if a path-restriction feature was made available, the usual DOM scripting security model would make it trivial to bypass this protection and access the data from any path.
The two primary risks when implementing these persistent storage features are letting hostile sites read information from other domains, and letting hostile sites write information that is then read from other domains.
Letting third-party sites read data that is not supposed to be read from their domain causes information leakage, For example, a user's shopping wishlist on one domain could be used by another domain for targeted advertising; or a user's work-in-progress confidential documents stored by a word-processing site could be examined by the site of a competing company.
Letting third-party sites write data to the persistent storage of other domains can result in information spoofing, which is equally dangerous. For example, a hostile site could add items to a user's wishlist; or a hostile site could set a user's session identifier to a known ID that the hostile site can then use to track the user's actions on the victim site.
Thus, strictly following the origin model described in this specification is important for user security.
User agent implementors are strongly encouraged to audit all
their supported SQL statements for security implications. For
example, LOAD DATA INFILE
is likely to pose
security risks and there is little reason to support it.
In general, it is recommended that user agents not support features that control how databases are stored on disk. For example, there is little reason to allow Web authors to control the character encoding used in the disk representation of the data, as all data in JavaScript is implicitly UTF-16.
Authors are strongly recommended to make use of the ?
placeholder feature of the executeSql()
method,
and to never construct SQL statements on the fly.
All references are normative unless marked "Non-normative".