You are here

public function DriverBase::parseSQLFile in Forena Reports 8

Break the contents of a sql file down to its source.

Parameters

$contents:

Return value

array

1 call to DriverBase::parseSQLFile()
DriverBase::loadBlockFromFile in src/FrxPlugin/Driver/DriverBase.php

File

src/FrxPlugin/Driver/DriverBase.php, line 307
Class that defines default methods for access control in an DriverBase

Class

DriverBase

Namespace

Drupal\forena\FrxPlugin\Driver

Code

public function parseSQLFile($contents) {
  $comment = $this->comment_prefix;
  $trim = $this->comment_suffix;
  $lines = explode("\n", $contents);
  $cnt = count($lines);
  $access = '';
  $i = 0;
  $data = '';
  $file = '';
  $skip = FALSE;
  $in_info = FALSE;
  $found_case = FALSE;
  $info_text = '';
  $tokens = array();
  $options = array();
  $switch = '';
  while ($i < $cnt) {
    $l = trim($lines[$i], "\r");
    @(list($d, $c) = explode($comment, $l, 2));
    if ($trim) {
      $c = trim($c, $trim);
    }
    if ($c) {
      $c = trim($c);
      @(list($a, $o) = explode('=', $c, 2));
      $a = trim($a);
      if ($a && $o || $c == 'END' || $c == 'ELSE' || $c == 'INFO') {
        if ($c != 'INFO') {
          $in_info = false;
        }
        switch ($a) {
          case 'ACCESS':
            $access = trim($o);
            break;
          case 'SWITCH':
            $switch = trim($o);
            $found_case = FALSE;
            break;
          case 'CASE':
            $match = $this->te
              ->replace($switch, TRUE) == $this->te
              ->replace($o);
            if ($match) {
              $found_case = TRUE;
            }
            $skip = !$match;
            break;
          case 'IF':
            $skip = !$this->te
              ->test(trim($o));
            break;
          case 'END':
            $skip = FALSE;
            $switch = '';
            break;
          case 'ELSE':
            $skip = $switch ? $found_case : !$skip;
            break;
          case 'INFO':
            $in_info = TRUE;
            break;
          case 'INCLUDE':
            if (!$skip) {
              $inc_block = $this
                ->getSQLInclude(trim($o));
              if ($inc_block) {
                $data .= $inc_block['source'];
                $tokens = array_merge($tokens, $inc_block['tokens']);
              }
            }
            break;
        }
      }
      if ($a != 'ACCESS') {
        $file .= "{$l}\n";
      }
    }
    else {
      $file .= "{$l}\n";
    }
    if ($in_info) {
      if (strpos($l, $comment) !== 0 && $l) {
        $info_text .= "{$l}\n";
      }
    }
    elseif (!$skip) {
      if (strpos($l, $comment) !== 0 && $l) {
        $data .= "{$l}\n";
      }
    }
    $i++;
  }
  $tokens = array_merge($tokens, $this
    ->tokens($contents));
  if ($info_text) {
    $parser = new Parser();
    try {
      $options = $parser
        ->parse($info_text);
    } catch (Exception $e) {
      $options = [];
    }
  }
  $block = array(
    'source' => $data,
    'file' => trim($file, " \n"),
    'tokens' => $tokens,
    'options' => $options,
    'access' => $access,
  );
  return $block;
}