<?php
/*
* search_oracle.php
* http://tasuke.dna.affrc.go.jp/
*
* Author: Ryutaro Itoh
*
* Copyright (c) 2013 National Institute of Agrobiological Sciences   
*
* This software is released under the MIT License.
*
* http://opensource.org/licenses/mit-license.php
*/

require '../../conf/config.php';

//////////////////////////////////  Connecting with DB
$conn = oci_connect($user, $pswd, $host."/".$db, "AL32UTF8");
if(!$conn) {
    $e = oci_error();
    error_log($e["message"]."\nConnection failed.\n");
    exit;
}

////////////////////////////////////////////////////////// Parameters
(isset($_GET['prm_keyword']) && !empty($_GET['prm_keyword'])) ? $prm_keyword=$_GET['prm_keyword'] : returnError("Error");
(isset($_GET['prm_chr']) && !empty($_GET['prm_chr'])) ? $prm_chr=$_GET['prm_chr'] : returnError("Error");

///////////////////////////////////////////////////////// Searching locus id
$param1="%".$prm_keyword."%";
$queryP = "SELECT count(id) from sys_track where parent=1 and contains(note,:bd_text)>0 or contains(id,:bd_text)>0";
$query = "SELECT id,pos,end,chr,note from sys_track where parent=1 and (contains(note,:bd_text)>0 or contains(id,:bd_text)>0) order by chr,pos";
$rtnString="";
$stmtflag=0;
$num=0;
$rowcount=0;

$stmtp = oci_parse($conn, $queryP);
oci_bind_by_name($stmtp, ':bd_text', $param1, -1, SQLT_CHR);
oci_execute($stmtp);
while (($rowP = oci_fetch_array($stmtp, OCI_NUM + OCI_RETURN_NULLS))!= false) {
    $num=$rowP[0];
}
oci_free_statement($stmtp);
if($num>0){
    $stmt = oci_parse($conn, $query);
    oci_bind_by_name($stmt, ':bd_text', $param1, -1, SQLT_CHR);
    oci_execute($stmt);
    while (($row = oci_fetch_array($stmt, OCI_NUM + OCI_RETURN_NULLS))!= false) {
        $rtnString.= $row[0]."|".$row[1]."|".$row[2]."|".$num."|".$row[3]."|".$row[4]."~";
        $rowcount++;
        if($rowcount >= 1000){
            break;
        }
    }
    oci_free_statement($stmt);
}
if($num < 1){
    $rtnString= "false|false2|false|false|false|~";
}
oci_close($conn);
$rtnString=rtrim($rtnString, "~");
echo $rtnString;

function returnError($str){
    echo $str;
}

function _cvtZoom($str) {
    preg_match("/^[0-9]+/", $str, $r1);
    preg_match('/[b,k]$/', $str, $r2);
    $v=$r1[0];
    if($r2[0] == "b"){
        $r=1*(int)$v;
    } else {
        $r=1000*(int)$v;
    }
    return (int)$r;
}

?>

