<?php

	//Security
	if( ! defined( 'IS_INIT' ) )
	{
		die( 'Unauthorized Access');
	}
	
class database
 {
     //Connection variable
     public  $conn;
     private $server;
     private $user;
     private $pass;
     private $connInfo;
     public $queries = 0;
     private $query_data;
     private $prepared;
     
     //Construct
     public function database() {}
     
     public function Connect( $server , $user , $pass )
     {
         //Set variables
         $this->server = $server;
         $this->user = $user;
         $this->pass = $pass;
         
         //Set conneciton array, used by sqlsrv_connect
         $this->connInfo = array( "UID" => $this->user , "PWD" => $this->pass );
         //Try to connect or die, used a function to keep it clean
         $this->conn = sqlsrv_connect( $this->server , $this->connInfo ) or die( $this->OnDieFunction() );
     }
     
     public function Query( $sql )
     {
     	$this->query_data = sqlsrv_query($this->conn , $sql );
     	$this->queries++;
     }
     
     public function Prepare( $sql , $values )
     {
     	$this->prepared = sqlsrv_prepare( $this->conn , $sql , $values );
     	$this->queries++;
     }
     
     public function Execute()
     {
     	sqlsrv_execute( $this->prepared );
     }
     
     public function Fetch( $type = 0 )
     {
     	if( is_bool( $this->query_data ) && is_bool( $this->prepared ) )
     	{
     		return 0;
     	}
     	else
     	{
     		switch( $type )
     		{
	     		//Prepared statement
	     		case "0":
	     			return sqlsrv_fetch( $this->prepared );
	     			$this->prepared = false;
	     			break;
	     		//Query
	     		case "1":
	     			return sqlsrv_fetch( $this->query_data );
	     			$this->query_data = false;
	     			break;
	     	}
     	}
     }
     
     public function NumRows()
     {
     	if( is_bool( $this->query_data ) && is_bool( $this->prepared ) )
     	{
     		echo "Error";
     	}
     	else
     	{
     		return ( is_bool( $this->query_data ) ) ? sqlsrv_num_rows( $this->query_data ) : sqlsrv_num_rows( $this->prepared );
     	}
     }
     
     public function HasRows( $type = 0)
     {
     	if( is_bool( $this->query_data ) && is_bool( $this->prepared ) )
     	{
     		echo "Error";
     	}
     	else
     	{
     		switch( $type )
     		{
     			//Prepared
     			case "0":
     				if( sqlsrv_has_rows( $this->prepared ) )
     				{
     					return (bool) true;
     				}
     				else
     				{
     					return false;
     				}
     				break;
     			//Query
     			case "1":
     				if( sqlsrv_has_rows( $this->query_data) )
     				{
     					return true;
     				}
     				else
     				{
     					return false;
     				}
     				break;
     		}
     	}    	
     }
     
     public function GetField( $key )
     {
     	return sqlsrv_get_field( $this->query_data , $key );
     }
     
     public function Fetch_Assoc( $type = 0 )
     {	
     	switch( $type )
     	{
     		//Prepared statement
     		case "0":
     			return sqlsrv_fetch_array( $this->prepared , SQLSRV_FETCH_ASSOC );
     			$this->prepared = false;
     			break;
     		//Query
     		case "1":
     			return sqlsrv_fetch_array( $this->query_data , SQLSRV_FETCH_ASSOC );
     			$this->query_data = false;
     			break;
     	}
     }
     
     public function Free()
     {
     	if( is_bool( $this->query_data ) && is_bool( $this->prepared ) )
     	{
     		echo "There is nothing to free.";
     	}     	
     	else
     	{
     		( ! is_bool( $this->query_data ) ) ? sqlsrv_free_stmt( $this->prepared )  :sqlsrv_free_stmt( $this->query_data );
     	}
     }
     
     public function OnDieFunction()
     {
         //Echo Only the errors, not the useless junk, <pre> so we can see clearly
         echo "<pre>";
         $k = (array) sqlsrv_errors();
         foreach( $k as $error => $message )
         {
         	echo $error . ". " . $message[2] . "<br />";
         }
         echo "</pre>";
         
         /*
          echo "<pre>";
         $k = (array) sqlsrv_errors();
         echo $k[0][2];
         echo "</pre>";
         */
     }

     public function Escape( $str )
     {
     	return str_replace( "'", "''" , $str );
     }
     
     public function __destruct()
     {
         if( ! ( is_bool( $this->conn ) ) )
         {
             sqlsrv_close( $this->conn );
         }
     }
 }
 
 ?>