Пример #1
0
 public function testWithoutWhere()
 {
     $client = CM_Service_Manager::getInstance()->getDatabases()->getMaster();
     $query = new CM_Db_Query_UpdateSequence($client, 't`est', 's`ort', -1, null, 4, 9);
     $this->assertSame('UPDATE `t``est` SET `s``ort` = `s``ort` + ? WHERE `s``ort` BETWEEN ? AND ?', $query->getSqlTemplate());
     $this->assertEquals(array(-1, 4, 9), $query->getParameters());
 }
Пример #2
0
 /**
  * @param string     $table
  * @param string     $column
  * @param int        $position
  * @param array      $whereRow Associative array field=>value
  * @param array|null $where    Associative array field=>value
  * @throws CM_Exception_Invalid
  */
 public static function updateSequence($table, $column, $position, array $whereRow, array $where = null)
 {
     $table = (string) $table;
     $column = (string) $column;
     $position = (int) $position;
     if (null === $where) {
         $where = array();
     }
     if ($position <= 0 || $position > CM_Db_Db::count($table, $where)) {
         throw new CM_Exception_Invalid('Sequence out of bounds.');
     }
     $whereMerged = array_merge($whereRow, $where);
     $positionOld = CM_Db_Db::select($table, $column, $whereMerged)->fetchColumn();
     if (false === $positionOld) {
         throw new CM_Exception_Invalid('Could not retrieve original sequence number.');
     }
     $positionOld = (int) $positionOld;
     if ($position > $positionOld) {
         $upperBound = $position;
         $lowerBound = $positionOld;
         $direction = -1;
     } else {
         $upperBound = $positionOld;
         $lowerBound = $position;
         $direction = 1;
     }
     $client = self::getClient();
     $query = new CM_Db_Query_UpdateSequence($client, $table, $column, $direction, $where, $lowerBound, $upperBound);
     $query->execute();
     self::update($table, array($column => $position), $whereMerged);
 }