You are here

function ARC_sparql2sql_rewriter::get_union_select_sql in Taxonomy import/export via XML 6

Same name and namespace in other branches
  1. 5.2 arc/ARC_sparql2sql_rewriter.php \ARC_sparql2sql_rewriter::get_union_select_sql()
  2. 5 arc/ARC_sparql2sql_rewriter.php \ARC_sparql2sql_rewriter::get_union_select_sql()
  3. 6.2 arc/ARC_sparql2sql_rewriter.php \ARC_sparql2sql_rewriter::get_union_select_sql()
1 call to ARC_sparql2sql_rewriter::get_union_select_sql()
ARC_sparql2sql_rewriter::get_select_sql in arc/ARC_sparql2sql_rewriter.php

File

arc/ARC_sparql2sql_rewriter.php, line 163

Class

ARC_sparql2sql_rewriter

Code

function get_union_select_sql() {
  $infos = $this->infos;
  $result = "";
  $where_code = trim($this->where_code);
  while (preg_match("/__union_([0-9]+)__/", $where_code, $matches)) {
    $union_id = $matches[1];
    $branches = $this->union_branches[$union_id];
    for ($i = 0, $i_max = count($branches); $i < $i_max; $i++) {
      $union_branch_id = $union_id . "_" . ($i + 1);
      $left_join_code = $this
        ->get_left_join_code();

      /* detects alias_alternatives */

      /* select */
      $cur_result = "SELECT";

      /* distinct, not on first branch */
      if (strlen($result)) {
        $cur_result .= isset($infos["distinct"]) && $infos["distinct"] ? " DISTINCT" : " ALL";
      }

      /* count rows */
      if (isset($infos["count_rows"]) && $infos["count_rows"]) {
        $result .= " SQL_CALC_FOUND_ROWS";
      }

      /* vars */
      $cur_result .= $this
        ->get_result_vars_code(array(
        "union_branch_id" => $union_branch_id,
      ));

      /* from */
      $cur_result .= $this
        ->get_from_code();

      /* left joins */
      $cur_result .= strlen($left_join_code) ? "\n /* left-joins */" . $left_join_code : "";

      /* id2val joins */
      $id2val_join_code = $this
        ->get_id2val_join_code();
      $cur_result .= strlen($id2val_join_code) ? $id2val_join_code : "";

      /* where */
      $cur_result .= "\nWHERE \n ";

      /* dataset restrictions */
      $dataset_code = $this
        ->get_dataset_code();
      $cur_result .= strlen($dataset_code) ? "/* dataset restrictions */\n" . $dataset_code . "\n" : "";

      /* graph restrictions */
      $graph_code = $this
        ->get_graph_code();
      if (strlen($graph_code)) {
        $cur_result .= " /* graph restrictions */";
        $cur_result .= strlen($dataset_code) ? "\n AND \n " . $graph_code : "\n " . $graph_code;
        $cur_result .= "\n";
      }

      /* where_code */
      $cur_result .= " /* triple patterns and filters */\n ";
      $cur_result .= strlen($graph_code . $dataset_code) ? "AND \n " : "";
      $cur_result .= trim($where_code);

      /* equi-joins */
      $equi_join_code = $this
        ->get_equi_join_code();
      $cur_result .= strlen($equi_join_code) ? "\n /* equi-joins */\n AND " . $equi_join_code : "";

      /* unions */
      $cur_branch = $branches[$i];
      $branch_result = str_replace("__union_" . $union_id . "__", $cur_branch, $cur_result);
      $result .= $i == 0 ? "(" . $branch_result . "\n)" : "\nUNION \n(" . $branch_result . "\n)";
    }
    $where_code = str_replace("__union_" . $union_id . "__", "", $where_code);
  }

  /* order by */
  $result .= $this
    ->get_order_by_code();

  /* limit/offset */
  $result .= $this
    ->get_limit_offset_code();
  return $result;
}