@WrRan
2016-11-17T09:17:49.000000Z
字数 11502
阅读 2245
node-oracledb
A Connection object is obtained by a Pool class getConnection()
or Oracledb class getConnection()
call.
The connection is used to access an Oracle database.
The properties of a Connection object are listed below.
writeonly String action
The action attribute for end-to-end application tracing.
This is a write-only property. Displaying a Connection object will
show a value of null
for this attribute. See End-to-end Tracing, Mid-tier Authentication, and Auditing.
writeonly String clientId
The client identifier for end-to-end application tracing, use with mid-tier authentication, and with Virtual Private Databases.
This is a write-only property. Displaying a Connection object will show a value of null
for this attribute. See End-to-end Tracing, Mid-tier Authentication, and Auditing.
writeonly String module
The module attribute for end-to-end application tracing.
This is a write-only property. Displaying a Connection object will show a value of null
for this attribute. See End-to-end Tracing, Mid-tier Authentication, and Auditing.
readonly Number oracleServerVersion
This readonly property gives a numeric representation of the Oracle database version.
For version a.b.c.d.e, this property gives the number: (100000000 * a) + (1000000 * b) + (10000 * c) + (100 * d) + e
readonly Number stmtCacheSize
The number of statements to be cached in the statement cache of the connection. The default value is the stmtCacheSize
property in effect in the Pool object when the connection is created in the pool.
Callback:
break(function(Error error){});
Promise:
promise = break();
This call stops the currently running operation on the connection.
If there is no operation in progress or the operation has completed by the time the break is issued, the break()
is effectively a no-op.
If the running asynchronous operation is interrupted, its callback will return an error.
function(Error error)
The parameters of the callback function are:
Callback function parameter | Description |
---|---|
Error error | If break() succeeds, error is NULL. If an error occurs, then error contains the error message. |
Callback:
close(function(Error error){});
Promise:
promise = close();
Releases a connection. If the connection was obtained from the pool, the connection is returned to the pool and is available for reuse.
Note: calling close()
when connections are no longer required is strongly encouraged. Releasing helps avoid resource leakage and can improve system efficiency.
When a connection is released, any ongoing transaction on the connection is rolled back.
After releasing a connection to a pool, there is no guarantee a subsequent getConnection()
call gets back the same database connection. The application must redo any ALTER SESSION statements on the new connection object, as required.
function(Error error)
The parameters of the callback function are:
Callback function parameter | Description |
---|---|
Error error | If close() succeeds, error is NULL. If an error occurs, then error contains the error message. |
Callback:
commit(function(Error error){});
Promise:
promise = commit();
This call commits the current transaction in progress on the connection.
function(Error error)
The parameters of the callback function are:
Callback function parameter | Description |
---|---|
Error error | If commit() succeeds, error is NULL. If an error occurs, then error contains the error message. |
Callback:
execute(String sql, [Object bindParams, [Object options,]] function(Error error, [Object result]){});
Promise:
promise = execute(String sql, [Object bindParams, [Object options]]);
This call executes a SQL or PL/SQL statement. See SQL Execution for examples.
The statement to be executed may contain IN binds, OUT or IN OUT bind values or variables, which are bound using either an object or an array.
A callback function returns a result
object, containing any fetched rows, the values of any OUT and IN OUT bind variables, and the number of rows affected by the execution of DML statements.
Parameter | Description |
---|---|
String sql |
The SQL string that is executed. The SQL string may contain bind parameters. |
Object bindParams |
This function parameter is needed if there are bind parameters in the SQL statement. |
Object options |
This is an optional parameter to execute() that may be used to control statement execution. |
function(Error error, [Object result]) |
Callback function with the execution results. |
The parameters are discussed in the next sections.
execute()
: SQL Statement
String sql
The SQL or PL/SQL statement that execute()
executes. The statement
may contain bind variables.
execute()
: Bind Parameters
Object bindParams
This execute()
function parameter is needed if there are bind variables in the statement, or if options
are used. It can be either an object that associates values or JavaScript variables to the statement's bind variables by name, or an array of values or JavaScript variables that associate to the statement's bind variables by their relative positions. See
Bind Parameters for Prepared Statements for more details on binding.
If a bind value is an object it may have the following properties:
Bind Property | Description |
---|---|
dir |
The direction of the bind. One of the Oracledb Constants BIND_IN , BIND_INOUT , or BIND_OUT . |
maxArraySize |
The number of array elements to be allocated for a PL/SQL Collection INDEX OF associative array OUT or IN OUT array bind variable. |
maxSize |
The maximum number of bytes that an OUT or IN OUT bind variable of type STRING or BUFFER can use. The default value is 200. The maximum limit is 32767. |
type |
The datatype to be bound. One of the Oracledb Constants STRING , NUMBER , DATE , CURSOR or BUFFER . |
val |
The input value or variable to be used for an IN or IN OUT bind variable. |
The maximum size of a BUFFER
type is 2000 bytes, unless you are using Oracle Database 12c and the database initialization parameter MAX_STRING_SIZE
has a value of EXTENDED
. In this case the maximum size of a BUFFER
is 32767.
With OUT binds, where the type cannot be inferred by node-oracledb because there is no input data value, the type defaults to STRING
whenever type
is not specified.
Note CURSOR
bind variables can only be used for PL/SQL OUT binds.
See Bind Parameters for Prepared Statements for usage and examples.
execute()
: Options
Object options
This is an optional parameter to execute()
that may be used to control statement execution.
If there are no bind variables in the SQL statement, then a null bindParams
, for example {}, must be specified before options
otherwise you will get an error like ORA-01036: Illegal variable name/number or NJS-012: encountered invalid bind datatype.
The following properties can be set or overridden for the execution of a statement.
autoCommit
Boolean autoCommit
Overrides Oracledb autoCommit
.
extendedMetaData
Boolean extendedMetaData
Overrides Oracledb extendedMetaData.
fetchInfo
Object fetchInfo
Object defining how query column data should be represented in JavaScript.
The fetchInfo
property can be used to indicate that number or date columns in a query should be returned as strings instead of their native format. The property can be used in conjunction with, or instead of, the global setting fetchAsString
.
For example:
fetchInfo:
{
"HIRE_DATE": { type : oracledb.STRING }, // return the date as a string
"COMMISSION_PCT": { type : oracledb.DEFAULT } // override Oracledb.fetchAsString
}
Each column is specified by name, using Oracle's standard naming convention.
The valid values for type
are STRING
and DEFAULT
. The former indicates that the given column should be returned as a string. The latter can be used to override any global mapping given by fetchAsString
and allow the column data for this query to be returned in native format.
The maximum length of a string created by type mapping is 200 bytes. However, if a database column that is already of type STRING
is specified in fetchInfo
, then the actual database metadata will be used to determine the maximum length.
Columns fetched from REF CURSORS are not mapped by fetchInfo
settings in the execute()
call. Use the global fetchAsString
instead.
See Result Type Mapping for more information on query type mapping.
maxRows
Number maxRows
Overrides Oracledb maxRows
.
outFormat
String outFormat
Overrides Oracledb outFormat
.
prefetchRows
Number prefetchRows
Overrides Oracledb prefetchRows
.
resultSet
Boolean resultSet
Determines whether query results should be returned as a ResultSet
object or directly. The default is false
.
execute()
: Callback Function
function(Error error, [Object result])
The parameters of the execute()
callback function are:
Callback function parameter | Description |
---|---|
Error error | If execute() succeeds, error is NULL. If an error occurs, then error contains the error message. |
Object result | The result object, described below. For DDL statements and DML where the application only checks error for success or failure, the result parameter can be omitted. |
The properties of result
object from the execute()
callback are described below.
metaData
Array metaData
For SELECT
statements, this contains an array of objects describing details of columns for the select list. For non queries, this property is undefined.
Each column's name
is always given. If the Oracledb extendedMetaData
or execute()
option extendedMetaData
are true
then additional information is included.
name
: The column name follows Oracle's standard name-casing rules. It will commonly be uppercase, since most applications create tables using unquoted, case-insensitive names.fetchType
: one of the Node-oracledb Type Constant values.dbType
: one of the Oracle Database Type Constant values.byteSize
: the database byte size. This is only set for DB_TYPE_VARCHAR
, DB_TYPE_CHAR
and DB_TYPE_RAW
column types.precision
: set only for DB_TYPE_NUMBER
, DB_TYPE_TIMESTAMP
, DB_TYPE_TIMESTAMP_TZ
and DB_TYPE_TIMESTAMP_LTZ
columns.scale
: set only for DB_TYPE_NUMBER
columns.nullable
: indicates whether NULL
values are permitted for this column.For numeric columns: when precision
is 0
, then the column is simply a NUMBER
. If precision
is nonzero and scale
is -127
, then the column is a FLOAT
. Otherwise, it is a NUMBER(precision, scale)
.
Metadata for Result Sets and REF CURSORS is available in a ResultSet property. For Lobs, a Lob type property also indicates whether the object is a BLOB
or CLOB
.
See Query Column Metadata for examples.
outBinds
Array/object outBinds
This is either an array or an object containing OUT and IN OUT bind values. If bindParams
is passed as an array, then outBinds
is returned as an array. If bindParams
is passed as an object, then outBinds
is returned as an object.
resultSet
Object resultSet
For SELECT
statements when the resultSet
option is true
, use the resultSet
object to fetch rows. See ResultSet Class.
rows
Array rows
For SELECT
statements where the resultSet
option is false
or unspecified, rows
contains an array of fetched rows. It will be NULL if there is an error or the SQL statement was not a SELECT statement. By default, the rows are in an array of column value arrays, but this can be changed to arrays of objects by setting outFormat
to OBJECT
. If a single row is fetched, then rows
is an array that contains one single row. The number of rows returned is limited to the maxRows
configuration property of the Oracledb object, although this may be overridden in any execute()
call.
rowsAffected
Number rowsAffected
For DML statements (including SELECT FOR UPDATE) this contains the number of rows affected, for example the number of rows inserted. For non-DML statements such as queries, or if no rows are affected, then rowsAffected
will be zero.
queryStream(String sql, [Object bindParams, [Object options]]);
This method will return a Readable Stream for queries.
This function provides query streaming support. The parameters are the same as execute()
except a callback is not used. Instead this function returns a stream used to fetch data.
Each row is returned as a data
event. Query metadata is available via a metadata
event. The end
event indicates the end of the query results.
Query results must be fetched to completion to avoid resource leaks.
The connection must remain open until the stream is completely read.
For tuning purposes the oracledb.maxRows
property can be used to size an internal buffer used by queryStream()
. Note it does not limit the number of rows returned by the stream. The oracledb.prefetchRows
value will also affect performance.
See Streaming Query Results for more information.
See execute().
An alias for connection.close().
Callback:
rollback(function(Error error){});
Promise:
promise = rollback();
This call rolls back the current transaction in progress on the connection.
function(Error error)
The parameters of the callback function are:
Callback function parameter | Description |
---|---|
Error error | If rollback() succeeds, error is NULL. If an error occurs, then error contains the error message. |