Database

Moduleejs.db
Definition class Database
InheritanceDatabase inherit Object
Specifiedejscript-1.1
StabilityEvolving.

SQL Database support.

The Database class provides an interface over other database adapter classes such as SQLite or MySQL. Not all the functionality expressed by this API may be implemented by a specific database adapter.


Properties

QualifiersPropertyTypeDescription
public get connectionStringThe database connection string.
public static get setdefaultDatabaseDatabaseThe default database for the application.
public get nameStringThe database name defined via the connection string or constructor.

Database Methods

QualifiersMethod
public addColumn(table: String, column: String, datatype: String, options: Object = null): Void
 Add a column to a table.
public addIndex(table: String, column: String, index: String): Void
 Add an index on a column.
public changeColumn(table: String, column: String, datatype: String, options: Object = null): Void
 Change a column.
public close(): Void
 Close the database connection.
public commit(): Void
 Commit a database transaction.
public createDatabase(name: String, options: Object = null): Void
 Create a new database.
public createTable(table: String, columns: Array = null): Void
 Create a new table.
Database(adapter: String, connectionString: String)
 Initialize a database connection using the supplied database connection string.
public dataTypeToSqlType(dataType: String): String
 Map the database independant data type to a database dependant SQL data type.
public destroyDatabase(name: String): Void
 Destroy a database.
public destroyTable(table: String): Void
 Destroy a table.
public endTransaction(): Void
 End a transaction.
public getColumns(table: String): Array
 Get column information.
public getNumRows(table: String): Number
 Return the number of rows in a table.
public getTables(): Array
 Return list of tables in a database.
public query(cmd: String, tag: String = SQL , trace: Boolean = false): Array
 Execute a SQL command on the database.
public removeColumns(table: String, columns: Array): Void
 Remove columns from a table.
public removeIndex(table: String, index: String): Void
 Remove an index.
public renameColumn(table: String, oldColumn: String, newColumn: String): Void
 Rename a column.
public renameTable(oldTable: String, newTable: String): Void
 Rename a table.
public sql(cmd: String): Array
 Execute a SQL command on the database.
public sqlTypeToDataType(sqlType: String): String
 Map the SQL type to a database independant data type.
public sqlTypeToEjsType(sqlType: String): Type
 Map the SQL type to an Ejscript type class.
public startTransaction(): Void
 Start a new database transaction.
public trace(on: Boolean): Void
 Trace all SQL statements on this database.
public transaction(code: Function): Void
 Execute a database transaction.

Method Detail

public addColumn(table: String, column: String, datatype: String, options: Object = null): Void

Add a column to a table.

Parameters
table: String Name of the table.
column: String Name of the column to add.
datatype: String Database independant type of the column. Valid types are: binary, boolean, date, datetime, decimal, float, integer, number, string, text, time and timestamp.
options: Object Optional parameters. [default: null]

public addIndex(table: String, column: String, index: String): Void

Add an index on a column.

Parameters
table: String Name of the table.
column: String Name of the column to add.
index: String Name of the index.

public changeColumn(table: String, column: String, datatype: String, options: Object = null): Void

Change a column.

Parameters
table: String Name of the table holding the column.
column: String Name of the column to change.
datatype: String Database independant type of the column. Valid types are: binary, boolean, date, datetime, decimal, float, integer, number, string, text, time and timestamp.
options: Object Optional parameters. [default: null]

public close(): Void

Close the database connection.

Description
Database connections should be closed when no longer needed rather than waiting for the garbage collector to automatically close the connection when disposing the database instance.

public commit(): Void

Commit a database transaction.


public createDatabase(name: String, options: Object = null): Void

Create a new database.

Parameters
name: String Name of the database.
options: Object [default: null]
Options
OptionalParameters.

public createTable(table: String, columns: Array = null): Void

Create a new table.

Parameters
table: String Name of the table.
columns: Array Array of column descriptor tuples consisting of name:datatype. [default: null]
Options
OptionalParameters.

public Database(adapter: String, connectionString: String)

Initialize a database connection using the supplied database connection string.

Parameters
adapter: String Database adapter to use. E.g. "sqlite".
connectionString: String Connection string stipulating how to connect to the database. The format is one of the following forms:
  • adapter://host/database/username/password
  • filename
Where adapter specifies the kind of database. Sqlite is currently the only supported adapter. For sqlite connection strings, the abbreviated form is permitted where a filename is supplied and the connection string is assumed to be:
sqlite://localhost/filename

public dataTypeToSqlType(dataType: String): String

Map the database independant data type to a database dependant SQL data type.

Parameters
dataType: String Data type to map.
Returns
The corresponding SQL database type.

public destroyDatabase(name: String): Void

Destroy a database.

Parameters
name: String Name of the database to remove.

public destroyTable(table: String): Void

Destroy a table.

Parameters
table: String Name of the table to destroy.

public endTransaction(): Void

End a transaction.


public getColumns(table: String): Array

Get column information.

Parameters
table: String Name of the table to examine.
Returns
An array of column data. This is database specific content and will vary depending on the database connector in use.

public getNumRows(table: String): Number

Return the number of rows in a table.

Returns
The count of rows in a table in the currently opened database.

public getTables(): Array

Return list of tables in a database.

Returns
An array containing list of table names present in the currently opened database.

public query(cmd: String, tag: String = SQL , trace: Boolean = false): Array

Execute a SQL command on the database.

Parameters
cmd: String SQL command string.
tag: String Debug tag to use when logging the command. [default: SQL ]
trace: Boolean Set to true to eanble logging this command. [default: false]
Returns
An array of row results where each row is represented by an Object hash containing the column names and values.

public removeColumns(table: String, columns: Array): Void

Remove columns from a table.

Parameters
table: String Name of the table to modify.
columns: Array Array of column names to remove.

public removeIndex(table: String, index: String): Void

Remove an index.

Parameters
table: String Name of the table to modify.
index: String Name of the index to remove.

public renameColumn(table: String, oldColumn: String, newColumn: String): Void

Rename a column.

Parameters
table: String Name of the table to modify.
oldColumn: String Old column name.
newColumn: String New column name.

public renameTable(oldTable: String, newTable: String): Void

Rename a table.

Parameters
oldTable: String Old table name.
newTable: String New table name.

public sql(cmd: String): Array

Execute a SQL command on the database.

Description
This is a low level SQL command interface that bypasses logging. Use.
Parameters
cmd: String SQL command to issue. Note: "SELECT" is automatically prepended and ";" is appended for you.
Returns
An array of row results where each row is represented by an Object hash containing the column names and values.

public sqlTypeToDataType(sqlType: String): String

Map the SQL type to a database independant data type.

Parameters
sqlType: String SQL Data type to map.
Returns
The corresponding database independant type.

public sqlTypeToEjsType(sqlType: String): Type

Map the SQL type to an Ejscript type class.

Parameters
sqlType: String SQL Data type to map.
Returns
The corresponding type class.

public startTransaction(): Void

Start a new database transaction.


public trace(on: Boolean): Void

Trace all SQL statements on this database.

Description
Control whether trace is enabled for all SQL statements issued against the database.
Parameters
on: Boolean If true, display each SQL statement to the log.

public transaction(code: Function): Void

Execute a database transaction.

Parameters
code: Function Function to run inside a database transaction.