5 use SQL::Abstract::Test import => [qw/is_same_sql_bind is_same_bind/];
12 name => 'Nathan Wiger',
13 phone => '123-456-7890',
14 addr => 'Yeah, right',
21 name => 'Jimbo Bobson',
22 phone => '321-456-0987',
30 name => 'Mr. Garrison',
31 phone => '123-456-7890',
40 phone => '1-800-Sucky-Sucky',
41 addr => 'Mr. Garrison',
47 user => 'barbara_streisand',
48 name => 'MechaStreisand!',
56 # test insert() and values() for reentrancy
57 my($insert_hash, $insert_array, $numfields);
58 my $a_sql = SQL::Abstract->new;
59 my $h_sql = SQL::Abstract->new;
61 for my $record (@data) {
63 my $values = [ map { $record->{$_} } sort keys %$record ];
65 my ($h_stmt, @h_bind) = $h_sql->insert('h_table', $record);
66 my ($a_stmt, @a_bind) = $a_sql->insert('a_table', $values );
68 # init from first run, should not change afterwards
69 $insert_hash ||= $h_stmt;
70 $insert_array ||= $a_stmt;
71 $numfields ||= @$values;
73 is ( $a_stmt, $insert_array, 'Array-based insert statement unchanged' );
74 is ( $h_stmt, $insert_hash, 'Hash-based insert statement unchanged' );
76 is_deeply ( \@a_bind, \@h_bind, 'Bind values match after both insert() calls' );
77 is_deeply ( [$h_sql->values($record)] , \@h_bind, 'values() output matches bind values after insert()' );
79 is ( scalar @h_bind, $numfields, 'Number of fields unchanged' );
82 # test values() with literal sql
85 # The example is deliberately complicated by the addition of a literal ? in xfunc
86 # This is an intentional test making sure literal ? remains untouched.
87 # It is rather impractical in the field, as the user will have to insert
88 # a bindvalue for the literal position(s) in the correct offset of \@bind
90 my $sql = SQL::Abstract->new;
96 xfunc => \ 'xfunc(?)',
97 yfunc => ['yfunc(?)', 'ystuff' ],
98 zfunc => \['zfunc(?)', 'zstuff' ],
102 my ($stmt, @bind) = $sql->insert('table', $data);
107 'INSERT INTO table ( event, stuff, time, xfunc, yfunc, zfunc, zzlast) VALUES ( ?, ?, now(), xfunc (?), yfunc(?), zfunc(?), ? )',
108 [qw/rapture fluff ystuff zstuff zzstuff/], # event < stuff
112 [$sql->values($data)],
114 'values() output matches that of initial bind'
115 ) || diag "Corresponding SQL statement: $stmt";