From: <bh...@us...> - 2007-04-07 03:37:50
|
Revision: 16248 http://svn.sourceforge.net/gallery/?rev=16248&view=rev Author: bharat Date: 2007-04-06 20:37:51 -0700 (Fri, 06 Apr 2007) Log Message: ----------- Extend addMapEntry and updateMapEntry to use non-transactional database connections. This is phase 2 of preparation for the event logging system. By allowing addMapEntry and updateMapEntry to modify database rows outside of the transaction, we can safely log errors even when the main transaction is destined to get rolled back, which is useful for error situations. Bump GalleryCoreApi to 7.25 Modified Paths: -------------- trunk/gallery2/modules/core/classes/GalleryCoreApi.class trunk/gallery2/modules/core/classes/GalleryStorage/GalleryStorageExtras.class trunk/gallery2/modules/core/classes/GalleryStorage.class trunk/gallery2/modules/core/test/phpunit/StorageTest.class Modified: trunk/gallery2/modules/core/classes/GalleryCoreApi.class =================================================================== --- trunk/gallery2/modules/core/classes/GalleryCoreApi.class 2007-04-07 03:27:42 UTC (rev 16247) +++ trunk/gallery2/modules/core/classes/GalleryCoreApi.class 2007-04-07 03:37:51 UTC (rev 16248) @@ -79,7 +79,7 @@ * - delete GalleryTestCase::failWithStatus */ function getApiVersion() { - return array(7, 24); + return array(7, 25); } /** @@ -2757,9 +2757,11 @@ * @param string $mapName the map we're working on * @param array $match the entries to match * @param array $change the values to change + * @param boolean $useNonTransactionalConnection (optional) set to true if we should use a new + * non transactional database connection for this operation. Default is false. * @return object GalleryStatus a status code */ - function updateMapEntry($mapName, $match, $change) { + function updateMapEntry($mapName, $match, $change, $useNonTransactionalConnection=false) { global $gallery; if (sizeof($match) == 0 || sizeof($change) == 0) { @@ -2767,7 +2769,7 @@ } $storage =& $gallery->getStorage(); - $ret = $storage->updateMapEntry($mapName, $match, $change); + $ret = $storage->updateMapEntry($mapName, $match, $change, $useNonTransactionalConnection); if ($ret) { return $ret; } @@ -2829,13 +2831,15 @@ * * @param string $mapName the map we're working on * @param array $data an associative array of the entry data + * @param boolean $useNonTransactionalConnection (optional) set to true if we should use a new + * non transactional database connection for this operation. Default is false. * @return object GalleryStatus a status code */ - function addMapEntry($mapName, $data) { + function addMapEntry($mapName, $data, $useNonTransactionalConnection=false) { global $gallery; $storage =& $gallery->getStorage(); - $ret = $storage->addMapEntry($mapName, $data); + $ret = $storage->addMapEntry($mapName, $data, $useNonTransactionalConnection); if ($ret) { return $ret; } Modified: trunk/gallery2/modules/core/classes/GalleryStorage/GalleryStorageExtras.class =================================================================== --- trunk/gallery2/modules/core/classes/GalleryStorage/GalleryStorageExtras.class 2007-04-07 03:27:42 UTC (rev 16247) +++ trunk/gallery2/modules/core/classes/GalleryStorage/GalleryStorageExtras.class 2007-04-07 03:37:51 UTC (rev 16248) @@ -968,10 +968,18 @@ /** * @see GalleryStorage::addMapEntry */ - function addMapEntry($mapName, $entry) { - $ret = $this->_dbInit(true); - if ($ret) { - return $ret; + function addMapEntry($mapName, $entry, $useNonTransactionalConnection=false) { + if ($useNonTransactionalConnection && $this->_gs->isTransactional()) { + list ($ret, $db) = $this->_getNonTransactionalDatabaseConnection(); + if ($ret) { + return $ret; + } + } else { + $ret = $this->_dbInit(true); + if ($ret) { + return $ret; + } + $db = $this->_gs->_db; } list ($ret, $mapInfo) = $this->_gs->describeMap($mapName); @@ -987,7 +995,7 @@ } if (is_array($entry[$memberName])) { - return $this->_addMapEntries($mapInfo, $tableName, $entry); + return $this->_addMapEntries($mapInfo, $tableName, $entry, $db); } $columns[] = $this->_gs->_translateColumnName($memberName); $data[] = $this->_gs->_normalizeValue($entry[$memberName], $memberData); @@ -999,7 +1007,7 @@ $query .= ') VALUES (' . $markers . ')'; $this->_gs->_traceStart(); - $recordSet = $this->_gs->_db->Execute($query, $data); + $recordSet = $db->Execute($query, $data); $this->_gs->_traceStop(); if ($recordSet === false) { return GalleryCoreApi::error(ERROR_STORAGE_FAILURE); @@ -1018,10 +1026,11 @@ * @param string $tableName the translated table name * @param array $entry an associative array of data about the entry * each data element is an array of values + * @param object ADOdb $db the database connection to use * @return object GalleryStatus a status code * @access private */ - function _addMapEntries($mapInfo, $tableName, $entry) { + function _addMapEntries($mapInfo, $tableName, $entry, $db) { $columns = array(); foreach ($mapInfo as $memberName => $memberData) { $columns[$memberName] = $this->_gs->_translateColumnName($memberName); @@ -1043,7 +1052,7 @@ } $this->_gs->_traceStart(); - $recordSet = $this->_gs->_db->Execute($query, $data); + $recordSet = $db->Execute($query, $data); $this->_gs->_traceStop(); if ($recordSet === false) { return GalleryCoreApi::error(ERROR_STORAGE_FAILURE); Modified: trunk/gallery2/modules/core/classes/GalleryStorage.class =================================================================== --- trunk/gallery2/modules/core/classes/GalleryStorage.class 2007-04-07 03:27:42 UTC (rev 16247) +++ trunk/gallery2/modules/core/classes/GalleryStorage.class 2007-04-07 03:37:51 UTC (rev 16248) @@ -499,11 +499,13 @@ * * @param string $mapName the map we're working on * @param array $entry an associative array of data about the entry + * @param boolean $useNonTransactionalConnection (optional) set to true if we should use a new + * non transactional database connection for this operation. Default is false. * @return object GalleryStatus a status code */ - function addMapEntry($mapName, $entry) { + function addMapEntry($mapName, $entry, $useNonTransactionalConnection=false) { $extras =& $this->_getExtras(); - return $extras->addMapEntry($mapName, $entry); + return $extras->addMapEntry($mapName, $entry, $useNonTransactionalConnection); } /** @@ -632,14 +634,25 @@ * @param string $mapName the map we're working on * @param array $match the entries to match * @param array $change the values to change + * @param boolean $useNonTransactionalConnection (optional) set to true if we should use a new + * non transactional database connection for this operation. Default is false. * @return object GalleryStatus a status code */ - function updateMapEntry($mapName, $match, $change) { - if (!isset($this->_db)) { - list ($ret, $this->_db) = $this->_getConnection(); + function updateMapEntry($mapName, $match, $change, $useNonTransactionalConnection=false) { + if ($useNonTransactionalConnection && $this->isTransactional()) { + $extras =& $this->_getExtras(); + list ($ret, $db) = $extras->_getNonTransactionalDatabaseConnection(); if ($ret) { return $ret; } + } else { + if (!isset($this->_db)) { + list ($ret, $this->_db) = $this->_getConnection(); + if ($ret) { + return $ret; + } + } + $db = $this->_db; } $ret = $this->_guaranteeTransaction(); @@ -685,7 +698,7 @@ $data = array_merge($data, $wheredata); $this->_traceStart(); - $recordSet = $this->_db->Execute($query, $data); + $recordSet = $db->Execute($query, $data); $this->_traceStop(); if (!$recordSet) { return GalleryCoreApi::error(ERROR_STORAGE_FAILURE); Modified: trunk/gallery2/modules/core/test/phpunit/StorageTest.class =================================================================== --- trunk/gallery2/modules/core/test/phpunit/StorageTest.class 2007-04-07 03:27:42 UTC (rev 16247) +++ trunk/gallery2/modules/core/test/phpunit/StorageTest.class 2007-04-07 03:37:51 UTC (rev 16248) @@ -28,7 +28,8 @@ class StorageTest extends GalleryTestCase { var $_db; /* backup of ADODB object */ - var $_results; /* results from the generated query */ + var $_results; /* results from the generated query on the regular db connection */ + var $_resultsNonTransactional; /* results on the non transactional db connection */ function StorageTest($methodName) { $this->GalleryTestCase($methodName); @@ -42,10 +43,13 @@ } $storage =& $gallery->getStorage(); + $this->_db = $storage->_db; + $this->_nonTransactionalDb = $storage->_nonTransactionalDb; $this->_serverInfo = isset($storage->_serverInfo) ? $storage->_serverInfo : null; $this->_isEmptyAllowedForNotNullColumn = $storage->_isEmptyAllowedForNotNullColumn; - $storage->_db = new StorageTestDB($this); + $storage->_db = new StorageTestDB($this, '_results'); + $storage->_nonTransactionalDb = new StorageTestDB($this, '_resultsNonTransactional'); } function tearDown() { @@ -53,6 +57,7 @@ $storage =& $gallery->getStorage(); $storage->_db = $this->_db; + $storage->_nonTransactionalDb = $this->_nonTransactionalDb; $storage->_serverInfo = $this->_serverInfo; $storage->_isEmptyAllowedForNotNullColumn = $this->_isEmptyAllowedForNotNullColumn; @@ -125,6 +130,31 @@ $this->_results); } + function testAddMapNonTransactionalDatabaseConnection() { + global $gallery; + $storage =& $gallery->getStorage(); + + /* + * We really should use a fake map, but it's hard to insert a fake map definition into + * GalleryStorage at the moment. + */ + $ret = $storage->addMapEntry( + 'GalleryUserGroupMap', + array('userId' => 0, 'groupId' => 1), + true); + if ($ret) { + return $ret; + } + + $this->assertEquals( + array(array( + sprintf('INSERT INTO %sUserGroupMap (%suserId, %sgroupId) VALUES (?,?)', + $storage->_tablePrefix, $storage->_columnPrefix, + $storage->_columnPrefix), + array(0, 1))), + $this->_resultsNonTransactional); + } + function testAddMapTruncate() { global $gallery; $storage =& $gallery->getStorage(); @@ -258,12 +288,45 @@ array($storage->_tablePrefix . 'UserGroupMap', array($storage->_columnPrefix . 'userId', $storage->_columnPrefix . 'groupId'), 3)); + if ($ret) { + return $ret; + } $this->assertEquals( array(array($sql, array(1, 4, 2, 5, 3, 6))), $this->_results); } + function testAddMapArrayNonTransactionalDatabaseConnection() { + global $gallery; + $storage =& $gallery->getStorage(); + + /* + * We really should use a fake map, but it's hard to insert a fake map definition into + * GalleryStorage at the moment. + */ + $ret = $storage->addMapEntry( + 'GalleryUserGroupMap', + array('userId' =>array(1, 2, 3), 'groupId' => array(4, 5, 6)), + true); + if ($ret) { + return $ret; + } + + list ($ret, $sql) = $storage->getFunctionSql( + 'MULTI_INSERT', + array($storage->_tablePrefix . 'UserGroupMap', + array($storage->_columnPrefix . 'userId', + $storage->_columnPrefix . 'groupId'), 3)); + if ($ret) { + return $ret; + } + + $this->assertEquals( + array(array($sql, array(1, 4, 2, 5, 3, 6))), + $this->_resultsNonTransactional); + } + function testAddMapArrayTruncate() { global $gallery; $storage =& $gallery->getStorage(); @@ -287,6 +350,9 @@ array($storage->_columnPrefix . 'externalId', $storage->_columnPrefix . 'entityType', $storage->_columnPrefix . 'entityId'), 2)); + if ($ret) { + return $ret; + } $this->assertEquals( array(array($sql, array(str_repeat('x', 128), 'test1', 1, @@ -444,6 +510,28 @@ $this->_results); } + function testUpdateMapEntryNonTransactionalDatabaseConnection() { + global $gallery; + $storage =& $gallery->getStorage(); + + $mapName = 'ExternalIdMap'; + $match = array('externalId' => array(38, 40, 42)); + $set = array('entityType' => 54); + + $ret = GalleryCoreApi::updateMapEntry($mapName, $match, $set, true); + if ($ret) { + return $ret; + } + + $this->assertEquals( + array(array( + sprintf('UPDATE %1$sExternalIdMap SET %2$sentityType=? ' . + 'WHERE %2$sexternalId IN (?,?,?)', + $storage->_tablePrefix, $storage->_columnPrefix), + array(54, 38, 40, 42))), + $this->_resultsNonTransactional); + } + function testSaveEntity() { global $gallery; $storage =& $gallery->getStorage(); @@ -1050,8 +1138,9 @@ var $transCnt = 1; /* Pretend we're already in a transaction */ var $_recordSets; - function StorageTestDB(&$testCase) { + function StorageTestDB(&$testCase, $resultsType) { $this->_testCase =& $testCase; + $this->_results =& $testCase->$resultsType; $this->_recordSets = array(new StorageTestRecordSet()); } @@ -1060,12 +1149,12 @@ } function execute($query, $data=false) { - $this->_testCase->_results[] = array($query, $data); + $this->_results[] = array($query, $data); return empty($this->_recordSets) ? false : array_shift($this->_recordSets); } function SelectLimit($query, $nrows=-1, $offset=-1, $inputarr=false) { - $this->_testCase->_results[] = array($query, $nrows, $offset, $inputarr); + $this->_results[] = array($query, $nrows, $offset, $inputarr); return empty($this->_recordSets) ? false : array_shift($this->_recordSets); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |