PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB_DataObject » Bug #9902

Database connections are not stored by reference in global variable

Details

Submitted2007-01-21 19:35 UTC
Fromdavid dot ward at gatech dot edu
Assignedalan_k
StatusClosed
PackageDB_DataObject
PHP Version4.4.4
OSWindows XP
Roadmaps(Not assigned)

Comments

[2007-01-21 19:35 UTC] david dot ward at gatech dot edu

Description:
------------
In DataObject.php, the following lines (2221/2223/2231):

$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = DB::connect($dsn,$db_options);
$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = DB::connect($dsn);
$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = MDB2::connect($dsn,$db_options);

all need to be modified to the following to store a reference to the DB/MDB2 connection, rather than a copy:

$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &DB::connect($dsn,$db_options);
$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &DB::connect($dsn);
$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &MDB2::connect($dsn,$db_options);

Because of this bug, for MDB2 (at least), setting options on an object in $GLOBALS['_DB_DATAOBJECT']['CONNECTIONS'] does not change the option on the corresponding object in $GLOBALS['_MDB2_databases']. So the following line (DataObject/Generator.php, line 1066):

$__DB->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE);

has no effect when tableInfo() is subsequently called (line 1074):

$defs = $__DB->reverse->tableInfo($table);

[2007-01-22 01:14 UTC] david dot ward at gatech dot edu

After this change is made, I discovered that the MDB2 portability mode is not set consistently in different functions.

DB_DataObject_Generator::_createTableList() and
DB_DataObject_Generator::fillTableSchema() both set the MDB2 portability mode to allow mixed case (line 192/line 1066 from DB/DataObject/Generator.php):

$__DB->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE);

However, the portability mode is not set like this if other functions are called, such as DB_DataObject::_query(). Therefore the behavior is not consistent, since a query will assume that the DataObject's member variables are lowercase.

This can be fixed easily. The proper place to set the portability mode appears to be in DB_DataObject::_connect(), rather than in functions like DB_DataObject_Generator::_createTableList() and DB_DataObject_Generator::fillTableSchema().

So, lines 192 and 1066 of DB/DataObject/Generator.php (mentioned above) should be removed.

Then, if desired, the portability mode should be set once in DB_DataObject::_connect() by inserting this at line 2231 in DB/DataObject.php:

$db_options['portability'] = MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE;

[2007-01-28 03:17 UTC] david dot ward at gatech dot edu

Sorry for the delay, here it is.

--- DataObject.old.php Sat Jan 27 22:08:28 2007
+++ DataObject.php Sat Jan 27 22:11:22 2007
@@ -2228,6 +2228,7 @@
require_once 'MDB2.php';
// this allows the setings of compatibility on MDB2
$db_options = PEAR::getStaticProperty('MDB2','options');
+ $db_options['portability'] = MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE;
$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = MDB2::connect($dsn,$db_options);

}
--- Generator.old.php Sat Jan 27 22:08:28 2007
+++ Generator.php Sat Jan 27 22:11:52 2007
@@ -189,7 +189,6 @@
/**
* set portability and some modules to fetch the informations
*/
- $__DB->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE);
$__DB->loadModule('Manager');
$__DB->loadModule('Reverse');
}
@@ -1063,7 +1062,6 @@
/**
* set portability and some modules to fetch the informations
*/
- $__DB->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE);
$__DB->loadModule('Manager');
$__DB->loadModule('Reverse');
}

[2007-03-18 10:01 UTC] david dot ward at gatech dot edu

That is another way to change the portability option, yes, although MDB2 has the setOption function that I used in my diff.

However this will have no effect if you don't fix the three lines that allow the database connection variable to be stored by reference. You have to make that change for the second one to have any effect.

[2007-03-18 10:05 UTC] david dot ward at gatech dot edu

and now I realize that I didn't change those three lines to make the diff. Go figure. Do you want another diff or do you see what I'm talking about from my original message?

[2007-05-08 20:19 UTC] david dot ward at gatech dot edu

Sorry for the confusion and for not including everything in the earlier diff. Basically here is the diff of all the changes needed to fix this issue.

I have removed two lines in Generator.php because they are redundant and unnecessary after the fixes are made to DataObject.php.

If you have questions let me know.

--- DataObject.old.php Tue May 8 16:00:48 2007
+++ DataObject.php Tue May 8 16:04:18 2007
@@ -2218,9 +2218,9 @@
$db_options = PEAR::getStaticProperty('DB','options');
require_once 'DB.php';
if ($db_options) {
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = DB::connect($dsn,$db_options);
+ $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &DB::connect($dsn,$db_options);
} else {
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = DB::connect($dsn);
+ $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &DB::connect($dsn);
}

} else {
@@ -2228,7 +2228,8 @@
require_once 'MDB2.php';
// this allows the setings of compatibility on MDB2
$db_options = PEAR::getStaticProperty('MDB2','options');
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = MDB2::connect($dsn,$db_options);
+ $db_options['portability'] = MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE;
+ $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &MDB2::connect($dsn,$db_options);

}

--- Generator.old.php Tue May 8 15:56:44 2007
+++ Generator.php Tue May 8 16:06:56 2007
@@ -189,7 +189,6 @@
/**
* set portability and some modules to fetch the informations
*/
- $__DB->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE);
$__DB->loadModule('Manager');
$__DB->loadModule('Reverse');
}
@@ -1063,7 +1062,6 @@
/**
* set portability and some modules to fetch the informations
*/
- $__DB->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE);
$__DB->loadModule('Manager');
$__DB->loadModule('Reverse');
}

[2007-08-10 04:22 UTC] david dot ward at gatech dot edu

If you have any table/field names with spaces, DB_DataObject will throw several errors and not work, unless you include all of the options as I described. Try it and see for yourself. The changes I included are a bug fix, not just some personal configuration preferences I came up with. Please apply the entire list of changes I submitted to you if you want MDB2 to work properly. Thanks.