Database

Moduleejs.db
Definition class Database
InheritanceDatabase inherit Object
Specifiedejscript-2.5
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
get connectionOptionsObjectThe database connection options 8.
static get set defaultDatabaseDatabaseThe default database for the application.
get nameStringThe name of the database.

Database Class Methods

(No own class methods defined)

QualifiersMethod

Database Instance Methods

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

Method Detail

ejs.db Database(adapter: String, options: Object)
Description
Initialize a database connection using the supplied database connection string. The first opened database will also be defined as the default database.
Parameters
adapter: String Database adapter to use. E.g. "sqlite". Sqlite is currently the only supported adapter.
options: Object Connection options. This may be filename or an object hash of properties. If set to a filename, it should contain the filename of the database on the local system. If options is an object hash, it should contain adapter specific properties that specify how to attach to the database.
Options
nameDatabase name.
usernameDatabase username.
passwordDatabase password.
traceTrace database commands to the log.
socketDatabase communications socket.
moduleModule name containing the database connector class. This is a bare module name without ".mod" or any leading path.
classClass name containing the database backend.

addColumn(table: String, column: String, datatype: String, options = null): Void
Description
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 Optional parameters. [default: null]

addIndex(table: String, column: String, index: String): Void
Description
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.

changeColumn(table: String, column: String, datatype: String, options: Object = null): Void
Description
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]

close(): Void
Description
Close the database connection. 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.

createDatabase(name: String, options: Object = null): Void
Description
Create a new database.
Parameters
name: String Name of the database.
options: Object null [default: null]
Options
OptionalParameters.

createTable(table: String, columns: Array = null): Void
Description
Create a new table.
Parameters
table: String Name of the table.
columns: Array Array of column descriptor tuples consisting of name:datatype. [default: null]

dataTypeToSqlType(dataType: String): String
Description
Map the database independant data type to a database dependant SQL data type.
Parameters
dataType: String Data type to map.
Returns
A string containing the name of the the corresponding SQL database type.

destroyDatabase(name: String): Void
Description
Destroy a database.
Parameters
name: String Name of the database to remove.

destroyTable(table: String): Void
Description
Destroy a table.
Parameters
table: String Name of the table to destroy.

getColumns(table: String): Array
Description
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.

getNumRows(table: String): Number
Description
Return the number of rows in a table.
Returns
The count of rows in a table in the currently opened database.

getTables(): Array
Description
Return list of tables in a database.
Returns
An array containing list of table names present in the currently opened database.

query(cmd: String, tag: String = SQL, trace: Boolean = false): Array
Description
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.

removeColumns(table: String, columns: Array): Void
Description
Remove columns from a table.
Parameters
table: String Name of the table to modify.
columns: Array Array of column names to remove.

removeIndex(table: String, index: String): Void
Description
Remove an index.
Parameters
table: String Name of the table to modify.
index: String Name of the index to remove.

renameColumn(table: String, oldColumn: String, newColumn: String): Void
Description
Rename a column.
Parameters
table: String Name of the table to modify.
oldColumn: String Old column name.
newColumn: String New column name.

renameTable(oldTable: String, newTable: String): Void
Description
Rename a table.
Parameters
oldTable: String Old table name.
newTable: String New table name.

sql(cmd: String): Array
Description
Execute a SQL command on the database. 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.

sqlTypeToDataType(sqlType: String): String
Description
Map the SQL type to a database independant data type.
Parameters
sqlType: String Data type to map.
Returns
The corresponding database independant type.

sqlTypeToEjsType(sqlType: String): Type
Description
Map the SQL type to an Ejscript type class.
Parameters
sqlType: String Data type to map.
Returns
The corresponding type class.

trace(on: Boolean): Void
Description
Trace all SQL statements on this database. 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.