<commit_before><?php

/**
 * PostgresDumper 
 * 
 * @package Iceline.config 
 * @author oab1 / <NAME> / <EMAIL>  
 * @license AGPL {@link http://www.gnu.org/licenses/agpl-3.0.html}
 */
abstract class PostgresSchemaDumper {
	protected $conn;
	protected $fieldBuffer;

	/**
	 * __construct ~ this eats a connection, to avoid lots of management on socket creation.
	 * 
	 * @param resource $r ~ this is the type that the docs said, i think this needs testing 
	 * @return <self>
	 */
	function __construct(resource $c, Stream $o) {
		$this->conn=$c;
		$this->fieldBuffer=[];
		$this->out=$o;
	}

	public static simpleCreatePostgres(string $h, string $d, string $u, string $p):Resource {
		return pg_connect("host=$h dbname=$d user=$u password=$p" );
	}

// move tables with no foreign keys first
	public abstract tableSort($in):array;

	public abstract limitClause(string $table):string;


	private function read($sql):array {
		$res=pg_query($this->conn, $sql);
		$LENGTH =pg_num_rows($res);
		$i=0; $out=[]; 
		while( $i<$LENGTH) {
			$out[]=pg_fetch_array($res, $i, PGSQL_ASSOC );
			$i++;
		}
		return $out;
	}
	
	private function populateFields(string $table) {
		$schema=pg_meta_data($this->conn, $table);
		$this->fieldBuffer=array_keys($schema);	
	}

	private function listTables():array {
		$sql=<<<EOSQL
SELECT table_name 
FROM information_schema.tables 
WHERE table_schema='public' ;
EOSQL;
		$aa=$this->read($sql);
		$aa=array_map(function($a) { return $a['table_name']; }, $aa);
		return $this->tableSort($aa);
	}
	
	private function generateSearchQuery(string $table):string {
		$where=$this->limitClause($table);
		if($where) {
			$sql="select ".implode(", ", $this->fieldBuffer)." from $table where $where";
		} else {
			$sql="select ".implode(", ", $this->fieldBuffer)." from $table ";
		}
		return $sql;
	}

	private function generateInsertSQL(string $table):string {
		return "\ninsert into $tables (".implode(", ", $this->fieldBuffer).") values ";
	}

	private function convertDataToValues(array $values):string {
		$sql="";
		while( $i<$LENGTH) {
			$rr=array_values($values[$i]);
			$rr=array_map(
				function($a) { 
					if(is_numeric($a)) { 
						return $a; 
					} else { 
						return "'".pg_escape_string($a)."'";
					 }
				 },
				 $rr);
			if($i>0) { $sql.=","; }

			$sql.="\n( ".implode(", ", $rr ) .")";
			$i++;
		}
		return $sql;
	}

	public function process() {
		$sql="";
		$tables=$this->listTables();
		foreach($tables as $v) {
			$this->populateFields($v);
			$aRS=$this->read($this->generateSearchQuery($v));

			$sql.=$this->generateInsertSQL($v )
				.$this->convertDataToValues($aRS)
				.";\n\n";
		}
		fwrite($this->out, $sql, strlen($sql));
	}
}


<commit_msg>Fix errors caused by writing JS ES6 syntax to a PHP interpreter<commit_after><?php

/**
 * PostgresDumper 
 * 
 * @package Iceline.config 
 * @author oab1 / <NAME> / <EMAIL>  
 * @license AGPL {@link http://www.gnu.org/licenses/agpl-3.0.html}
 */
abstract class PostgresSchemaDumper {
	protected $conn;
	protected $fieldBuffer;

	/**
	 * __construct ~ this eats a connection, to avoid lots of management on socket creation.
	 * 
	 * @param Resource $r ~ this is the type that the docs said, i think this needs testing 
	 * @param Stream $o ~ where to put the output SQL
	 * @return <self>
	 */
	function __construct(Resource $c, Stream $o) {
		$this->conn=$c;
		$this->fieldBuffer=[];
		$this->out=$o;
	}

	public static function simpleCreatePostgres(string $h, string $d, string $u, string $p):Resource {
		return pg_connect("host=$h dbname=$d user=$u password=$p" );
	}

// move tables with no foreign keys first
	public abstract function tableSort($in):array;

	public abstract function limitClause(string $table):string;


	private function read($sql):array {
		$res=pg_query($this->conn, $sql);
		$LENGTH =pg_num_rows($res);
		$i=0; $out=[]; 
		while( $i<$LENGTH) {
			$out[]=pg_fetch_array($res, $i, PGSQL_ASSOC );
			$i++;
		}
		return $out;
	}
	
	private function populateFields(string $table) {
		$schema=pg_meta_data($this->conn, $table);
		$this->fieldBuffer=array_keys($schema);	
	}

	private function listTables():array {
		$sql=<<<EOSQL
SELECT table_name 
FROM information_schema.tables 
WHERE table_schema='public' ;
EOSQL;
		$aa=$this->read($sql);
		$aa=array_map(function($a) { return $a['table_name']; }, $aa);
		return $this->tableSort($aa);
	}
	
	private function generateSearchQuery(string $table):string {
		$where=$this->limitClause($table);
		if($where) {
			$sql="select ".implode(", ", $this->fieldBuffer)." from $table where $where";
		} else {
			$sql="select ".implode(", ", $this->fieldBuffer)." from $table ";
		}
		return $sql;
	}

	private function generateInsertSQL(string $table):string {
		return "\ninsert into $tables (".implode(", ", $this->fieldBuffer).") values ";
	}

	private function convertDataToValues(array $values):string {
		$sql="";
		while( $i<$LENGTH) {
			$rr=array_values($values[$i]);
			$rr=array_map(
				function($a) { 
					if(is_numeric($a)) { 
						return $a; 
					} else { 
						return "'".pg_escape_string($a)."'";
					 }
				 },
				 $rr);
			if($i>0) { $sql.=","; }

			$sql.="\n( ".implode(", ", $rr ) .")";
			$i++;
		}
		return $sql;
	}

	public function process() {
		$sql="";
		$tables=$this->listTables();
		foreach($tables as $v) {
			$this->populateFields($v);
			$aRS=$this->read($this->generateSearchQuery($v));

			$sql.=$this->generateInsertSQL($v )
				.$this->convertDataToValues($aRS)
				.";\n\n";
		}
		fwrite($this->out, $sql, strlen($sql));
// should I do a close here or not?
// as it was created outside of this scope, I err to not
	}
}



{'fraction_non_alphanumeric': 0.18697888948021998, 'fraction_numerical': 0.001951392584708178, 'mean_word_length': 3.8771626297577853, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3716978', 'n_tokens_mistral': 2220, 'n_tokens_neox': 1972, 'n_words': 447}