4.17. SQL::Datasource Class

Note: This class is not available with the PO_NO_DATABASE parse option.

The Datasource class is the high-level Qore interface to Qore's DBI layer, and as such, Datasource objects allow Qore programs to access databases that have a Qore DBI driver. The Datasource class will attempt to load any DBI driver that is not currently loaded in the constructor. For connection pooling support, see the DatasourcePool class.

Datasource objects will implicitly call Datasource::open() if no connection has yet been established and a method is called requiring a connection to the database server. Therefore any method that requires communication with the database server can also throw any exception that the open method can throw.

Some Qore DBI drivers allow "select" queries to be executed through the Datasource::exec() method, and allow SQL commands (procedure calls, etc) to be executed through the Datasource::select() method, and some DBI drivers do not (depends on the underlying DB API). At any rate, the transaction lock is set when auto-commit is disabled and when the Datasource::exec() or Datasource::beginTransaction() methods are executed as documented above. Therefore executing a transaction relevant command through the Datasource::select() method while auto-commit mode is disabled and a transaction has not yet started will not result in the transaction lock being allocated to the current thread and therefore could cause transaction errors when sharing the Datasource object between multiple threads.

Only databases with an existing Qore DBI driver can be accessed through the Qore Datasource class.

All Qore DBI drivers set new connections to use transaction isolation level "read committed".

The Datasource class provides consistent, high-level, per-connection locking on requests at a level above the DBI drivers to ensure that the communication between clients and servers is properly serialized.

4.17.1. Datasource Binding By Value and By Placeholder

All Datasource methods accepting SQL strings to execute understand a special syntax used in the query string to bind Qore data by value and to specify placeholders for output variables (for example, when executing a stored procedure or database function). Placeholder binding is DBI driver specific, but binding by value is supported with the same syntax in all drivers. Additionally, the %d numeric specifier is supported equally in all Qore DBI drivers.

Table 4.538. Datasource Format Specifiers

Format Specification

Description

%d

If any value other than NOTHING or NULL is given, then the value is converted to an integer and this value is substituted in the string at this position; if the value is NOTHING or NULL, then a literal 'null' is substituted instead.

%s

The argument is converted to a string and the string is inserted literally without any conversion or escape sequences in the string; this is useful for table or schema prefixes, etc

%v

The argument is bound by value according to the DBI driver's implementation.


To bind qore data values directly in a binary format in an SQL command, use %v in the command string, and include the value as an argument after the string. Binding by value means that Qore's DBI driver will take care of formatting the data properly for use in the query with the database server. That means that strings do not need to be quoted, date/time values do not need special formatting, binary object (with BLOB columns, for example) can be used directly in queries, etc.

Here is an example:

$rows = $pool.exec("insert into table (varchar_col, timestamp_col, blob_col, numeric_col)
values (%v, %v, %v, %d)", $string, now(), $binary, 100);

To insert a numeric value or a literal 'null' in a query, use %d in the command string, and include the value as an argument after the string. If the value is NOTHING or NULL, a literal 'null' will be written to the string; otherwise the argument is converted to a floating-point value or integer if necessary and written to the string. This is useful for working with DECIMAL (NUMERIC, NUMBER) types in a database-independent way; for example PostgreSQL servers do not do type conversions to DECIMAL types when a string, integer, or float is bound by value, therefore to ensure that integral decimal values can be used in a database-independent way (with 'null' substitution when no value is bound), it's best to use the %d code in the command string instead of %v.

For binding placeholders for output variables, write a unique name in the string and prefix it with a colon (ex: :code). In this case the method will return a hash of the output variables using the placeholder names as keys, but without the colon prefix. By default, a string type will be bound to the position. To bind other variable types to placeholder positions, include the type constant (see Type Constants) as an argument after the command string. For BLOBs, use Binary, for CLOBs, use the string "clob" (constants will be provided in a future release). Not all DBI drivers require placeholder buffer specifications; see the documentation for the DBI driver in question for more information and examples regarding placeholder buffer specifications.

4.17.2. Datasource Transaction Locks

Datasource objects have an internal transaction lock which will be grabbed when the Datasource::exec(), Datasource::vexec() Datasource::execRaw(), or Datasource::beginTransaction() methods are executed and autocommit is not enabled. This enables a single datasource to be safely used for transaction management by several threads simultaneously. Note that an exception in a Datasource method that would acquire the lock (such as the Datasource::exec() method) when it's not already held, will have the effect that the transaction lock is not acquired.

Any thread attempting to do transaction-relevant actions on a Datasource with auto-commit disabled while a transaction is in progress by another thread will block until the thread currently executing a transaction executes the Datasource::commit() or Datasource::rollback() methods (or the Datasource is deleted, reset, or closed, in which case the lock is released and an exception is raised as well).

There is a timeout associated with the transaction lock; if a thread waits for the transaction lock for more than the timeout period, then an exception will be raised in the waiting thread. The timeout value can be read and changed with the Datasource::getTransactionLockTimeout() and Datasource::setTransactionLockTimeout() methods, respectively. The default transaction lock timeout value is 120 seconds.

Table 4.539. SQL::Datasource Method Overview

Method

Except?

Description

Datasource::constructor(string $driver, string $user = "", string $pass = "", string $dbname = "", string $encoding = "", string $host = "", softint $port = 0)

Datasource::constructor(hash $params)

Y

Creates the Datasource object; attempts to load a DBI driver if the driver is not already present in Qore.

Datasource::destructor()

Y

Destroys the object.

Datasource::copy()

N

Creates a new Datasource object with the same driver as the original and copies of all the connection parameters.

Datasource::open() returns nothing

Y

Opens a connection to a database; an exception is thrown if any errors occur.

Datasource::close() returns nothing

N

Closes the connection to the database; an exception is thrown if any errors occur.

Datasource::commit() returns nothing

Y

Commits the transaction and releases the transaction lock; an exception is thrown if any errors occur.

Datasource::rollback() returns nothing

Y

Rolls back the transaction and releases the transaction lock; an exception is thrown if any errors occur.

Datasource::setAutoCommit(softbool $b = True) returns nothing

N

Turns autocommit on or off for this object.

Datasource::exec(string $sql, ...) returns any

Datasource::exec() returns nothing (RT_NOOP)

Y

Executes SQL code on the DB connection.

Datasource::execRaw(string $sql) returns any

Y

Executes SQL code (like Datasource::exec()) on the DB connection without any variable binding.

Datasource::vexec(string $sql, list $args) returns any

Datasource::vexec(string $sql) returns any

Datasource::vexec() returns nothing (RT_NOOP)

Y

Executes SQL code on the DB connection, taking a list for all bind arguments.

Datasource::select(string $sql, ...) returns any

Datasource::select() returns nothing (RT_NOOP)

Y

Executes a select statement on the server and returns the results in a hash (column names) of lists (rows).

Datasource::vselect(string $sql, list $args) returns any

Datasource::vselect(string $sql) returns any

Datasource::vselect() returns nothing (RT_NOOP)

Y

Executes a select statement on the server and returns the results in a hash (column names) of lists (rows), taking a list for all bind arguments.

Datasource::selectRow(string $sql, ...) returns any

Datasource::selectRow() returns nothing (RT_NOOP)

Y

Executes a select statement on the server and returns the first row as a hash (column names and values).

Datasource::vselectRow(string $sql, list $args) returns any

Datasource::vselectRow(string $sql) returns any

Datasource::vselectRow() returns nothing (RT_NOOP)

Y

Executes a select statement on the server and returns the first row as a hash (column names and values), taking a list for all bind arguments.

Datasource::selectRows(string $sql, ...) returns any

Datasource::selectRows() returns nothing (RT_NOOP)

Y

Executes a select statement on the server and returns the results in a list (rows) of hashes (column names and values).

Datasource::vselectRows(string $sql, list $args) returns any

Datasource::vselectRows(string $sql) returns any

Datasource::vselectRows() returns nothing (RT_NOOP)

Y

Executes a select statement on the server and returns the results in a list (rows) of hashes (column names and values), taking a list for all bind arguments.

Datasource::beginTransaction() returns nothing

Y

Manually grabs the transaction lock

Datasource::setUserName(string $user) returns nothing

Datasource::setUserName() returns nothing (RT_NOOP)

N

Sets the username parameter for the next open.

Datasource::getUserName() returns any

N

Returns the username parameter as a string or NOTHING if none is set.

Datasource::setPassword(string $pass) returns nothing

Datasource::setPassword() returns nothing (RT_NOOP)

N

Sets the password parameter for the next open.

Datasource::getPassword() returns any

N

Returns the password parameter as a string or NOTHING if none is set.

Datasource::setDBName(string $db) returns nothing

Datasource::setDBName() returns nothing (RT_NOOP)

N

Sets the DB name parameter for the next open.

Datasource::getDBName() returns any

N

Returns the dbname parameter as a string or NOTHING if none is set.

Datasource::setDBCharset(string $encoding) returns nothing

Datasource::setDBCharset() returns nothing (RT_NOOP)

N

Sets the charset (encoding) parameter for the next open.

Datasource::getDBCharset() returns any

N

Returns the DBI driver specific charset name for the current connection as a string or NOTHING if none is set.

Datasource::getOSCharset() returns string

N

Returns the Qore charset name for the current connection as a string or "(unknown)" if none is set.

Datasource::setHostName(string $host) returns nothing

Datasource::setHostName() returns nothing (RT_NOOP)

N

Sets the hostname parameter for the next open.

Datasource::getHostName() returns any

N

Returns the hostname parameter as a string or NOTHING if none is set.

Datasource::setPort(softint $port = 0) returns nothing

N

Sets the port parameter for the next open.

Datasource::getPort() returns any

N

Returns the port parameter as an int or NOTHING if none is set.

Datasource::getDriverName() returns string

N

Returns the name of the driver used for the object.

Datasource::setTransactionLockTimeout(softint $timeout_ms = 0) returns nothing

Datasource::setTransactionLockTimeout(date $timeout) returns nothing

N

Sets the transaction lock timeout value in milliseconds. Set to 0 for no timeout.

Datasource::getTransactionLockTimeout() returns int

N

Retrieves the transaction lock timeout value as an integer in milliseconds.

Datasource::getServerVersion() returns any

Y

Returns the driver-specific server version data for the current connection.

Datasource::getClientVersion() returns any

Y

Returns the driver-specific client library version data. Not implemented by all drivers.

Datasource::inTransaction() returns bool

N

Returns True if a transaction is in progress, False if not.


4.17.3. Datasource::constructor()

Synopsis

Creates a Datasource object. The constructor variant taking separate arguments requires the datasource type as the first argument, while most other parameters are optional.

Prototype

Datasource::constructor(string $driver, string $user = "", string $pass = "", string $dbname = "", string $encoding = "", string $host = "", softint $port = 0)

Datasource::constructor(hash $params)

Example
my Datasource $db(DSPGSQL, "user", "pass", "database", "utf8", "localhost", 5432);

Table 4.540. Arguments for Datasource::constructor(string $driver, string $user = "", string $pass = "", $dbname = "", string $encoding = "", string $host = "", softint $port = 0) Variant

Argument

Description

string $driver

The name of the DBI driver for the Datasource. See SQL Constants for builtin constants for DBI drivers shipped with Qore, or see the DBI driver documentation to use an add-on driver.

string $user = ""

The user name for the new connection. Also see Datasource::setUserName() for a method that allows this parameter to be set after the constructor.

string $pass = ""

The password for the new connection. Also see Datasource::setPassword() for a method that allows this parameter to be set after the constructor.

string $dbname = ""

The database name for the new connection. Also see Datasource::setDBName() for a method that allows this parameter to be set after the constructor.

string $encoding = ""

The database-specific name of the character encoding to use for the new connection. Also see Datasource::setDBCharset() for a method that allows this parameter to be set after the constructor. If no value is passed for this parameter, then the database character encoding corresponding to the default character encoding for the Qore process will be used instead.

string $host = ""

The host name for the new connection. Also see Datasource::setHostName() for a method that allows this parameter to be set after the constructor.

softint $port = 0

The port number for the new connection. Also see Datasource::setPort() for a method that allows this parameter to be set after the constructor.


Table 4.541. Arguments for Datasource::constructor(hash $params) Variant

Argument

Description

hash params

A hash of parameters for the Datasource; see Datasource Constructor Hash for more information.


Table 4.542. Datasource Constructor Hash

Key

Type

Description

type

string

The name of the database driver to use; this key is mandatory; if not present, an exception will be raised. See SQL Constants for builtin constants for DBI drivers shipped with Qore, or see the DBI driver documentation to use an add-on driver.

user

string

The user name for the new connection. Also see Datasource::setUserName() for a method that allows this parameter to be set after the constructor.

pass

string

The password for the new connection. Also see Datasource::setPassword() for a method that allows this parameter to be set after the constructor.

db

string

The database name for the new connection. Also see Datasource::setDBName() for a method that allows this parameter to be set after the constructor.

charset

string

The database-specific name of the character encoding to use for the new connection. Also see Datasource::setDBCharset() for a method that allows this parameter to be set after the constructor. If no value is passed for this parameter, then the database character encoding corresponding to the default character encoding for the Qore process will be used instead.

host

string

The host name for the new connection. Also see Datasource::setHostName() for a method that allows this parameter to be set after the constructor.

port

softint

The port number for the new connection. Also see Datasource::setPort() for a method that allows this parameter to be set after the constructor. If this key is present and is 0 then an exception will be raised.


Table 4.543. Exceptions Thrown by Datasource::constructor()

err

desc

DATASOURCE-PARAMETER-EEROR

Missing DBI driver identifier as first argument.

DATASOURCE-UNSUPPORTED-DATABASE

Could not load a driver for the database identified.


4.17.4. Datasource::destructor()

Synopsis

Closes the datasource if it's open (if any operations are in progress, will block until the operations complete) and destroys the object.

Example
delete $db;

Table 4.544. Exceptions Thrown by Datasource::destructor()

err

desc

DATASOURCE-TRANSACTION-EXCEPTION

The Datasource was destroyed while a transaction was still in progress; the transaction will be automatically rolled back.


4.17.5. Datasource::copy()

Synopsis

Creates a new Datasource object with the same driver as the original and copies of all the connection parameters.

Example
my Datasource $new_ds = $ds.copy();

4.17.6. Datasource::beginTransaction()

Synopsis

Manually grabs the transaction lock. This method should be called when the Datasource object will be shared between more than 1 thread, and a transaction will be started with a Datasource::select() method.

Prototype

Datasource::beginTransaction() returns nothing

Example
$db.beginTransaction();

Table 4.545. Exceptions Thrown by Datasource::beginTransaction()

err

desc

AUTOCOMMIT-ERROR

Cannot start a transaction when autocommit is enabled.

TRANSACTION-LOCK-TIMEOUT

Timeout trying to acquire the transaction lock.


4.17.7. Datasource::open()

Synopsis

Opens a connection to the datasouce, using the connection parameters already set. If any errors are encountered, an exception is raised.

Prototype

Datasource::open() returns nothing

Example
$db.open();

Table 4.546. Exceptions Thrown by Datasource::open()

err

desc

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.8. Datasource::close()

Synopsis

Closes the connection to the database. If any actions are in progress on the database, the close call will block until the actions complete. If any errors are encountered, an exception is raised.

Prototype

Datasource::close() returns nothing

Example
$db.close();

4.17.9. Datasource::commit()

Synopsis

Commits the current transaction and releases the transaction lock.

Prototype

Datasource::commit() returns nothing

Example
$db.commit();

Table 4.547. Exceptions Thrown by Datasource::commit()

err

desc

TRANSACTION-LOCK-TIMEOUT

Timeout trying to acquire the transaction lock.

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.10. Datasource::inTransaction()

Synopsis

Returns True if the Datasource is currently in a transaction (has the transaction lock allocated to the calling thread), False if not.

Prototype

Datasource::inTransaction() returns bool

Example
my bool $is_trans = $db.inTransaction();

Table 4.548. Return Value for Datasource::inTransaction()

Return Type

Description

bool

Returns True if the Datasource is currently in a transaction (has the transaction lock allocated to the calling thread), False if not.


4.17.11. Datasource::rollback()

Synopsis

Rolls back the current transaction and releases the transaction lock.

Prototype

Datasource::rollback() returns nothing

Example
$db.rollback();

Table 4.549. Exceptions Thrown by Datasource::rollback()

err

desc

TRANSACTION-LOCK-TIMEOUT

Timeout trying to acquire the transaction lock.

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.12. Datasource::setAutoCommit()

Synopsis

Turns autocommit on or off for this object.

Prototype

Datasource::setAutoCommit(softbool $b = True) returns nothing

Example
$db.setAutoCommit(False);

Table 4.550. Arguments for Datasource::setAutoCommit()

Argument

Description

softbool $b = True

True to turn on autocommit (a commit will be executed after every Datasource::exec()), False to turn off autocommit (commits must be manually triggered).


4.17.13. Datasource::exec()

Synopsis

Grabs the transaction lock (if autocommit is disabled) and executes an SQL command on the server and returns either the row count (for example, for updates and inserts) or the data retrieved (for example, if a stored procedure is executed that returns values).

Prototype

Datasource::exec(string $sql, ...) returns any

Datasource::exec() returns nothing (RT_NOOP)

Example
$rows = $db.exec("insert into table (varchar_col, timestamp_col, blob_col, numeric_col)
values (%v, %v, %v, %d)", $string, now(), $binary, 100);

Table 4.551. Arguments for Datasource::exec()

Argument

Description

string $sql

The SQL command to execute on the server.

...

Include any values to be bound (using %v in the command string) or placeholder specifications (using :<key_name> in the command string) in order after the command string.


Table 4.552. Return Values for Datasource::exec()

Return Type

Description

any

The return value depends on the DBI driver; normally, for commands with placeholders, a hash is returned holding the values acquired from executing the SQL statement. For all other commands, normally an int row count is returned. However, some DBI drivers also allow select statements to be executed through this interface, which would also return a hash of list.


Table 4.553. Exceptions Thrown by Datasource::exec()

err

desc

TRANSACTION-LOCK-TIMEOUT

Timeout trying to acquire the transaction lock.

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.14. Datasource::execRaw()

Synopsis

Grabs the transaction lock (if autocommit is disabled) and executes an SQL command on the server and returns either the row count (for example, for updates and inserts) or the data retrieved (for example, if a stored procedure is executed that returns values).

This method does not do any variable binding, so it's useful for example for DDL statements etc.

Warning

Using this method for OLTP statements can affect the application performance. See used DB server documentation for variable binding.

Prototype

Datasource::execRaw(string $sql) returns any

Example
$rows = $db.exec("create table my_tab (id number, some_text varchar2(30))");

Table 4.554. Arguments for Datasource::execRaw()

Argument

Description

string $sql

The SQL command to execute on the server.


Table 4.555. Return Values for Datasource::execRaw()

Return Type

Description

any

The return value depends on the DBI driver; normally an int row count is returned. However, some DBI drivers also allow select statements to be executed through this interface, which would also return a hash of list.


Table 4.556. Exceptions Thrown by Datasource::execRaw()

err

desc

TRANSACTION-LOCK-TIMEOUT

Timeout trying to acquire the transaction lock.

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.15. Datasource::vexec()

Synopsis

Same as the Datasource::exec() method, except this method takes a single argument after the SQL command giving the list of bind parameters.

Prototype

Datasource::vexec(string $sql, list $args) returns any

Datasource::vexec(string $sql) returns any

Datasource::vexec() returns nothing (RT_NOOP)

Example
$rows = $db.vexec("insert into example_table value (%v, %v, %v)", $arg_list);

Table 4.557. Arguments for Datasource::vexec()

Argument

Description

string $sql

The SQL command to execute on the server.

[list $args]

Include any values to be bound (using %v in the command string) or placeholder specifications (using :<key_name> in the command string) in a single optional list in order after the command string.


Table 4.558. Return Values for Datasource::vexec()

Return Type

Description

any

The return value depends on the DBI driver; normally, for commands with placeholders, a hash is returned holding the values acquired from executing the SQL statement. For all other commands, normally an int row count is returned. However, some DBI drivers also allow select statements to be executed through this interface, which would also return a hash of list.


Table 4.559. Exceptions Thrown by Datasource::vexec()

err

desc

TRANSACTION-LOCK-TIMEOUT

Timeout trying to acquire the transaction lock.

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.16. Datasource::select()

Synopsis

Executes an SQL select statement on the server and returns the result as a hash (column names) of lists (rows). This format is suitable for use with context Statements, for easy iteration and processing of query results. Additionally, this format is more efficient format than that returned by the Datasource::selectRows() method, because the column names are not repeated for each row returned. Therefore, for retrieving anything greater than small amounts of data, it is recommended to use this method instead of Datasource::selectRows().

Prototype

Datasource::select(string $sql, ...) returns any

Datasource::select() returns nothing (RT_NOOP)

Example
# bind a string and a date/time value by value in a query
$query = $db.select("select * from table where varchar_column = %v and timestamp_column > %v", $string, 2007-10-11T15:31:26.289);

Table 4.560. Arguments for Datasource::select()

Argument

Description

string $sql

The SQL select command to execute on the server.

...

Include any values to be bound (using %v in the command string) or placeholder specifications (using :<key_name> in the command string) in order after the command string.


Table 4.561. Return Values for Datasource::select()

Return Type

Description

any

Normally returns a hash (the keys are the column names) of list (each hash key's value is a list giving the row data), however some DBI drivers allow any SQL to be executed through this method, in which case also int.


Table 4.562. Exceptions Thrown by Datasource::select()

err

desc

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.17. Datasource::vselect()

Synopsis

Same as the Datasource::select() method, except this method takes a single argument after the SQL command giving the list of bind value parameters.

Prototype

Datasource::vselect(string $sql, list $args) returns any

Datasource::vselect(string $sql) returns any

Datasource::vselect() returns nothing (RT_NOOP)

Example
$query = $db.vselect("select * from example_table where id = %v and name = %v", $arg_list);

Table 4.563. Arguments for Datasource::vselect()

Argument

Description

string $sql

The SQL select statement to execute on the server.

[list $args]

Include any values to be bound (using %v in the command string) or placeholder specifications (using :<key_name> in the command string) in a single optional list in order after the command string.


Table 4.564. Return Values for Datasource::vselect()

Return Type

Description

any

Normally returns a hash (the keys are the column names) of list (each hash key's value is a list giving the row data), however some DBI drivers allow any SQL to be executed through this method, in which case also int.


Table 4.565. Exceptions Thrown by Datasource::vselect()

err

desc

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.18. Datasource::selectRow()

Synopsis

Executes an SQL select statement on the server and returns the first row as a hash (the column values). If more than one row is returned, then all but the first row are discarded. For a similar method taking a list for all bind arguments, see Datasource::vselectRow().

This method also accepts all bind parameters (%d, %v, etc) as documented in Datasource Binding.

Prototype

Datasource::selectRow(string $sql, ...) returns any

Datasource::selectRow() returns nothing (RT_NOOP)

Example
$list = $db.selectRow("select * from example_table");

Table 4.566. Arguments for Datasource::selectRow()

Argument

Description

string $sql

The SQL select command to execute on the server.

...

Include any values to be bound (using %v in the command string) or placeholder specifications (using :<key_name> in the command string) in order after the command string.


Table 4.567. Return Values for Datasource::selectRow()

Return Type

Description

any

Normally returns a hash (the keys are the column names) of row data or nothing if no row is found for the query. However, DBI could return other types (all known drivers at the time of writing this documentation only return hash or nothing).


Table 4.568. Exceptions Thrown by Datasource::selectRow()

err

desc

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.19. Datasource::vselectRow()

Synopsis

Same as the Datasource::selectRow() method, except this method takes a single argument after the SQL command giving the list of bind value parameters.

Prototype

Datasource::vselectRow(string $sql, list $args) returns any

Datasource::vselectRow(string $sql) returns any

Datasource::vselectRow() returns nothing (RT_NOOP)

Example
$list = $db.vselectRow("select * from example_table where id = %v and name = %v", $arg_list);

Table 4.569. Arguments for Datasource::vselectRow()

Argument

Description

string $sql

The SQL select statement to execute on the server.

[list $args]

Include any values to be bound (using %v in the command string) or placeholder specifications (using :<key_name> in the command string) in a single optional list in order after the command string.


Table 4.570. Return Values for Datasource::vselectRow()

Return Type

Description

any

Normally returns a hash (the keys are the column names) of row data or nothing if no row is found for the query. However, DBI could return other types (all known drivers at the time of writing this documentation only return hash or nothing).


Table 4.571. Exceptions Thrown by Datasource::vselectRow()

err

desc

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.20. Datasource::selectRows()

Synopsis

Executes an SQL select statement on the server and returns the result as a list (rows) of hashes (the column values). This format is not as efficient as that returned by the Datasource::select() method, therefore for larger amounts of data, it is recommended to use Datasource::select().

This method also accepts all bind parameters (%d, %v, etc) as documented in Datasource Binding.

Prototype

Datasource::selectRows(string $sql, ...) returns any

Datasource::selectRows() returns nothing (RT_NOOP)

Example
$list = $db.selectRows("select * from example_table");

Table 4.572. Arguments for Datasource::selectRows()

Argument

Description

string $sql

The SQL select command to execute on the server.

...

Include any values to be bound (using %v in the command string) or placeholder specifications (using :<key_name> in the command string) in order after the command string.


Table 4.573. Return Values for Datasource::selectRows()

Return Type

Description

any

Normally returns a list (rows) of hash (where the keys are the column names of each row) or nothing if no rows are found for the query. However, DBI could return other types; for DBI drivers that allow executing generic SQL through this method, any result sets returns should also be in the format list (rows) of hash.


Table 4.574. Exceptions Thrown by Datasource::selectRows()

err

desc

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.21. Datasource::vselectRows()

Synopsis

Same as the Datasource::selectRows() method, except this method takes a single argument after the SQL command giving the list of bind value parameters.

Prototype

Datasource::vselectRows(string $sql, list $args) returns any

Datasource::vselectRows(string $sql) returns any

Datasource::vselectRows() returns nothing (RT_NOOP)

Example
$list = $db.vselectRows("select * from example_table where id = %v and name = %v", $arg_list);

Table 4.575. Arguments for Datasource::vselectRows()

Argument

Description

string $sql

The SQL select statement to execute on the server.

[list $args]

Include any values to be bound (using %v in the command string) or placeholder specifications (using :<key_name> in the command string) in a single optional list in order after the command string.


Table 4.576. Return Values for Datasource::vselectRows()

Return Type

Description

any

Normally returns a list (rows) of hash (where the keys are the column names of each row) or nothing if no rows are found for the query. However, DBI could return other types; for DBI drivers that allow executing generic SQL through this method, any result sets returns should also be in the format list (rows) of hash.


Table 4.577. Exceptions Thrown by Datasource::vselectRows()

err

desc

depends on DBI driver

See documentation for the DBI driver for driver-specific exceptions.


4.17.22. Datasource::setTransactionLockTimeout()

Synopsis

Sets the transaction lock timeout value in milliseconds; set to 0 for no timeout. Like all Qore functions and methods taking timeout values, a relative time value may be passed instead of an integer to make the timeout units clear (ex: 2500ms for 2.5 seconds).

Prototype

Datasource::setTransactionLockTimeout(softint $timeout_ms = 0) returns nothing

Datasource::setTransactionLockTimeout(date $timeout) returns nothing

Example
$db.setTransactionLockTimeout(5m); # transaction lock timeout set to 5 minutes

Table 4.578. Arguments for Datasource::setTransactionLockTimeout()

Argument

Description

softint $timeout_ms = 0 or date $timeout

The timeout value to set in seconds. For no timeout, set to 0. Like all Qore functions and methods taking timeout values, a relative time value may be passed instead of an integer to make the timeout units clear (ex: 2500ms for 2.5 seconds).


4.17.23. Datasource::getTransactionLockTimeout()

Synopsis

Retrieves the transaction lock timeout value as an integer in milliseconds.

Prototype

Datasource::getTransactionLockTimeout() returns int

Example
$int = $db.getTransactionLockTimeout();

Table 4.579. Return Values for Datasource::getTransactionLockTimeout()

Return Type

Description

int

The transaction lock timeout value in milliseconds.


4.17.24. Datasource::setUserName()

Synopsis

Sets the username to use for the connection. Invalid usernames will cause an exception to be thrown when the connection is opened.

Prototype

Datasource::setUserName(string $user) returns nothing

Datasource::setUserName() returns nothing (RT_NOOP)

Example
$db.setUserName("user");

Table 4.580. Arguments for Datasource::setUserName()

Argument

Description

string $user

The username to be used for the connection.


4.17.25. Datasource::getUserName()

Synopsis

Retrieves the username parameter for connections to the database.

Prototype

Datasource::getUserName() returns any

Example
$str = $db.getUserName();

Table 4.581. Return Values for Datasource::getUserName()

Return Type

Description

string or nothing

The username connection parameter or NOTHING if no value is set.


4.17.26. Datasource::setPassword()

Synopsis

Sets the password to use for the connection. Invalid passwords will cause an exception to be thrown when the connection is opened.

Prototype

Datasource::setPassword(string $pass) returns nothing

Datasource::setPassword() returns nothing (RT_NOOP)

Example
$db.setPassword("pass");

Table 4.582. Arguments for Datasource::setPassword()

Argument

Description

string $pass

The password name to be used for the connection.


4.17.27. Datasource::getPassword()

Synopsis

Retrieves the password connection parameter.

Prototype

Datasource::getPassword() returns any

Example
$str = $db.getPassword();

Table 4.583. Return Values for Datasource::getPassword()

Return Type

Description

string or nothing

Retrieves the password connection parameter or NOTHING if none is set.


4.17.28. Datasource::setDBName()

Synopsis

Sets the database name to use for the connection. Invalid database names will cause an exception to be thrown when the connection is opened.

Prototype

Datasource::setDBName(string $db) returns nothing

Datasource::setDBName() returns nothing (RT_NOOP)

Example
$db.setDBName("database");

Table 4.584. Arguments for Datasource::setDBName()

Argument

Description

string $db

The database name to be used for the connection.


4.17.29. Datasource::getDBName()

Synopsis

Retrieves the dbname connection parameter.

Prototype

Datasource::getDBName() returns any

Example
$str = $db.getDBName();

Table 4.585. Return Values for Datasource::getDBName()

Return Type

Description

string or nothing

Retrieves the password connection parameter or NOTHING if none is set.


4.17.30. Datasource::setDBCharset()

Synopsis

Sets the database-specific character encoding to use for the connection. Invalid character encoding names will cause an exception to be thrown when the connection is opened.

Prototype

Datasource::setDBCharset(string $encoding) returns nothing

Datasource::setDBCharset() returns nothing (RT_NOOP)

Example
$db.setDBCharset("ALU32UTF8"); # Oracle UTF-8 encoding equivalent

Table 4.586. Arguments for Datasource::setDBCharset()

Argument

Description

string $encoding

The database-specific name for the encoding to be used for the connection.


4.17.31. Datasource::setHostName()

Synopsis

Sets the hostname to use for the connection (for DBI drivers that support this parameter, such as the mysql and pgsql, for example). Invalid hostnames will cause an exception to be thrown when the connection is opened.

Prototype

Datasource::setHostName(string $host) returns nothing

Datasource::setHostName() returns nothing (RT_NOOP)

Example
$db.setHostName("localhost");

Table 4.587. Arguments for Datasource::setHostName()

Argument

Description

string $host

The hostname to be used for the connection.


4.17.32. Datasource::setPort()

Synopsis

Sets the port number to use for the connection (for DBI drivers that support this parameter, such as the mysql, pgsql, and oracle drivers, for example). Invalid port numbers will cause an exception to be thrown when the connection is opened.

Prototype

Datasource::setPort(softint $port = 0) returns nothing

Example
$db.setPort(5432);

Table 4.588. Arguments for Datasource::setPort()

Argument

Description

softint $port = 0

The port number to be used for the connection.


4.17.33. Datasource::getDBCharset()

Synopsis

Retrieves the database-specific charset set encoding for the current connection.

Prototype

Datasource::getDBCharset() returns any

Example
$str = $db.getDBCharset();

Table 4.589. Return Values for Datasource::getDBCharset()

Return Type

Description

string or nothing

Retrieves the database-specific charset set encoding for the current connection or NOTHING if none is set.


4.17.34. Datasource::getOSCharset()

Synopsis

Retrieves the Qore charset set encoding for the current connection.

Prototype

Datasource::getOSCharset() returns string

Example
$str = $db.getOSCharset();

Table 4.590. Return Values for Datasource::getOSCharset()

Return Type

Description

string

Retrieves the Qore charset set encoding for the current connection or "unknown" if unknown.


4.17.35. Datasource::getHostName()

Synopsis

Retrieves the hostname connection parameter.

Prototype

Datasource::getHostName() returns any

Example
$str = $db.getHostName();

Table 4.591. Return Values for Datasource::getHostName()

Return Type

Description

string or nothing

Retrieves the hostname connection parameter or NOTHING if none is set.


4.17.36. Datasource::getPort()

Synopsis

Retrieves the port connection parameter or NOTHING if none is set.

Prototype

Datasource::getPort() returns any

Example
$port = $db.getPort();

Table 4.592. Return Values for Datasource::getPort()

Return Type

Description

int or nothing

Retrieves the port connection parameter or no value if no port is set.


4.17.37. Datasource::getDriverName()

Synopsis

Returns the name of the DBI driver used for this object.

Prototype

Datasource::getDriverName() returns string

Example
$name = $db.getDriverName();

Table 4.593. Return Values for Datasource::getDriverName()

Return Type

Description

string

The name of the database driver used for this object.


4.17.38. Datasource::getServerVersion()

Synopsis

Retrieves the driver-specific server version information for the current connection.

Prototype

Datasource::getServerVersion() returns any

Example
$data = $db.getServerVersion();

Table 4.594. Return Values for Datasource::getServerVersion()

Return Type

Description

any

The return type and value depends on the driver; see the documentation for the DBI driver used for the connection.


4.17.39. Datasource::getClientVersion()

Synopsis

Retrieves the driver-specific client library version information. Not implemented for all drivers.

Prototype

Datasource::getClientVersion() returns any

Example
$data = $db.getClientVersion();

Table 4.595. Return Values for Datasource::getClientVersion()

Return Type

Description

any

The return type and value depends on the driver; see the documentation for the DBI driver used for the connection.