Merge the relationship resolution rework
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / SQLAnywhere / Cursor.pm
1 package DBIx::Class::Storage::DBI::SQLAnywhere::Cursor;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Storage::DBI::Cursor';
6 use mro 'c3';
7
8 use DBIx::Class::ResultSource::FromSpec::Util 'fromspec_columns_info';
9 use namespace::clean;
10
11 =head1 NAME
12
13 DBIx::Class::Storage::DBI::SQLAnywhere::Cursor - GUID Support for SQL Anywhere
14 over L<DBD::SQLAnywhere>
15
16 =head1 DESCRIPTION
17
18 This class is for normalizing GUIDs retrieved from SQL Anywhere via
19 L<DBD::SQLAnywhere>.
20
21 You probably don't want to be here, see
22 L<DBIx::Class::Storage::DBI::SQLAnywhere> for information on the SQL Anywhere
23 driver.
24
25 Unfortunately when using L<DBD::SQLAnywhere>, GUIDs come back in binary, the
26 purpose of this class is to transform them to text.
27 L<DBIx::Class::Storage::DBI::SQLAnywhere> sets
28 L<cursor_class|DBIx::Class::Storage::DBI/cursor_class> to this class by default.
29 It is overridable via your
30 L<connect_info|DBIx::Class::Storage::DBI/connect_info>.
31
32 You can use L<DBIx::Class::Cursor::Cached> safely with this class and not lose
33 the GUID normalizing functionality,
34 L<::Cursor::Cached|DBIx::Class::Cursor::Cached> uses the underlying class data
35 for the inner cursor class.
36
37 =cut
38
39 my $unpack_guids = sub {
40   my ($select, $col_infos, $data, $storage) = @_;
41
42   for my $select_idx (0..$#$select) {
43     next unless (
44       defined $data->[$select_idx]
45         and
46       length($data->[$select_idx]) == 16
47     );
48
49     my $selected = $select->[$select_idx];
50
51     my $data_type = $col_infos->{$select->[$select_idx]}{data_type}
52       or next;
53
54     $data->[$select_idx] = $storage->_uuid_to_str($data->[$select_idx])
55       if $storage->_is_guid_type($data_type);
56   }
57 };
58
59
60 sub next {
61   my $self = shift;
62
63   my @row = $self->next::method(@_);
64
65   $unpack_guids->(
66     $self->args->[1],
67     $self->{_colinfos} ||= fromspec_columns_info($self->args->[0]),
68     \@row,
69     $self->storage
70   );
71
72   return @row;
73 }
74
75 sub all {
76   my $self = shift;
77
78   my @rows = $self->next::method(@_);
79
80   $unpack_guids->(
81     $self->args->[1],
82     $self->{_colinfos} ||= fromspec_columns_info($self->args->[0]),
83     $_,
84     $self->storage
85   ) for @rows;
86
87
88   return @rows;
89 }
90
91 =head1 FURTHER QUESTIONS?
92
93 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
94
95 =head1 COPYRIGHT AND LICENSE
96
97 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
98 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
99 redistribute it and/or modify it under the same terms as the
100 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
101
102 =cut
103
104 1;
105
106 # vim:sts=2 sw=2: