DBIx::Class
handle boolean values with help of DBIx::Class::InflateColumn::Boolean:
Load this component and declare columns as boolean values.
package Table;
__PACKAGE__->load_components(qw/InflateColumn::Boolean Core/);
__PACKAGE__->table('table');
__PACKAGE__->true_is('Y');
__PACKAGE__->add_columns(
foo => {
data_type => 'varchar',
is_boolean => 1,
},
bar => {
data_type => 'varchar',
is_boolean => 1,
true_is => qr/^(?:yes|ja|oui|si)$/i,
},
baz => {
data_type => 'int',
is_boolean => 1,
false_is => ['0', '-1'],
},
);
Then you can treat the specified column as a boolean:
print 'table.foo is ', $table->foo ? 'true' : 'false', "\n";
print 'table.bar is ', $table->bar ? 'true' : 'false', "\n";
The boolean object still stringifies to the actual field value:
print $table->foo; # prints "Y" if it is true
UPD How to select rows whose fields are true
Because DBIx::Class
uses SQL::Abstract
to search fields with TRUE
value you should refer to bool
unary operator:
my %where = (
-bool => 'is_user',
-not_bool => 'is_enabled',
);
Would give you:
WHERE is_user AND NOT is_enabled