7 use SQL::Abstract::Test import => [qw/is_same_sql_bind is_same_bind/];
14 name => 'Nathan Wiger',
15 phone => '123-456-7890',
16 addr => 'Yeah, right',
23 name => 'Jimbo Bobson',
24 phone => '321-456-0987',
32 name => 'Mr. Garrison',
33 phone => '123-456-7890',
42 phone => '1-800-Sucky-Sucky',
43 addr => 'Mr. Garrison',
49 user => 'barbara_streisand',
50 name => 'MechaStreisand!',
59 plan tests => (@data * 5 + 2);
61 # test insert() and values() for reentrancy
62 my($insert_hash, $insert_array, $numfields);
63 my $a_sql = SQL::Abstract->new;
64 my $h_sql = SQL::Abstract->new;
66 for my $record (@data) {
68 my $values = [ map { $record->{$_} } sort keys %$record ];
70 my ($h_stmt, @h_bind) = $h_sql->insert('h_table', $record);
71 my ($a_stmt, @a_bind) = $a_sql->insert('a_table', $values );
73 # init from first run, should not change afterwards
74 $insert_hash ||= $h_stmt;
75 $insert_array ||= $a_stmt;
76 $numfields ||= @$values;
78 is ( $a_stmt, $insert_array, 'Array-based insert statement unchanged' );
79 is ( $h_stmt, $insert_hash, 'Hash-based insert statement unchanged' );
81 is_deeply ( \@a_bind, \@h_bind, 'Bind values match after both insert() calls' );
82 is_deeply ( [$h_sql->values ($record)] , \@h_bind, 'values() output matches bind values after insert()' );
84 is ( scalar @h_bind, $numfields, 'Number of fields unchanged' );
87 # test values() with literal sql
90 # The example is deliberately complicated by the addition of a literal ? in xfunc
91 # This is an intentional test making sure literal ? remains untouched.
92 # It is rather impractical in the field, as the user will have to insert
93 # a bindvalue for the literal position(s) in the correct offset of \@bind
95 my $sql = SQL::Abstract->new;
101 xfunc => \ 'xfunc(?)',
102 yfunc => ['yfunc(?)', 'ystuff' ],
103 zfunc => \['zfunc(?)', 'zstuff' ],
107 my ($stmt, @bind) = $sql->insert ('table', $data);
112 'INSERT INTO table ( event, stuff, time, xfunc, yfunc, zfunc, zzlast) VALUES ( ?, ?, now(), xfunc (?), yfunc(?), zfunc(?), ? )',
113 [qw/rapture fluff ystuff zstuff zzstuff/], # event < stuff
117 [$sql->values ($data)],
119 'values() output matches that of initial bind'
120 ) || diag "Corresponding SQL statement: $stmt";