• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/tools/lock.php
  • Класс: BitrixImOpenLinesToolsLock
  • Вызов: Lock::set
public function set($name, $time = 60)
{
	$result = false;

	$row = $this->connection->query('SELECT ID, (LOCK_TIME >= NOW()) as BLOCK, PID
		FROM ' . LockTable::getTableName() . ' 
		WHERE ID='' . $this->sqlHelper->forSql($name, 255) . '' FOR UPDATE;
	')->fetch();

	if($row == false)
	{
		try
		{
			$this->connection->queryExecute('INSERT INTO ' . LockTable::getTableName() . ' 
				SET ID='' . $this->sqlHelper->forSql($name, 255) . '', 
				DATE_CREATE=NOW(), 
				LOCK_TIME=TIMESTAMPADD(SECOND, ' . $this->sqlHelper->forSql($time, 255) . ', NOW()), 
				PID='' . $this->sqlHelper->forSql($this->getUniqId(), 255) . '';
			');

			$result = true;
		}
		catch (Exception $e)
		{
			$result = false;
		}
	}
	elseif($row['BLOCK'] == 0 || $row['PID'] == $this->getUniqId())
	{
		$this->connection->queryExecute('UPDATE  ' . LockTable::getTableName() . ' 
				SET DATE_CREATE=NOW(), 
				LOCK_TIME=TIMESTAMPADD(SECOND, ' . $this->sqlHelper->forSql($time, 255) . ', NOW()), 
				PID='' . $this->sqlHelper->forSql($this->getUniqId(), 255) . ''
				WHERE
				ID='' . $this->sqlHelper->forSql($name, 255) . '';
			');

		$result = true;
	}

	$this->connection->queryExecute('COMMIT;');

	return $result;
}