<?
class CL_QUERY{
	private	$deb_que = "1";  
	private $query;
	private	$ans_type;
	private $ans;
	private $ans_rsc;
	public 	$classname = "CL_QUERY";
	private $num_rows;

	public function cl_query($query, $ans_type="nothing", $deb_que=0 ){
		$this->query = $query;
		$this->ans_type = strtolower( $ans_type );
		$this->deb_que = $deb_que;
		$this->deb_que == 1?	$echostr = "class $this->classname construtor\n" : $echostr= "" ;
		echo  $echostr;
	}
// mysql_query() or cl_log::mydie("cl_query:__ error: " . mysql_error(), "db error" );

	public function set_query( $query ){
		$this->query = $query;
	}
	
	public function set_ans_type( $ans_type ){
		$this->ans_type = strtolower( $ans_type );
	}

	public function next(){
		$this->deb_que == 1?	$echostr = "class $this->classname:next\n" : $echostr= "" ;
		echo  $echostr;
		return mysql_fetch_row( $this->ans );
	}

	public function print_ans(){
		if( $this->ans_type == "row" ){
			for( $i=0; $i < count( $this->ans ); $i++ ){
				print $this->ans[$i] . ": ";
			}
			print "\n";
		} elseif( $this->ans_type == "rows" ) {
			$row = $this->next();
			while( $row	){
				for( $i=0; $i < count( $row ); $i++ ){
					print $row[$i] . ": ";
				} 
				print "\n";
				$row = $this->next();
			}
		} else {
					echo "$this->classname:print_ans: no answer to print\n";
		}
	}

	public function select(){
		$this->deb_que == 1?	$echostr = "class $this->classname:select \n" : $echostr= "" ;
		echo  $echostr;
		$this->ans_rsc = mysql_query( $this->query ) or CL_LOG::mydie( "cl_query::select error: ". mysql_error(), "db error" );
		switch( $num = mysql_num_rows( $this->ans_rsc ) ){
			case 1: 
				$this->ans = mysql_fetch_row( $this->ans_rsc );
				$this->ans_type = "row";
				$this->num_rows = $num;
				break;
			default: 
				if( $num > 1){	
		 			$this->ans = $this->ans_rsc;
					$this->ans_type = "rows";
					$this->num_rows = $num;
				}else {
					$this->ans_type = "nothing";
					$this->num_rows = $num;
				}
		}
	}
	

	public function update(){
		$this->deb_que == 1?	$echostr = "class $this->classname:update \n" : $echostr= "" ;
		echo  $echostr;
		mysql_query( $this->query ) or cl_log::mydie("cl_query:update() error: " . mysql_error(), "db error" );
	}

	public function insert(){
		$this->deb_que == 1?	$echostr = "class $this->classname:insert() \n" : $echostr= "" ;
		echo  $echostr;
		mysql_query( $this->query ) or cl_log::mydie("cl_query:insert() error: " . mysql_error(), "db error" );
	}

	public function delete_f(){
		$this->deb_que == 1?	$echostr = "class $this->classname:delete_f()\n" : $echostr= "" ;
		echo  $echostr;
		mysql_query( $this->query ) or cl_log::mydie("cl_query:delete_f() error: " . mysql_error(), "db error" );
	}
	
	public function set_valid( $cond, $table ){
		$this->deb_que == 1?	$echostr = "class $this->classname:set_valid()\n" : $echostr= "" ;
		echo  $echostr;

		$this->query = "UPDATE $table SET valid=1 WHERE $cond";
		$this->update();
	}

	public function unset_valid( $cond, $table ){
		$this->deb_que == 1?	$echostr = "class $this->classname:set_unvalid()\n" : $echostr= "" ;
		echo  $echostr;

		$this->query = "UPDATE $table SET valid=0 WHERE $cond";
		$this->update();
	}
	public function get_rsc(){
//napicu
			print_r( mysql_fetch_row( $this->ans_rsc ) );
			return  $this->ans_rsc;
	}
	public function get_num_rows(){
			return $this->num_rows;
	}

}

?>
