DBIx::Class
在DBIx::Class::InflateColumn::Boolean的帮助下处理布尔值:
加载此组件并将列声明为布尔值。
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'],
},
);
然后您可以将指定的列视为布尔值:
print 'table.foo is ', $table->foo ? 'true' : 'false', "\n";
print 'table.bar is ', $table->bar ? 'true' : 'false', "\n";
布尔对象仍然字符串化为实际的字段值:
print $table->foo; # prints "Y" if it is true
UPD如何选择字段为真的行
因为DBIx::Class
用于SQL::Abstract
搜索具有TRUE
值的字段,所以您应该参考bool
一元运算符:
my %where = (
-bool => 'is_user',
-not_bool => 'is_enabled',
);
会给你:
WHERE is_user AND NOT is_enabled