Portability
Description
Each database management system (DBMS) has its own behaviour. For example, some databases capitalize field names in their output, some lowercase them, while others leave them alone. These quirks make it difficult to port your scripts over to another server type. PEAR MDB2 strives to overcome these differences so your program can switch between DBMSs without any changes.
You control which portability modes are enabled by using
the portability
configuration option.
Configuration options are set via
factory() and
setOption().
The portability modes are bitwised, so they can be combined using
|
and removed using ^
.
See the examples section below on how to do this.
NB: MDB2 portability modes are meant to change the behaviour of the returned
values only, not that of the query itself. For instance, if you created your
tables quoting the identifiers, remember to use the quoteIdentifier()
method in all your queries or you'll get "not found" or "not exists" errors.
Also check for the quote_identifier
option,
if it's false then quoting won't be applied if the check_option is used.
Portability Mode Constants
-
MDB2_PORTABILITY_ALL (default)
turn on all portability features. this is the default setting.
-
MDB2_PORTABILITY_DELETE_COUNT
Force reporting the number of rows deleted. Some DBMSs don't count the number of rows deleted when performing simple
DELETE FROM tablename
queries. This mode tricks such DBMSs into telling the count by addingWHERE 1=1
to the end of DELETE queries. -
MDB2_PORTABILITY_EMPTY_TO_NULL
Convert empty strings values to null in data in and output. Needed because Oracle considers empty strings to be null, while most other DBMSs know the difference between empty and null.
-
MDB2_PORTABILITY_ERRORS
Makes certain error messages in certain drivers compatible with those from other DBMSs
Error Code Re-mappings Driver Description Old Constant New Constant mysql, mysqli unique and primary key constraints MDB2_ERROR_ALREADY_EXISTS MDB2_ERROR_CONSTRAINT mysql, mysqli not-null constraints MDB2_ERROR_CONSTRAINT MDB2_ERROR_CONSTRAINT_NOT_NULL -
MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES
This removes any qualifiers from keys in associative fetches. Some RDBMS, for example SQLite, will default to use the fully qualified name for a column in assoc fetches if it is qualified in a query.
-
MDB2_PORTABILITY_FIX_CASE
Convert names of tables and fields to lower or upper case in all methods. The case depends on the
field_case
option that may be set to either CASE_LOWER (default) or CASE_UPPER. NB: the case change is applied to the returned values only, not to the field and table names in the query. -
MDB2_PORTABILITY_NONE
Turn off all portability features
-
MDB2_PORTABILITY_NUMROWS
Enable hack that makes numRows() work in Oracle
-
MDB2_PORTABILITY_RTRIM
Right trim the data output for all data fetches. This does not apply to drivers for RDBMS that automatically right trim values of fixed length character values, even if they do not right trim value of variable length character values.
Example
Disabling all portability options while connecting
<?php
require_once 'MDB2.php';
$dsn = 'mysql://user:password@host/database'
$options = array(
'debug' => 2,
'portability' => MDB2_PORTABILITY_NONE,
);
$mdb2 =& MDB2::connect($dsn, $options);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}
?>
Using setOption() to enable portability for lowercasing and trimming
<?php
// Once you have a valid MDB2 object named $mdb2...
$mdb2->setOption('field_case', CASE_LOWER);
$mdb2->setOption('portability',
MDB2_PORTABILITY_FIX_CASE | MDB2_PORTABILITY_RTRIM);
?>
Using setOption() to enable all portability options except trimming
<?php
// Once you have a valid MDB2 object named $mdb2...
$mdb2->setOption('portability',
MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_RTRIM);
?>