-- --------------------------------------------------------
-- Host:                         localhost
-- Server Version:               10.1.19-MariaDB - mariadb.org binary distribution
-- Server Betriebssystem:        Win32
-- HeidiSQL Version:             9.4.0.5125
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;


-- Exportiere Datenbank Struktur für taschenrechner
CREATE DATABASE IF NOT EXISTS `taschenrechner` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */;
USE `taschenrechner`;

-- Exportiere Struktur von Tabelle taschenrechner.action
CREATE TABLE IF NOT EXISTS `action` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `session` varchar(40) COLLATE utf8_german2_ci NOT NULL,
  `type` enum('action','number') COLLATE utf8_german2_ci NOT NULL,
  `action` varchar(50) COLLATE utf8_german2_ci NOT NULL,
  `scope` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2205 DEFAULT CHARSET=utf8 COLLATE=utf8_german2_ci;

-- Exportiere Daten aus Tabelle taschenrechner.action: ~13 rows (ungefähr)
DELETE FROM `action`;
/*!40000 ALTER TABLE `action` DISABLE KEYS */;
INSERT INTO `action` (`id`, `session`, `type`, `action`, `scope`) VALUES
	(1092, '8eo0epact0qqn63ekilsin10n4', 'number', '-850', 0),
	(1139, 'f156inikbect8111hujg2smgm2', 'number', '32', 0),
	(1525, 'fru7r2tc60q569kqqoguj3ieh3', 'number', '12', 0),
	(1567, '1666', 'number', '', 0),
	(1621, '532ibaq8jj4nt4gdrshuseh346', 'number', '2', 0),
	(1622, '532ibaq8jj4nt4gdrshuseh346', 'action', '*', 0),
	(1623, '532ibaq8jj4nt4gdrshuseh346', 'number', '2', 0),
	(1624, '252', 'number', '22', 0),
	(1809, 'e0252lio998dq35cc6mbsebkl0', 'number', '1.6817928305074292', 0),
	(2078, 'rjacct0vgb24d32vr696o8fsa3', 'number', '3', 0),
	(2079, 'rjacct0vgb24d32vr696o8fsa3', 'action', '+', 0),
	(2080, 'rjacct0vgb24d32vr696o8fsa3', 'number', '3', 0),
	(2203, 'rnr71tkdbr4fknji6vjl27j6c6', 'number', '1.7976931348623157e308', 0);
/*!40000 ALTER TABLE `action` ENABLE KEYS */;

-- Exportiere Struktur von Funktion taschenrechner.actionCheckMin
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `actionCheckMin`() RETURNS bit(1)
BEGIN
	IF (actionGetLastType() IS NULL) THEN 
		RETURN 0; 													-- Kein 		Eintrag 
	ELSE 
		RETURN 1; 													-- Min.  1  Eintrag
	END IF;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoBack
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoBack`()
BEGIN
	CASE
		WHEN (@lType="action") THEN CALL actionDoClear();
		WHEN (@lType="number") THEN UPDATE `action` SET `action`=(SUBSTR(@lAction, 1, (CHAR_LENGTH(@lAction)-1))) WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoClear
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoClear`()
BEGIN  
	DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
	CASE						
		WHEN (actionGetLastAction()='sin') 		THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='cos') 		THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='tan') 		THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='sin-1') 	THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='cos-1') 	THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='tan-1') 	THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='√') 		THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='²√') 		THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='³√') 		THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
		WHEN (actionGetLastAction()='^') 		THEN DELETE FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
	ELSE
		BEGIN
		END;
	END CASE;	
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoClearAll
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoClearAll`()
BEGIN
	DELETE FROM `action` WHERE `session`=@pSession;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoGeteilt
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoGeteilt`()
BEGIN
	CASE
		WHEN (@lAction='-') 		THEN BEGIN END;
		WHEN (@lType='number') 	THEN CALL actionInsert( 'action', '/');
		WHEN (@lAction=')') 		THEN CALL actionInsert( 'action', '/');
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoHoch
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoHoch`()
BEGIN
	CASE
		WHEN (@lAction='-') 		THEN BEGIN END;
		WHEN (@lType='number') 	THEN CALL actionInsert('action', '^'); CALL actionInsert('action', '(');
		WHEN (@lAction=')') 		THEN CALL actionInsert('action', '^'); CALL actionInsert('action', '(');
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoKlammerAuf
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoKlammerAuf`()
BEGIN
	CASE
		WHEN (@lAction='-') 		THEN CALL actionDoNumberAdd('1'); 		CALL actionInsert('action', '*'); CALL actionInsert('action', '(');
		WHEN (@lAction=')') 		THEN CALL actionInsert('action', '*'); CALL actionInsert('action', '(');
		WHEN (@lType='action') 	THEN CALL actionInsert('action', '(');
		WHEN (@lType='number') 	THEN CALL actionInsert('action', '*'); CALL actionInsert('action', '(');		
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoKlammerZu
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoKlammerZu`()
BEGIN
	IF (actionGetNextScope()>0) THEN 
		CASE
			WHEN (@lAction='-') 		THEN BEGIN END;
			WHEN (@lType='number') 	THEN CALL actionInsert('action', ')');
			WHEN (@lAction=')') 		THEN CALL actionInsert('action', ')');
			-- WHEN (@lAction='(') 		THEN CALL actionInsert('action', ')');
		ELSE
			BEGIN
			END;
		END CASE;
	END IF;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoKomma
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoKomma`()
BEGIN
	CASE
		WHEN ((@lsAction LIKE 'sin-1') AND @lAction='1') 	THEN BEGIN END;
		WHEN (@lAction='(')  		THEN CALL actionInsert('number', '0'); 	CALL actionDoNumberAdd('.');
		WHEN (@lAction=')')  		THEN BEGIN END;
		WHEN (@lType='action') 		THEN CALL actionInsert('number', '0'); 	CALL actionDoNumberAdd('.');
		WHEN (@lAction='-')  		THEN CALL actionDoNumberAdd('0'); 			CALL actionDoNumberAdd('.');
		WHEN (@lAction LIKE'%.%')  THEN BEGIN END;
		WHEN (@lType='number') 		THEN CALL actionDoNumberAdd('.');			
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoMal
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoMal`()
BEGIN
	CASE
		WHEN (@lAction='-') 		THEN BEGIN END;
		WHEN (@lType='number') 	THEN CALL actionInsert('action', '*');
		WHEN (@lAction=')') 		THEN CALL actionInsert('action', '*');
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoMinus
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoMinus`()
BEGIN  
	CASE					
	--	WHEN (@lsAction LIKE '%√')		THEN BEGIN END;
		WHEN (@lAction=')') 				THEN CALL actionInsert('action', '-');
		WHEN (@lAction='(') 				THEN CALL actionInsert('number', '-');
		WHEN (@lType='action') 			THEN CALL actionInsert('action', '('); CALL actionInsert('number', '-');
		WHEN (@lAction='-') 				THEN BEGIN END;
		WHEN (@lType='number') 			THEN CALL actionInsert('action', '-');
	ELSE
		BEGIN
		END;
	END CASE;	
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoNumber
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoNumber`()
BEGIN  
	CASE						
		WHEN (@lAction=')') 		THEN BEGIN END;	
		WHEN (@lType='action') 	THEN CALL actionInsert('number',@pAction);	-- Neu
		WHEN (@lType='number') 	THEN CALL actionDoNumberAdd(@pAction);		-- Anhängen
	ELSE
		BEGIN
		END;
	END CASE;	
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoNumberAdd
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoNumberAdd`(
	IN `pAction` VARCHAR(6)



)
BEGIN
	UPDATE `action` SET `action`=CONCAT(actionGetLastAction(),pAction) WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoPlus
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoPlus`()
BEGIN
	CASE
		WHEN (@lAction='-') 		THEN BEGIN END;
		WHEN (@lType='number') 	THEN CALL actionInsert('action', '+');
		WHEN (@lAction=')') 		THEN CALL actionInsert('action', '+');
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoWinkel
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoWinkel`()
BEGIN
	CASE
		WHEN (@lAction=')') 			THEN CALL actionInsert('action', '*'); CALL actionInsert('action', @pAction); CALL actionInsert('action', '(');
		WHEN (@lType='number') 		THEN CALL actionInsert('action', '*'); CALL actionInsert('action', @pAction); CALL actionInsert('action', '(');
		WHEN (@lType='action') 		THEN CALL actionInsert('action', @pAction); CALL actionInsert('action', '(');
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoWinkelDeg
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoWinkelDeg`()
BEGIN  
	CASE						
		WHEN (sessionGetWinkelMode()='rad') THEN CALL sessionSetWinkelMode();
	ELSE
		BEGIN
		END;
	END CASE;	
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoWinkelRad
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoWinkelRad`()
BEGIN  
	CASE						
		WHEN (sessionGetWinkelMode()='deg') THEN CALL sessionSetWinkelMode();
	ELSE
		BEGIN
		END;
	END CASE;	
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionDoWurzel
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionDoWurzel`()
BEGIN
	CASE
		WHEN (@lAction='2') 			THEN CALL actionDoClear(); 				CALL actionInsertZero('action', '²√'); CALL actionInsert('action', '(');
		WHEN (@lAction='3') 			THEN CALL actionDoClear(); 				CALL actionInsertZero('action', '³√'); CALL actionInsert('action', '(');
		WHEN (@lType='number') 		THEN CALL actionInsert('action', '*'); CALL actionInsert('action', '√'); 		CALL actionInsert('action', '(');
		WHEN (@lAction=')') 			THEN CALL actionInsert('action', '*'); CALL actionInsert('action', '√'); 		CALL actionInsert('action', '(');
		WHEN (@lType='action') 		THEN CALL actionInsert('action', '√'); CALL actionInsert('action', '(');	
	ELSE
		BEGIN
		END;
	END CASE;
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.actionGetLastAction
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `actionGetLastAction`() RETURNS varchar(50) CHARSET utf8 COLLATE utf8_german2_ci
BEGIN
	RETURN (SELECT `action` FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1);
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.actionGetLastActionSpecial
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `actionGetLastActionSpecial`() RETURNS varchar(6) CHARSET utf8 COLLATE utf8_bin
BEGIN
	RETURN (SELECT `action` FROM `action` WHERE `session`=@pSession AND `action`!='(' AND `id`<(SELECT `id` FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1) ORDER BY `id` DESC LIMIT 1);
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.actionGetLastScope
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `actionGetLastScope`() RETURNS tinyint(4)
BEGIN
	RETURN (SELECT `scope` FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1);
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.actionGetLastType
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `actionGetLastType`() RETURNS varchar(6) CHARSET utf8 COLLATE utf8_german2_ci
BEGIN
	RETURN (SELECT `type` FROM `action` WHERE `session`=@pSession ORDER BY `id` DESC LIMIT 1);
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.actionGetNextScope
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `actionGetNextScope`() RETURNS tinyint(4)
BEGIN
	IF actionGetLastAction()=")" THEN		-- Ist Action = ")"						
		RETURN (actionGetLastScope()-1);		-- LastScope -1
	ELSE 
		RETURN actionGetLastScope();			-- LastScope
	END IF;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionHandler
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionHandler`()
BEGIN
	SET @lAction 	= actionGetLastAction();
	SET @lsAction 	= actionGetLastActionSpecial();
	SET @lType	 	= actionGetLastType();
	IF actionCheckMin()=0 THEN -- Wenn 0 Einträge, dann...
		BEGIN
			CASE @pAction
				WHEN 'deg' 		THEN CALL actionDoWinkelDeg();
				WHEN 'rad' 		THEN CALL actionDoWinkelRad();
				WHEN '-' 		THEN CALL actionInsertZero('number', '-');
				WHEN '(' 		THEN CALL actionInsertOne('action', '(');
				WHEN '√' 		THEN CALL actionInsertZero('action', '√');
								  		  CALL actionInsertOne('action', '(');
				WHEN ',' 		THEN CALL actionInsertZero('number', '0');
								  		  CALL actionDoNumberAdd('.');	
				WHEN 'sin-1' 	THEN CALL actionInsertZero('action', 'sin-1');
								  		  CALL actionInsertOne('action', '(');
				WHEN 'cos-1' 	THEN CALL actionInsertZero('action', 'cos-1');
								  		  CALL actionInsertOne('action', '(');
				WHEN 'tan-1' 	THEN CALL actionInsertZero('action', 'tan-1');
								  		  CALL actionInsertOne('action', '(');
				WHEN 'sin' 		THEN CALL actionInsertZero('action', 'sin');
								  		  CALL actionInsertOne('action', '(');
				WHEN 'cos' 		THEN CALL actionInsertZero('action', 'cos');
								  		  CALL actionInsertOne('action', '(');
				WHEN 'tan' 		THEN CALL actionInsertZero('action', 'tan');
								  		  CALL actionInsertOne('action', '(');
	
			ELSE 
				IF @pAction BETWEEN '0' AND '9' THEN
					CALL actionInsertZero('number',@pAction);
				END IF;			
			END CASE;
		END;
	ELSE
		BEGIN	
			CASE @pAction
				WHEN 'deg' 		THEN CALL actionDoWinkelDeg();
				WHEN 'rad' 		THEN CALL actionDoWinkelRad();
				WHEN '<'			THEN CALL actionDoBack();
				WHEN 'C'  		THEN CALL actionDoClear();
				WHEN 'AC' 		THEN CALL actionDoClearAll();
				WHEN '+' 		THEN CALL actionDoPlus();
				WHEN '-' 		THEN CALL actionDoMinus();
				WHEN '*' 		THEN CALL actionDoMal();
				WHEN '^' 		THEN CALL actionDoHoch();
				WHEN '√' 		THEN CALL actionDoWurzel();
				WHEN '/' 		THEN CALL actionDoGeteilt();
				WHEN '(' 		THEN CALL actionDoKlammerAuf();
				WHEN ')' 		THEN CALL actionDoKlammerZu();
				WHEN ',' 		THEN CALL actionDoKomma();				
				WHEN 'sin-1'	THEN CALL actionDoWinkel();
				WHEN 'cos-1' 	THEN CALL actionDoWinkel();
				WHEN 'tan-1' 	THEN CALL actionDoWinkel();
				WHEN 'sin' 		THEN CALL actionDoWinkel();
				WHEN 'cos' 		THEN CALL actionDoWinkel();
				WHEN 'tan' 		THEN CALL actionDoWinkel();
				WHEN '=' 		THEN CALL solve();	
			ELSE 
				IF @pAction BETWEEN '0' AND '9' THEN
					CALL actionDoNumber();
				END IF;			
			END CASE;
		END;
	END IF;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionInsert
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionInsert`(
	IN `pType` ENUM('action','number')
,
	IN `pAction` VARCHAR(6)
)
BEGIN
	DECLARE scope TINYINT DEFAULT actionGetLastScope();
	CASE
		WHEN (pAction='(') THEN SET scope=scope+1;
		WHEN (actionGetLastAction()=')') THEN SET scope=scope-1; -- HOLA UND SO
	ELSE 
		BEGIN 
		END;
	END CASE;
	INSERT INTO `action` (`session`,`type`,`action`,`scope`) VALUES (@pSession,pType,pAction,scope);
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionInsertOne
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionInsertOne`(
	IN `pType` ENUM('action','number')
,
	IN `pAction` VARCHAR(6)



)
BEGIN
	INSERT INTO `action` (`session`,`type`,`action`,`scope`) VALUES (@pSession,pType,pAction,1);
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.actionInsertZero
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `actionInsertZero`(
	IN `pType` ENUM('action','number')
,
	IN `pAction` VARCHAR(6)


)
BEGIN
	INSERT INTO `action` (`session`,`type`,`action`,`scope`) VALUES (@pSession,pType,pAction,0);
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.calcBuilder
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `calcBuilder`() RETURNS longtext CHARSET utf32
BEGIN
	-- 1. Variablen
	DECLARE bis int DEFAULT 0;
	DECLARE history text DEFAULT "";
	DECLARE currentNumber varchar(50) DEFAULT "";
	DECLARE winkelButton varchar(100);
	DECLARE cssNoAction varchar(100) DEFAULT "";
	DECLARE cssSolve varchar(100) DEFAULT "";
	-- 2. Verlauf holen und derzeitige Zahl subtrahieren
	IF actionCheckMin()=1	THEN
		IF (@history = "" OR @backroll=TRUE) THEN
			SET history = REPLACE((SELECT GROUP_CONCAT(`action` SEPARATOR '') FROM `action` WHERE `session`=@pSession ORDER BY `id` ASC),'.',',');
			SET bis = CHARACTER_LENGTH(history);
			IF actionGetLastType()='number' THEN 
				SET currentNumber = REPLACE(actionGetLastAction(),'.',',');
				SET bis = bis-CHARACTER_LENGTH(currentNumber);
			END IF;
			SET history = SUBSTRING(history,1,bis);
		ELSE
			SET currentNumber = REPLACE(actionGetLastAction(),'.',',');
			SET history = @history;
		END IF;
		
	END IF;
	
	-- Winkel
	IF (sessionGetWinkelMode()='deg') THEN
		SET winkelButton = "<button class='winkel' type='submit' name='action' value='rad'>RAD</button>";
	ELSE
		SET winkelButton = "<button class='winkel' type='submit' name='action' value='deg'>DEG</button>";
	END IF;
	
	IF (@lastAction="action") THEN
		SET cssNoAction = "<link rel='stylesheet' type='text/css' href='./css/noAction.css' />";
	END IF;
	
	IF (@pAction="=" AND (actionGetLastType()='number' OR  actionGetLastAction()=')')) THEN
		IF (@backroll=FALSE) THEN
			SET cssSolve = "<link rel='stylesheet' type='text/css' href='./css/solve.css' />";
		ELSE 
			SET cssSolve = "<link rel='stylesheet' type='text/css' href='./css/error.css' />";
		END IF;
	END IF;
-- 3. HTML erzeugen
RETURN CONCAT("
<!DOCTYPE html>
<html>
	<head>
		<title>Heyho :D</title>
		<meta charset='utf8'>
		<link rel='stylesheet' type='text/css' href='./css/default.css' />
		",cssSolve,
		cssNoAction,"
	</head>
	<body>
		<form  action='#' method='POST'>
			<div id='rechner'>
			<div id='top'>
				<input id='current' type='text' value='",currentNumber,"' disabled><br /> 
				<input id='history' type='text' value='",history,"' disabled>
			</div>
			<div id='bottom'>	
				<button class='sonst' 		type='submit' name='action' value='('>(</button>
				<button class='sonst' 		type='submit' name='action' value=')'>)</button>
				<button class='sonst' 		type='submit' name='action' value='C'>C</button>
				<button class='sonst' 		type='submit' name='action' value='AC'>AC</button>
				",winkelButton,"<br />
				<button class='number' 		type='submit' name='action' value='7'>7</button>
				<button class='number' 		type='submit' name='action' value='8'>8</button>
				<button class='number' 		type='submit' name='action' value='9'>9</button>
				<button class='operator' 	type='submit' name='action' value='/'>/</button>
				<button class='winkel' 		type='submit' name='action' value='sin'>sin</button>
				<button class='winkel' 		type='submit' name='action' value='sin-1'>sin-1</button><br />
				<button class='number' 		type='submit' name='action' value='4'>4</button>
				<button class='number' 		type='submit' name='action' value='5'>5</button>
				<button class='number' 		type='submit' name='action' value='6'>6</button>
				<button class='operator' 	type='submit' name='action' value='*'>*</button>
				<button class='winkel' 		type='submit' name='action' value='cos'>cos</button>
				<button class='winkel' 		type='submit' name='action' value='cos-1'>cos-1</button><br />				
				<button class='number' 		type='submit' name='action' value='1'>1</button>
				<button class='number' 		type='submit' name='action' value='2'>2</button>
				<button class='number' 		type='submit' name='action' value='3'>3</button>
				<button class='operator' 	type='submit' name='action' value='+'>+</button>
	
				<button class='winkel' 		type='submit' name='action' value='tan'>tan</button>
				<button class='winkel' 		type='submit' name='action' value='tan-1'>tan-1</button><br />
				<button class='sonst' 		type='submit' name='action' value=','>,</button>
				<button class='number' 		type='submit' name='action' value='0'>0</button>
				<button class='solve' 		type='submit' name='action' value='='>=</button>
				<button class='operator' 	type='submit' name='action' value='-'>-</button>
				<button class='operator' 	type='submit' name='action' value='^'>^</button>
				<button class='operator' 	type='submit' name='action' value='√'>√</button><br />
			</div>
			</div>
		</form>
		",@error,"								
	</body>
</html>");
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.input
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `input`(
	`pSession` VARCHAR(40),
	`pAction` VARCHAR(6)






) RETURNS longtext CHARSET utf32
BEGIN
	SET @pSession = pSession;
	SET @pAction  = pAction;
	SET @history  = "";
	SET @backroll = FALSE;
	SET @error	  = "";
	
	CALL 		sessionCheck();   -- 1. Session überprüfen                     									  
	CALL 		actionHandler(); 	-- 2. Aktion Ausführen
	RETURN 	calcBuilder(); 	-- 3. Formular erzeugen
END//
DELIMITER ;

-- Exportiere Struktur von Tabelle taschenrechner.session
CREATE TABLE IF NOT EXISTS `session` (
  `session` varchar(40) COLLATE utf8_german2_ci NOT NULL,
  `lastChange` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `created` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `winkelMode` set('rad','deg') COLLATE utf8_german2_ci DEFAULT 'deg',
  PRIMARY KEY (`session`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_german2_ci;

-- Exportiere Daten aus Tabelle taschenrechner.session: ~28 rows (ungefähr)
DELETE FROM `session`;
/*!40000 ALTER TABLE `session` DISABLE KEYS */;
INSERT INTO `session` (`session`, `lastChange`, `created`, `winkelMode`) VALUES
	('0as7dn2qc3fetjfc6vmm925ib4', '2017-01-25 22:35:13', '2017-01-25 22:09:35', 'deg'),
	('1', '2017-01-21 00:41:06', '2017-01-21 00:41:06', 'deg'),
	('2', '2017-01-21 03:30:26', '2017-01-21 03:30:26', 'deg'),
	('252', '2017-01-26 22:20:52', '2017-01-26 22:20:45', 'deg'),
	('2553454354543534554352', '2017-01-26 22:21:20', '2017-01-26 22:21:14', 'deg'),
	('3', '2017-01-21 03:44:08', '2017-01-21 03:44:08', 'deg'),
	('4', '2017-01-18 22:58:38', '2017-01-18 22:58:38', 'deg'),
	('4333', '2017-01-18 22:58:56', '2017-01-18 22:58:56', 'deg'),
	('532ibaq8jj4nt4gdrshuseh346', '2017-01-26 22:20:26', '2017-01-26 22:20:20', 'deg'),
	('6qvfe19f84b0umaj8tlutema04', '2017-01-23 17:29:03', '2017-01-23 17:28:37', 'deg'),
	('876k1iadce4n627voku3sa6de6', '2017-01-30 04:31:56', '2017-01-30 03:25:27', 'deg'),
	('8eo0epact0qqn63ekilsin10n4', '2017-01-23 23:54:40', '2017-01-23 23:28:08', 'deg'),
	('a71835ap9qtrng243eu1a4a5b3', '2017-01-21 04:56:15', '2017-01-21 04:02:16', 'deg'),
	('e0252lio998dq35cc6mbsebkl0', '2017-01-27 22:16:12', '2017-01-27 22:10:56', 'deg'),
	('e57niq9de5muslrj3tmr58kjh1', '2017-01-22 19:00:46', '2017-01-22 17:59:30', 'deg'),
	('e8da0imbsmnhqpgl9h2bb3gnf5', '2017-01-23 20:17:21', '2017-01-23 20:06:54', 'deg'),
	('f156inikbect8111hujg2smgm2', '2017-01-24 00:03:37', '2017-01-23 16:59:50', 'deg'),
	('fru7r2tc60q569kqqoguj3ieh3', '2017-01-25 00:01:19', '2017-01-24 18:47:18', 'deg'),
	('ljrke1cbr8ffa8e0s74oivnef7', '2017-01-21 23:59:48', '2017-01-21 23:59:08', 'deg'),
	('lr38dq78vqsucjst3ce1ppi873', '2017-01-21 04:04:09', '2017-01-21 03:26:46', 'deg'),
	('nhfl0bg0oks3bftocg9hdbmnj0', '2017-01-23 14:12:06', '2017-01-23 13:49:06', 'deg'),
	('oj56u7m6vkrc44j7jmsh55ldh4', '2017-01-24 18:49:10', '2017-01-24 18:47:34', 'deg'),
	('q0u274n9kros46fj69fm0f0916', '2017-01-21 04:09:13', '2017-01-21 04:01:23', 'deg'),
	('r247lt537ml93182hreld1mob3', '2017-01-25 22:19:25', '2017-01-25 16:46:31', 'deg'),
	('rjacct0vgb24d32vr696o8fsa3', '2017-03-02 01:21:49', '2017-03-01 23:55:05', 'deg'),
	('rnr71tkdbr4fknji6vjl27j6c6', '2017-03-02 18:26:38', '2017-03-02 17:21:29', 'deg'),
	('tkgllki6l4jvus5vk0e75vjnu0', '2017-01-23 14:22:09', '2017-01-23 14:13:08', 'deg'),
	('vpfukkggvui8d45l05hb8q5if3', '2017-01-24 14:14:47', '2017-01-24 12:36:46', 'deg');
/*!40000 ALTER TABLE `session` ENABLE KEYS */;

-- Exportiere Struktur von Prozedur taschenrechner.sessionCheck
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sessionCheck`()
BEGIN
	IF (sessionGetSession()) IS NULL THEN 
		CALL sessionInsert();					
	ELSE 
		BEGIN
			IF (TIMESTAMPDIFF(HOUR,sessionGetLastChange(),CURRENT_TIMESTAMP))>=2 THEN  	-- |1.1| letzte Änderung über 2h her?
				CALL sessionReset();			-- >> Zurücksetzten
			ELSE 
				CALL sessionUpdate();		-- >> Updaten							
			END IF; 		
		END;			
	END IF;                       
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.sessionDelete
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sessionDelete`()
BEGIN
	DELETE FROM `session` WHERE `session`=@pSession;
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.sessionGetLastChange
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `sessionGetLastChange`() RETURNS timestamp
BEGIN
	RETURN (SELECT `lastChange` FROM `session` WHERE `session`=@pSession);
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.sessionGetSession
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `sessionGetSession`() RETURNS varchar(40) CHARSET utf8 COLLATE utf8_german2_ci
BEGIN
	RETURN (SELECT `session` FROM `session` WHERE `session`=@pSession);
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.sessionGetWinkelMode
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `sessionGetWinkelMode`() RETURNS varchar(6) CHARSET utf8 COLLATE utf8_bin
BEGIN
	RETURN (SELECT `winkelMode` FROM `session` WHERE `session`=@pSession);
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.sessionInsert
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sessionInsert`()
BEGIN
	INSERT INTO `session` (`session`) VALUES (@pSession);
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.sessionReset
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sessionReset`()
BEGIN
	CALL actionDoClearAll();
	CALL sessionDelete();
	CALL sessionInsert();
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.sessionSetWinkelMode
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sessionSetWinkelMode`()
BEGIN
	UPDATE `session` SET `winkelMode`=@pAction WHERE `session`=@pSession;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.sessionUpdate
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sessionUpdate`()
BEGIN
	UPDATE `session` SET `lastChange`=CURRENT_TIMESTAMP WHERE `session`=@pSession;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.solve
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `solve`()
BEGIN
	IF (actionGetLastType()='number' OR  actionGetLastAction()=')') THEN
		solveLabel: BEGIN
			DECLARE bis TINYINT DEFAULT 0;	
			SET @highestScope = solveGetHighestScope();
		
			WHILE actionGetNextScope()>0 DO
				CALL actionInsert('action', ')');
			END WHILE;
			
			SAVEPOINT solveSave;
			SET @history = REPLACE((SELECT GROUP_CONCAT(`action` SEPARATOR '') FROM `action` WHERE `session`=@pSession ORDER BY `id` ASC),'.',',');
			
			WHILE @highestScope>=0 DO
				CALL solveActionRight('sin');
				CALL solveActionRight('cos');
				CALL solveActionRight('tan');
				CALL solveActionRight('sin-1');
				CALL solveActionRight('cos-1');
				CALL solveActionRight('tan-1');
				CALL solveActionRight('³√');
				CALL solveActionRight('²√');
				CALL solveActionRight('√');
				CALL solveAction('^');
				CALL solveAction('/');
				CALL solveAction('*');
				CALL solveAction('-');
				CALL solveAction('+');
				
				IF (@backroll = TRUE) THEN
					ROLLBACK TO solveSave;
					RELEASE SAVEPOINT solveSave;
					LEAVE solveLabel;
				END IF;

				IF @highestScope>0 THEN			
					SET bis = solveCountActionInScope('(');
					WHILE 0<bis DO
						CALL solveKlammer();
						SET bis = bis-1;
					END WHILE;													
				END IF;
					
				SET @highestScope = @highestScope-1;	
			END WHILE;					
		END solveLabel;
	END IF;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.solveAction
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `solveAction`(
	IN `pAction` VARCHAR(6)













)
doAction: BEGIN
	DECLARE bis TINYINT DEFAULT 0;
	DECLARE newNumber DOUBLE DEFAULT 0;
	DECLARE pOperatorId INT DEFAULT 0;
	DECLARE pNumberLeft DOUBLE DEFAULT 0;
	DECLARE pNumberRight DOUBLE DEFAULT 0;
	DECLARE EXIT HANDLER FOR 1690	BEGIN
		SET @backroll = TRUE;
		SET @error = CONCAT("<p id='error'>Das Ergebnis dieser Zahl ist zu groß! <br>[",pNumberLeft,pAction,pNumberRight,"]</p>");
	END;
			
	IF (@backroll = TRUE) THEN
		LEAVE doAction;
	END IF;
	
	SET bis = solveCountActionInScope(pAction);
	WHILE 0<bis DO
		CALL solveGetOperationVaules(pAction, pOperatorId, pNumberLeft, pNumberRight);	
		CASE
			WHEN (pAction='^') THEN SET newNumber = POW(pNumberLeft,pNumberRight);
			WHEN (pAction='/') THEN 
				IF (pNumberRight=0) THEN 
					SET @backroll = TRUE;
					SET @error = CONCAT("<p id='error'>Es darf nicht durch 0 geteilt werden! <br>[",pNumberLeft,"/0]</p>");
					LEAVE doAction;
				END IF;
				SET newNumber = pNumberLeft/pNumberRight;
			WHEN (pAction='*') THEN SET newNumber = pNumberLeft*pNumberRight;
			WHEN (pAction='-') THEN SET newNumber = pNumberLeft-pNumberRight;
			WHEN (pAction='+') THEN SET newNumber = pNumberLeft+pNumberRight;
		END CASE;
		CALL solveUpdateSolution(newNumber, pOperatorId);		
		SET bis = bis-1;
	END WHILE;
END doAction//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.solveActionRight
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `solveActionRight`(
	IN `pAction` VARCHAR(6)









)
doAction: BEGIN
	DECLARE bis TINYINT DEFAULT 0;
	DECLARE newNumber DOUBLE DEFAULT 0.0;
	DECLARE pOperatorId INT DEFAULT 0;
	DECLARE pNumberRight DOUBLE DEFAULT 0;
	
	SET bis = solveCountActionInScope(pAction);
	WHILE 0<bis DO
		CALL solveGetOperationVaulesRight(pAction, pOperatorId, pNumberRight);	
		CASE
			WHEN (pAction='³√') THEN 
				IF (pNumberRight<0) THEN
					SET @backroll = TRUE;
					SET @error = CONCAT("<p id='error'>Es darf keine Wurzel aus einer negativen Zahl gezogen werden! <br>[³√(",pNumberRight,")]</p>");
					LEAVE doAction;
				END IF;
				SET newNumber = SQRT(SQRT(SQRT(pNumberRight)));
			WHEN (pAction='²√') THEN 
				IF (pNumberRight<0) THEN
					SET @backroll = TRUE;
					SET @error = CONCAT("<p id='error'>Es darf keine Wurzel aus einer negativen Zahl gezogen werden! <br>[²√(",pNumberRight,")]</p>");
					LEAVE doAction;
				END IF;
				SET newNumber = SQRT(SQRT(pNumberRight));
			WHEN (pAction='√')  THEN 
				IF (pNumberRight<0) THEN
					SET @backroll = TRUE;
					SET @error = CONCAT("<p id='error'>Es darf keine Wurzel aus einer negativen Zahl gezogen werden! <br>[√(",pNumberRight,")]</p>");
					LEAVE doAction;
				END IF;
				SET newNumber = SQRT(pNumberRight);
			WHEN (pAction='sin-1') THEN 
				IF (pNumberRight<-1 || pNumberRight>1) THEN
					SET @backroll = TRUE;
					SET @error = CONCAT("<p id='error'>Sin-1 muss zwischen -1 und 1 liegen! <br>[sin-1(",pNumberRight,")]</p>");
					LEAVE doAction;
				END IF;
				IF sessionGetWinkelMode()='deg' THEN
					SET newNumber = ASIN(RADIANS(pNumberRight));
				ELSE
					SET newNumber = ASIN(pNumberRight);
				END IF;
			WHEN (pAction='cos-1') THEN 
				IF (pNumberRight<-1 || pNumberRight>1) THEN
					SET @backroll = TRUE;
					SET @error = CONCAT("<p id='error'>Cos-1 muss zwischen -1 und 1 liegen! <br>[cos-1(",pNumberRight,")]</p>");
					LEAVE doAction;
				END IF;
				IF sessionGetWinkelMode()='deg' THEN
					SET newNumber = DEGREES(ACOS(pNumberRight));
				ELSE
					SET newNumber = ACOS(pNumberRight);
				END IF;
			WHEN (pAction='tan-1')  THEN 
				IF sessionGetWinkelMode()='deg' THEN
					SET newNumber = DEGREES(ATAN(pNumberRight));
				ELSE
					SET newNumber = ATAN(pNumberRight);
				END IF;
			WHEN (pAction='sin') THEN
				IF sessionGetWinkelMode()='deg' THEN
					SET newNumber = DEGREES(SIN(pNumberRight));
				ELSE
					SET newNumber = SIN(pNumberRight);
				END IF;
			WHEN (pAction='cos') THEN 
				IF sessionGetWinkelMode()='deg' THEN
					SET newNumber = COS(RADIANS(pNumberRight));
				ELSE
					SET newNumber = COS(pNumberRight);
				END IF;
			WHEN (pAction='tan')  THEN
				IF sessionGetWinkelMode()='deg' THEN
					SET newNumber = TAN(RADIANS(pNumberRight));
				ELSE
					SET newNumber = TAN(pNumberRight);
				END IF;

		END CASE;
		CALL solveUpdateSolution(newNumber, pOperatorId);		
		SET bis = bis-1;
	END WHILE;
END doAction//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.solveCheckIsOne
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `solveCheckIsOne`() RETURNS bit(1)
BEGIN
	IF ((SELECT COUNT(*) FROM `action` WHERE `session`=@pSession LIMIT 2)=1) THEN
		RETURN 1;
	ELSE 
		RETURN 0;
	END IF;
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.solveCountActionInScope
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `solveCountActionInScope`(
	`pAction` VARCHAR(6)



) RETURNS tinyint(4)
BEGIN
	RETURN (SELECT COUNT(*) FROM `action` WHERE `session`=@pSession AND `action`=pAction AND `type`='action' AND `scope`=@highestScope);
END//
DELIMITER ;

-- Exportiere Struktur von Funktion taschenrechner.solveGetHighestScope
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `solveGetHighestScope`() RETURNS tinyint(4)
BEGIN
	RETURN (SELECT `scope` FROM `action` WHERE `session`=@pSession ORDER BY `scope` DESC LIMIT 1);
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.solveGetOperationVaules
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `solveGetOperationVaules`(
	IN `pAction` VARCHAR(6),
	INOUT `pOperatorId` INT,
	OUT `pNumberLeft` DOUBLE,
	OUT `pNumberRight` DOUBLE

















)
BEGIN
	DECLARE pNumberLeftString VARCHAR(50);
	DECLARE pNumberRightString VARCHAR(50);
	SET pOperatorId = (SELECT `id` FROM `action` WHERE (`session`=@pSession AND `scope`=@highestScope AND `action`=pAction AND `type`='action') ORDER BY `id` ASC LIMIT 1);
	SET pNumberLeftString =  (SELECT `action` FROM `action` WHERE (`session`=@pSession AND `scope`=@highestScope AND `id`<pOperatorId) ORDER BY `id` DESC LIMIT 1);
	SET pNumberRightString = (SELECT `action` FROM `action` WHERE (`session`=@pSession AND `scope`=@highestScope AND `id`>pOperatorId) ORDER BY `id` ASC LIMIT 1);
	
	IF (SELECT pNumberLeftString LIKE '-%')=1 THEN
		SET pNumberLeft = -1*CAST(SUBSTRING(pNumberLeftString,2) AS DOUBLE);	
	ELSE 
		SET pNumberLeft =  CAST(pNumberLeftString AS DOUBLE);
	END IF;
	
	IF (SELECT pNumberRightString LIKE '-%')=1 THEN
		SET pNumberRight = -1*CAST(SUBSTRING(pNumberRightString,2) AS DOUBLE);		
	ELSE 
		SET pNumberRight =  CAST(pNumberRightString AS DOUBLE);
	END IF;
		
	DELETE FROM `action` WHERE `session`=@pSession AND `scope`=@highestScope AND `id`<pOperatorId ORDER BY `id` DESC LIMIT 1;
	DELETE FROM `action` WHERE `session`=@pSession AND `scope`=@highestScope AND `id`>pOperatorId ORDER BY `id` ASC LIMIT 1;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.solveGetOperationVaulesRight
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `solveGetOperationVaulesRight`(
	IN `pAction` VARCHAR(6),
	INOUT `pOperatorId` INT,
	INOUT `pNumberRight` DOUBLE











)
BEGIN
	DECLARE pNumberRightString VARCHAR(50);
	SET pOperatorId = (SELECT `id` FROM `action` WHERE (`session`=@pSession AND `scope`=@highestScope AND `action`=pAction AND `type`='action') ORDER BY `id` ASC LIMIT 1);
	SET pNumberRightString = (SELECT `action` FROM `action` WHERE (`session`=@pSession AND `scope`=@highestScope AND `id`>pOperatorId) ORDER BY `id` ASC LIMIT 1);
	
	IF (SELECT pNumberRightString LIKE '-%')=1 THEN
		SET pNumberRight = -1*CAST(SUBSTRING(pNumberRightString,2) AS DOUBLE);		
	ELSE 
		SET pNumberRight =  CAST(pNumberRightString AS DOUBLE);
	END IF;
		
	DELETE FROM `action` WHERE `session`=@pSession AND `scope`=@highestScope AND `id`>pOperatorId ORDER BY `id` ASC LIMIT 1;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.solveKlammer
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `solveKlammer`()
BEGIN
	DECLARE idLeft int DEFAULT (SELECT `id` FROM `action` WHERE `session`=@pSession AND `Scope`=@highestScope AND `action`='(' ORDER BY `id` ASC LIMIT 1);
	DECLARE idRight int DEFAULT (SELECT `id` FROM `action` WHERE `session`=@pSession AND `Scope`=@highestScope AND `action`=')' ORDER BY `id` ASC LIMIT 1);
	DECLARE idmid int DEFAULT (SELECT `id` FROM `action` WHERE `session`=@pSession AND `id` BETWEEN idLeft+1 AND idRight-1);
	IF ( idmid IS NOT NULL) THEN
		UPDATE `action` SET `scope`=(@highestScope-1) WHERE `id`=idmid;
	END IF;
	DELETE FROM `action` WHERE `id`=idLeft;
	DELETE FROM `action` WHERE `id`=idRight;
END//
DELIMITER ;

-- Exportiere Struktur von Prozedur taschenrechner.solveUpdateSolution
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `solveUpdateSolution`(
	IN `pNewNumber` DOUBLE,
	IN `pOperatorId` INT






)
BEGIN
	UPDATE `action` SET `action`=CAST(pNewNumber AS CHAR(50)), `type`='number' WHERE `id`=pOperatorId;
END//
DELIMITER ;

/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
