# Jaxer.DB : Object
Return to: Jaxer Framework index

The namespace that holds functions and other objects for working with a database.

## Platform Support

Jaxer Server Framework | Jaxer Client Framework
--- | ---
1.0 | no

## Properties

Property | Action | Jaxer Server Framework | Jaxer Client Framework
--- | --- | --- | ---
static Connection : Object Holds a reference to the constructor of the current default database connection used for database interactions, e.g. by Jaxer.DB.execute. This is determined by the settings in configApps.js and in config.js. It can be different for different requests. Its type is Jaxer.DB.MySQL.Connection or Jaxer.DB.SQLite.Connection, etc. (Advanced) | No Details | 1.0 | no

Visibility
  advanced

static defaultConnections : Object A hashmap that holds references to the currently-recognized default database connections, according to configApps.js, config.js, and the page requests encountered so far. It's keyed off the (resolved) name of the current app. (Advanced) | No Details | 1.0 | no

Visibility
  advanced

static FrameworkConnection : Object Holds a reference to the constructor of the current default connection used for the Jaxer framework's internal database interactions, e.g. by Jaxer.DB.frameworkExecute. This is determined by the settings in config.js. Its type is Jaxer.DB.MySQL.Connection or Jaxer.DB.SQLite.Connection, etc. (Advanced) | No Details | 1.0 | no

Visibility
  advanced

static lastInsertId : Number
When you INSERT a row that has an AUTO_INCREMENT-type column in the default framework database, the value of that column in this row is automatically set by the database. To know what it was set to (an integer), retrieve the lastInsertId right after you execute the INSERT statement. Under other circumstances, the behavior of lastInsertId depends on the database implementation; for example, SQLite always returns the rowid of the last successfully inserted row, whether or not it had an INTEGER PRIMARY KEY AUTO_INCREMENT column.
(Advanced)

No Details

1.0

no

Visibility
  advanced

static lastInsertRowId : Number
When you INSERT a row that has an AUTO_INCREMENT-type column in the default framework database, the value of that column in this row is automatically set by the database. To know what it was set to (an integer), retrieve the lastInsertRowId right after you execute the INSERT statement. Under other circumstances, the behavior of lastInsertRowId depends on the database implementation; for example, SQLite always returns the rowid of the last successfully inserted row, whether or not it had an INTEGER PRIMARY KEY AUTO_INCREMENT column.
(Advanced)

No Details

1.0

no

Visibility
  advanced

static connection : Object Holds a reference to the current default connection that will be used for database interactions, e.g. by Jaxer.DB.execute. This is determined by the settings in configApps.js and in config.js. It can be different for different requests. Its type is Jaxer.DB.MySQL.Connection or Jaxer.DB.SQLite.Connection, etc. | Show Details | 1.0 | no

See Also

  Jaxer.DB.Connection

static frameworkConnection : Object Holds a reference to the current default connection that will be used for the Jaxer framework's internal database interactions, e.g. by Jaxer.DB.frameworkExecute. This is determined by the settings in config.js. Its type is Jaxer.DB.MySQL.Connection or Jaxer.DB.SQLite.Connection, etc. | Show Details | 1.0 | no

See Also

  Jaxer.DB.FrameworkConnection

static lastInsertId : Number
When you INSERT a row that has an AUTO_INCREMENT-type column in the default developer database, the value of that column in this row is automatically set by the database. To know what it was set to (an integer), retrieve the lastInsertId right after you execute the INSERT statement. Under other circumstances, the behavior of lastInsertId depends on the database implementation; for example, SQLite always returns the rowid of the last successfully inserted row, whether or not it had an INTEGER PRIMARY KEY AUTO_INCREMENT column.

No Details

1.0

no

static lastInsertRowId : Number
When you INSERT a row that has an AUTO_INCREMENT-type column in the default developer database, the value of that column in this row is automatically set by the database. To know what it was set to (an integer), retrieve the lastInsertRowId right after you execute the INSERT statement. Under other circumstances, the behavior of lastInsertRowId depends on the database implementation; for example, SQLite always returns the rowid of the last successfully inserted row, whether or not it had an INTEGER PRIMARY KEY AUTO_INCREMENT column.

No Details

1.0

no

## Functions

Method | Action | Jaxer Server Framework | Jaxer Client Framework
--- | --- | --- | ---
static closeAllConnections(Boolean onlyIfCloseAfterRequest) : void Closes all the default DB connections that are open. This is called by the framework when there is a fatal or response error, or after every request if CLOSE_AFTER_REQUEST is set to true in the connection's configuration parameters (Advanced) | Show Details | 1.0 | no

Parameters

Boolean | onlyIfCloseAfterRequest | Set this to true if you only want to close connections whose CLOSE_AFTER_REQUEST parameter is true

static frameworkExecute(String sql) : Jaxer.DB.ResultSet
Executes the given SQL query string on the default framework database (as defined in Jaxer.Config.DB_FRAMEWORK). If the SQL includes ?'s (question marks) as parameter placeholders, the values of those parameters should be passed in as extra arguments to this function, either as individual arguments or as a single array.
(Advanced)

Show Details

1.0

no

Parameters

String | sql | The SQL to execute.

Returns

Jaxer.DB.ResultSet | The results of the query (which may be an empty resultset)

Examples

       rs = Jaxer.DB.frameworkExecute("SELECT * FROM containers"); rs = Jaxer.DB.frameworkExecute("SELECT * FROM containers
                              WHERE id=? AND name=?", myId, myName); rs = Jaxer.DB.frameworkExecute("SELECT * FROM containers WHERE id=? AND name=?", [
                              myId, myName ] );

See Also

  Jaxer.DB.ResultSet

static initDefault(String name, Object params) : void Initializes a default database connection, which entails creating it (not necessarily opening it) and adding it to the Jaxer.DB.defaultConnections hashmap (Advanced) | Show Details | 1.0 | no

Parameters

String | name | The (resolved) name of the app for which this connection should be used
Object | params | The parameters to use for the database connection. In particular this object must have an IMPLEMENTATION property equal to "MySQL", "SQLite", or other supported databases.

static setDefault(String name, [Object params]) : void Sets the given named connection as the default one from now on (though this is by default set anew for each request). If the given name is not already a recognized (i.e. initialized) connection, it will first be initialized. (Advanced) | Show Details | 1.0 | no

Parameters

String | name | The (resolved) name of the app for which this connection should be used
Object | params | (optional)The parameters to use for the database connection if the name is not already an initialized connection. In particular this object must have an IMPLEMENTATION property equal to "MySQL", "SQLite", or other supported databases.

static sqlToString(Object sql, [Jaxer.Log.ModuleLogger log]) : String Attempts to convert the given sql to a string, if needed, and then trim it. If the conversion fails or the string is empty, throws a Jaxer.Exception. (Advanced) | Show Details | 1.0 | no

Parameters

Object | sql | The sql to convert to a string, if needed
Jaxer.Log.ModuleLogger | log | (optional)The logger into which errors should be logged. Defaults to the DB module's logger.

Returns

String | The string representation of the sql.

static execute(String sql) : Array<Object>|Jaxer.DB.ResultSet|Number
Executes the given SQL query string on the current default database (as defined in configApps.js). If the SQL includes ?'s (question marks) as parameter placeholders, the values of those parameters should be passed in as extra arguments to this function, either as individual arguments or as a single array.

Show Details

1.0

no

Parameters

String | sql | The SQL to execute.

Returns

Array<Object> | The results of the query. For a SELECT-type query, a Jaxer.DB.ResultSet is returned, with 0 or more rows. For an INSERT/UPDATE/DELETE-type query, the number of rows affected is returned. On MySQL only: if multiple queries were issued (or a stored procedure was executed) the result will be a corresponding array of Jaxer.DB.ResultSet or Number objects.
Jaxer.DB.ResultSet | The results of the query. For a SELECT-type query, a Jaxer.DB.ResultSet is returned, with 0 or more rows. For an INSERT/UPDATE/DELETE-type query, the number of rows affected is returned. On MySQL only: if multiple queries were issued (or a stored procedure was executed) the result will be a corresponding array of Jaxer.DB.ResultSet or Number objects.
Number | The results of the query. For a SELECT-type query, a Jaxer.DB.ResultSet is returned, with 0 or more rows. For an INSERT/UPDATE/DELETE-type query, the number of rows affected is returned. On MySQL only: if multiple queries were issued (or a stored procedure was executed) the result will be a corresponding array of Jaxer.DB.ResultSet or Number objects.

Examples

       rs = Jaxer.DB.execute("SELECT * FROM myTable"); rs = Jaxer.DB.execute("SELECT * FROM myTable WHERE id=? AND zip=?",
                              myId, myZip); rs = Jaxer.DB.execute("SELECT * FROM myTable WHERE id=? AND zip=?", [ myId, myZip ] );

See Also

  Jaxer.DB.ResultSet

static mapExecute(String sql, Array arrayOfParameters, [Object options]) : Object Prepares the given SQL query string on the current default database (as defined in configApps.js) and then iteratively executes it over the given array of parameters. | Show Details | 1.0 | no

Parameters

String | sql | The SQL to execute, using ?'s (question marks) as parameter placeholders
Array | arrayOfParameters | An array of parameters to use for each execution. Each element of the array may itself be a single value or an array of values (corresponding to the ?'s in the SQL)
Object | options | (optional)An optional hashmap of options. Currently one option is supported: flatten. If its value is true, the returned result will be a single ResultSet with its rows being the concatenated rows of each query.

Returns

Object | A corresponding array of Jaxer.DB.ResultSet's for each query, or a single Jaxer.DB.ResultSet if the 'flatten' option is true.

Examples

       [ rsA, rsB ] = Jaxer.DB.mapExecute("SELECT * FROM myTable WHERE id=?", [ idA, idB ] ); [ rsA, rsB ] = Jaxer.DB.mapExecute("SELECT
                              * FROM myTable WHERE id=? AND zip=?", [ [ idA, zipA ] , [ idB, zipB ] ] );

See Also

  Jaxer.DB.ResultSet

static mapFrameworkExecute(String sql, Array arrayOfParameters, [Object options]) : Object Prepares the given SQL query string on the default framework database (as defined in Jaxer.Config.DB_FRAMEWORK) and then iteratively executes it over the given array of parameters. | Show Details | 1.0 | no

Parameters

String | sql | The SQL to execute, using ?'s (question marks) as parameter placeholders
Array | arrayOfParameters | An array of parameters to use for each execution. Each element of the array may itself be a single value or an array of values (corresponding to the ?'s in the SQL)
Object | options | (optional)An optional hashmap of options. Currently one option is supported: flatten. If its value is true, the returned result will be a single ResultSet with its rows being the concatenated rows of each query.

Returns

Object | A corresponding array of Jaxer.DB.ResultSet's for each query, or a single Jaxer.DB.ResultSet if the 'flatten' option is true.

Examples

       [ rsA, rsB ] = Jaxer.DB.mapFrameworkExecute("SELECT * FROM containers WHERE id=?", [ idA, idB ] ); [ rsA, rsB ] = Jaxer.DB.mapFrameworkExecute("SELECT
                              * FROM containers WHERE id=? AND name=?", [ [ idA, nameA ] , [ idB, nameB ] ] );

See Also

  Jaxer.DB.ResultSet

aptana_docs