| 1 | package DBIx::Class::Helper::ResultSet::DateMethods1; | ||||||||||||||||||||||||||||
| 2 | 56 | $DBIx::Class::Helper::ResultSet::DateMethods1::VERSION = '2.037000'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Work with dates in your RDBMS nicely | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 56 | ▶ | use parent 'DBIx::Class::ResultSet'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 56 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 56 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 56 | ▶ | use DBI qw(:sql_types); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 56 | ▶ | use DBIx::Introspector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 56 | ▶ | use Safe::Isa; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 427 | ▶ | sub _flatten_thing { | ||||||||||||||||||||||||||
|
Subroutine
_flatten_thing: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 15 | 427 | my ($self, $thing) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 427 | ▶ | die 'you dummy' unless defined $thing; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 427 | my $ref = ref $thing; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 427 | ▶ | return ('?', $thing) if !$ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 329 | ▶ | if ($ref eq 'HASH' && exists $thing->{'-ident'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ref eq "HASH" and exists $thing->{"-ident"}
MC/DC50%1/2
$ref eq "HASH" and exists $thing->{"-ident"}
$ref eq "HASH"
exists $thing->{"-ident"}
Truth table66%2/3
A and B
A= $ref eq "HASH"B= exists $thing->{"-ident"}
| |||||||||||||||||||||||||||||
| 23 | 287 | my $thing = $thing->{'-ident'}; | |||||||||||||||||||||||||||
| 24 | 287 | ▶ | $thing = $self->current_source_alias . $thing if $thing =~ m/^\./; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 287 | return $self->result_source->storage->sql_maker->_quote($thing) | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 47 | ▶ | return ${$thing} if $ref eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # FIXME: this should have the right bind type | ||||||||||||||||||||||||||||
| 31 | 46 | ▶ | return ('?', $self->utc($thing)) if $thing->$_isa('DateTime'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 25 | return @{${$thing}}; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub _introspector { | ||||||||||||||||||||||||||||
| 36 | 5 | my $d = DBIx::Introspector->new(drivers => '2013-12.01'); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 5 | $d->decorate_driver_unconnected(MSSQL => now_utc_sql => 'GETUTCDATE()'); | |||||||||||||||||||||||||||
| 39 | 5 | $d->decorate_driver_unconnected(SQLite => now_utc_sql => q<DATETIME('now')>); | |||||||||||||||||||||||||||
| 40 | 5 | $d->decorate_driver_unconnected(mysql => now_utc_sql => 'UTC_TIMESTAMP()'); | |||||||||||||||||||||||||||
| 41 | 5 | $d->decorate_driver_unconnected(Oracle => now_utc_sql => 'sys_extract_utc(SYSTIMESTAMP)'); | |||||||||||||||||||||||||||
| 42 | 5 | $d->decorate_driver_unconnected(Pg => now_utc_sql => 'CURRENT_TIMESTAMP'); | |||||||||||||||||||||||||||
| 43 | MSSQL: { | ||||||||||||||||||||||||||||
| 44 | 5 | my %part_map = ( | |||||||||||||||||||||||||||
| 45 | year => 'year', | ||||||||||||||||||||||||||||
| 46 | quarter => 'quarter', | ||||||||||||||||||||||||||||
| 47 | month => 'month', | ||||||||||||||||||||||||||||
| 48 | day_of_year => 'dayofyear', | ||||||||||||||||||||||||||||
| 49 | day_of_month => 'day', | ||||||||||||||||||||||||||||
| 50 | week => 'week', | ||||||||||||||||||||||||||||
| 51 | day_of_week => 'ISO_WEEK', | ||||||||||||||||||||||||||||
| 52 | hour => 'hour', | ||||||||||||||||||||||||||||
| 53 | minute => 'minute', | ||||||||||||||||||||||||||||
| 54 | second => 'second', | ||||||||||||||||||||||||||||
| 55 | millisecond => 'millisecond', | ||||||||||||||||||||||||||||
| 56 | nanosecond => 'nanosecond', | ||||||||||||||||||||||||||||
| 57 | non_iso_day_of_week => 'weekday', | ||||||||||||||||||||||||||||
| 58 | timezone_as_minutes => 'TZoffset', | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | $d->decorate_driver_unconnected(MSSQL => datepart_sql => sub { | ||||||||||||||||||||||||||||
| 62 | sub { | ||||||||||||||||||||||||||||
| 63 | my ($date_sql, $part) = @_; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | my ($sql, @args) = @$date_sql; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | return [ | ||||||||||||||||||||||||||||
| 68 | "DATEPART($part_map{$part}, $sql)", | ||||||||||||||||||||||||||||
| 69 | @args | ||||||||||||||||||||||||||||
| 70 | ] | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 5 | }); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 5 | my %diff_part_map = %part_map; | |||||||||||||||||||||||||||
| 76 | 5 | $diff_part_map{day} = delete $diff_part_map{day_of_year}; | |||||||||||||||||||||||||||
| 77 | 5 | delete $diff_part_map{day_of_month}; | |||||||||||||||||||||||||||
| 78 | 5 | delete $diff_part_map{day_of_week}; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | $d->decorate_driver_unconnected(MSSQL => dateadd_sql => sub { | ||||||||||||||||||||||||||||
| 81 | sub { | ||||||||||||||||||||||||||||
| 82 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 85 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | return [ | ||||||||||||||||||||||||||||
| 88 | "DATEADD($diff_part_map{$unit}, CAST($a_sql AS int), $d_sql)", | ||||||||||||||||||||||||||||
| 89 | @a_args, @d_args, | ||||||||||||||||||||||||||||
| 90 | ]; | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 5 | }); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | $d->decorate_driver_unconnected(MSSQL => datesubtract_sql => sub { | ||||||||||||||||||||||||||||
| 95 | sub { | ||||||||||||||||||||||||||||
| 96 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 99 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | return [ # no idea if this works.. | ||||||||||||||||||||||||||||
| 102 | "DATEADD($diff_part_map{$unit}, -1 * CAST($a_sql AS int), $d_sql)", | ||||||||||||||||||||||||||||
| 103 | @a_args, @d_args, | ||||||||||||||||||||||||||||
| 104 | ]; | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 5 | }); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | SQLITE: { | ||||||||||||||||||||||||||||
| 110 | 5 | my %part_map = ( | |||||||||||||||||||||||||||
| 111 | month => 'm', | ||||||||||||||||||||||||||||
| 112 | day_of_month => 'd', | ||||||||||||||||||||||||||||
| 113 | year => 'Y', | ||||||||||||||||||||||||||||
| 114 | hour => 'H', | ||||||||||||||||||||||||||||
| 115 | day_of_year => 'j', | ||||||||||||||||||||||||||||
| 116 | minute => 'M', | ||||||||||||||||||||||||||||
| 117 | second => 'S', | ||||||||||||||||||||||||||||
| 118 | day_of_week => 'w', | ||||||||||||||||||||||||||||
| 119 | week => 'W', | ||||||||||||||||||||||||||||
| 120 | # maybe don't support these or prefix them with 'sqlite.'? | ||||||||||||||||||||||||||||
| 121 | julian_day => 'J', | ||||||||||||||||||||||||||||
| 122 | seconds_since_epoch => 's', | ||||||||||||||||||||||||||||
| 123 | fractional_seconds => 'f', | ||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | $d->decorate_driver_unconnected(SQLite => datepart_sql => sub { | ||||||||||||||||||||||||||||
| 127 | sub { | ||||||||||||||||||||||||||||
| 128 | my ($date_sql, $part) = @_; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | my ($sql, @args) = @$date_sql; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | return [ | ||||||||||||||||||||||||||||
| 133 | "STRFTIME('%$part_map{$part}', $sql)", | ||||||||||||||||||||||||||||
| 134 | @args | ||||||||||||||||||||||||||||
| 135 | ] | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 5 | }); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 5 | my %diff_part_map = ( | |||||||||||||||||||||||||||
| 140 | day => 'days', | ||||||||||||||||||||||||||||
| 141 | hour => 'hours', | ||||||||||||||||||||||||||||
| 142 | minute => 'minutes', | ||||||||||||||||||||||||||||
| 143 | second => 'seconds', | ||||||||||||||||||||||||||||
| 144 | month => 'months', | ||||||||||||||||||||||||||||
| 145 | year => 'years', | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | $d->decorate_driver_unconnected(SQLite => dateadd_sql => sub { | ||||||||||||||||||||||||||||
| 149 | sub { | ||||||||||||||||||||||||||||
| 150 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 153 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | die "unknown part $unit" unless $diff_part_map{$unit}; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | return [ | ||||||||||||||||||||||||||||
| 158 | "DATETIME($d_sql, $a_sql || ?)", | ||||||||||||||||||||||||||||
| 159 | @d_args, @a_args, " $diff_part_map{$unit}" | ||||||||||||||||||||||||||||
| 160 | ]; | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 5 | }); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | $d->decorate_driver_unconnected(SQLite => datesubtract_sql => sub { | ||||||||||||||||||||||||||||
| 165 | sub { | ||||||||||||||||||||||||||||
| 166 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 169 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | die "unknown part $unit" unless $diff_part_map{$unit}; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | return [ | ||||||||||||||||||||||||||||
| 174 | "DATETIME($d_sql, '-' || $a_sql || ?)", | ||||||||||||||||||||||||||||
| 175 | @d_args, @a_args, " $diff_part_map{$unit}" | ||||||||||||||||||||||||||||
| 176 | ]; | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 5 | }); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | PG: { | ||||||||||||||||||||||||||||
| 182 | 5 | my %part_map = ( | |||||||||||||||||||||||||||
| 183 | century => 'century', | ||||||||||||||||||||||||||||
| 184 | decade => 'decade', | ||||||||||||||||||||||||||||
| 185 | day_of_month => 'day', | ||||||||||||||||||||||||||||
| 186 | day_of_week => 'dow', | ||||||||||||||||||||||||||||
| 187 | day_of_year => 'doy', | ||||||||||||||||||||||||||||
| 188 | seconds_since_epoch => 'epoch', | ||||||||||||||||||||||||||||
| 189 | hour => 'hour', | ||||||||||||||||||||||||||||
| 190 | iso_day_of_week => 'isodow', | ||||||||||||||||||||||||||||
| 191 | iso_year => 'isoyear', | ||||||||||||||||||||||||||||
| 192 | microsecond => 'microseconds', | ||||||||||||||||||||||||||||
| 193 | millenium => 'millenium', | ||||||||||||||||||||||||||||
| 194 | millisecond => 'milliseconds', | ||||||||||||||||||||||||||||
| 195 | minute => 'minute', | ||||||||||||||||||||||||||||
| 196 | month => 'month', | ||||||||||||||||||||||||||||
| 197 | quarter => 'quarter', | ||||||||||||||||||||||||||||
| 198 | second => 'second', | ||||||||||||||||||||||||||||
| 199 | timezone => 'timezone', | ||||||||||||||||||||||||||||
| 200 | timezone_hour => 'timezone_hour', | ||||||||||||||||||||||||||||
| 201 | timezone_minute => 'timezone_minute', | ||||||||||||||||||||||||||||
| 202 | week => 'week', | ||||||||||||||||||||||||||||
| 203 | year => 'year', | ||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 5 | my %diff_part_map = %part_map; | |||||||||||||||||||||||||||
| 207 | 5 | delete $diff_part_map{qw( | |||||||||||||||||||||||||||
| 208 | day_of_week day_of_year iso_day_of_week iso_year millenium quarter | ||||||||||||||||||||||||||||
| 209 | seconds_since_epoch timezone timezone_hour timezone_minute | ||||||||||||||||||||||||||||
| 210 | )}; | ||||||||||||||||||||||||||||
| 211 | 5 | $diff_part_map{day} = delete $diff_part_map{day_of_month}; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | $d->decorate_driver_unconnected(Pg => datepart_sql => sub { | ||||||||||||||||||||||||||||
| 214 | sub { | ||||||||||||||||||||||||||||
| 215 | my ($date_sql, $part) = @_; | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | my ($sql, @args) = @$date_sql; | ||||||||||||||||||||||||||||
| 218 | @args = ([{ dbd_attrs => SQL_TIMESTAMP }, $args[0]]) | ||||||||||||||||||||||||||||
| 219 | if $sql eq '?' && @args == 1; | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | return [ | ||||||||||||||||||||||||||||
| 222 | "date_part(?, $sql)", | ||||||||||||||||||||||||||||
| 223 | $part_map{$part}, @args | ||||||||||||||||||||||||||||
| 224 | ] | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | 5 | }); | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | $d->decorate_driver_unconnected(Pg => dateadd_sql => sub { | ||||||||||||||||||||||||||||
| 229 | sub { | ||||||||||||||||||||||||||||
| 230 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 233 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | @d_args = ([{ dbd_attrs => SQL_TIMESTAMP }, $d_args[0]]) | ||||||||||||||||||||||||||||
| 236 | if $d_sql eq '?' && @d_args == 1; | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | die "unknown part $unit" unless $diff_part_map{$unit}; | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | return [ | ||||||||||||||||||||||||||||
| 241 | "($d_sql + $a_sql * interval '1 $diff_part_map{$unit}')", | ||||||||||||||||||||||||||||
| 242 | @d_args, @a_args, | ||||||||||||||||||||||||||||
| 243 | ]; | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | 5 | }); | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | $d->decorate_driver_unconnected(Pg => datesubtract_sql => sub { | ||||||||||||||||||||||||||||
| 248 | sub { | ||||||||||||||||||||||||||||
| 249 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 252 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | @d_args = ([{ dbd_attrs => SQL_TIMESTAMP }, $d_args[0]]) | ||||||||||||||||||||||||||||
| 255 | if $d_sql eq '?' && @d_args == 1; | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | die "unknown part $unit" unless $diff_part_map{$unit}; | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | return [ | ||||||||||||||||||||||||||||
| 260 | "($d_sql - $a_sql * interval '1 $diff_part_map{$unit}')", | ||||||||||||||||||||||||||||
| 261 | @d_args, @a_args, | ||||||||||||||||||||||||||||
| 262 | ]; | ||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | 5 | }); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | MYSQL: { | ||||||||||||||||||||||||||||
| 268 | 5 | my %part_map = ( | |||||||||||||||||||||||||||
| 269 | microsecond => 'MICROSECOND', | ||||||||||||||||||||||||||||
| 270 | second => 'SECOND', | ||||||||||||||||||||||||||||
| 271 | minute => 'MINUTE', | ||||||||||||||||||||||||||||
| 272 | hour => 'HOUR', | ||||||||||||||||||||||||||||
| 273 | day_of_month => 'DAY', | ||||||||||||||||||||||||||||
| 274 | week => 'WEEK', | ||||||||||||||||||||||||||||
| 275 | month => 'MONTH', | ||||||||||||||||||||||||||||
| 276 | quarter => 'QUARTER', | ||||||||||||||||||||||||||||
| 277 | year => 'YEAR', | ||||||||||||||||||||||||||||
| 278 | second_microsecond => 'SECOND_MICROSECOND', | ||||||||||||||||||||||||||||
| 279 | minute_microsecond => 'MINUTE_MICROSECOND', | ||||||||||||||||||||||||||||
| 280 | minute_second => 'MINUTE_SECOND', | ||||||||||||||||||||||||||||
| 281 | hour_microsecond => 'HOUR_MICROSECOND', | ||||||||||||||||||||||||||||
| 282 | hour_second => 'HOUR_SECOND', | ||||||||||||||||||||||||||||
| 283 | hour_minute => 'HOUR_MINUTE', | ||||||||||||||||||||||||||||
| 284 | day_microsecond => 'DAY_MICROSECOND', | ||||||||||||||||||||||||||||
| 285 | day_second => 'DAY_SECOND', | ||||||||||||||||||||||||||||
| 286 | day_minute => 'DAY_MINUTE', | ||||||||||||||||||||||||||||
| 287 | day_hour => 'DAY_HOUR', | ||||||||||||||||||||||||||||
| 288 | year_month => 'YEAR_MONTH', | ||||||||||||||||||||||||||||
| 289 | ); | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 5 | my %diff_part_map = %part_map; | |||||||||||||||||||||||||||
| 292 | $diff_part_map{day} = delete $diff_part_map{day_of_month}; | ||||||||||||||||||||||||||||
| 293 | delete $diff_part_map{qw( | ||||||||||||||||||||||||||||
| 294 | second_microsecond minute_microsecond minute_second | ||||||||||||||||||||||||||||
| 295 | hour_microsecond hour_second hour_minute day_microsecond | ||||||||||||||||||||||||||||
| 296 | day_second day_minute day_hour year_month | ||||||||||||||||||||||||||||
| 297 | )}; | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | $d->decorate_driver_unconnected(mysql => datepart_sql => sub { | ||||||||||||||||||||||||||||
| 300 | sub { | ||||||||||||||||||||||||||||
| 301 | my ($date_sql, $part) = @_; | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | my ($sql, @args) = @$date_sql; | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | return [ | ||||||||||||||||||||||||||||
| 306 | "EXTRACT($part_map{$part} FROM $sql)", @args | ||||||||||||||||||||||||||||
| 307 | ] | ||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | }); | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | $d->decorate_driver_unconnected(mysql => dateadd_sql => sub { | ||||||||||||||||||||||||||||
| 312 | sub { | ||||||||||||||||||||||||||||
| 313 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 316 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | die "unknown part $unit" unless $diff_part_map{$unit}; | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | return [ | ||||||||||||||||||||||||||||
| 321 | "DATE_ADD($d_sql, INTERVAL $a_sql $diff_part_map{$unit})", | ||||||||||||||||||||||||||||
| 322 | @d_args, @a_args, | ||||||||||||||||||||||||||||
| 323 | ]; | ||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | }); | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | $d->decorate_driver_unconnected(mysql => datesubtract_sql => sub { | ||||||||||||||||||||||||||||
| 328 | sub { | ||||||||||||||||||||||||||||
| 329 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 332 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | die "unknown part $unit" unless $diff_part_map{$unit}; | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | return [ | ||||||||||||||||||||||||||||
| 337 | "DATE_SUB($d_sql, INTERVAL $a_sql $diff_part_map{$unit})", | ||||||||||||||||||||||||||||
| 338 | @d_args, @a_args, | ||||||||||||||||||||||||||||
| 339 | ]; | ||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | }); | ||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | ORACLE: { | ||||||||||||||||||||||||||||
| 345 | my %part_map = ( | ||||||||||||||||||||||||||||
| 346 | second => 'SECOND', | ||||||||||||||||||||||||||||
| 347 | minute => 'MINUTE', | ||||||||||||||||||||||||||||
| 348 | hour => 'HOUR', | ||||||||||||||||||||||||||||
| 349 | day_of_month => 'DAY', | ||||||||||||||||||||||||||||
| 350 | month => 'MONTH', | ||||||||||||||||||||||||||||
| 351 | year => 'YEAR', | ||||||||||||||||||||||||||||
| 352 | ); | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | $d->decorate_driver_unconnected(Oracle => datepart_sql => sub { | ||||||||||||||||||||||||||||
| 355 | sub { | ||||||||||||||||||||||||||||
| 356 | my ($date_sql, $part) = @_; | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | my ($sql, @args) = @$date_sql; | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | return [ | ||||||||||||||||||||||||||||
| 361 | "EXTRACT($part_map{$part} FROM TO_TIMESTAMP($sql))", @args | ||||||||||||||||||||||||||||
| 362 | ] | ||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | }); | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | my %diff_part_map = %part_map; | ||||||||||||||||||||||||||||
| 367 | $diff_part_map{day} = delete $diff_part_map{day_of_month}; | ||||||||||||||||||||||||||||
| 368 | delete $diff_part_map{$_} for qw(year month); | ||||||||||||||||||||||||||||
| 369 | $d->decorate_driver_unconnected(Oracle => dateadd_sql => sub { | ||||||||||||||||||||||||||||
| 370 | sub { | ||||||||||||||||||||||||||||
| 371 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 374 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | die "unknown unit $unit" unless $diff_part_map{$unit}; | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | return [ | ||||||||||||||||||||||||||||
| 379 | "(TO_TIMESTAMP($d_sql) + NUMTODSINTERVAL($a_sql, ?))", | ||||||||||||||||||||||||||||
| 380 | @d_args, @a_args, $diff_part_map{$unit} | ||||||||||||||||||||||||||||
| 381 | ]; | ||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | }); | ||||||||||||||||||||||||||||
| 384 | $d->decorate_driver_unconnected(Oracle => datesubtract_sql => sub { | ||||||||||||||||||||||||||||
| 385 | sub { | ||||||||||||||||||||||||||||
| 386 | my ($date_sql, $unit, $amount_sql) = @_; | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | my ($d_sql, @d_args) = @{$date_sql}; | ||||||||||||||||||||||||||||
| 389 | my ($a_sql, @a_args) = @{$amount_sql}; | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | die "unknown unit $unit" unless $diff_part_map{$unit}; | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | return [ # no idea if this works.. | ||||||||||||||||||||||||||||
| 394 | "(TO_TIMESTAMP($d_sql) - NUMTODSINTERVAL($a_sql, ?))", | ||||||||||||||||||||||||||||
| 395 | @d_args, @a_args, $diff_part_map{$unit} | ||||||||||||||||||||||||||||
| 396 | ]; | ||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | }); | ||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | return $d; | ||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 56 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 10 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 407 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | $self = $self->as_subselect_rs | ||||||||||||||||||||||||||||
| 410 | 10 | ▶ | if $self->_resolved_attrs->{_DBICH_DM1}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 10 | return $self->next::method(@_); | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 416 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | $self = $self->as_subselect_rs | ||||||||||||||||||||||||||||
| 419 | 0 | ▶ | if $self->_resolved_attrs->{_DBICH_DM1}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 0 | return $self->next::method(@_); | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 30 | ▶ | sub utc { | ||||||||||||||||||||||||||
|
Subroutine
utc: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 425 | 30 | my ($self, $datetime) = @_; | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 30 | my $tz_name = $datetime->time_zone->name; | |||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 30 | ▶ | die "floating dates are not allowed" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | if $tz_name eq 'floating'; | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 25 | ▶ | $datetime = $datetime->clone->set_time_zone('UTC') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 433 | unless $tz_name eq 'UTC'; | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 25 | $_[0]->result_source->storage->datetime_parser->format_datetime($datetime) | |||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 20 | ▶ | sub dt_before { | ||||||||||||||||||||||||||
|
Subroutine
dt_before: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 439 | 20 | my ($self, $l, $r) = @_; | |||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 20 | my ($l_sql, @l_args) = _flatten_thing($self, $l); | |||||||||||||||||||||||||||
| 442 | 20 | my ($r_sql, @r_args) = _flatten_thing($self, $r); | |||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 20 | return $self->search(\[ | |||||||||||||||||||||||||||
| 445 | "$l_sql < $r_sql", @l_args, @r_args | ||||||||||||||||||||||||||||
| 446 | ], { _DBICH_DM1 => 1 }); | ||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 18 | ▶ | sub dt_on_or_before { | ||||||||||||||||||||||||||
|
Subroutine
dt_on_or_before: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 450 | 18 | my ($self, $l, $r) = @_; | |||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 18 | my ($l_sql, @l_args) = _flatten_thing($self, $l); | |||||||||||||||||||||||||||
| 453 | 18 | my ($r_sql, @r_args) = _flatten_thing($self, $r); | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 18 | $self->search(\[ | |||||||||||||||||||||||||||
| 456 | "$l_sql <= $r_sql", @l_args, @r_args | ||||||||||||||||||||||||||||
| 457 | ], { _DBICH_DM1 => 1 }); | ||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 18 | ▶ | sub dt_on_or_after { | ||||||||||||||||||||||||||
|
Subroutine
dt_on_or_after: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 461 | 18 | my ($self, $l, $r) = @_; | |||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 18 | my ($l_sql, @l_args) = _flatten_thing($self, $l); | |||||||||||||||||||||||||||
| 464 | 18 | my ($r_sql, @r_args) = _flatten_thing($self, $r); | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 18 | return $self->search(\[ | |||||||||||||||||||||||||||
| 467 | "$l_sql >= $r_sql", @l_args, @r_args | ||||||||||||||||||||||||||||
| 468 | ], { _DBICH_DM1 => 1 }); | ||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 18 | ▶ | sub dt_after { | ||||||||||||||||||||||||||
|
Subroutine
dt_after: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 472 | 18 | my ($self, $l, $r) = @_; | |||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | 18 | my ($l_sql, @l_args) = _flatten_thing($self, $l); | |||||||||||||||||||||||||||
| 475 | 18 | my ($r_sql, @r_args) = _flatten_thing($self, $r); | |||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | 18 | return $self->search(\[ | |||||||||||||||||||||||||||
| 478 | "$l_sql > $r_sql", @l_args, @r_args | ||||||||||||||||||||||||||||
| 479 | ], { _DBICH_DM1 => 1 }); | ||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 56 | my $d; | |||||||||||||||||||||||||||
| 483 | 7 | ▶ | sub utc_now { | ||||||||||||||||||||||||||
|
Subroutine
utc_now: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 484 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 485 | 7 | my $storage = $self->result_source->storage; | |||||||||||||||||||||||||||
| 486 | 7 | $storage->ensure_connected; | |||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | 7 | ▶ | $d ||= _introspector(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$d ||= _introspector()
MC/DC0%0/2
$d ||= _introspector()
$d
_introspector()
Truth table66%2/3
A ||= B
A= $dB= _introspector()
| |||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 7 | return \( $d->get($storage->dbh, undef, 'now_utc_sql') ); | |||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 51 | ▶ | sub dt_SQL_add { | ||||||||||||||||||||||||||
|
Subroutine
dt_SQL_add: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 494 | 51 | my ($self, $thing, $unit, $amount) = @_; | |||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | 51 | my $storage = $self->result_source->storage; | |||||||||||||||||||||||||||
| 497 | 51 | $storage->ensure_connected; | |||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | 51 | ▶ | $d ||= _introspector(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$d ||= _introspector()
MC/DC0%0/2
$d ||= _introspector()
$d
_introspector()
Truth table33%1/3
A ||= B
A= $dB= _introspector()
| |||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | return \( | ||||||||||||||||||||||||||||
| 502 | 51 | $d->get($storage->dbh, undef, 'dateadd_sql')->( | |||||||||||||||||||||||||||
| 503 | [ _flatten_thing($self, $thing) ], | ||||||||||||||||||||||||||||
| 504 | $unit, | ||||||||||||||||||||||||||||
| 505 | [ _flatten_thing($self, $amount) ], | ||||||||||||||||||||||||||||
| 506 | ) | ||||||||||||||||||||||||||||
| 507 | ); | ||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | 35 | ▶ | sub dt_SQL_subtract { | ||||||||||||||||||||||||||
|
Subroutine
dt_SQL_subtract: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 511 | 35 | my ($self, $thing, $unit, $amount) = @_; | |||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | 35 | my $storage = $self->result_source->storage; | |||||||||||||||||||||||||||
| 514 | 35 | $storage->ensure_connected; | |||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 35 | ▶ | $d ||= _introspector(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$d ||= _introspector()
MC/DC0%0/2
$d ||= _introspector()
$d
_introspector()
Truth table33%1/3
A ||= B
A= $dB= _introspector()
| |||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | return \( | ||||||||||||||||||||||||||||
| 519 | 35 | $d->get($storage->dbh, undef, 'datesubtract_sql')->( | |||||||||||||||||||||||||||
| 520 | [ _flatten_thing($self, $thing) ], | ||||||||||||||||||||||||||||
| 521 | $unit, | ||||||||||||||||||||||||||||
| 522 | [ _flatten_thing($self, $amount) ], | ||||||||||||||||||||||||||||
| 523 | ) | ||||||||||||||||||||||||||||
| 524 | ); | ||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | 102 | ▶ | sub dt_SQL_pluck { | ||||||||||||||||||||||||||
|
Subroutine
dt_SQL_pluck: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 528 | 102 | my ($self, $thing, $part) = @_; | |||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | 102 | my $storage = $self->result_source->storage; | |||||||||||||||||||||||||||
| 531 | 102 | $storage->ensure_connected; | |||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 102 | ▶ | $d ||= _introspector(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$d ||= _introspector()
MC/DC0%0/2
$d ||= _introspector()
$d
_introspector()
Truth table33%1/3
A ||= B
A= $dB= _introspector()
| |||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | return \( | ||||||||||||||||||||||||||||
| 536 | 102 | $d->get($storage->dbh, undef, 'datepart_sql')->( | |||||||||||||||||||||||||||
| 537 | [ _flatten_thing($self, $thing) ], | ||||||||||||||||||||||||||||
| 538 | $part, | ||||||||||||||||||||||||||||
| 539 | ) | ||||||||||||||||||||||||||||
| 540 | ); | ||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | 56 | 1; | |||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | __END__ | ||||||||||||||||||||||||||||