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