2012-09-25 72 views
4

我在我的perl脚本中使用DBIx :: Class与sqlite数据库进行交互。DBIx :: Class如何处理布尔值?

当进行插入/搜索时,DBIx :: Class会认为'true'和'false'是什么?

如:

$schema->resultset('SomeObject')->create({ some_boolean => 1, primary_key => 1 });

$schema->resultset('SomeObject')->search({ some_boolean => 'true' });

任何帮助或文档赞赏(我一直没能找到它在DBIx ::类文档,但也许我错过一些东西。

在此先感谢。

回答

0

DBIx ::类会像对待真与假perl的方式呢.. 。0,'',undef,()和{}是“false”,而其他的都是“true”。

+0

什么null值?我认为这个类将undef视为NULL。 – Blaskovicz

+0

我不这么认为,但我找不到支持这两种方式的文档。 – harleypig

+0

我可以确认它将undef视为null。 – Blaskovicz

0

因为人们发现这是很有帮助的,我想我会在get_bool扔()函数I写了一个perl值翻译成DBIx可接受格式:与DBIx::Class::InflateColumn::Boolean帮助

sub get_bool { 
    my $self = shift; 
    my $bool = shift; 
    return undef if(!defined $bool || $bool =~ m/^null$/i); 
    return 0 if($bool =~ m/^(fail|not_ok|error|0)/i || $bool =~ m/^\s*$/); 
    return 1; 
} 
0

DBIx::Class手柄布尔值:

负荷此组件和声明为博列齐墩果值。

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"; 

布尔对象仍然stringifies实际字段值:

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