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!',
58 # test insert() and values() for reentrancy
59 my($insert_hash, $insert_array, $numfields);
60 my $a_sql = SQL::Abstract->new;
61 my $h_sql = SQL::Abstract->new;
63 for my $record (@data) {
65 my $values = [ map { $record->{$_} } sort keys %$record ];
67 my ($h_stmt, @h_bind) = $h_sql->insert('h_table', $record);
68 my ($a_stmt, @a_bind) = $a_sql->insert('a_table', $values );
70 # init from first run, should not change afterwards
71 $insert_hash ||= $h_stmt;
72 $insert_array ||= $a_stmt;
73 $numfields ||= @$values;
75 is ( $a_stmt, $insert_array, 'Array-based insert statement unchanged' );
76 is ( $h_stmt, $insert_hash, 'Hash-based insert statement unchanged' );
78 is_deeply ( \@a_bind, \@h_bind, 'Bind values match after both insert() calls' );
79 is_deeply ( [$h_sql->values ($record)] , \@h_bind, 'values() output matches bind values after insert()' );
81 is ( scalar @h_bind, $numfields, 'Number of fields unchanged' );
84 # test values() with literal sql
87 # The example is deliberately complicated by the addition of a literal ? in xfunc
88 # This is an intentional test making sure literal ? remains untouched.
89 # It is rather impractical in the field, as the user will have to insert
90 # a bindvalue for the literal position(s) in the correct offset of \@bind
92 my $sql = SQL::Abstract->new;
98 xfunc => \ 'xfunc(?)',
99 yfunc => ['yfunc(?)', 'ystuff' ],
100 zfunc => \['zfunc(?)', 'zstuff' ],
104 my ($stmt, @bind) = $sql->insert ('table', $data);
109 'INSERT INTO table ( event, stuff, time, xfunc, yfunc, zfunc, zzlast) VALUES ( ?, ?, now(), xfunc (?), yfunc(?), zfunc(?), ? )',
110 [qw/rapture fluff ystuff zstuff zzstuff/], # event < stuff
114 [$sql->values ($data)],
116 'values() output matches that of initial bind'
117 ) || diag "Corresponding SQL statement: $stmt";