You are here

function units_mathematical_expression_recreate_stored_functions in Units of Measurement 7.2

Initialize DB stored functions necessary for the module.

Parameters

bool $create: Whether to create (TRUE) or delete (FALSE) the stored functions

2 calls to units_mathematical_expression_recreate_stored_functions()
units_disable in ./units.install
Implements hook_disable().
units_enable in ./units.install
Implements hook_enable().

File

./units.module, line 911
Provide API for managing and converting units of measurement.

Code

function units_mathematical_expression_recreate_stored_functions($create = TRUE) {

  // TODO: for now we run on PostgreSQL, so we disable this whole thing.
  return;
  $operators = units_get_operator_by_sign();
  $units_token_type_name = 'units_token_type';
  $units_token_type = <<<EOF
CREATE FUNCTION `{<span class="php-variable">$units_token_type_name</span>}` ( token VARCHAR(255) ) RETURNS varchar(255)
BEGIN

IF (token IN (:operator_signs)) THEN
  RETURN ('operator');
ELSEIF EXISTS (SELECT 1 FROM {units_unit} WHERE machine_name = token) THEN
  RETURN ('unit');
ELSE
  RETURN ('constant');
END if;

END
EOF;
  units_mathematical_expression_recreate_stored_function($create, $units_token_type_name, $units_token_type, array(
    ':operator_signs' => array_keys($operators),
  ));
  $units_token_name = 'units_token';
  $units_token = <<<EOF
CREATE FUNCTION `{<span class="php-variable">$units_token_name</span>}` ( expression VARCHAR(255), pos INT(10) ) RETURNS VARCHAR(32)
  DETERMINISTIC
BEGIN

DECLARE length INT;
DECLARE token_length INT;
DECLARE token VARCHAR(32);
DECLARE delimiter VARCHAR(1);

SET delimiter = ' ';

SET expression = CONCAT(expression, delimiter);
SET length = CHAR_LENGTH(expression) + 1;

SET token_length = LOCATE(delimiter, expression, pos) - pos;
SET token = SUBSTRING(expression, pos, token_length);

RETURN (token);

END
EOF;
  units_mathematical_expression_recreate_stored_function($create, $units_token_name, $units_token);
  $units_decompose_postfix_name = 'units_decompose_postfix';
  $units_decompose_postfix = <<<EOF
CREATE FUNCTION `{<span class="php-variable">$units_decompose_postfix_name</span>}` ( expression VARCHAR(255) ) RETURNS VARCHAR(255)
BEGIN

DECLARE i INT;

DECLARE token VARCHAR(32);

SET i = 1;

WHILE i < CHAR_LENGTH(expression) + 1 DO
  SET token = units_token(expression, i);
  SET i = i + CHAR_LENGTH(token) + 1;

  IF (units_token_type(token) = 'unit') THEN
    SELECT REPLACE(expression, machine_name, decomposition_postfix) INTO expression FROM {units_unit} WHERE machine_name = token AND decomposition_postfix <> '' AND decomposition_postfix NOT LIKE :units_quantity;
  END IF;
END WHILE;

RETURN (expression);

END
EOF;
  $units_decompose_postfix_args = array(
    ':units_quantity' => '%' . db_like(UNITS_QUANTITY) . '%',
  );
  units_mathematical_expression_recreate_stored_function($create, $units_decompose_postfix_name, $units_decompose_postfix, $units_decompose_postfix_args);
  $units_evaluate_postfix_iteration_name = 'units_evaluate_postfix_iteration';
  $units_evaluate_postfix_iteration_args = array();
  $units_evaluate_postfix_iteration = array();
  foreach ($operators as $operator) {
    $sign = $operator['sign'];
    $name = $operator['name'];
    $sql_evaluate_fragment = $operator['sql evaluate fragment'];
    $operand_normalization = array();
    for ($i = 1; $i < 3; $i++) {
      if ($operator['transparent operand' . $i]) {
        $operand_normalization[] = "\n        IF (units_token_type(token{$i}) = 'unit') THEN\n          SET token{$i} = :{$name}_token{$i};\n        END IF;\n        ";
        $units_evaluate_postfix_iteration_args[$name . '_token' . $i] = $operator['transparent operand' . $i];
      }
    }
    $operand_normalization = implode("\n", $operand_normalization);
    $units_evaluate_postfix_iteration[] = <<<EOF
    WHEN '{<span class="php-variable">$sign</span>}' THEN
      {<span class="php-variable">$operand_normalization</span>}
      {<span class="php-variable">$sql_evaluate_fragment</span>}
EOF;
  }
  $units_evaluate_postfix_iteration = implode("\n", $units_evaluate_postfix_iteration);
  $units_evaluate_postfix_iteration = <<<EOF
CREATE FUNCTION `{<span class="php-variable">$units_evaluate_postfix_iteration_name</span>}` ( expression VARCHAR(255) ) RETURNS VARCHAR(255)
BEGIN

DECLARE i INT;

DECLARE token1 VARCHAR(32);
DECLARE token2 VARCHAR(32);
DECLARE token3 VARCHAR(32);

DECLARE token1_length INT;
DECLARE token2_length INT;
DECLARE token3_length INT;

DECLARE result FLOAT;

SET i = 1;

WHILE i < CHAR_LENGTH(expression) + 1 DO
  SET token1 = units_token(expression, i);
  SET token1_length = CHAR_LENGTH(token1);

  SET token2 = units_token(expression, i + token1_length + 1);
  SET token2_length = CHAR_LENGTH(token2);

  SET token3 = units_token(expression, i + token1_length + 1 + token2_length + 1);
  SET token3_length = CHAR_LENGTH(token3);

  IF (units_token_type(token1) IN ('constant', 'unit')) AND (units_token_type(token2) IN ('constant', 'unit')) AND (units_token_type(token3) = 'operator') THEN
    CASE token3
{<span class="php-variable">$units_evaluate_postfix_iteration</span>}
    END CASE;
    SET expression = CONCAT(SUBSTRING(expression, 1, i - 1), result, SUBSTRING(expression, i + token1_length + 1 + token2_length + 1 + token3_length + 1 - 1));
  ELSE
    SET i = i + token1_length + 1;
  END IF;

END WHILE;

RETURN (expression);

END
EOF;
  units_mathematical_expression_recreate_stored_function($create, $units_evaluate_postfix_iteration_name, $units_evaluate_postfix_iteration, $units_evaluate_postfix_iteration_args);
  $units_evaluate_postfix_name = 'units_evaluate_postfix';
  $units_evaluate_postfix = <<<EOF
CREATE FUNCTION `{<span class="php-variable">$units_evaluate_postfix_name</span>}` ( expression VARCHAR(255) ) RETURNS FLOAT
BEGIN

DECLARE old_expression VARCHAR(255);

SET expression = units_decompose_postfix(expression);

REPEAT
  SET old_expression = expression;
  SET expression = units_evaluate_postfix_iteration(expression);
UNTIL old_expression = expression END REPEAT;

RETURN (expression);

END
EOF;
  units_mathematical_expression_recreate_stored_function($create, $units_evaluate_postfix_name, $units_evaluate_postfix);
}