<?php

function FullInstall($Manager): void
{
   $Manager->Execute('CREATE TABLE IF NOT EXISTS `SystemVersion` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Revision` int(11) NOT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
  $Manager->Execute('INSERT INTO `SystemVersion` (`Id`, `Revision`) VALUES (NULL, 65);');
  $Manager->Execute('CREATE TABLE IF NOT EXISTS `measure` (
  `Id` int(11) NOT NULL auto_increment,
  `Name` varchar(255) collate utf8_general_ci NOT NULL,
  `Description` varchar(255) collate utf8_general_ci NOT NULL,
  `Divider` int(11) NOT NULL default 1,
  `Unit` varchar(16) collate utf8_general_ci NOT NULL,
  `Continuity` tinyint(1) NOT NULL default 0,
  `Period` int(11) NOT NULL default 60,
  `OldName` varchar(32) collate utf8_general_ci NOT NULL,
  `PermissionView` varchar(255) collate utf8_general_ci NOT NULL default "all",
  `PermissionAdd` varchar(255) collate utf8_general_ci NOT NULL default "localhost.localdomain",
  `Info` varchar(255) collate utf8_general_ci NOT NULL,
  `Enabled` int(11) NOT NULL default 1,
  `Cumulative` int(11) NOT NULL default 0,
  `DataTable` varchar(32) collate utf8_general_ci NOT NULL default "data",
  `DataType` varchar(32) collate utf8_general_ci NOT NULL,
  PRIMARY KEY  (`Id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;');
}

function UpdateTo67($Manager): void
{
  $Manager->Execute('RENAME TABLE `measure` TO `Measure`;');
/*
   $Manager->Execute('CREATE TABLE IF NOT EXISTS `Permission` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Measure` int(11) NOT NULL,
  `Address` varchar(255) NOT NULL,
  `Operation` varchar(255) NOT NULL,
  PRIMARY KEY (`Id`),
  KEY `Measure` (`Measure`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
  $Manager->Execute('ALTER TABLE `Permission` ADD FOREIGN KEY (`Measure`) '.
  'REFERENCES `Measure`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;');
  $DbResult = $Manager->Execute('SELECT `Id`, `PermissionAdd` FROM `Measure`');
  while ($DbRow = $DbResult->fetch_assoc())
  {
    $Manager->Execute('INSERT INTO `Permission` (`Measure`, `Operation`) VALUES ('.
	    $DbRow['Id'].', "add");');
	}
  $DbResult = $Manager->Execute('SELECT `Id`, `PermissionView` FROM `Measure`');
  while ($DbRow = $DbResult->fetch_assoc())
  {
	  $Manager->Execute('INSERT INTO `Permission` (`Measure`, `Operation`) VALUES ('.
	    $DbRow['Id'].', "view");');
  }
  */
}

function UpdateTo79($Manager): void
{
  $DbResult = $Manager->Execute('SELECT `DataTable`,`DataType` FROM `Measure`;');
  while ($Measure = $DbResult->fetch_assoc())
  {
    $Table = $Measure['DataTable'];
    $Type = $Measure['DataType'];
    $Manager->Execute('DELETE FROM `'.$Table.'` WHERE CAST(`time` AS CHAR(20))="0000-00-00 00:00:00"');
    $Manager->Execute('ALTER TABLE `'.$Table.'` CHANGE `time` `Time` DATETIME NULL DEFAULT NULL');
    $Manager->Execute('ALTER TABLE `'.$Table.'` CHANGE `measure` `Measure` SMALLINT(11) NOT NULL DEFAULT "0"');
    $Manager->Execute('ALTER TABLE `'.$Table.'` CHANGE `min` `Min` '.$Type.' NOT NULL DEFAULT "0"');
    $Manager->Execute('ALTER TABLE `'.$Table.'` CHANGE `avg` `Avg` '.$Type.' NOT NULL DEFAULT "0"');
    $Manager->Execute('ALTER TABLE `'.$Table.'` CHANGE `max` `Max` '.$Type.' NOT NULL DEFAULT "0"');
    $Manager->Execute('ALTER TABLE `'.$Table.'` CHANGE `continuity` `Continuity` TINYINT(1) NOT NULL DEFAULT "0"');
    $Manager->Execute('ALTER TABLE `'.$Table.'` CHANGE `level` `Level` TINYINT(4) NOT NULL DEFAULT "0"');
  }
}

class Updates
{
  function Get(): array
  {
    return array(
      65 => array('Revision' => 67, 'Function' => 'UpdateTo67'),
      67 => array('Revision' => 79, 'Function' => 'UpdateTo79'),
    );
  }
}
