コード例 #1
0
ファイル: DeleteExecutor.php プロジェクト: addiks/phpsql
 public function executeJob(StatementJob $statement, array $parameters = array())
 {
     /* @var $statement DeleteStatement */
     $result = new TemporaryResult();
     $executionContext = new ExecutionContext($this->schemaManager, $statement, $parameters);
     /* @var $conditionValue Value */
     $conditionValue = $statement->getCondition();
     $rowCount = 0;
     $rowSkip = 0;
     $limitOffset = $statement->getLimitOffset();
     $limitCount = $statement->getLimitRowCount();
     foreach ($statement->getDeleteTables() as $tableSpecifier) {
         /* @var $tableSpecifier TableSpecifier */
         /* @var $tableResource Table */
         $tableResource = $this->tableManager->getTable($tableSpecifier->getTable(), $tableSpecifier->getDatabase());
         $sortedIterator = new SortedResourceIterator($tableResource, $this->valueResolver);
         /* @var $tableSchema TableSchema */
         $tableSchema = $tableResource->getTableSchema();
         if (!is_null($statement->getOrderColumn())) {
             $orderColumns = $statement->getOrderColumn();
             $sortedIterator->setTemporaryBuildChildIteratorByValue($orderColumns, $tableResource);
         } else {
             $primaryKeyColumns = $tableSchema->getPrimaryKeyColumns();
             $primaryIndexId = $tableSchema->getIndexIdByColumns(array_keys($primaryKeyColumns));
             if (!is_null($primaryIndexId)) {
                 /* @var $index Index */
                 $index = $tableResource->getIndex($primaryIndexId);
                 // TODO: try to extract begin/end values from conditions
                 $beginValue = null;
                 $endValue = null;
                 $iterator = $index->getIterator($beginValue, $endValue);
                 $sortedIterator->setChildIterator($iterator);
             } else {
                 /* @var $sortIndex Quicksort */
                 $sortIndex = $sortedIterator->getSortIndexByColumns($primaryKeyColumns);
                 foreach ($tableResource as $rowId => $row) {
                     $sortIndex->addRow($rowId, $row);
                 }
                 $sortIndex->sort();
                 $sortedIterator->setChildIterator($sortIndex);
             }
         }
         foreach ($sortedIterator as $rowId => $row) {
             $executionContext->setCurrentSourceRow($row);
             $isConditionMatch = true;
             if (!is_null($conditionValue)) {
                 $isConditionMatch = $this->valueResolver->resolveValue($conditionValue, $executionContext);
             }
             if ($isConditionMatch) {
                 if (!is_null($limitOffset) && $rowSkip < $limitOffset) {
                     $rowSkip++;
                     continue;
                 }
                 if (!is_null($limitCount) && $rowCount > $limitCount) {
                     continue;
                 }
                 $rowCount++;
                 foreach ($tableResource->getTableSchema()->getIndexIterator() as $indexId => $indexPage) {
                     /* @var $indexPage Index */
                     /* @var $indexResource Index */
                     $indexResource = $tableResource->getIndex($indexId);
                     $originalRow = $tableResource->getRowData($rowId);
                     $rowIdStr = $this->decstr($rowId);
                     $indexResource->removeRow($originalRow, $rowIdStr);
                 }
                 $tableResource->removeRow($rowId);
             }
         }
     }
     return $result;
 }