/** * Deletes the data row from the appropriate table. * @param integer $dbID The {@link DBHandler} database ID to query. * @param integer $dataID The ID in the database of the data to be deleted. * @access public * @return void */ function prune($dbID, $dataID) { if (!$dataID) { return; } // delete ourselves from our data table $table = $this->_table; $query = new DeleteQuery(); $query->setTable($table); $query->setWhere("id='" . addslashes($dataID) . "'"); $dbHandler = Services::getService("DatabaseManager"); $res = $dbHandler->query($query, $dbID); if (!$res) { throwError(new UnknownDBError("StorablePrimitive")); } }
/** * Delete a Part and all its Parts. * * @param object Id $partId * * @throws object RepositoryException An exception with one of * the following messages defined in * org.osid.repository.RepositoryException may be thrown: {@link * org.osid.repository.RepositoryException#OPERATION_FAILED * OPERATION_FAILED}, {@link * org.osid.repository.RepositoryException#PERMISSION_DENIED * PERMISSION_DENIED}, {@link * org.osid.repository.RepositoryException#CONFIGURATION_ERROR * CONFIGURATION_ERROR}, {@link * org.osid.repository.RepositoryException#UNIMPLEMENTED * UNIMPLEMENTED}, {@link * org.osid.repository.RepositoryException#NULL_ARGUMENT * NULL_ARGUMENT}, {@link * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID} * * @access public */ function deletePart(Id $partId) { $string = $partId->getIdString(); if (preg_match("/(.*)-(" . implode("|", array_keys($this->_parts)) . ")/", $string, $r)) { $recordId = $r[1]; $field = $r[2]; if ($this->_isLastPart($field)) { $dbHandler = Services::getService("DatabaseManager"); // Delete the data $query = new DeleteQuery(); $query->setTable("dr_file_url"); $query->setWhere("fk_file = '" . $this->_id->getIdString() . "'"); $dbHandler->query($query, $this->_configuration->getProperty("database_index")); // Delete the thumbnail $query = new DeleteQuery(); $query->setTable("dr_thumbnail"); $query->setWhere("fk_file = '" . $this->_id->getIdString() . "'"); $dbHandler->query($query, $this->_configuration->getProperty("database_index")); // delete the file row. $query = new DeleteQuery(); $query->setTable("dr_file"); $query->setWhere("id = '" . $this->_id->getIdString() . "'"); $dbHandler->query($query, $this->_configuration->getProperty("database_index")); } else { $this->_parts[$field]->updateValue("NULL"); } } else { throwError(new Error(RepositoryException::UNKNOWN_ID() . ": {$string}", "FileRecord", true)); } $this->_asset->updateModificationDate(); }
public function testWhere() { $q = new DeleteQuery(array('test')); $q->setWhere(new AndOp(array(new Condition('=', new Field('group'), 'test'), new Condition('=', new Field('author'), null)))); $this->assertEquals('DELETE FROM `test` WHERE (`test`.`group` = :p1 AND `test`.`author` IS NULL)', $q->sql()); }
function test_All_Queries() { $value = "'Depeche Mode rocks!'"; $this->dbhandler->connect(); // create a new queue of queries to execuete $queryQueue = new Queue(); $query = new InsertQuery(); $query->setTable("test1"); $query->setColumns(array("value")); $query->addRowOfValues(array($value)); $queryQueue->add($query); $query = new InsertQuery(); $query->setTable("test1"); $query->setColumns(array(id, value)); $query->addRowOfValues(array("3000000", $value)); $queryQueue->add($query); $query = new DeleteQuery(); $query->setTable("test1"); $query->setWhere("id = 3000000"); $queryQueue->add($query); $query = new UpdateQuery(); $query->setTable("test1"); $query->setColumns(array("value")); $query->setValues(array($value)); $query->setWhere("id > 1000 AND id < 1006"); $queryQueue->add($query); $resultQueue = $this->dbhandler->queryQueue($queryQueue); $this->assertEqual($this->dbhandler->getTotalNumberOfQueries(), 4); $this->assertEqual($this->dbhandler->getTotalNumberOfSuccessfulQueries(), 4); $this->assertEqual($this->dbhandler->getTotalNumberOfFailedQueries(), 0); $result = $resultQueue->next(); $this->assertEqual($result->getNumberOfRows(), 1); $this->assertNotNull($result->getLastAutoIncrementValue()); $id = $result->getLastAutoIncrementValue(); $result = $resultQueue->next(); $this->assertEqual($result->getNumberOfRows(), 1); $this->assertNotNull($result->getLastAutoIncrementValue()); $result = $resultQueue->next(); $this->assertEqual($result->getNumberOfRows(), 1); $result = $resultQueue->next(); $query = new SelectQuery(); $query->setColumns(array("value")); $query->addTable("test1"); $query->setWhere("id = {$id}"); $result = $this->dbhandler->query($query); $this->assertEqual($this->dbhandler->getTotalNumberOfQueries(), 5); $this->assertEqual($this->dbhandler->getTotalNumberOfSuccessfulQueries(), 5); $this->assertEqual($this->dbhandler->getTotalNumberOfFailedQueries(), 0); $this->assertEqual("'" . $result->field("value") . "'", $value); $result->free(); }
/** * Commits any changes that have been made to the database. If neither update() nor prune() have been * called, even if changes have been made, they will not be reflected in the database. * @return bool * @access public */ function commit() { $dbHandler = Services::getService("DatabaseManager"); if ($this->_update) { // let's re-cast our primitive to a storablePrimitive $this->recastAsStorable(); // first we need to commit the actual Primitive value // so that we can get its ID if (!$this->_dataID) { $this->_dataID = $this->_primitive->insert(DATAMANAGER_DBID); } else { $this->_primitive->update(DATAMANAGER_DBID, $this->_dataID); } $this->_date = DateAndTime::now(); if ($this->_myID) { // we're already in the DB. just update the entry $query = new UpdateQuery(); $query->setWhere("id='" . addslashes($this->_myID) . "'"); $query->setColumns(array("value_index", "active", "modified")); $query->setValues(array($this->_parent->getIndex(), $this->_active ? 1 : 0, $dbHandler->toDBDate($this->_date, DATAMANAGER_DBID))); } else { // we have to insert a new one $query = new InsertQuery(); $idManager = Services::getService("Id"); $newID = $idManager->createId(); $this->_myID = $newID->getIdString(); $query->setColumns(array("id", "fk_record", "fk_schema_field", "value_index", "fk_data", "active", "modified")); $schema = $this->_parent->_parent->_parent->getSchema(); $schemaField = $this->_parent->_parent->getSchemaField(); $query->addRowOfValues(array("'" . addslashes($this->_myID) . "'", "'" . addslashes($this->_parent->_parent->_parent->getID()) . "'", "'" . addslashes($schemaField->getID()) . "'", $this->_parent->getIndex(), "'" . addslashes($this->_dataID) . "'", $this->_active ? 1 : 0, $dbHandler->toDBDate($this->_date, DATAMANAGER_DBID))); } $query->setTable("dm_record_field"); $result = $dbHandler->query($query, DATAMANAGER_DBID); if (!$result) { throwError(new UnknownDBError("DMRecord")); return false; } } if ($this->_prune && $this->_dataID) { if ($id = $this->getID()) { // ok, let's get rid of ourselves... completely! $query = new DeleteQuery(); $query->setTable("dm_record_field"); $query->setWhere("id='" . addslashes($id) . "'"); $res = $dbHandler->query($query, DATAMANAGER_DBID); if (!$res) { throwError(new UnknownDBError("DMRecord")); } // now tell the data object to prune itself $this->recastAsStorable(); $this->_primitive->prune(DATAMANAGER_DBID, $this->_dataID); // and we have to get rid of any tag mappings where we are included. $query = new DeleteQuery(); $query->setTable("dm_tag_map"); $query->setWhere("fk_record_field='" . addslashes($id) . "'"); $res = $dbHandler->query($query, DATAMANAGER_DBID); if (!$res) { throwError(new UnknownDBError("DMRecord")); } } } // reset the prune flag $this->_prune = false; // reset the update flag $this->_update = false; return true; }
/** * Attempts to commit our {@link DMRecord}s to the database and update our mapping. * @param boolean optional $ignoreMandatory If true, doesn't fail if mandatory * fields don't have values. * @return void */ function commit($ignoreMandatory = false) { $ids = array(); if (count($this->_records)) { for ($i = 0; $i < count($this->_records); $i++) { $this->_records[$i]->commit($ignoreMandatory); $ids[] = $this->_records[$i]->getID(); } $this->_records = array(); $this->_fetchMode = -1; } if ($this->_dirty) { // syncrhonize the database $ids = array_merge($ids, $this->_storedRecordIDs); // Make sure that we only have one ID for each record. $ids = array_unique($ids); $dbHandler = Services::getService("DatabaseManager"); // first delete all the old mappings $query = new DeleteQuery(); $query->setTable("dm_record_set"); $query->setWhere("dm_record_set.id='" . addslashes($this->_myID) . "'"); // printpre(MySQL_SQLGenerator::generateSQLQuery($query)); $dbHandler->query($query, DATAMANAGER_DBID); if (count($ids)) { // next insert all our mappings back in. $query = new InsertQuery(); $query->setTable("dm_record_set"); $query->setColumns(array("id", "fk_record")); foreach ($ids as $id) { $query->addRowOfValues(array("'" . addslashes($this->_myID) . "'", "'" . addslashes($id) . "'")); if (!in_array($id, $this->_storedRecordIDs)) { $this->_storedRecordIDs[] = $id; } } // printpre(MySQL_SQLGenerator::generateSQLQuery($query)); $dbHandler->query($query, DATAMANAGER_DBID); // done! } } }
/** * Delete a Part and all its Parts. * * @param object Id $partId * * @throws object RepositoryException An exception with one of * the following messages defined in * org.osid.repository.RepositoryException may be thrown: {@link * org.osid.repository.RepositoryException#OPERATION_FAILED * OPERATION_FAILED}, {@link * org.osid.repository.RepositoryException#PERMISSION_DENIED * PERMISSION_DENIED}, {@link * org.osid.repository.RepositoryException#CONFIGURATION_ERROR * CONFIGURATION_ERROR}, {@link * org.osid.repository.RepositoryException#UNIMPLEMENTED * UNIMPLEMENTED}, {@link * org.osid.repository.RepositoryException#NULL_ARGUMENT * NULL_ARGUMENT}, {@link * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID} * * @access public */ function deletePart(Id $partId) { $string = $partId->getIdString(); if (preg_match("/(.*)-(FILE_SIZE|FILE_NAME|FILE_DATA|MIME_TYPE|THUMBNAIL_DATA|THUMBNAIL_MIME_TYPE)/", $string, $r)) { $recordId = $r[1]; $field = $r[2]; if ($this->_isLastPart($field)) { $dbHandler = Services::getService("DatabaseManager"); // Delete the data $file = $this->_parts['FILE_DATA']->_getFilePath(); if (!unlink($file)) { throwError(new Error(RepositoryException::OPERATION_FAILED() . ": '{$file}' could not be deleted.", "FileSystemFileRecord", true)); } // Delete the thumbnail $query = new DeleteQuery(); $query->setTable("dr_thumbnail"); $query->setWhere("fk_file = '" . $this->_id->getIdString() . "'"); $dbHandler->query($query, $this->_configuration->getProperty("database_index")); // Delete the data row in case we were switching from another type // that used it. $query = new DeleteQuery(); $query->setTable("dr_file_data"); $query->setWhere("fk_file = '" . $this->_id->getIdString() . "'"); $dbHandler->query($query, $this->_configuration->getProperty("database_index")); // delete the file row. $query = new DeleteQuery(); $query->setTable("dr_file"); $query->setWhere("id = '" . $this->_id->getIdString() . "'"); $dbHandler->query($query, $this->_configuration->getProperty("database_index")); } else { if ($field != "FILE_SIZE") { $this->_parts[$field]->updateValue("NULL"); } } } else { throwError(new Error(RepositoryException::UNKNOWN_ID() . ": {$string}", "FileSystemFileRecord", true)); } }
/** * Create a new unique identifier. * * @return object Id * * @throws object IdException An exception with one of the following * messages defined in org.osid.id.IdException: {@link * org.osid.id.IdException#OPERATION_FAILED OPERATION_FAILED}, * {@link org.osid.id.IdException#PERMISSION_DENIED * PERMISSION_DENIED}, {@link * org.osid.id.IdException#CONFIGURATION_ERROR * CONFIGURATION_ERROR}, {@link * org.osid.id.IdException#UNIMPLEMENTED UNIMPLEMENTED} * * @access public */ function createId() { if (isset($this->createId_stmt)) { $this->createId_stmt->execute(); $newID = $this->harmoni_db->lastInsertId('id', 'id_value'); $this->deleteId_stmt->bindValue(1, $newID); $this->deleteId_stmt->execute(); } else { debug::output("Attempting to generate new id.", 20, "IdManager"); $dbHandler = Services::getService("DatabaseManager"); $query = new InsertQuery(); $query->setAutoIncrementColumn("id_value", "id_id_value_seq"); $query->setTable("id"); $query->addRowOfValues(array()); $result = $dbHandler->query($query, $this->_dbIndex); if ($result->getNumberOfRows() != 1) { throwError(new Error(IdException::CONFIGURATION_ERROR(), "IdManager", true)); } $newID = $result->getLastAutoIncrementValue(); // Clear out any values smaller than our last one to keep the table from // exploding size. $query = new DeleteQuery(); $query->setTable("id"); $query->setWhere("id_value < '" . $newID . "'"); $result = $dbHandler->query($query, $this->_dbIndex); } $newID = $this->_prefix . strval($newID); debug::output("Successfully created new id '{$newID}'.", DEBUG_SYS5, "IdManager"); $id = new HarmoniId($newID); // cache the id // $this->_ids[$newID] = $id; return $id; }
/** * Commits (either inserts or updates) the data for this DMRecord into the database. * @param boolean optional $ignoreMandatory If true, doesn't fail if mandatory * fields don't have values. * @return bool */ function commit($ignoreMandatory = false) { // Ensure that we have fields for all labels, incase // the schema has changed since we were loaded. foreach ($this->_schema->getAllLabels() as $label) { $this->_checkLabel($label); } // Get the DBHandler $dbHandler = Services::getService("DatabaseManager"); // the first thing we're gonna do is check to make sure that all our required fields // have at least one value. if (!$this->_delete) { foreach ($this->_schema->getAllIDs() as $id) { $fieldDef = $this->_schema->getField($id); if ($fieldDef->isRequired() && ($this->_fields[$id]->numValues(true) == 0 || $this->_fields[$id]->numValues() == 0) && !$ignoreMandatory) { throwError(new Error("Could not commit DMRecord to database because the required field '{$id}' does\n\t\t\t\t\tnot have any values!", "DMRecord", true)); return false; } } if ($this->_myID) { // we're already in the database $query = new UpdateQuery(); $query->setTable("dm_record"); $query->setColumns(array("ver_control")); $query->setValues(array($this->_versionControlled ? 1 : 0)); $query->setWhere("id='" . addslashes($this->_myID) . "'"); } else { // we'll have to make a new entry $schemaManager = Services::getService("SchemaManager"); $newID = $this->_idManager->createId(); $this->_myID = $newID->getIdString(); $query = new InsertQuery(); $query->setTable("dm_record"); $query->setColumns(array("id", "fk_schema", "created", "ver_control")); $query->addRowOfValues(array("'" . addslashes($this->_myID) . "'", "'" . addslashes($this->_schema->getID()) . "'", $dbHandler->toDBDate($this->_creationDate, DATAMANAGER_DBID), $this->_versionControlled ? 1 : 0)); } // execute the query; $result = $dbHandler->query($query, DATAMANAGER_DBID); if (!$result) { throwError(new UnknownDBError("DMRecord")); return false; } } // now let's cycle through our FieldValues and commit them foreach ($this->_schema->getAllIDs() as $id) { $this->_fields[$id]->commit(); } if ($this->_prune) { $constraint = $this->_pruneConstraint; // check if we have to delete any dataset tags based on our constraints $constraint->checkTags($this); $tagMgr = Services::getService("RecordTagManager"); // if we are no good any more, delete ourselves completely if ($this->_delete) { // now, remove any tags from the DB that have to do with us, since they will no longer // be valid. $tagMgr->pruneTags($this); $query = new DeleteQuery(); $query->setTable("dm_record"); $query->setWhere("id='" . addslashes($this->getID()) . "'"); $dbHandler->query($query, DATAMANAGER_DBID); $query = new DeleteQuery(); $query->setTable("dm_record_set"); $query->setWhere("fk_record='" . addslashes($this->getID()) . "'"); $dbHandler->query($query, DATAMANAGER_DBID); } else { // if we're pruning but not deleting the whole shebang, let's // make sure that there are no tags in the database with no // mappings whatsoever. $tagMgr->checkForEmptyTags($this); } } return true; }
/** * Deletes all tags stored in the database for {@link DMRecord} id $id. * @access public * @param int $id * @return void */ function deleteRecordTags($id) { if (!$id) { return; } // first get a list of RecordTag IDs for this dataset. $query = new SelectQuery(); $query->addTable("dm_tag"); $query->addColumn("id"); $query->setWhere("fk_record='" . addslashes($id) . "'"); $dbHandler = Services::getService("DatabaseManager"); $res = $dbHandler->query($query, DATAMANAGER_DBID); $ids = array(); while ($res->hasMoreRows()) { $ids[] = $res->field(0); $res->advanceRow(); } $res->free(); // now delete the datasets $query = new DeleteQuery(); $query->setTable("dm_tag"); $query->setWhere("fk_record='" . addslashes($id) . "'"); $dbHandler->query($query, DATAMANAGER_DBID); // and delete the mappings $wheres = array(); foreach ($ids as $tagID) { $wheres[] = "fk_tag='" . addslashes($tagID) . "'"; } $query->reset(); $query->setTable("dm_tag_map"); $query->setWhere(implode(" OR ", $wheres)); $dbHandler->query($query, DATAMANAGER_DBID); }
/** * Delete an Asset from this Repository. * * @param object Id $assetId * * @throws object RepositoryException An exception with one of * the following messages defined in * org.osid.repository.RepositoryException may be thrown: {@link * org.osid.repository.RepositoryException#OPERATION_FAILED * OPERATION_FAILED}, {@link * org.osid.repository.RepositoryException#PERMISSION_DENIED * PERMISSION_DENIED}, {@link * org.osid.repository.RepositoryException#CONFIGURATION_ERROR * CONFIGURATION_ERROR}, {@link * org.osid.repository.RepositoryException#UNIMPLEMENTED * UNIMPLEMENTED}, {@link * org.osid.repository.RepositoryException#NULL_ARGUMENT * NULL_ARGUMENT}, {@link * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID} * * @access public */ function deleteAsset(Id $assetId, $parentId = null) { ArgumentValidator::validate($assetId, ExtendsValidatorRule::getRule("Id")); // Get the asset $asset = $this->getAsset($assetId); $assetIdString = $assetId->getIdString(); $children = $asset->getAssets(); // deeper and deeper while ($children->hasNext()) { $child = $children->next(); $this->deleteAsset($child->getId(), $assetIdString); } // climbing out if multiparent unlink parent $parents = $asset->_node->getParents(); if ($parents->count() > 1) { $idManager = Services::getService("Id"); $asset->_node->removeParent($idManager->getId($parentId)); return; } // not multiparent delete asset itself // Delete the Records for the Asset $records = $asset->getRecords(); while ($records->hasNext()) { $record = $records->next(); $recordId = $record->getId(); $asset->deleteRecord($recordId); } // Delete the Record Set $recordMgr = Services::getService("RecordManager"); $assetId = $asset->getId(); $recordMgr->deleteRecordSet($assetId->getIdString()); // Delete the Asset info $query = new DeleteQuery(); $query->setTable("dr_asset_info"); $query->setWhere("asset_id='" . addslashes($assetId->getIdString()) . "'"); $dbc = Services::getService("DatabaseManager"); $dbc->query($query, $this->_configuration->getProperty('database_index')); // Delete the Node for this Asset $this->_hierarchy->deleteNode($assetId); // Delete this asset from the createdAssets cache unset($this->_createdAssets[$assetId->getIdString()]); // unset our asset types as the list may have now changed. unset($this->_assetTypes); }