<?php

include("include.php");
include("magicdefs.php");
include("db.php");

$tables=array("fSequenceFirst" => "Sequences",
              "fRunNumber"     => "RunData",
              "fDataSetNumber" => "DataSets");

$prims=array("SequenceNumber" => "fSequenceFirst",
             "RunNumber"      => "fRunNumber",
             "DataSetNumber"  => "fDataSetNumber");

$joins=array("fSequenceFirst" => " LEFT JOIN Star ON Star.fSequenceFirst=Sequences.fSequenceFirst "
             ."LEFT JOIN Calibration ON Calibration.fSequenceFirst=Sequences.fSequenceFirst",
             "fRunNumber" => " LEFT JOIN DataCheck ON DataCheck.fRunNumber=RunData.fRunNumber ",
             "fDataSetNumber" =>" LEFT JOIN Ganymed ON Ganymed.fDataSetNumber=DataSets.fDataSetNumber");

function GetList($host, $user, $pw, $db, $column, $tables)
{
    $tab_fSequenceFirst=array("Sequences", "Calibration", "Star");
    $tab_fRunNumber=array("RunData");
    $tab_fDataSetNumber=array("DataSets", "Ganymed");

    $table=$tables[$column];
    foreach(${"tab_".$column} as $tab)
    {
        $query="EXPLAIN " . $tab;

        mysql_connect($host, $user, $pw);
        mysql_select_db($db);

        $result=mysql_query($query);

        while ($row = mysql_fetch_row($result))
            $list[] = $row[0];
    }
    return $list;
}


function GetQuery($tables, $joins, $set)
{
    $primary=$_GET["prim"];
    $column=$_GET[$primary."Col"];
    $table=$tables[$primary];
    $primstart=$_GET["primstart"];
    $primstop=$_GET["primstop"];
    $start1=$_GET[$column."Start"];
    $stop1=$_GET[$column."Stop"];

    $query="SELECT " . $table . "." . $primary . ", ";
    if ($column==$primary)
        $query.=$table . "." . $column;
    else
        $query.=$column;

    if ($_GET["plot"]=="val")
    {
        $column2=$_GET[$primary."Col2"];
        $start2=$_GET[$column2."Start2"];
        $stop2=$_GET[$column2."Stop2"];

        if ($column2==$primary)
            $query.=", " . $table . "." . $column2;
        else
            $query.=", " . $column2;
    }
    $query.=" FROM " . $table;
    $query.=$joins[$primary];
    if (!empty($primstart) && !empty($primstop))
        $query.=" WHERE " . $table . "." . $primary . " BETWEEN " . $primstart . " AND " . $primstop;

    if (!empty($start1) && !empty($stop1))
    {
        if (strpos($query, " WHERE ")==FALSE)
            $query.=" WHERE " . $column . " BETWEEN " . $start1 . " AND " . $stop1;
        else
            $query.=" AND " . $column . " BETWEEN " . $start1 . " AND " . $stop1;
    }

    if ($_GET["plot"]=="val" && !empty($start2) && !empty($stop2))
    {
        if (strpos($query, " WHERE ")==FALSE)
            $query.=" WHERE " . $column2 . " BETWEEN " . $start2 . " AND " . $stop2;
        else
            $query.=" AND " . $column2 . " BETWEEN " . $start2 . " AND " . $stop2;
    }

    if (!empty($set) && !empty($_GET[$set]))
    {
        if (strpos($query, " WHERE ")==FALSE)
            $query .= " WHERE ";
        else
            $query.=" AND ";
        $query.= $table . "." . $primary . " IN (" . str_replace(" ", ",", $_GET[$set]) . ") ";
    }

    return $query;
}


?>
