Beispiel #1
0
 protected function makeResrictionForAsscotion(array &$arrDataRow, $sFromTableName = null, array $arrFromKeys, $sToTableName = null, array $arrToKeys)
 {
     $aRestriction = new Restriction();
     foreach ($arrFromKeys as $nIdx => $sFromKey) {
         if ($sFromTableName) {
             $sFromKey = $sFromTableName . '.' . $sFromKey;
         }
         $aRestriction->expression(array(SQL::createRawColumn($sToTableName, $arrToKeys[$nIdx]), '=', SQL::transValue($arrDataRow[$sFromKey])), true, true);
     }
     return $aRestriction;
 }
Beispiel #2
0
 public function setData($sColumnName, $value = null, $bValueExpr = false)
 {
     list($sTableName, $sColumnName) = SQL::splitColumn($sColumnName);
     $arrRawSet =& $this->rawClause(SQL::CLAUSE_SET);
     if (!isset($arrRawSet['subtree'][$sColumnName]) and !empty($arrRawSet['subtree'])) {
         $arrRawSet['subtree'][] = ',';
     }
     $arrRawSet['subtree'][$sColumnName] = array('expr_type' => 'assignment', 'pretree' => array(SQL::createRawColumn($sTableName, $sColumnName), '='));
     if ($bValueExpr) {
         $arrRawSet['subtree'][$sColumnName]['subtree'] =& SQL::parseSql($value, 'values', true);
     } else {
         $arrRawSet['subtree'][$sColumnName]['subtree'] = array(SQL::transValue($value));
     }
 }
Beispiel #3
0
 public function execute(DB $aDB, Model $aModel)
 {
     $aPrototype = $aModel->prototype();
     $aDelete = new Delete($aPrototype->tableName());
     // 设置limit
     $aDelete->criteria()->setLimit(1);
     // delete 当前model
     foreach ($aPrototype->keys() as $sClmName) {
         if ($aModel->changed($sClmName)) {
             //主键发生修改
             throw new ORMException('org\\jecat\\framework\\mvc\\model\\db\\orm\\Updater : Key 有修改,无法进行Delete操作');
         } else {
             //用主键作为查询条件
             $aDelete->criteria()->where()->expression(array(SQL::createRawColumn(null, $sClmName), '=', SQL::transValue($aModel->data($sClmName))), true, true);
         }
     }
     $aDB->execute($aDelete);
     // 仅delete hasAndBelongsTo的桥表
     foreach ($aPrototype->associations() as $aAssociation) {
         $aAssociatedModel = $aModel->child($aAssociation->name());
         if (!$aAssociatedModel) {
             continue;
         }
         switch ($aAssociation->type()) {
             case Association::hasOne:
                 $this->setAssociatedModelData($aModel, $aAssociatedModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssociatedModel->delete()) {
                     return false;
                 }
                 break;
             case Association::belongsTo:
                 // nothing todo ...
                 break;
             case Association::hasMany:
                 $this->setAssociatedModelData($aModel, $aAssociatedModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssociatedModel->delete()) {
                     return false;
                 }
                 break;
             case Association::hasAndBelongsToMany:
                 // delete bridge table
                 foreach ($aAssociatedModel->childIterator() as $aOneChildModel) {
                     $this->deleteBridge($aDB, $aAssociation, $aModel, $aOneChildModel);
                 }
                 break;
         }
     }
     return true;
 }
Beispiel #4
0
 public function setData($sColumn, $value = null, $bValueExpr = false, $nRow = 0)
 {
     $arrRawValues =& $this->rawClauseValue();
     $sValueRowKey = 'ROW' . $nRow;
     // 更改已经存在的数据
     if (isset($arrRawValues['pretree']['COLUMNS']['subtree'][$sColumn])) {
         $arrRawValues['subtree'][$sValueRowKey]['subtree'][$sColumn]['subtree'] = $bValueExpr ? array($value) : array(SQL::transValue($value));
     } else {
         if (!empty($arrRawValues['pretree']['COLUMNS']['subtree'])) {
             $arrRawValues['pretree']['COLUMNS']['subtree'][] = ',';
         }
         $arrRawValues['pretree']['COLUMNS']['subtree'][$sColumn] = self::createRawColumn(null, $sColumn);
         // 插入行
         if (!isset($arrRawValues['subtree'][$sValueRowKey])) {
             // 删除开始的 (  和 删除结尾的 )
             array_shift($arrRawValues['subtree']);
             array_pop($arrRawValues['subtree']);
             if (!empty($arrRawValues['subtree'])) {
                 $arrRawValues['subtree'][] = ',';
             }
             $arrRawValues['subtree'][$sValueRowKey] = array('expr_type' => 'values_row', 'subtree' => array());
             // 套上 ( 和 )
             array_unshift($arrRawValues['subtree'], '(');
             array_push($arrRawValues['subtree'], ')');
         }
         // 写入数据
         if (!empty($arrRawValues['subtree'][$sValueRowKey]['subtree'])) {
             $arrRawValues['subtree'][$sValueRowKey]['subtree'][] = ',';
         }
         if ($bValueExpr) {
             // todo ...
         } else {
             $arrRawValues['subtree'][$sValueRowKey]['subtree'][$sColumn] = SQL::transValue($value);
         }
     }
     return $this;
 }
Beispiel #5
0
 public function hasExists(Model $aModel, Prototype $aPrototype = null, Select $aSelect = null, DB $aDB = null)
 {
     if (!$aPrototype) {
         $aPrototype = $aModel->prototype();
     }
     if (!$aSelect) {
         $aSelect = $aPrototype->sharedStatementSelect();
     }
     if (!$aDB) {
         $aDB = DB::singleton();
     }
     // 备份原来的 limit
     $arrRawOriLimit =& $aSelect->rawClause(SQL::CLAUSE_LIMIT, false);
     $aSelect->criteria()->setLimit(1, 0);
     $aRestriction = $aSelect->where()->createRestriction();
     foreach ($aPrototype->keys() as $nIdx => $sKey) {
         $aRestriction->expression(array(SQL::createRawColumn($aPrototype->sqlTableAlias(), $sKey), '=', SQL::transValue($aModel->data($sKey))), true, true);
     }
     // 查询
     $bExists = $aDB->queryCount($aSelect) > 0;
     // 移除条件
     $aSelect->where()->remove($aRestriction);
     // 还原原来的 limit
     $aSelect->setRawClause(SQL::CLAUSE_LIMIT, $arrRawOriLimit);
     return $bExists;
 }
Beispiel #6
0
 public function execute(DB $aDB, Model $aModel)
 {
     $aPrototype = $aModel->prototype();
     $aUpdate = $aPrototype->sharedStatementUpdate();
     // 从 belongs to model 中设置外键值
     foreach ($aPrototype->associations() as $aAssociation) {
         if ($aAssociation->isType(Association::belongsTo)) {
             if (!($aAssociatedModel = $aModel->child($aAssociation->name()))) {
                 continue;
             }
             if (!$aAssociatedModel->save()) {
                 return false;
             }
             $this->setAssociatedModelData($aAssociatedModel, $aModel, $aAssociation->toKeys(), $aAssociation->fromKeys());
         }
     }
     // 产生一个criteria并设置给$aUpdate
     //$aCriteria = StatementFactory::singleton()->createCriteria() ;
     //$aUpdate->setCriteria($aCriteria);
     // 处理主键
     $aUpdate->where()->clear();
     foreach ($aPrototype->keys() as $sKey) {
         //主键发生修改
         if ($aModel->changed($sKey)) {
             throw new ORMException('org\\jecat\\framework\\mvc\\model\\db\\orm\\Updater : Key 有修改,无法进行Update操作');
         } else {
             $aUpdate->where()->expression(array(SQL::createRawColumn(null, $sKey), '=', SQL::transValue($aModel->data($sKey))), true, true);
         }
     }
     $aUpdate->clearData();
     $bFlagChanged = false;
     //当前表是否有修改
     foreach ($aPrototype->columns() as $sClmName) {
         //只update发生修改的项
         if ($aModel->changed($sClmName)) {
             $aUpdate->setData($sClmName, $aModel->data($sClmName), false);
             $bFlagChanged = true;
         }
     }
     //只有当有修改的时候才发生更新
     if ($bFlagChanged) {
         $aDB->execute($aUpdate);
     }
     // update关联model
     foreach ($aPrototype->associations() as $aAssociation) {
         $aAssociatedModel = $aModel->child($aAssociation->name());
         if (!$aAssociatedModel) {
             continue;
         }
         switch ($aAssociation->type()) {
             case Association::hasOne:
                 $this->setAssociatedModelData($aModel, $aAssociatedModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssociatedModel->save()) {
                     return false;
                 }
                 break;
             case Association::belongsTo:
                 // nothing todo ...
                 break;
             case Association::hasMany:
                 $this->setAssociatedModelData($aModel, $aAssociatedModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssociatedModel->save()) {
                     return false;
                 }
                 break;
             case Association::hasAndBelongsToMany:
                 if (!$aAssociatedModel->save()) {
                     return false;
                 }
                 // update bridge table
                 foreach ($aAssociatedModel->childIterator() as $aOneChildModel) {
                     $this->buildBridge($aDB, $aAssociation, $aModel, $aOneChildModel);
                 }
                 break;
         }
     }
     return true;
 }
Beispiel #7
0
 public static function buildRestriction(Prototype $aPrototype, $values = null, $keys = null)
 {
     if ($values === null) {
         return null;
     }
     $keys = $keys ? (array) $keys : $aPrototype->keys();
     if ($values instanceof Restriction) {
         return $values;
     } else {
         $aRestriction = new Restriction();
         $sSqlTableAlias = $aPrototype->sqlTableAlias();
         $values = array_values((array) $values);
         foreach ($keys as $nIdx => $sKey) {
             list($sTable, $sColumn) = SQL::splitColumn($sKey);
             $aRestriction->expression(array(SQL::createRawColumn($sTable, $sColumn), '=', SQL::transValue($values[$nIdx])), true, true);
         }
         return $aRestriction;
     }
 }