comparison DBD/t/12bindplaceholder.t @ 54:b0ac51c36919

Properly handle bound parameters with question marks in them
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Fri, 14 Oct 2022 17:24:58 +0200 (2022-10-14)
parents
children
comparison
equal deleted inserted replaced
53:a2a23aa2bf8e 54:b0ac51c36919
1 #!perl -I./t
2
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 #
7 # Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
8
9 $| = 1;
10
11 use strict;
12 use warnings;
13 use DBI();
14 use DBD_TEST();
15
16 use Test::More;
17
18 if (defined $ENV{DBI_DSN}) {
19 plan tests => 9;
20 } else {
21 plan skip_all => 'Cannot test without DB info';
22 }
23
24 my $dbh = DBI->connect or die "Connect failed: $DBI::errstr\n";
25 ok ( defined $dbh, 'Connection');
26
27 my $do_execute = 0;
28
29 sub process
30 {
31 my $query = shift;
32 my $sth = $dbh->prepare($query);
33
34 my $expected = $sth->{NUM_OF_PARAMS};
35 my @params = @_;
36 my $nparams = @params;
37 if ($nparams != $expected) {
38 print "# expected $expected parameters, got $nparams\n";
39 return undef;
40 }
41
42 return 1 unless $do_execute;
43
44 print("# EXECUTE $query");
45 print("# PARMS ", join('|', @params)) if @params;
46 $sth->execute(@params);
47 my @row;
48 while (@row = $sth->fetchrow_array()) {
49 print("# ROW ", join(' | ', @row), '\n');
50 }
51
52 return 1;
53 }
54
55
56 ok( process("SELECT 42"), 'no placeholders');
57
58 ok( process("SELECT ?", 42), 'one placeholder');
59
60 ok( process("-- '?\nSELECT 42"), 'not a real placeholder, is in a comment');
61
62 ok( process("-- '?\nSELECT ?", 42), 'commented placeholder and real placeholder');
63
64 ok( process("SELECT 42 -- ?"), 'commented placeholder at end');
65
66 ok( process("SELECT 42 --?\nWHERE TRUE"), 'commented placeholder, then more query');
67
68 ok( process("SELECT R'\\' ?", 'foo'), 'sdf');
69
70 ok( process("SELECT '\\' ?'"), 'not fooled by the backslash escape');