kentfredric (owner)

Revisions

gist: 222959 Download_button fork
public
Public Clone URL: git://gist.github.com/222959.git
Embed All Files: show embed
Diff #
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Index: t/03crosstable_leak.t
===================================================================
--- t/03crosstable_leak.t (revision 0)
+++ t/03crosstable_leak.t (revision 0)
@@ -0,0 +1,46 @@
+#! /usr/bin/perl -w
+
+use strict;
+use warnings;
+use Test::More tests => 6;
+use Digest;
+
+use File::Spec;
+use FindBin '$Bin';
+use lib File::Spec->catdir( $Bin, 'lib' );
+
+#1
+use_ok("DigestTest");
+
+# ABOUT THIS TEST;
+#
+# TableA is not encoded.
+# TableB is encoded.
+#
+# Both share a field with the same name.
+#
+# This test is to demonstrate, that one is inheriting the encoding options wrongly from the other.
+#
+
+my $schema = DigestTest->init_schema;
+my $tablea = $schema->resultset('TableA');
+my $tableb = $schema->resultset('TableB');
+
+my $objecta = $tablea->create( { conflicting_name => 'foo' } );
+my $objectb = $tableb->create( { conflicting_name => 'bar' } );
+
+is( $objecta->conflicting_name, 'foo', 'Table requested to not be encoded is not encoded' );
+unlike( $objectb->conflicting_name, qr/^(bar|foo)$/, 'Table requested to be encoded is encoded' );
+
+is( $objecta->can('check_conflict'), undef, 'Table that is requested to not be encoded has no check_conflict method' );
+ok( $objectb->can('check_conflict'), 'Table that is requested encoded has check_conflict method' );
+
+ok( $objectb->check_conflict( 'bar') , 'Hash-digest validation on encoded fields still works');
+
+END {
+
+ # In the END section so that the test DB file gets closed before we attempt to unlink it
+ DigestTest::clear($schema);
+}
+
+1;
Index: t/lib/DigestTest/Schema/TableA.pm
===================================================================
--- t/lib/DigestTest/Schema/TableA.pm (revision 0)
+++ t/lib/DigestTest/Schema/TableA.pm (revision 0)
@@ -0,0 +1,25 @@
+package # hide from PAUSE
+ DigestTest::Schema::TableA;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/EncodedColumn Core/);
+__PACKAGE__->table('tablea');
+__PACKAGE__->add_columns(
+ id => {
+ data_type => 'int',
+ is_nullable => 0,
+ is_auto_increment => 1
+ },
+ conflicting_name => {
+ data_type => 'char',
+ size => 43,
+ encode_column => 0,
+ encode_class => 'Digest',
+ encode_check_method => 'check_conflict',
+ },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;
Index: t/lib/DigestTest/Schema/TableB.pm
===================================================================
--- t/lib/DigestTest/Schema/TableB.pm (revision 0)
+++ t/lib/DigestTest/Schema/TableB.pm (revision 0)
@@ -0,0 +1,25 @@
+package # hide from PAUSE
+ DigestTest::Schema::TableB;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/EncodedColumn Core/);
+__PACKAGE__->table('tableb');
+__PACKAGE__->add_columns(
+ id => {
+ data_type => 'int',
+ is_nullable => 0,
+ is_auto_increment => 1
+ },
+ conflicting_name => {
+ data_type => 'char',
+ size => 43,
+ encode_column => 1,
+ encode_class => 'Digest',
+ encode_check_method => 'check_conflict',
+ },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;
Index: t/lib/DigestTest/Schema.pm
===================================================================
--- t/lib/DigestTest/Schema.pm (revision 7824)
+++ t/lib/DigestTest/Schema.pm (working copy)
@@ -3,6 +3,6 @@
 
 use base qw/DBIx::Class::Schema/;
 
-__PACKAGE__->load_classes(qw/Test/);
+__PACKAGE__->load_classes(qw/Test TableA TableB/);
 
 1;
Index: lib/DBIx/Class/EncodedColumn/Crypt/Eksblowfish/Bcrypt.pm
===================================================================
--- lib/DBIx/Class/EncodedColumn/Crypt/Eksblowfish/Bcrypt.pm (revision 7824)
+++ lib/DBIx/Class/EncodedColumn/Crypt/Eksblowfish/Bcrypt.pm (working copy)
@@ -41,7 +41,7 @@
   #fast fast fast
   return eval qq^ sub {
     my \$col_v = \$_[0]->get_column('${col}');
- \$_[0]->_column_encoders->{${col}}->(\$_[1], \$col_v) eq \$col_v;
+ \$_[0]->_column_encoders->{ \$_[0]->result_class }->{${col}}->(\$_[1], \$col_v) eq \$col_v;
   } ^ || die($@);
 }
 
Index: lib/DBIx/Class/EncodedColumn/Digest.pm
===================================================================
--- lib/DBIx/Class/EncodedColumn/Digest.pm (revision 7824)
+++ lib/DBIx/Class/EncodedColumn/Digest.pm (working copy)
@@ -68,7 +68,7 @@
   return eval qq^ sub {
     my \$col_v = \$_[0]->get_column('${col}');
     my \$salt = substr(\$col_v, ${len});
- \$_[0]->_column_encoders->{${col}}->(\$_[1], \$salt) eq \$col_v;
+ \$_[0]->_column_encoders->{\$_[0]->result_class}->{${col}}->(\$_[1], \$salt) eq \$col_v;
   } ^ || die($@);
 }
 
Index: lib/DBIx/Class/EncodedColumn.pm
===================================================================
--- lib/DBIx/Class/EncodedColumn.pm (revision 7824)
+++ lib/DBIx/Class/EncodedColumn.pm (working copy)
@@ -14,8 +14,8 @@
 sub register_column {
   my $self = shift;
   my ($column, $info) = @_;
+
   $self->next::method(@_);
-
   return unless exists $info->{encode_column} && $info->{encode_column} == 1;
   $self->throw_exception("'encode_class' is a required argument.")
     unless exists $info->{encode_class} && defined $info->{encode_class};
@@ -31,7 +31,7 @@
 
   defined( my $encode_sub = eval{ $class->make_encode_sub($column, $args) }) ||
     $self->throw_exception("Failed to create encoder with class '$class': $@");
- $self->_column_encoders->{$column} = $encode_sub;
+ $self->_column_encoders->{ $self->result_class }->{$column} = $encode_sub;
 
   if ( exists $info->{encode_check_method} && $info->{encode_check_method} ){
     no strict 'refs';
@@ -44,7 +44,7 @@
 
 sub set_column {
   my $self = shift;
- my $encs = $self->_column_encoders;
+ my $encs = $self->_column_encoders->{ $self->result_class };
   if(exists $encs->{$_[0]} && defined(my $encoder = $encs->{$_[0]})){
     return $self->next::method($_[0], $encoder->($_[1]));
   }
@@ -53,7 +53,7 @@
 
 sub new {
   my($self, $attr, @rest) = @_;
- my $encoders = $self->_column_encoders;
+ my $encoders = $self->_column_encoders->{ $self->result_class };
   for my $col (grep { defined $encoders->{$_} } keys %$encoders ) {
     next unless exists $attr->{$col} && defined $attr->{$col};
     $attr->{$col} = $encoders->{$col}->( $attr->{$col} );
Index: Changes
===================================================================
--- Changes (revision 7824)
+++ Changes (working copy)
@@ -1,3 +1,4 @@
+ - Fix intra-table digest collisons ( Kent Fredric )
         - Fix build_requires version number for SQLA ( fREW )
 
 0.00005 2009-10-11