Skip to content

Commit

Permalink
- improved error handling
Browse files Browse the repository at this point in the history
- fetch all data at once since otherwise we get notices from MDB


git-svn-id: http://svn.php.net/repository/pear/packages/RDF/trunk@175809 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Lukas Smith committed Dec 28, 2004
1 parent d153c59 commit 18da9e6
Showing 1 changed file with 71 additions and 26 deletions.
97 changes: 71 additions & 26 deletions RDF/Model/MDB.php
Expand Up @@ -54,7 +54,7 @@ class RDF_Model_MDB extends RDF_Model
*/ */
function RDF_Model_MDB(&$dbConnection, $modelURI, $modelID, $baseURI = null) function RDF_Model_MDB(&$dbConnection, $modelURI, $modelID, $baseURI = null)
{ {
$this->dbConn = &$dbConnection; $this->dbConn =& $dbConnection;
$this->modelURI = $modelURI; $this->modelURI = $modelURI;
$this->modelID = $modelID; $this->modelID = $modelID;
$this->baseURI = $this->_checkBaseURI($baseURI); $this->baseURI = $this->_checkBaseURI($baseURI);
Expand Down Expand Up @@ -191,7 +191,7 @@ function toString()
*/ */
function toStringIncludingTriples() function toStringIncludingTriples()
{ {
$Model_Memory = &$this->getMemModel(); $Model_Memory =& $this->getMemModel();
return $Model_Memory->toStringIncludingTriples(); return $Model_Memory->toStringIncludingTriples();
} }


Expand All @@ -214,7 +214,7 @@ function &getMemModel()
*/ */
function writeAsHtml() function writeAsHtml()
{ {
$Model_Memory = &$this->getMemModel(); $Model_Memory =& $this->getMemModel();
$Model_Memory->writeAsHtml(); $Model_Memory->writeAsHtml();
} }


Expand All @@ -225,7 +225,7 @@ function writeAsHtml()
*/ */
function writeAsHtmlTable() function writeAsHtmlTable()
{ {
$Model_Memory = &$this->getMemModel(); $Model_Memory =& $this->getMemModel();
RDF_Util::writeHTMLTable($Model_Memory); RDF_Util::writeHTMLTable($Model_Memory);
} }


Expand All @@ -237,7 +237,7 @@ function writeAsHtmlTable()
*/ */
function writeRDFToString() function writeRDFToString()
{ {
$Model_Memory = &$this->getMemModel(); $Model_Memory =& $this->getMemModel();
return $Model_Memory->writeRDFToString(); return $Model_Memory->writeRDFToString();
} }


Expand Down Expand Up @@ -300,7 +300,9 @@ function containsAll(&$model)
return true; return true;
} elseif (is_a($model, 'RDF_Model_MDB')) { } elseif (is_a($model, 'RDF_Model_MDB')) {
$result = $this->_getRecordSet($model); $result = $this->_getRecordSet($model);
while (is_array($row = $this->dbConn->fetchInto($result))) { $all = $this->dbConn->fetchAll($result);
reset($all);
while (is_array($row = next($all))) {
if (!$this->_containsRow($row)) { if (!$this->_containsRow($row)) {
return false; return false;
} }
Expand Down Expand Up @@ -330,8 +332,10 @@ function containsAny(&$model)
} }
return false; return false;
} elseif (is_a($model, 'RDF_Model_MDB')) { } elseif (is_a($model, 'RDF_Model_MDB')) {
$result = &$this->_getRecordSet($model); $result =& $this->_getRecordSet($model);
while (is_array($row = $this->dbConn->fetchInto($result))) { $all = $this->dbConn->fetchAll($result);
reset($all);
while (is_array($row = next($all))) {
if ($this->_containsRow($row)) { if ($this->_containsRow($row)) {
return true; return true;
} }
Expand Down Expand Up @@ -405,7 +409,7 @@ function find($subject, $predicate, $object)
*/ */
function findRegex($subject_regex, $predicate_regex, $object_regex) function findRegex($subject_regex, $predicate_regex, $object_regex)
{ {
$mm = &$this->getMemModel(); $mm =& $this->getMemModel();


return $mm->findRegex($subject_regex, $predicate_regex, $object_regex); return $mm->findRegex($subject_regex, $predicate_regex, $object_regex);
} }
Expand Down Expand Up @@ -598,7 +602,11 @@ function equals(&$that)
return false; return false;
} }


if (!$this->containsAll($that)) { $result = $this->containsAll($that);
if (PEAR::isError($result)) {
return $result;
}
if (!$result) {
return false; return false;
} }
return true; return true;
Expand Down Expand Up @@ -638,11 +646,11 @@ function &unite(&$model)
} }


if (is_a($model, 'RDF_Model_Memory')) { if (is_a($model, 'RDF_Model_Memory')) {
$thisModel = &$this->getMemModel(); $thisModel =& $this->getMemModel();
return $thisModel->unite($model); return $thisModel->unite($model);
} elseif (is_a($model, 'RDF_Model_MDB')) { } elseif (is_a($model, 'RDF_Model_MDB')) {
$thisModel = &$this->getMemModel(); $thisModel =& $this->getMemModel();
$thatModel = &$model->getMemModel(); $thatModel =& $model->getMemModel();
return $thisModel->unite($thatModel); return $thisModel->unite($thatModel);
} }
} }
Expand All @@ -665,11 +673,11 @@ function &subtract(&$model)
} }


if (is_a($model, 'RDF_Model_Memory')) { if (is_a($model, 'RDF_Model_Memory')) {
$thisModel = &$this->getMemModel(); $thisModel =& $this->getMemModel();
return $thisModel->subtract($model); return $thisModel->subtract($model);
} elseif (is_a($model, 'RDF_Model_MDB')) { } elseif (is_a($model, 'RDF_Model_MDB')) {
$thisModel = &$this->getMemModel(); $thisModel =& $this->getMemModel();
$thatModel = &$model->getMemModel(); $thatModel =& $model->getMemModel();
return $thisModel->subtract($thatModel); return $thisModel->subtract($thatModel);
} }
} }
Expand All @@ -686,11 +694,11 @@ function &subtract(&$model)
function &intersect(&$model) function &intersect(&$model)
{ {
if (is_a($model, 'RDF_Model_Memory')) { if (is_a($model, 'RDF_Model_Memory')) {
$thisModel = &$this->getMemModel(); $thisModel =& $this->getMemModel();
return $thisModel->intersect($model); return $thisModel->intersect($model);
} elseif (is_a($model, 'RDF_Model_MDB')) { } elseif (is_a($model, 'RDF_Model_MDB')) {
$thisModel = &$this->getMemModel(); $thisModel =& $this->getMemModel();
$thatModel = &$model->getMemModel(); $thatModel =& $model->getMemModel();
return $thisModel->intersect($thatModel); return $thisModel->intersect($thatModel);
} }


Expand Down Expand Up @@ -723,15 +731,21 @@ function addModel(&$model)
if (is_a($model, 'RDF_Model_Memory')) { if (is_a($model, 'RDF_Model_Memory')) {
$this->dbConn->autoCommit(false); $this->dbConn->autoCommit(false);
foreach ($model->triples as $statement) { foreach ($model->triples as $statement) {
$this->_addStatementFromAnotherModel($statement, $blankNodes_tmp); $result = $this->_addStatementFromAnotherModel($statement, $blankNodes_tmp);
if (PEAR::isError($result)) {
return $result;
}
} }
$this->dbConn->commit(); $this->dbConn->commit();
$this->dbConn->autoCommit(true); $this->dbConn->autoCommit(true);
} elseif (is_a($model, 'RDF_Model_MDB')) { } elseif (is_a($model, 'RDF_Model_MDB')) {
$this->dbConn->autoCommit(false); $this->dbConn->autoCommit(false);
$Model_Memory =& $model->getMemModel(); $Model_Memory =& $model->getMemModel();
foreach($Model_Memory->triples as $statement) { foreach($Model_Memory->triples as $statement) {
$this->_addStatementFromAnotherModel($statement, $blankNodes_tmp); $result = $this->_addStatementFromAnotherModel($statement, $blankNodes_tmp);
if (PEAR::isError($result)) {
return $result;
}
} }
$this->dbConn->commit(); $this->dbConn->commit();
$this->dbConn->autoCommit(true); $this->dbConn->autoCommit(true);
Expand All @@ -747,7 +761,7 @@ function addModel(&$model)
*/ */
function &reify() function &reify()
{ {
$Model_Memory = &$this->getMemModel(); $Model_Memory =& $this->getMemModel();
return $Model_Memory->reify(); return $Model_Memory->reify();
} }


Expand Down Expand Up @@ -803,8 +817,17 @@ function getUniqueResourceURI($prefix)
while (true) { while (true) {
$uri = $this->getBaseURI() . $prefix . $counter; $uri = $this->getBaseURI() . $prefix . $counter;
$tempbNode =& RDF_BlankNode::factory($uri); $tempbNode =& RDF_BlankNode::factory($uri);
if (PEAR::isError($tempbNode)) {
return $tempbNode;
}
$res1 = $this->find($tempbNode, null, null); $res1 = $this->find($tempbNode, null, null);
if (PEAR::isError($res1)) {
return $res1;
}
$res2 = $this->find(null, null, $tempbNode); $res2 = $this->find(null, null, $tempbNode);
if (PEAR::isError($res2)) {
return $res2;
}
if ($res1->size() == 0 && $res2->size() == 0) { if ($res1->size() == 0 && $res2->size() == 0) {
return $uri; return $uri;
} }
Expand Down Expand Up @@ -864,29 +887,51 @@ function _getNodeFlag($object)
function _convertRecordSetToMemModel($result) function _convertRecordSetToMemModel($result)
{ {
$res =& new RDF_Model_Memory($this->getBaseURI()); $res =& new RDF_Model_Memory($this->getBaseURI());
while (is_array($row = $this->dbConn->fetchInto($result))) { $all = $this->dbConn->fetchAll($result);
reset($all);
while (is_array($row = next($all))) {
// subject // subject
if ($row[5] == 'r') { if ($row[5] == 'r') {
$sub =& RDF_Resource::factory($row[0]); $sub =& RDF_Resource::factory($row[0]);
} else { } else {
$sub =& RDF_BlankNode::factory($row[0]); $sub =& RDF_BlankNode::factory($row[0]);
} }
if (PEAR::isError($sub)) {
return $sub;
}
// predicate // predicate
$pred =& RDF_Resource::factory($row[1]); $pred =& RDF_Resource::factory($row[1]);
if (PEAR::isError($pred)) {
return $pred;
}
// object // object
if ($row[6] == 'r') { if ($row[6] == 'r') {
$obj =& RDF_Resource::factory($row[2]); $obj =& RDF_Resource::factory($row[2]);
if (PEAR::isError($obj)) {
return $obj;
}
} elseif ($row[6] == 'b') { } elseif ($row[6] == 'b') {
$obj =& RDF_BlankNode::factory($row[2]); $obj =& RDF_BlankNode::factory($row[2]);
if (PEAR::isError($obj)) {
return $obj;
}
} else { } else {
$obj =& RDF_Literal::factory($row[2], $row[3]); $obj =& RDF_Literal::factory($row[2], $row[3]);
if (PEAR::isError($obj)) {
return $obj;
}
if ($row[4]) { if ($row[4]) {
$obj->setDatatype($row[4]); $obj->setDatatype($row[4]);
} }
} }

$statement =& RDF_Statement::factory($sub, $pred, $obj); $statement =& RDF_Statement::factory($sub, $pred, $obj);
$res->add($statement); if (PEAR::isError($statement)) {
return $statement;
}
$result = $res->add($statement);
if (PEAR::isError($result)) {
return $result;
}
} }
$this->dbConn->freeResult($result); $this->dbConn->freeResult($result);
return $res; return $res;
Expand Down Expand Up @@ -972,7 +1017,7 @@ function _containsRow($row)
AND subject_is=' . $this->dbConn->getValue('text', $row[5]) . ' AND subject_is=' . $this->dbConn->getValue('text', $row[5]) . '
AND object_is=' . $this->dbConn->getValue('text', $row[6]); AND object_is=' . $this->dbConn->getValue('text', $row[6]);


$result = &$this->dbConn->queryOne($sql); $result =& $this->dbConn->queryOne($sql);


if (MDB::isError($result)) { if (MDB::isError($result)) {
return $result; return $result;
Expand Down

0 comments on commit 18da9e6

Please sign in to comment.