<commit_before>#!/usr/bin/env perl

# perl -MCPAN -e 'install XML::Writer'

# http://search.cpan.org/~abigail/Regexp-Common-2016020301/lib/Regexp/Common.pm

# Une fonction peut ne pas avoir de valeur de retour
#CREATE FUNCTION sales_tax(subtotal real, OUT tax real) AS $$
#BEGIN
#    tax := subtotal * 0.06;
#END;
#$$ LANGUAGE plpgsql;

use strict;
use warnings;
use Getopt::Long;
use File::Path;
use Data::Dumper;
use XML::Writer;
use IO::File;
use File::Basename;
use Configuration;
use PgXMLExporter;
use SqlDatabase;
use SqlFunction;

use utf8;

my $VERSION = '0.1';

my (@requestFiles,@cursorFiles);
my (@userFunctionsList,%invokedFunctions);
my ($model,$conf);

sub version {
	print "Alatar version $VERSION\n\n";
	exit 1;
}

sub loadSQLSchema {
	my ($filePath) = @_;
	my $data;
	
	open(DATA,Configuration->getOption('schemaPath')) || die "You must specify a SQL schema";
	while( defined( my $l = <DATA> ) ) {
   		$data = $data . $l;
	}
	close(DATA);
	
	return $data;
}

sub cleanSchema {
	my ($data) = @_;
	# remove comments with --
	$data =~ s/--//g;
	# replace newlines and tabulations with a blank character
	$data =~ s/[\t\n\r]+/ /g;
	# remove comments with /* .. */
	$data =~ s/\/\*.*?\*\///g;
	return $data;
}

sub saveRequest {
	my ($destFile,$request) = @_;
	open(FD,'>',$destFile) or die("The SQL request can't be exported");
	print(FD $request);
	close(FD);
}

# protege les parentheses et les virgules
# pour obtenir un chemin d'acces utilisable
sub protectPath {
	my ($path) = @_;
	$path =~ s/\(/\\(/;
	$path =~ s/\)/\\)/;
	$path =~ s/,/\\,/;
	
	return $path;
}

# save request on disk
sub saveRequests {
	my $dest;
	foreach my $r ($model->getSqlRequests()) {
		$dest = Configuration->getOption('requestsPath') . $REQUESTS_FOLDER . '/' . $r->getName() . '.sql';
		push(@requestFiles,$r->getName());
		saveRequest($dest,$r->getRequest());
	}
	foreach my $r ($model->getSqlCursorRequests()) {
		$dest = Configuration->getOption('requestsPath') . $CURSORS_FOLDER . '/' . $r->getName() . '.sql';
		push(@cursorFiles,$r->getName());
		saveRequest($dest,$r->getRequest());
	}
}

sub formatWithCRLF {
	my (@data) = @_;
	my $result = '';
	foreach my $item (@data) {
		$result = $result . $item . "\n\n";
	}
	return $result;
}

# add an element in the compact schema file
sub addToCompactSchema {
	my ($fd,@data) = @_;
	foreach my $item (@data) {
		print($fd ($item . "\n\n"));
	}
}

# Produce a compacted schema
sub createCompactSchema {
	my ($destPath,$schema) = @_;
	my $fd;
	my @items;
	<commit_msg>Removing REQUESTS and CURSORS folder constants<commit_after>#!/usr/bin/env perl

# perl -MCPAN -e 'install XML::Writer'

# http://search.cpan.org/~abigail/Regexp-Common-2016020301/lib/Regexp/Common.pm

# Une fonction peut ne pas avoir de valeur de retour
#CREATE FUNCTION sales_tax(subtotal real, OUT tax real) AS $$
#BEGIN
#    tax := subtotal * 0.06;
#END;
#$$ LANGUAGE plpgsql;

use strict;
use warnings;
use Getopt::Long;
use File::Path;
use Data::Dumper;
use XML::Writer;
use IO::File;
use File::Basename;
use Configuration;
use PgXMLExporter;
use SqlDatabase;
use SqlFunction;

use utf8;

my $VERSION = '0.1';

my (@requestFiles,@cursorFiles);
my (@userFunctionsList,%invokedFunctions);
my ($model,$conf);

sub version {
	print "Alatar version $VERSION\n\n";
	exit 1;
}

sub loadSQLSchema {
	my ($filePath) = @_;
	my $data;
	
	open(DATA,Configuration->getOption('schemaPath')) || die "You must specify a SQL schema";
	while( defined( my $l = <DATA> ) ) {
   		$data = $data . $l;
	}
	close(DATA);
	
	return $data;
}

sub cleanSchema {
	my ($data) = @_;
	# remove comments with --
	$data =~ s/--//g;
	# replace newlines and tabulations with a blank character
	$data =~ s/[\t\n\r]+/ /g;
	# remove comments with /* .. */
	$data =~ s/\/\*.*?\*\///g;
	return $data;
}

sub saveRequest {
	my ($destFile,$request) = @_;
	open(FD,'>',$destFile) or die("The SQL request can't be exported");
	print(FD $request);
	close(FD);
}

# protege les parentheses et les virgules
# pour obtenir un chemin d'acces utilisable
sub protectPath {
	my ($path) = @_;
	$path =~ s/\(/\\(/;
	$path =~ s/\)/\\)/;
	$path =~ s/,/\\,/;
	
	return $path;
}

# save request on disk
sub saveRequests {
	my $dest;
	foreach my $r ($model->getSqlRequests()) {
		$dest = Configuration->getOption('requestsPath') . Configuration->getOption('requests_folder') . '/' . $r->getName() . '.sql';
		push(@requestFiles,$r->getName());
		saveRequest($dest,$r->getRequest());
	}
	foreach my $r ($model->getSqlCursorRequests()) {
		$dest = Configuration->getOption('requestsPath') . Configuration->getOption('cursors_folder') . '/' . $r->getName() . '.sql';
		push(@cursorFiles,$r->getName());
		saveRequest($dest,$r->getRequest());
	}
}

sub formatWithCRLF {
	my (@data) = @_;
	my $result = '';
	foreach my $item (@data) {
		$result = $result . $item . "\n\n";
	}
	return $result;
}

# add an element in the compact schema file
sub addToCompactSchema {
	my ($fd,@data) = @_;
	foreach my $item (@data) {
		print($fd ($item . "\n\n"));
	}
}

# Produce a compacted schema
sub createCompactSchema {
	my ($destPath,$schema) = @_;
	my $fd;
	my @items;
	
{'fraction_non_alphanumeric': 0.190811339198436, 'fraction_numerical': 0.006647116324535679, 'mean_word_length': 3.8723809523809525, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '3539014', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1906, 'n_words': 493}