|
NAMEDBIx::Class::Helper::ResultSet::DateMethods1 - Work with dates in your RDBMS nicelySYNOPSISpackage MySchema::ResultSet::Bar; use strict; use warnings; use parent 'DBIx::Class::ResultSet'; __PACKAGE__->load_components('Helper::ResultSet::DateMethods1'); # in code using resultset # get count per year/month $rs->search(undef, { columns => { count => '*', year => $rs->dt_SQL_pluck({ -ident => '.start' }, 'year'), month => $rs->dt_SQL_pluck({ -ident => '.start' }, 'month'), }, group_by => [ $rs->dt_SQL_pluck({ -ident => '.start' }, 'year'), $rs->dt_SQL_pluck({ -ident => '.start' }, 'month'), ], }); # mysql (SELECT `me`.*, EXTRACT(MONTH FROM `me`.`start`), EXTRACT(YEAR FROM `me`.`start`) FROM `HasDateOps` `me` GROUP BY EXTRACT(YEAR FROM `me`.`start`), EXTRACT(MONTH FROM `me`.`start`)) # SQLite (SELECT "me".*, STRFTIME('%m', "me"."start"), STRFTIME('%Y', "me"."start") FROM "HasDateOps" "me" GROUP BY STRFTIME('%Y', "me"."start"), STRFTIME('%m', "me"."start")) DESCRIPTIONSee "NOTE" in DBIx::Class::Helper::ResultSet for a nice way to apply it to your entire schema.This ResultSet component gives the user tools to do mostly portable date manipulation in the database. Before embarking on a cross database project, take a look at "IMPLEMENTATION" to see what might break on switching databases. This package has a few types of methods.
TYPESBecause these methods are so limited in scope they can be a bit more smart than typical "SQL::Abstract" trees.There are "smart types" that this package supports.
Anything not mentioned in the above list will explode, one way or another. IMPLEMENTATIONThe exact details for the functions your database engine provides.If a piece of functionality is flagged with ⚠, it means that the feature in question is not portable at all, and only supported on that engine. "SQL Server"
"SQLite"
"PostgreSQL"
"MySQL"
"Oracle"
CONTRIBUTORSThese people worked on the original implementation, and thus deserve some credit for at least providing me a reference to implement this based off of:
WHENCE dt_SQL_diff?The original implementation of these date helpers (originally dubbed date operators) included a third operator called "diff". It existed to subtract one date from another and return a duration. After using it a few times and getting bitten every time, I decided to stop using it and instead compare against actual dates always. If someone can come up with a good use case I am interested in re-implementing "dt_SQL_diff", but I worry that it will be very unportable and generally not very useful.METHODSutc$rs->search({ 'some_date' => $rs->utc($datetime), })->all Takes a DateTime object, updates the "time_zone" to "UTC", and formats it according to whatever database engine you are using. Dies if you pass it a date with a "floating time_zone". utc_nowReturns a "ScalarRef" representing the way to get the current date and time in "UTC" for whatever database engine you are using.dt_before$rs->dt_before({ -ident => '.start' }, { -ident => '.end' })->all Takes two values, each an expression of "TYPES". dt_on_or_before$rs->dt_on_or_before({ -ident => '.start' }, DateTime->now)->all Takes two values, each an expression of "TYPES". dt_on_or_after$rs->dt_on_or_after(DateTime->now, { ident => '.end' })->all Takes two values, each an expression of "TYPES". dt_after$rs->dt_after({ ident => '.end' }, $rs->get_column('datecol')->as_query)->all Takes two values, each an expression of "TYPES". dt_SQL_add# which ones start in 3 minutes? $rs->dt_on_or_after( { ident => '.start' }, $rs->dt_SQL_add($rs->utc_now, 'minute', 3) )->all Takes three arguments: a date conforming to "TYPES", a unit, and an amount. The idea is to add the given unit to the datetime. See your "IMPLEMENTATION" for what units are accepted. dt_SQL_subtractSame as dt_SQL_add, but subtracts the amount.Only confirmed to work with Postgres, MySQL and SQLite. It should work with Oracle and MSSQL, but due to lack of access to those DB engines the implementation was done only based on docs. This method was implemented by Thomas Klausner <https://domm.plix.at> and sponsored by Ctrl O <https://www.ctrlo.com/>. dt_SQL_pluck# get count per year $rs->search(undef, { columns => { count => '*', year => $rs->dt_SQL_pluck({ -ident => '.start' }, 'year'), }, group_by => [$rs->dt_SQL_pluck({ -ident => '.start' }, 'year')], })->hri->all Takes two arguments: a date conforming to "TYPES" and a unit. The idea is to pluck a given unit from the datetime. See your "IMPLEMENTATION" for what units are accepted. AUTHORArthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>COPYRIGHT AND LICENSEThis software is copyright (c) 2020 by Arthur Axel "fREW" Schmidt.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |