How does DBIx :: Class handle boolean values?

I am using DBIx :: Class in my perl script to interact with sqlite database.

When performing insertions / searches, what will DBIx :: Class consider "true" and "false"?

eg:

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

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

Any help or documentation is appreciated (I could not find it in the DBIx :: Class docs, but maybe something is missing for me.

Thanks in advance.

+4
source share
4 answers

http://sqlite.org/datatype3.html says that SQLite does not have a logical data type, but false is stored as an integer 0 and true as an integer 1.

0
source

DBIx :: Class will handle true and false, as perl ... 0, '', undef, () and {} will be false, and something else will be true.

0
source

Since people find this useful, I thought I would choose the get_bool () function that I wrote to translate the perl value into an acceptable DBIx format:

 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
source

DBIx::Class handle boolean values ​​using DBIx :: Class :: InflateColumn :: Boolean :

Download this component and declare the columns as booleans.

  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 binds to the actual value of the field:

  print $table->foo; # prints "Y" if it is true 

UPD How to select rows whose fields are true
Since DBIx::Class uses SQL::Abstract to search for fields with a value of TRUE , you must call the unary unary operator bool :

 my %where = ( -bool => 'is_user', -not_bool => 'is_enabled', ); 

Would give you:

 WHERE is_user AND NOT is_enabled 
0
source

Source: https://habr.com/ru/post/1436247/


All Articles