AlaK4X
Linux lhjmq-records 5.15.0-118-generic #128-Ubuntu SMP Fri Jul 5 09:28:59 UTC 2024 x86_64



Your IP : 18.227.46.54


Current Path : /var/www/lhjmq-records.qc.ca/public_html/classes/
Upload File :
Current File : /var/www/lhjmq-records.qc.ca/public_html/classes/class.query.builder.php

<?php
/*The query builder class is an intelligent SELECT clause builder
class that can build multi-table query. The biggest usage for this
is the data_interfacer which will only accept query_builders for
entry queries.

This class is a standalone class but is required by other classes
in the data manipulator package by mathieu dumoulin.*/
class query_builder{
	/*System arrays that keeps various information about the query*/
	var $fields;
	var $tables;
	var $joins;
	var $wheres;
	var $orders;
	var $limit;
	var $primarykeys;
	var $autoincrements;
	var $excludes;
	/*Adds a field and a table name*/
	function add_field($fieldname){
		//Default setup
		$table = "";
		$alias = "";
		$exclude = "";
		//Extract the opt args
		switch(func_num_args()){
			case 3:
				$alias = func_get_arg(2);
			case 2:
				$table = func_get_arg(1);
		}
		//Add the field, alias and exclude prop
		$this->fields[] = array($fieldname, $table, $alias);
	}
	/*Adds a table to the query*/
	function add_table($tablename){
		//Add the table to the list of tables
		$this->tables[] = $tablename;
	}
	/*Adds a join to the querry*/
	function add_join($joinon, $sourcetable, $sourcefield, $joinfield){
		//Add the join to the list of joins
		$this->joins[] = array($joinon, $sourcetable, $sourcefield, $joinfield);
	}
	/*Adds a where clause*/
	function add_where($fieldname, $operator, $operant){
		//Default setup
		$binaryop = "";
		$table = "";
		//Extract the opt args
		switch(func_num_args()){
			case 5:
				$table = func_get_arg(4);
			case 4:
				$binaryop = func_get_arg(3);
		}
		//Add the where clause to the list of wheres
		$this->wheres[] = array($binaryop, $table, $fieldname, $operator, $operant);
	}
	/*Adds a order by clause*/
	function add_order($field){
		//Default setup
		$orderby = "ASC";
		$table = "";
		//Extract the opt args
		switch(func_num_args()){
			case 3:
				$table = func_get_arg(2);
			case 2:
				$orderby = func_get_arg(1);
		}
		//Add the order by clause to the list of orders
		$this->orders[] = array($table, $field, $orderby);
	}
	/*Set the limit and page start for the sql query*/
	function add_limit($maxnum){
		//Default setup
		$startat = 0;
		//Extract the opt args
		if(func_num_args() > 1){
			$startat = func_get_arg(1);
		}
		//Set the limit query
		$this->limit = array($maxnum, $startat);
	}
	/*Set the limit and page start for the sql query*/
	function add_param($paramname, $paramvalue){
		//Add the param
		$this->params["$paramname"] = array($paramname, $paramvalue);
	}
	/*Set the primary key for the sql query*/
	function add_primarykey($fieldname, $tablename){
		//Add the primarykey
		$this->primarykeys[] = array($fieldname, $tablename);
	}
	/*Set the autoincrement fields for the sql query*/
	function add_autoincrement($fieldname, $tablename){
		//Add the autoincrement
		$this->autoincrements[] = array($fieldname, $tablename);
	}
	/*Set the exluded fields for the sql query*/
	function add_exclude($fieldname, $tablename){
		//Add the autoincrement
		$this->excludes[] = array($fieldname, $tablename);
	}
	function debug(){
		//Write all debug information
		if(is_array($this->fields)){
			echo "FIELDS:<br>";
			foreach($this->fields as $value) echo $value[0] . ", " . $value[1] . "," . $value[2] . "<br>";
		}
		if(is_array($this->tables)){
			echo "TABLES:<br>";
			foreach($this->tables as $value) echo $value[0] . ", " . "<br>";
		}
		if(is_array($this->joins)){
			echo "JOINS:<br>";
			foreach($this->joins as $value) echo $value[0] . ", " . $value[1] . "," . $value[2] . "," . $value[3] . "<br>";
		}
		if(is_array($this->wheres)){
			echo "WHERES:<br>";
			foreach($this->wheres as $value) echo $value[0] . ", " . $value[1] . "," . $value[2] . "," . $value[3] . "," . $value[4] . "<br>";
		}
		if(is_array($this->orders)){
			echo "ORDER BYS:<br>";
			foreach($this->orders as $value) echo  $value[0] . ", " . $value[1] . "," . $value[2] . "<br>";
		}
		if(is_array($this->limits)){
			echo "LIMITS:<br>";
			foreach($this->limits as $value) echo  $value[0] . ", " . $value[1] . "<br>";
		}
		if(is_array($this->params)){
			echo "PARAMS:<br>";
			foreach($this->params as $value) echo  $value[0] . ", " . $value[1] . "<br>";
		}
		if(is_array($this->primarykeys)){
			echo "PRIMARYS:<br>";
			foreach($this->primarykeys as $value) echo  $value[0] . ", " . $value[1] . "<br>";
		}
		if(is_array($this->autoincrements)){
			echo "AUTOINCREMENTS:<br>";
			foreach($this->autoincrements as $value) echo  $value[0] . ", " . $value[1] . "<br>";
		}
		if(is_array($this->excludes)){
			echo "EXCLUDES:<br>";
			foreach($this->excludes as $value) echo  $value[0] . ", " . $value[1] . "<br>";
		}
	}
	function generate_select(){
		//Setup
		$seperator = "";
		$sql = "SELECT ";
		//Add the fields
		foreach($this->fields as $fieldarray){
			//Setup
			$fieldclause = "";
			//Extract the data
			$fieldname = $fieldarray[0];
			$tablename = $fieldarray[1];
			$fieldalias = $fieldarray[2];
			//Build the sql field clause
			if($tablename){
				$fieldclause = $tablename . ".";
			}
			$fieldclause .= $fieldname;
			if($fieldalias){
				$fieldclause .= " AS " . $fieldalias;
			}
			//Add to the sql select query
			$sql .= $seperator . $fieldclause;
			//Setup
			$seperator = ", ";
		}
		//Setup
		$seperator = "";
		$sql .= " FROM ";
		//Add the tables
		foreach($this->tables as $tablename){
			//Add to the sql select query
			$sql .= $seperator . $tablename;
			//Setup
			$seperator = ", ";
		}
		if(count($this->joins)){
			//Setup
			$seperator = "";
			//Add the joins
			foreach($this->joins as $joinarray){
				//Setup
				$joinclause = "";
				//Extract the data
				$joinon = $joinarray[0];
				$sourcetable = $joinarray[1];
				$sourcefield = $joinarray[2];
				$joinfield = $joinarray[3];
				//Add to the sql select query
				$sql .= " LEFT JOIN " . $joinon . " ON " . $sourcetable . "." . $sourcefield . " = " . $joinon . "." . $joinfield;
			}
		}
		if(count($this->wheres)){
			//Setup
			$sql .= " WHERE ";
			//Add the wheres
			foreach($this->wheres as $wherearray){
				//Setup
				$whereclause = "";
				//Extract the data
				$binaryop = $wherearray[0];
				$tablename = $wherearray[1];
				$fieldname = $wherearray[2];
				$operator = $wherearray[3];
				$operand = $wherearray[4];
				//Build the sql field clause
				if($binaryop){
					$whereclause = $binaryop . " ";
				}
				if($tablename){
					$whereclause .= $tablename . ".";
				}
				$whereclause .= $fieldname . " " . $operator . " " . $operand . " ";
				//Add to the sql select query
				$sql .= $whereclause;
			}
		}
		if(count($this->orders)){
			//Setup
			$seperator = "";
			$sql .= " ORDER BY ";
			//Add the orders
			foreach($this->orders as $orderarray){
				//Setup
				$orderclause = "";
				//Extract the data
				$tablename = $orderarray[0];
				$fieldname = $orderarray[1];
				$orderby = $orderarray[2];
				//Build the sql field clause
				if($tablename){
					$orderclause .= $tablename . ".";
				}
				$orderclause .= $fieldname . " " . $orderby;
				//Add to the sql select query
				$sql .= $seperator . $orderclause;
				//Setup
				$seperator = ", ";
			}
		}
		if(is_array($this->limit)){
			//Setup
			$sql .= " LIMIT ";
			$startat = 0;
			//Get the args
			$maxnum = $this->limit[0];
			$startat = $this->limit[1];
			//Add the args to the sql
			$sql .= $startat . ", " . $maxnum;
		}
		/*Replace the params*/
		if(is_array($this->params)){
			//Substitute de params and values
			foreach($this->params as $paramarray){
				//Substitute de data
				$paramname = $paramarray[0];
				$paramvalue = $paramarray[1];
				//Replace
				$sql = str_replace("$paramname", "$paramvalue", $sql);
			}
		}
		//Return the sql
		return $sql;
	}
	/*Reads a file from a directory and builds all query for you*/
	function query_from_file($filename){
		//Check that the file is there
		if(!is_file($filename)){
			//Exit in fatal error
			exit("Fatal error: File $filename could not be found");
		}
		//Open the file in read only
		$filepointer = fopen($filename, "r");
		//Read all the lines and parse then
		while(!feof($filepointer)){
			//Read a file line (Add a space for search so if <@@> is found it will return 1 instead of 0)
			$line = " " . fgets($filepointer, 1024);
			//Get the type of the line
			if(strpos($line, "<@field@>")){
				//Remove the <@@>
				$line = str_replace("<@field@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_field(trim($params[0]), trim($params[1]), trim($params[2]));
			}elseif(strpos($line, "<@table@>")){
				//Remove the <@@>
				$line = str_replace("<@table@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_table(trim($params[0]));
			}elseif(strpos($line, "<@join@>")){
				//Remove the <@@>
				$line = str_replace("<@join@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_join(trim($params[0]), trim($params[1]), trim($params[2]), trim($params[3]));
			}elseif(strpos($line, "<@where@>")){
				//Remove the <@@>
				$line = str_replace("<@where@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_where(trim($params[0]), trim($params[1]), trim($params[2]), trim($params[3]), trim($params[4]));
			}elseif(strpos($line, "<@order@>")){
				//Remove the <@@>
				$line = str_replace("<@order@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_order(trim($params[0]), trim($params[1]), trim($params[2]));
			}elseif(strpos($line, "<@limit@>")){
				//Remove the <@@>
				$line = str_replace("<@limit@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_limit(trim($params[0]), trim($params[1]));
			}elseif(strpos($line, "<@param@>")){
				//Remove the <@@>
				$line = str_replace("<@param@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_param(trim($params[0]), trim($params[1]));
			}elseif(strpos($line, "<@primary@>")){
				//Remove the <@@>
				$line = str_replace("<@primary@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_primarykey(trim($params[0]), trim($params[1]));
			}elseif(strpos($line, "<@autoincrement@>")){
				//Remove the <@@>
				$line = str_replace("<@autoincrement@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_autoincrement(trim($params[0]), trim($params[1]));
			}elseif(strpos($line, "<@exclude@>")){
				//Remove the <@@>
				$line = str_replace("<@exclude@>", "", $line);
				//Parse the params
				$params = explode(",", $line);
				//Send the params
				$this->add_exclude(trim($params[0]), trim($params[1]));
			}
		}
		//Close the file
		fclose($filepointer);
	}
}
?>