File: 09_create_function.t

package info (click to toggle)
libdbd-sqlite3-perl 1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,004 kB
  • sloc: ansic: 167,715; perl: 1,788; pascal: 277; makefile: 9
file content (160 lines) | stat: -rw-r--r-- 5,390 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use 5.00503;
use strict;
use warnings;
use lib "t/lib";
use SQLiteTest qw/connect_ok @CALL_FUNCS/;
use Test::More;
use if -d ".git", "Test::FailWarnings";
use DBD::SQLite;
use DBD::SQLite::Constants;
use Digest::MD5 qw/md5/;
use DBI qw/:sql_types/;

my @function_flags = (undef, 0);
if ($DBD::SQLite::sqlite_version_number >= 3008003) {
  push @function_flags, DBD::SQLite::Constants::SQLITE_DETERMINISTIC;
}

sub now {
    return time();
}

sub add2 {
    my ( $a, $b ) = @_;
    return $a + $b;
}

sub my_sum {
    my $sum = 0;
    foreach my $x (@_) {
        $sum += $x;
    }
    return $sum;
}

sub error {
    die "function is dying: ", @_, "\n";
}

sub void_return {
}

sub return2 {
    return ( 1, 2 );
}

sub return_null {
    return undef;
}

sub my_defined {
    defined($_[0]) ? 1 : 0;
}

sub noop {
    return $_[0];
}

sub md5_text {
    return md5($_[0]);
}

sub md5_blob {
    return [md5($_[0]), SQL_BLOB];
}

foreach my $call_func (@CALL_FUNCS) { for my $flags (@function_flags) {
	my $dbh = connect_ok( PrintError => 0 );

	ok($dbh->$call_func( "now", 0, \&now, defined $flags ? $flags : (), "create_function" ));
	my $result = $dbh->selectrow_arrayref( "SELECT now()" );

	ok( $result->[0], 'Got a result' );

	$dbh->do( 'CREATE TEMP TABLE func_test ( a, b )' );
	$dbh->do( 'INSERT INTO func_test VALUES ( 1, 3 )' );
	$dbh->do( 'INSERT INTO func_test VALUES ( 0, 4 )' );

	ok($dbh->$call_func( "add2", 2, \&add2, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT add2(1,3)" );
	is($result->[0], 4, "SELECT add2(1,3)" );

	$result = $dbh->selectall_arrayref( "SELECT add2(a,b) FROM func_test" );
	is_deeply( $result, [ [4], [4] ], "SELECT add2(a,b) FROM func_test" );

	ok($dbh->$call_func( "my_sum", -1, \&my_sum, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT my_sum( '2', 3, 4, '5')" );
	is( $result->[0], 14, "SELECT my_sum( '2', 3, 4, '5')" );

	ok($dbh->$call_func( "error", -1, \&error, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT error( 'I died' )" );
	ok( !$result );
	like( $DBI::errstr, qr/function is dying: I died/ );

	ok($dbh->$call_func( "void_return", -1, \&void_return, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT void_return( 'I died' )" );
	is_deeply( $result, [ undef ], "SELECT void_return( 'I died' )" );

	ok($dbh->$call_func( "return_null", -1, \&return_null, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT return_null()" );
	is_deeply( $result, [ undef ], "SELECT return_null()" );

	ok($dbh->$call_func( "return2", -1, \&return2, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT return2()" );
	is_deeply( $result, [ 2 ], "SELECT return2()" );

	ok($dbh->$call_func( "my_defined", 1, \&my_defined, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT my_defined(1)" );
	is_deeply( $result, [ 1 ], "SELECT my_defined(1)" );

	$result = $dbh->selectrow_arrayref( "SELECT my_defined('')" );
	is_deeply( $result, [ 1 ], "SELECT my_defined('')" );

	$result = $dbh->selectrow_arrayref( "SELECT my_defined('abc')" );
	is_deeply( $result, [ 1 ], "SELECT my_defined('abc')" );

	$result = $dbh->selectrow_arrayref( "SELECT my_defined(NULL)" );
	is_deeply( $result, [ '0' ], "SELECT my_defined(NULL)" );

	ok($dbh->$call_func( "noop", 1, \&noop, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT noop(NULL)" );
	is_deeply( $result, [ undef ], "SELECT noop(NULL)" );

	$result = $dbh->selectrow_arrayref( "SELECT noop(1)" );
	is_deeply( $result, [ 1 ], "SELECT noop(1)" );

	$result = $dbh->selectrow_arrayref( "SELECT noop('')" );
	is_deeply( $result, [ '' ], "SELECT noop('')" );

	$result = $dbh->selectrow_arrayref( "SELECT noop(1.0625)" );
	is_deeply( $result, [ 1.0625 ], "SELECT noop(1.0625)" );

	# 2147483648 == 1<<31
	$result = $dbh->selectrow_arrayref( "SELECT noop(2147483648)" );
	is_deeply( $result, [ 2147483648 ], "SELECT noop(2147483648)" );

	$result = $dbh->selectrow_arrayref( "SELECT typeof(noop(2147483648))" );
	is_deeply( $result, [ 'integer' ], "SELECT typeof(noop(2147483648))" );

	ok($dbh->$call_func( "md5_text", 1, \&md5_text, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT md5_text('my_blob')" );
	is_deeply( $result, [ md5('my_blob') ], "SELECT md5_text('my_blob')" );

	$result = $dbh->selectrow_arrayref( "SELECT typeof(md5_text('my_blob'))" );
	is_deeply( $result, [ 'text' ], "SELECT typeof(md5_text('my_blob'))" );

	ok($dbh->$call_func( "md5_blob", 1, \&md5_blob, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT md5_blob('my_blob')" );
	is_deeply( $result, [ md5('my_blob') ], "SELECT md5_blob('my_blob')" );

	$result = $dbh->selectrow_arrayref( "SELECT typeof(md5_blob('my_blob'))" );
	is_deeply( $result, [ 'blob' ], "SELECT typeof(md5_blob('my_blob'))" );

	ok($dbh->$call_func( "md5_blob", 1, undef, defined $flags ? $flags : (), "create_function" ));
	$result = $dbh->selectrow_arrayref( "SELECT md5_blob('my_blob')" );
	is_deeply( $result, undef, "SELECT md5_blob('my_blob')" );

	$dbh->disconnect;
}}

done_testing;