gpt4 book ai didi

perl - DBIx::Class 如何处理 bool 值?

转载 作者:行者123 更新时间:2023-12-04 21:47:12 25 4
gpt4 key购买 nike

我在 perl 脚本中使用 DBIx::Class 与 sqlite 数据库进行交互。

在进行插入/搜索时,DBIx::Class 会考虑“真”和“假”什么?

例如:
$schema->resultset('SomeObject')->create({ some_boolean => 1, primary_key => 1 });$schema->resultset('SomeObject')->search({ some_boolean => 'true' });
感谢任何帮助或文档(我无法在 DBIx::Class 文档中找到它,但也许我遗漏了一些东西。

提前致谢。

最佳答案

DBIx::ClassDBIx::Class::InflateColumn::Boolean 的帮助下处理 bool 值:

加载此组件并将列声明为 bool 值。

  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'],
},
);

然后您可以将指定的列视为 bool 值:
  print 'table.foo is ', $table->foo ? 'true' : 'false', "\n";
print 'table.bar is ', $table->bar ? 'true' : 'false', "\n";

bool 对象仍然字符串化为实际的字段值:
  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

关于perl - DBIx::Class 如何处理 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12592241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com