<commit_before><?php

class QUERYview {
	public $sql_query;
	public $filtered_sql_query;
	public $limited_sql_query;

	//a uniq id that is given to thw view based on the query
	public $viewIdentifier;
	//how many rows to show
	public $limit = 200;

	//the spliter that is used in the csv files
	public $csv_split = ',';

	//Maps colun numers to column names and the reverse
	public $col2num = array();
	public $num2col = array();

	//The columns we will use to order and the direction to order them by
	public $orders = array();

	//If the selection is to be inverted
	public $invert = array();

	//If the selection is to be queryied with and instead of or
	public $and = array();

	//The columns we will filter in and the value we want to filter
	public $filters = array();
	//are we trying to create an export file
	public $export = false;
	//The url path where we can view the table
	public $path = '';
	//information about the column in the databse
	public $column_metas = array();
	//number of rows the query returns when we have no filters taken from the last non filterd execution
	public $estimated_num_rows = 0;
	//The name that each column gets in the GUI
	public $visible_column_names = array();
	//The columns from the query that we show the user
	public $visible_columns = array();

	//The rows that the query returned
	public $rows = array();

	//functions to change the value of a column in the table (to make a value clickable with the sue of a <a>)
	public $html_col_map = array();

	//functions to chnage the value of a column in the table
	public $col_map = array();
	//functions to chnage the value of a column in the select boxes
	public $col_map_distinct = array();
	//functions or values that map to other values in the database
	public $col_sql_map = array();

	//prints a csv file with the featched files
	public function csv($Split = ','){
		$outstream = fopen("php://temp", 'r+');

		//get and write column names
		$column_names = array();
		foreach($this->visible_columns AS $column_name){
			$ColVisibleName = $column_name;
			if(isset($this->col2num[$column_name])){
				if(isset($this->visible_column_names[$column_name])){
					$ColVisibleName = $this->visible_column_names[$column_name];
				}
				$column_names[] = $ColVisibleName;
			}
		}
		fputcsv($outstream, $column_names, $Split, '"');
		foreach($this->rows as $dataRow){
			$visRow = array();
			foreach($this->visible_columns AS $column_name){
				if(isset($this->col2num[$column_name])){

					//map the values
					$colval = $dataRow[$this->col2num[$column_name]];
					if(isset($this->col_map[$column_name])){
						$colval = $this->col_map[$column_name]($colval);
					}
					$visRow[] = $colval;
				}
			}
			fputcsv($outstream, $visRow, $Split, '"');
		}
		rewind($outstream);
		$csv = '';
		while(!feof($outstream)){
			echo(fgets($outstream));
		}
		fclose($outstream);
	}

	//handles any input from the browser and initiates the object
	public function __construct($path ,$sql_query){
		global $TABLE_Classes;
		$this->path = $path;
		$this->sql_query = $sql_query;
		$this->viewIdentifier = crc32('query_'.md5($this->sql_query));
		$this->filtered_sql_query = $this->sql_query;

		if(!isset($_SESSION['query_view'])){
			$_SESSION['query_view'] = array();
		}
		if(!isset($_SESSION['query_view'][$this->viewIdentifier])){
			$_SESSION['query_view'][$this->viewIdentifier] = array();
		}



		//Add filters
		if(isset($_POST[$this->viewIdentifier])){

		}
		if(isset($_GET[$this->viewIdentifier])){

			if(isset($_GET[$this->viewIdentifier]['invert'])){
				foreach($_GET[$this->viewIdentifier]['invert'] AS $column => $invert){
					if($invert == 'inv'){
						$this->invert[$column] = true;
					}
				}
			}
			if(isset($_GET[$this->viewIdentifier]['and'])){
				foreach($_GET[$this->viewIdentifier]['and'] AS $column => $and){
					if($and == 'and'){
						$this->and[$column] = true;
					}
				}
			}
			if(isset($_GET[$this->viewIdentifier]['order'])){
				foreach($_GET[$this->viewIdentifier]['order'] AS $column => $direction){
					if($direction == 'ASC' || $direction == 'DESC'){
						$this->orders[$column] = $direction;
					}
				}
			}
			if(isset($_GET[$this->viewIdentifier]['filter'])){
				foreach($_GET[$this->viewIdentifier]['filter'] AS $column => $value){

					//if we have an array of diffrent values
					if(is_array($value)){
						$vals = $value;
					}else{
						$vals = array($value);
					}

					$col_filts = array();
					foreach($vals AS $val){
						$col_filts[] = $val;
					}
					if(count($col_filts) != 0){
						$this->filters[$column] = $col_filts;
					}
				}
			}
			if(isset($_GET[$this->viewIdentifier]['csv_export'])){
				$this->limit = 0;
				$this->export = 'csv';
			}
			if(isset($_GET[$this->viewIdentifier]['nolimit'])){
				$this->limit = 0;
			}
		}
	}

	//Creates the final query and executes it spits out the export if that was what the user wanted
	public function handle_exec(){
		global $MY_SQL_Handle;

		//Load the column metadata
		$this->load_column_metadata();

		$filtsql = explode('GROUP BY', $this->filtered_sql_query);
		if(count($this->filters) != 0){
			$filter_sql = array();
			foreach($this->filters AS $column => $vals){
				$filter_or_sql = array();
				foreach($vals AS $user_filter){
					if(isset($this->col_sql_map[$column])){
						$filter_or_sql[] = $this->col_sql_map[$column]($user_filter);
					}else{
						$filter_or_sql[] = $column." = '".$MY_SQL_Handle->real_escape_string($user_filter)."'";
					}
				}
				$ImplodeJoiner = ' OR ';
				if(isset($this->and[$column]) && $this->and[$column]){
					$ImplodeJoiner = ' AND ';
				}
				if(isset($this->invert[$column]) && $this->invert[$column]){
					$ImplodeJoiner = ' AND ';<commit_msg>Fixed issue when inverted and anded<commit_after><?php

class QUERYview {
	public $sql_query;
	public $filtered_sql_query;
	public $limited_sql_query;

	//a uniq id that is given to thw view based on the query
	public $viewIdentifier;
	//how many rows to show
	public $limit = 200;

	//the spliter that is used in the csv files
	public $csv_split = ',';

	//Maps colun numers to column names and the reverse
	public $col2num = array();
	public $num2col = array();

	//The columns we will use to order and the direction to order them by
	public $orders = array();

	//If the selection is to be inverted
	public $invert = array();

	//If the selection is to be queryied with and instead of or
	public $and = array();

	//The columns we will filter in and the value we want to filter
	public $filters = array();
	//are we trying to create an export file
	public $export = false;
	//The url path where we can view the table
	public $path = '';
	//information about the column in the databse
	public $column_metas = array();
	//number of rows the query returns when we have no filters taken from the last non filterd execution
	public $estimated_num_rows = 0;
	//The name that each column gets in the GUI
	public $visible_column_names = array();
	//The columns from the query that we show the user
	public $visible_columns = array();

	//The rows that the query returned
	public $rows = array();

	//functions to change the value of a column in the table (to make a value clickable with the sue of a <a>)
	public $html_col_map = array();

	//functions to chnage the value of a column in the table
	public $col_map = array();
	//functions to chnage the value of a column in the select boxes
	public $col_map_distinct = array();
	//functions or values that map to other values in the database
	public $col_sql_map = array();

	//prints a csv file with the featched files
	public function csv($Split = ','){
		$outstream = fopen("php://temp", 'r+');

		//get and write column names
		$column_names = array();
		foreach($this->visible_columns AS $column_name){
			$ColVisibleName = $column_name;
			if(isset($this->col2num[$column_name])){
				if(isset($this->visible_column_names[$column_name])){
					$ColVisibleName = $this->visible_column_names[$column_name];
				}
				$column_names[] = $ColVisibleName;
			}
		}
		fputcsv($outstream, $column_names, $Split, '"');
		foreach($this->rows as $dataRow){
			$visRow = array();
			foreach($this->visible_columns AS $column_name){
				if(isset($this->col2num[$column_name])){

					//map the values
					$colval = $dataRow[$this->col2num[$column_name]];
					if(isset($this->col_map[$column_name])){
						$colval = $this->col_map[$column_name]($colval);
					}
					$visRow[] = $colval;
				}
			}
			fputcsv($outstream, $visRow, $Split, '"');
		}
		rewind($outstream);
		$csv = '';
		while(!feof($outstream)){
			echo(fgets($outstream));
		}
		fclose($outstream);
	}

	//handles any input from the browser and initiates the object
	public function __construct($path ,$sql_query){
		global $TABLE_Classes;
		$this->path = $path;
		$this->sql_query = $sql_query;
		$this->viewIdentifier = crc32('query_'.md5($this->sql_query));
		$this->filtered_sql_query = $this->sql_query;

		if(!isset($_SESSION['query_view'])){
			$_SESSION['query_view'] = array();
		}
		if(!isset($_SESSION['query_view'][$this->viewIdentifier])){
			$_SESSION['query_view'][$this->viewIdentifier] = array();
		}



		//Add filters
		if(isset($_POST[$this->viewIdentifier])){

		}
		if(isset($_GET[$this->viewIdentifier])){

			if(isset($_GET[$this->viewIdentifier]['invert'])){
				foreach($_GET[$this->viewIdentifier]['invert'] AS $column => $invert){
					if($invert == 'inv'){
						$this->invert[$column] = true;
					}
				}
			}
			if(isset($_GET[$this->viewIdentifier]['and'])){
				foreach($_GET[$this->viewIdentifier]['and'] AS $column => $and){
					if($and == 'and'){
						$this->and[$column] = true;
					}
				}
			}
			if(isset($_GET[$this->viewIdentifier]['order'])){
				foreach($_GET[$this->viewIdentifier]['order'] AS $column => $direction){
					if($direction == 'ASC' || $direction == 'DESC'){
						$this->orders[$column] = $direction;
					}
				}
			}
			if(isset($_GET[$this->viewIdentifier]['filter'])){
				foreach($_GET[$this->viewIdentifier]['filter'] AS $column => $value){

					//if we have an array of diffrent values
					if(is_array($value)){
						$vals = $value;
					}else{
						$vals = array($value);
					}

					$col_filts = array();
					foreach($vals AS $val){
						$col_filts[] = $val;
					}
					if(count($col_filts) != 0){
						$this->filters[$column] = $col_filts;
					}
				}
			}
			if(isset($_GET[$this->viewIdentifier]['csv_export'])){
				$this->limit = 0;
				$this->export = 'csv';
			}
			if(isset($_GET[$this->viewIdentifier]['nolimit'])){
				$this->limit = 0;
			}
		}
	}

	//Creates the final query and executes it spits out the export if that was what the user wanted
	public function handle_exec(){
		global $MY_SQL_Handle;

		//Load the column metadata
		$this->load_column_metadata();

		$filtsql = explode('GROUP BY', $this->filtered_sql_query);
		if(count($this->filters) != 0){
			$filter_sql = array();
			foreach($this->filters AS $column => $vals){
				$filter_or_sql = array();
				foreach($vals AS $user_filter){
					if(isset($this->col_sql_map[$column])){
						$filter_or_sql[] = $this->col_sql_map[$column]($user_filter);
					}else{
						$filter_or_sql[] = $column." = '".$MY_SQL_Handle->real_escape_string($user_filter)."'";
					}
				}
				$ImplodeJoiner = ' OR ';
				if(isset($this->and[$column]) && $this->and[$column]){
					$ImplodeJoiner = ' AND ';
				}
				if(isset($this->invert[$column]) && $this->invert[$column]){
					$ImplodeJoiner = ' AND ';
					if(isset($this->and[$column]) && $this->and[$column]){
						$ImplodeJoiner = ' OR ';
					}
{'fraction_non_alphanumeric': 0.16568813676394406, 'fraction_numerical': 0.0027629079606285617, 'mean_word_length': 3.7104514030093534, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4067628', 'n_tokens_mistral': 4485, 'n_tokens_neox': 3779, 'n_words': 1001}