| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package TheSchwartz::Moosified::Utils; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
7
|
use base 'Exporter'; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
148
|
|
|
4
|
2
|
|
|
2
|
|
8
|
use Carp; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
96
|
|
|
5
|
2
|
|
|
2
|
|
9
|
use vars qw/@EXPORT_OK/; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
746
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
@EXPORT_OK = qw/insert_id sql_for_unixtime bind_param_attr run_in_txn order_by_priority/; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub insert_id { |
|
10
|
0
|
|
|
0
|
0
|
|
my ( $dbh, $sth, $table, $col ) = @_; |
|
11
|
|
|
|
|
|
|
|
|
12
|
0
|
|
|
|
|
|
my $driver = $dbh->{Driver}{Name}; |
|
13
|
0
|
0
|
|
|
|
|
if ( $driver eq 'mysql' ) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
return $dbh->{mysql_insertid}; |
|
15
|
|
|
|
|
|
|
} |
|
16
|
|
|
|
|
|
|
elsif ( $driver eq 'Pg' ) { |
|
17
|
0
|
|
|
|
|
|
return $dbh->last_insert_id( undef, undef, undef, undef, |
|
18
|
|
|
|
|
|
|
{ sequence => join( "_", $table, $col, 'seq' ) } ); |
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
elsif ( $driver eq 'SQLite' ) { |
|
21
|
0
|
|
|
|
|
|
return $dbh->func('last_insert_rowid'); |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
else { |
|
24
|
0
|
|
|
|
|
|
croak "Don't know how to get last insert id for $driver"; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# SQL doesn't define a function to ask a machine of its time in |
|
29
|
|
|
|
|
|
|
# unixtime form. MySQL does |
|
30
|
|
|
|
|
|
|
# but for sqlite and others, we assume "remote" time is same as local |
|
31
|
|
|
|
|
|
|
# machine's time, which is especially true for sqlite. |
|
32
|
|
|
|
|
|
|
sub sql_for_unixtime { |
|
33
|
0
|
|
|
0
|
0
|
|
my ($dbh) = @_; |
|
34
|
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
my $driver = $dbh->{Driver}{Name}; |
|
36
|
0
|
0
|
0
|
|
|
|
if ( $driver and $driver eq 'mysql' ) { |
|
37
|
0
|
|
|
|
|
|
return "UNIX_TIMESTAMP()"; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
0
|
0
|
0
|
|
|
|
if ( $driver and $driver eq 'Pg' ) { |
|
40
|
0
|
|
|
|
|
|
return "EXTRACT(EPOCH FROM NOW())::integer"; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
return time(); |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub bind_param_attr { |
|
47
|
0
|
|
|
0
|
0
|
|
my ( $dbh, $col ) = @_; |
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
return if $col ne 'arg'; |
|
50
|
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my $driver = $dbh->{Driver}{Name}; |
|
52
|
0
|
0
|
0
|
|
|
|
if ( $driver and $driver eq 'Pg' ) { |
|
|
|
0
|
0
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
return { pg_type => DBD::Pg::PG_BYTEA() }; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
elsif ( $driver and $driver eq 'SQLite' ) { |
|
56
|
0
|
|
|
|
|
|
return DBI::SQL_BLOB(); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
0
|
|
|
|
|
|
return; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub order_by_priority { |
|
62
|
0
|
|
|
0
|
0
|
|
my $dbh = shift; |
|
63
|
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
my $driver = $dbh->{Driver}{Name}; |
|
65
|
0
|
0
|
0
|
|
|
|
if ( $driver and $driver eq 'Pg' ) { |
|
66
|
|
|
|
|
|
|
# make NULL sort as if it were 0, consistent with SQLite |
|
67
|
|
|
|
|
|
|
# Suggestion: |
|
68
|
|
|
|
|
|
|
# CREATE INDEX ix_job_piro_non_null ON job (COALESCE(priority,0)); |
|
69
|
0
|
|
|
|
|
|
return 'ORDER BY COALESCE(priority,0) DESC'; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
0
|
|
|
|
|
|
return 'ORDER BY priority DESC'; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub run_in_txn (&$) { |
|
75
|
0
|
|
|
0
|
0
|
|
my $code = shift; |
|
76
|
0
|
|
|
|
|
|
my $dbh = shift; |
|
77
|
0
|
|
|
|
|
|
local $dbh->{RaiseError} = 1; |
|
78
|
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
my $need_txn = $dbh->{AutoCommit} ? 1 : 0; |
|
80
|
0
|
0
|
|
|
|
|
return $code->() unless $need_txn; |
|
81
|
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my @rv; |
|
83
|
|
|
|
|
|
|
my $rv; |
|
84
|
0
|
|
|
|
|
|
eval { |
|
85
|
0
|
|
|
|
|
|
$dbh->begin_work; |
|
86
|
0
|
0
|
|
|
|
|
if (wantarray) { |
|
87
|
0
|
|
|
|
|
|
@rv = $code->(); |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
else { |
|
90
|
0
|
|
|
|
|
|
$rv = $code->(); |
|
91
|
|
|
|
|
|
|
} |
|
92
|
0
|
|
|
|
|
|
$dbh->commit; |
|
93
|
|
|
|
|
|
|
}; |
|
94
|
0
|
0
|
|
|
|
|
if (my $err = $@) { eval { $dbh->rollback }; die $err } |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
|
96
|
0
|
0
|
|
|
|
|
return @rv if wantarray; |
|
97
|
0
|
|
|
|
|
|
return $rv; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
|
101
|
|
|
|
|
|
|
__END__ |