The goog.gears.Database Class

goog.gears.Database
> goog.events.EventTarget
> goog.Disposable

goog.gears.Database(userId, appName)

Class that for accessing a Gears database

userId {string}
the id token for this user.
appName {string}
the name of the application creating this database.

The goog.gears.Database.EventType Enum

Constants for transaction event names. … more

The goog.gears.Database.TransactionEvent Class

Event info for transaction events. … more

.addEventListener(type, handler, opt_capture, opt_handlerScope)

Inherited from goog.events.EventTarget .

Adds an event listener to the event target. The same handler can only be added once per the type. Even if you add the same handler multiple times using the same type then it will only be called once when the event is dispatched. Supported for legacy but use goog.events.listen(src, type, handler) instead.

type {string}
The type of the event to listen for.
handler {Function|Object}
The function to handle the event. The handler can also be an object that implements the handleEvent method which takes the event object as argument.
opt_capture {boolean=}
In DOM-compliant browsers, this determines whether the listener is fired during the capture or bubble phase of the event.
opt_handlerScope {Object=}
Object in whose scope to call the listener.

.addOnDisposeCallback(callback, opt_scope)

Inherited from goog.Disposable .

Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.

callback {!Function}
The callback function.
opt_scope {Object=}
An optional scope to call the callback in.

.begin()

Marks the beginning of a database transaction using the default begin type. Does a real BEGIN operation if useTransactions is true for this database and this is the first opened transaction. This will throw an exception if this is a nested begin and is trying to elevate the begin type from the original BEGIN that was processed.

returns {boolean}
true if the BEGIN has been executed.

.beginDeferred()

Marks the beginning of a deferred database transaction. Does a real BEGIN operation if useTransactions is true for this database and this is the first opened transaction.

returns {boolean}
true if the BEGIN has been executed.

.beginExclusive()

Marks the beginning of an exclusive database transaction. Does a real BEGIN operation if useTransactions is true for this database and this is the first opened transaction. This will throw an exception if this is a nested begin and is trying to elevate the begin type from the original BEGIN that was processed.

returns {boolean}
true if the BEGIN has been executed.

.beginImmediate()

Marks the beginning of an immediate database transaction. Does a real BEGIN operation if useTransactions is true for this database and this is the first opened transaction. This will throw an exception if this is a nested begin and is trying to elevate the begin type from the original BEGIN that was processed.

returns {boolean}
true if the BEGIN has been executed.

.close()

Closes the database.

.commit()

Marks the end of a successful transaction. Will do a real COMMIT if this the last outstanding transaction unless a nested transaction was closed with a ROLLBACK in which case a real ROLLBACK will be performed.

returns {boolean}
true if the COMMIT has been executed.

.creationStack {string}

Inherited from goog.Disposable .

If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.

.dispatchEvent(e)

Inherited from goog.events.EventTarget .

Dispatches an event (or event like object) and calls all listeners listening for events of this type. The type of the event is decided by the type property on the event object. If any of the listeners returns false OR calls preventDefault then this function will return false. If one of the capture listeners calls stopPropagation, then the bubble listeners won't fire.

e {string|Object|goog.events.Event}
Event object.
returns {boolean}
If anyone called preventDefault on the event object (or if any of the handlers returns false this will also return false.

.dispose()

Inherited from goog.Disposable .

Disposes of the object. If the object hasn't already been disposed of, calls {@link #disposeInternal}. Classes that extend {@code goog.Disposable} should override {@link #disposeInternal} in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.

returns {void}
Nothing.

.ensureNoTransaction(opt_logMsgPrefix)

Ensures there is no open transaction upon return. An existing open transaction is rolled back.

opt_logMsgPrefix {string=}
a prefix to the message that is logged when an unexpected open transaction is found.

.execute(sql, var_args)

Execute a sql statement with a set of arguments

sql {string}
The sql statement to execute.
var_args {...*}
The arguments to execute, either as a single array argument or as var_args.
returns {GearsResultSet}
The results.

.forEachRow(sql, f, opt_this, var_args)

For each row in the result set f will be called with the following parameters: row (array of values), rowIndex and columnNames. Rows will continue being processed as long as f returns true.

sql {string}
The SQL statement to execute.
f {Function}
Function to call for each row.
opt_this {Object=}
If present f will be called using this object as 'this'.
var_args {...*}
query params. An array or multiple arguments.

.forEachValue(sql, f, opt_this, var_args)

For each value in the result set f will be called with the following parameters; value, rowIndex, columnIndex, columnName. Values will continue being processed as long as f returns true.

sql {string}
The SQL statement to execute.
f {Function}
Function to call for each value.
opt_this {Object=}
If present f will be called using this object as 'this'.
var_args {...*}
query params. An array or multiple arguments.

.getExecutionTime()

Returns the time in Ms that database operations have currently consumed. This only exists in debug builds, but it still may be useful for goog.gears.Trace.

returns {number}
The time in Ms that database operations have currently consumed.

.getLastInsertRowId()

returns {number}
The id of the last inserted row.

.getParentEventTarget()

Inherited from goog.events.EventTarget .

Returns the parent of this event target to use for bubbling.

returns {goog.events.EventTarget}
The parent EventTarget or null if there is no parent.

.getUseTransactions()

Whether transactions are used for the database

returns {boolean}
true if transactions should be used.

.isDisposed()

Inherited from goog.Disposable .

returns {boolean}
Whether the object has been disposed of.

.isInTransaction()

Returns whether or not we're in a transaction.

returns {boolean}
true if a transaction has been started and is not yet complete.

.isLockedException(ex)

Determines if the exception is a locking error.

ex {Error|string}
The exception object or error string.
returns {boolean}
Whether this is a database locked exception.

.needsRollback()

Returns whether or not the current transaction has a pending rollback. Returns false if there is no current transaction.

returns {boolean}
Whether a started transaction has a rollback pending.

.open()

Opens the database.

.queryArray(sql, var_args)

This calls query on the database and returns the first row as an array

sql {string}
SQL statement.
var_args {...*}
query params. An array or multiple arguments.
returns {Array}
The first row as an array.

.queryArrays(sql, var_args)

This calls query on the database and builds a two dimensional array containing the result.

sql {string}
The SQL statement.
var_args {...*}
Query params. An array or multiple arguments.
returns {Array}
An array of arrays containing the results of the query.

.queryObject(sql, var_args)

This calls query on the database and returns the first row as a hash map where the keys are the column names.

sql {string}
SQL statement.
var_args {...*}
query params. An array or multiple arguments.
returns {Object}
The first row as a hash map.

.queryObjectArray(sql, var_args)

This calls query on the database and builds an array containing hashes

sql {string}
Ths SQL statement.
var_args {...*}
query params. An array or multiple arguments.
returns {Array}
An array of hashes containing the results of the query.

.queryValue(sql, var_args)

This calls query on the database and returns the first value in the first row.

sql {string}
SQL statement.
var_args {...*}
query params. An array or multiple arguments.
returns {(number,string,null)}
The first value in the first row.

.queryValueArray(sql, var_args)

This calls query on the database and returns an array containing the values in the first column. This is useful if the result set only contains one column.

sql {string}
SQL statement.
var_args {...*}
query params. An array or multiple arguments.
returns {Array}
The values in the first column.

.registerDisposable(disposable)

Inherited from goog.Disposable .

Associates a disposable object with this object so that they will be disposed together.

disposable {goog.disposable.IDisposable}
that will be disposed when this object is disposed.

.remove()

Removes the database.

@throws
{Error} This requires Gears 0.5 or newer and will throw an error if called on a too old version of Gears.

.removeEventListener(type, handler, opt_capture, opt_handlerScope)

Inherited from goog.events.EventTarget .

Removes an event listener from the event target. The handler must be the same object as the one added. If the handler has not been added then nothing is done.

type {string}
The type of the event to listen for.
handler {Function|Object}
The function to handle the event. The handler can also be an object that implements the handleEvent method which takes the event object as argument.
opt_capture {boolean=}
In DOM-compliant browsers, this determines whether the listener is fired during the capture or bubble phase of the event.
opt_handlerScope {Object=}
Object in whose scope to call the listener.

.resultSetToArray(rs)

Returns an array of the first row in the result set

rs {GearsResultSet}
the result set returned by execute.
returns {Array}
An array containing the values in the first result set. Returns an empty array if there no matching rows.

.resultSetToArrays(rs)

Returns an array of arrays, where each sub array contains the selected values for each row in the result set. result values

rs {GearsResultSet}
the result set returned by execute.
returns {Array}
An array of arrays. Returns an empty array if there are no matching rows.

.resultSetToObject(rs)

Returns a single hashed object from the result set (the first row), where the column names in the query are used as the members of the object.

rs {GearsResultSet}
the result set returned by execute.
returns {Object}
a hash map with the key-value-pairs from the first row. Returns null is there are no matching rows.

.resultSetToObjectArray(rs)

Returns a array of hash objects, one per row in the result set, where the column names in the query are used as the members of the object.

rs {GearsResultSet}
the result set returned by execute.
returns {Array.<Object>}
An array containing hashes. Returns an empty array if there are no matching rows.

.resultSetToValue(rs)

Returns a single value from the results (first column in first row).

rs {GearsResultSet}
the result set returned by execute.
returns {(number,string,null)}
The first item in the first row of the result set. Returns null if there are no matching rows.

.resultSetToValueArray(rs)

Returns an array containing the first item of each row in a result set. This is useful for query that returns one column

rs {GearsResultSet}
the result set returned by execute.
returns {Array.<Object>}
An array containing the values in the first column Returns an empty array if there are no matching rows.

.rollback(opt_e)

Marks the end of an unsuccessful transaction. Will do a real ROLLBACK if this the last outstanding transaction, otherwise the real ROLLBACK will be deferred until the last outstanding transaction is closed.

opt_e {Error=}
the exception that caused this rollback. If it is provided then that exception is rethown if the rollback does not take place.
returns {boolean}
true if the ROLLBACK has been executed or if we are not using transactions.

.setDefaultBeginType(beginType)

Sets the default begin type.

beginType {string}
The default begin type.

.setParentEventTarget(parent)

Inherited from goog.events.EventTarget .

Sets the parent of this event target to use for bubbling.

parent {goog.events.EventTarget?}
Parent EventTarget (null if none).

.setUseTransactions(b)

Whether transactions are used for the database

b {boolean}
Whether to use transactions or not.

.transact(func)

Executes a function transactionally.

func {Function}
the function to execute transactionally. Takes no params.
returns {Object|number|boolean|string|null|undefined}
the return value of 'func()'.