+sub sshpsql ($$) {
+ my ($data,$sql) = @_;
+ $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
+ my ($userhost,$dbname) = ($`,$'); #';
+ my @rows;
+ my @cmd = (access_cfg_ssh, $userhost,
+ "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
+ printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
+ open P, "-|", @cmd or die $!;
+ while (<P>) {
+ chomp or die;
+ printdebug("$debugprefix>|$_|\n");
+ push @rows, $_;
+ }
+ $!=0; $?=0; close P or die "$! $?";
+ @rows or die;
+ my $nrows = pop @rows;
+ $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
+ @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
+ @rows = map { [ split /\|/, $_ ] } @rows;
+ my $ncols = scalar @{ shift @rows };
+ die if grep { scalar @$_ != $ncols } @rows;
+ return @rows;
+}
+
+sub sql_injection_check {
+ foreach (@_) { die "$_ $& ?" if m/[']/; }
+}
+
+sub archive_query_sshpsql ($$) {
+ my ($proto,$data) = @_;
+ sql_injection_check $isuite, $package;
+ my @rows = sshpsql($data, <<END);
+ SELECT source.version, component.name, files.filename
+ FROM source
+ JOIN src_associations ON source.id = src_associations.source
+ JOIN suite ON suite.id = src_associations.suite
+ JOIN dsc_files ON dsc_files.source = source.id
+ JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
+ JOIN component ON component.id = files_archive_map.component_id
+ JOIN files ON files.id = dsc_files.file
+ WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
+ AND source.source='$package'
+ AND files.filename LIKE '%.dsc';
+END
+ @rows = sort { -version_compare_string($a->[0],$b->[0]) } @rows;
+ @rows = map {
+ my ($vsn,$component,$filename) = @$_;
+ [ $vsn, "/pool/$component/$filename" ];
+ } @rows;
+ return @rows;
+}
+
+sub canonicalise_suite_sshpsql ($$) {
+ my ($proto,$data) = @_;
+ sql_injection_check $isuite;
+ my @rows = sshpsql($data, <<END);
+ SELECT suite.codename
+ FROM suite where suite_name='$isuite' or codename='$isuite';
+END
+ @rows = map { $_->[0] } @rows;
+ fail "unknown suite $isuite" unless @rows;
+ die "ambiguous $isuite: @rows ?" if @rows>1;
+ return $rows[0];
+}
+
+sub canonicalise_suite_dummycat ($$) {
+ my ($proto,$data) = @_;
+ my $dpath = "$data/suite.$isuite";
+ if (!open C, "<", $dpath) {
+ $!==ENOENT or die "$dpath: $!";
+ printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
+ return $isuite;
+ }
+ $!=0; $_ = <C>;
+ chomp or die "$dpath: $!";
+ close C;
+ printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
+ return $_;
+}
+
+sub archive_query_dummycat ($$) {
+ my ($proto,$data) = @_;
+ canonicalise_suite();
+ my $dpath = "$data/package.$csuite.$package";
+ if (!open C, "<", $dpath) {
+ $!==ENOENT or die "$dpath: $!";
+ printdebug "dummycat query $csuite $package $dpath ENOENT\n";
+ return ();
+ }
+ my @rows;
+ while (<C>) {
+ next if m/^\#/;
+ next unless m/\S/;
+ die unless chomp;
+ printdebug "dummycat query $csuite $package $dpath | $_\n";
+ my @row = split /\s+/, $_;
+ @row==2 or die "$dpath: $_ ?";
+ push @rows, \@row;
+ }
+ C->error and die "$dpath: $!";
+ close C;
+ return sort { -version_compare_string($a->[0],$b->[0]); } @rows;
+}
+