Browse Source

New condition hash format used where its possible

tags/2.0.0-beta
Alexander Kochetov 12 years ago
parent
commit
a03d1164ad
  1. 93
      framework/rbac/DbManager.php

93
framework/rbac/DbManager.php

@ -117,7 +117,7 @@ class DbManager extends Manager
$query = new Query; $query = new Query;
$parents = $query->select(array('parent')) $parents = $query->select(array('parent'))
->from($this->itemChildTable) ->from($this->itemChildTable)
->where('child=:name', array(':name' => $itemName)) ->where(array('child' => $itemName))
->createCommand($this->db) ->createCommand($this->db)
->queryColumn(); ->queryColumn();
foreach ($parents as $parent) { foreach ($parents as $parent) {
@ -144,7 +144,7 @@ class DbManager extends Manager
} }
$query = new Query; $query = new Query;
$rows = $query->from($this->itemTable) $rows = $query->from($this->itemTable)
->where('name=:name1 OR name=:name2', array( ->where(array('or', 'name=:name1', 'name=:name2'), array(
':name1' => $itemName, ':name1' => $itemName,
':name2' => $childName ':name2' => $childName
)) ))
@ -183,9 +183,9 @@ class DbManager extends Manager
public function removeItemChild($itemName, $childName) public function removeItemChild($itemName, $childName)
{ {
return $this->db->createCommand() return $this->db->createCommand()
->delete($this->itemChildTable, 'parent=:parent AND child=:child', array( ->delete($this->itemChildTable, array(
':parent' => $itemName, 'parent' => $itemName,
':child' => $childName 'child' => $childName
)) > 0; )) > 0;
} }
@ -200,9 +200,10 @@ class DbManager extends Manager
$query = new Query; $query = new Query;
return $query->select(array('parent')) return $query->select(array('parent'))
->from($this->itemChildTable) ->from($this->itemChildTable)
->where('parent=:parent AND child=:child', array( ->where(array(
':parent' => $itemName, 'parent' => $itemName,
':child' => $childName)) 'child' => $childName
))
->createCommand($this->db) ->createCommand($this->db)
->queryScalar() !== false; ->queryScalar() !== false;
} }
@ -215,21 +216,14 @@ class DbManager extends Manager
*/ */
public function getItemChildren($names) public function getItemChildren($names)
{ {
if (is_string($names)) {
$condition = 'parent=' . $this->db->quoteValue($names);
} elseif (is_array($names) && !empty($names)) {
foreach ($names as &$name) {
$name = $this->db->quoteValue($name);
}
$condition = 'parent IN (' . implode(', ', $names) . ')';
}
$query = new Query; $query = new Query;
$rows = $query->select(array('name', 'type', 'description', 'bizrule', 'data')) $rows = $query->select(array('name', 'type', 'description', 'bizrule', 'data'))
->from(array( ->from(array(
$this->itemTable, $this->itemTable,
$this->itemChildTable $this->itemChildTable
)) ))
->where($condition . ' AND name=child') ->where(array('parent'=>$names))
->andWhere('name=child')
->createCommand($this->db) ->createCommand($this->db)
->queryAll(); ->queryAll();
$children = array(); $children = array();
@ -276,9 +270,9 @@ class DbManager extends Manager
public function revoke($userId, $itemName) public function revoke($userId, $itemName)
{ {
return $this->db->createCommand() return $this->db->createCommand()
->delete($this->assignmentTable, 'itemname=:itemname AND userid=:userid', array( ->delete($this->assignmentTable, array(
':userid' => $userId, 'userid' => $userId,
':itemname' => $itemName 'itemname' => $itemName
)) > 0; )) > 0;
} }
@ -293,9 +287,9 @@ class DbManager extends Manager
$query = new Query; $query = new Query;
return $query->select(array('itemname')) return $query->select(array('itemname'))
->from($this->assignmentTable) ->from($this->assignmentTable)
->where('itemname=:itemname AND userid=:userid', array( ->where(array(
':userid' => $userId, 'userid' => $userId,
':itemname' => $itemName 'itemname' => $itemName
)) ))
->createCommand($this->db) ->createCommand($this->db)
->queryScalar() !== false; ->queryScalar() !== false;
@ -312,9 +306,9 @@ class DbManager extends Manager
{ {
$query = new Query; $query = new Query;
$row = $query->from($this->assignmentTable) $row = $query->from($this->assignmentTable)
->where('itemname=:itemname AND userid=:userid', array( ->where(array(
':userid' => $userId, 'userid' => $userId,
':itemname' => $itemName 'itemname' => $itemName
)) ))
->createCommand($this->db) ->createCommand($this->db)
->queryRow(); ->queryRow();
@ -338,7 +332,7 @@ class DbManager extends Manager
{ {
$query = new Query; $query = new Query;
$rows = $query->from($this->assignmentTable) $rows = $query->from($this->assignmentTable)
->where('userid=:userid', array(':userid' => $userId)) ->where(array('userid' => $userId))
->createCommand($this->db) ->createCommand($this->db)
->queryAll(); ->queryAll();
$assignments = array(); $assignments = array();
@ -361,7 +355,7 @@ class DbManager extends Manager
->update($this->assignmentTable, array( ->update($this->assignmentTable, array(
'bizrule' => $assignment->getBizRule(), 'bizrule' => $assignment->getBizRule(),
'data' => serialize($assignment->getData()), 'data' => serialize($assignment->getData()),
), 'itemname=:itemname AND userid=:userid', array( ), array(
'userid' => $assignment->getUserId(), 'userid' => $assignment->getUserId(),
'itemname' => $assignment->getItemName() 'itemname' => $assignment->getItemName()
)); ));
@ -383,7 +377,7 @@ class DbManager extends Manager
->createCommand($this->db); ->createCommand($this->db);
} elseif ($userId === null) { } elseif ($userId === null) {
$command = $query->from($this->itemTable) $command = $query->from($this->itemTable)
->where('type=:type', array(':type' => $type)) ->where(array('type' => $type))
->createCommand($this->db); ->createCommand($this->db);
} elseif ($type === null) { } elseif ($type === null) {
$command = $query->select(array('name', 'type', 'description', 't1.bizrule', 't1.data')) $command = $query->select(array('name', 'type', 'description', 't1.bizrule', 't1.data'))
@ -391,7 +385,8 @@ class DbManager extends Manager
$this->itemTable . ' t1', $this->itemTable . ' t1',
$this->assignmentTable . ' t2' $this->assignmentTable . ' t2'
)) ))
->where('name=itemname AND userid=:userid', array(':userid' => $userId)) ->where(array('userid' => $userId))
->andWhere('name=itemname')
->createCommand($this->db); ->createCommand($this->db);
} else { } else {
$command = $query->select('name', 'type', 'description', 't1.bizrule', 't1.data') $command = $query->select('name', 'type', 'description', 't1.bizrule', 't1.data')
@ -399,10 +394,11 @@ class DbManager extends Manager
$this->itemTable . ' t1', $this->itemTable . ' t1',
$this->assignmentTable . ' t2' $this->assignmentTable . ' t2'
)) ))
->where('name=itemname AND type=:type AND userid=:userid', array( ->where(array(
':userid' => $userId, 'userid' => $userId,
':type' => $type 'type' => $type
)) ))
->andWhere('name=itemname')
->createCommand($this->db); ->createCommand($this->db);
} }
$items = array(); $items = array();
@ -452,20 +448,13 @@ class DbManager extends Manager
{ {
if ($this->usingSqlite()) { if ($this->usingSqlite()) {
$this->db->createCommand() $this->db->createCommand()
->delete($this->itemChildTable, 'parent=:name1 OR child=:name2', array( ->delete($this->itemChildTable, array('or', 'parent=:name1', 'child=:name2'), array(
':name1' => $name, ':name1' => $name,
':name2' => $name ':name2' => $name
)); ));
$this->db->createCommand() $this->db->createCommand()->delete($this->assignmentTable, array('itemname' => $name));
->delete($this->assignmentTable, 'itemname=:name', array(
':name' => $name,
));
} }
return $this->db->createCommand()->delete($this->itemTable, array('name' => $name)) > 0;
return $this->db->createCommand()
->delete($this->itemTable, 'name=:name', array(
':name' => $name
)) > 0;
} }
/** /**
@ -477,7 +466,7 @@ class DbManager extends Manager
{ {
$query = new Query; $query = new Query;
$row = $query->from($this->itemTable) $row = $query->from($this->itemTable)
->where('name=:name', array(':name' => $name)) ->where(array('name' => $name))
->createCommand($this->db) ->createCommand($this->db)
->queryRow(); ->queryRow();
@ -501,20 +490,20 @@ class DbManager extends Manager
$this->db->createCommand() $this->db->createCommand()
->update($this->itemChildTable, array( ->update($this->itemChildTable, array(
'parent' => $item->getName(), 'parent' => $item->getName(),
), 'parent=:whereName', array( ), array(
':whereName' => $oldName, 'parent' => $oldName,
)); ));
$this->db->createCommand() $this->db->createCommand()
->update($this->itemChildTable, array( ->update($this->itemChildTable, array(
'child' => $item->getName(), 'child' => $item->getName(),
), 'child=:whereName', array( ), array(
':whereName' => $oldName, 'child' => $oldName,
)); ));
$this->db->createCommand() $this->db->createCommand()
->update($this->assignmentTable, array( ->update($this->assignmentTable, array(
'itemname' => $item->getName(), 'itemname' => $item->getName(),
), 'itemname=:whereName', array( ), array(
':whereName' => $oldName, 'itemname' => $oldName,
)); ));
} }
@ -525,8 +514,8 @@ class DbManager extends Manager
'description' => $item->getDescription(), 'description' => $item->getDescription(),
'bizrule' => $item->getBizRule(), 'bizrule' => $item->getBizRule(),
'data' => serialize($item->getData()), 'data' => serialize($item->getData()),
), 'name=:whereName', array( ), array(
':whereName' => $oldName === null ? $item->getName() : $oldName, 'name' => $oldName === null ? $item->getName() : $oldName,
)); ));
} }

Loading…
Cancel
Save