blib/lib/DBIx/QuickORM/Value.pm

filter: Statementstmt 80.0% 28 / 35 Branchbran 50.0% 4 / 8 Conditioncond 41.6% 5 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 83.3% 10 / 12 Podpod 100% 5 / 5 totaltotal 69.2% 54 / 78 CC 9 SCAR 12.6
CC
9
Cov
67%
sql
6.6
sql
6.6
_mark
2.5
CRAP 3.5
« blib/lib/DBIx/QuickORM/Dialect/SQLite.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm »
1 package DBIx::QuickORM::Value;
2 267 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 267 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 267 our $VERSION = '0.000033';
6  
7 267 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 267 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 267 our @EXPORT_OK = qw/raw inflated binary sql/;
11  
12 267 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
13 <kind
14 <value
15 <binds
16 267 };
17  
18 =pod
19  
20 =encoding UTF-8
21  
22 =head1 NAME
23  
24 DBIx::QuickORM::Value - Mark how one value is sent to the database.
25  
26 =head1 DESCRIPTION
27  
28 Wrapping a value in a marker says how the ORM treats that one value. A marker
29 overrides the call's C<raw> flag and the usual deflation rules (see
30 L<DBIx::QuickORM/"CRUD operations">).
31  
32 Markers work in inserted values, changes, compare-and-swap values, primary key
33 values, a handle's bulk changes, and as values in a handle's C<where>, where
34 the SQL builder turns them into SQL and binds.
35  
36 =head1 SYNOPSIS
37  
38 use DBIx::QuickORM::Value qw/raw inflated binary sql/;
39  
40 my $ann = $orm->insert(
41 users => {
42 name => 'Ann',
43 created => sql('now()'),
44 prefs => raw('{"theme":"dark"}'),
45 },
46 );
47  
48 # Every value is in database form (raw => 1) except prefs, which still needs deflating.
49 $orm->insert(
50 users => {
51 name => 'Bo',
52 created => '2024-01-02 03:04:05',
53 prefs => inflated({theme => 'light'}),
54 },
55 raw => 1,
56 );
57  
58 my @today = $orm->handle('users')->where({created => {'>=' => sql('CURRENT_DATE')}})->all;
59  
60 # A query knows no field types, so mark a binary value (files has a BYTEA data column).
61 my @same = $orm->handle(\'SELECT * FROM files')->where({data => binary($bytes)})->all;
62  
63 =head1 EXPORTS
64  
65 All are exported on request. C<raw>, C<inflated>, and C<binary> each croak
66 unless given exactly one value.
67  
68 =over 4
69  
70 =item $marker = raw($value)
71  
72 Already in database form: never deflated.
73  
74 Also the way to send a reference to a field without a type, such as a
75 PostgreSQL array.
76  
77 # tags is a TEXT[] column of a labels table
78 my $label = $orm->insert(labels => {tags => raw(['perl', 'orm'])});
79  
80 =item $marker = inflated($value)
81  
82 Deflated through the field's type, even under C<< raw => 1 >> and even when
83 the type cannot tell the value is inflated.
84  
85 use DateTime;
86  
87 # A where value is sent as given, so mark one to deflate it.
88 my $cutoff = DateTime->now->subtract(days => 30);
89 my @recent = $orm->handle('users')->where({created => {'>=' => inflated($cutoff)}})->all;
90  
91 The type still decides: on a JSON field a plain value is JSON text, sent as
92 is (see L<DBIx::QuickORM::Type::JSON>).
93  
94 =item $marker = binary($value)
95  
96 Raw, and bound as binary whatever the field.
97  
98 =back
99  
100 =cut
101  
102 36 ▶sub raw { __PACKAGE__->_mark(raw => @_) }
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
103 23 ▶sub inflated { __PACKAGE__->_mark(inflated => @_) }
Subroutine inflated: called CC=1 SCAR=0.0
Pod documented
104 25 ▶sub binary { __PACKAGE__->_mark(binary => @_) }
Subroutine binary: called CC=1 SCAR=0.0
Pod documented
105  
106 =pod
107  
108 =over 4
109  
110 =item $marker = sql($sql, @binds)
111  
112 Literal SQL in place of the value, used as written: nothing in it is quoted,
113 and its binds are sent without types.
114  
115 $post->update({views => sql('views + ?', 10)});
116 my $views = $post->field('views'); # 10 more than before
117  
118 Croaks unless the SQL is a non-blank string.
119  
120 In inserted values and changes the field's new value comes back through
121 C<RETURNING>, except from an insert with C<< as_given => 1 >>, which leaves it
122 out of the row. A database without it (MySQL and MariaDB, for an update) leaves
123 the field out of the row too. Either way, a row object's first read of it
124 fetches it with one more query.
125  
126 It croaks in compare-and-swap values and primary key lookups. A primary key
127 set by it in an update without C<RETURNING> (MySQL, MariaDB) croaks too, since
128 the new key could not be known. An insert without C<RETURNING> falls back to
129 the last insert id for a single-field key, as C<insert> describes.
130  
131 =back
132  
133 =cut
134  
135 121 ▶sub sql {
Subroutine sql: called CC=4 SCAR=18.9
Pod documented
136 0 ▶ my ($sql, @binds) = @_;
Subroutine sql: not called CC=4 SCAR=18.9
Pod documented
137 0 ▶ croak "sql() needs the SQL as a string" unless defined($sql) && !ref($sql) && $sql =~ m/\S/;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $sql and not ref $sql and $sql =~ /\S/ 3 118 121
unless defined $sql and not ref $sql and $sql =~ /\S/ 0 0 0
Condition41%5/12
defined $sql and not ref $sql and $sql =~ /\S/
!ll&&!rl&&r
12118
defined $sql and not ref $sql
!ll&&!rl&&r
01120
defined $sql and not ref $sql and $sql =~ /\S/
!ll&&!rl&&r
000
defined $sql and not ref $sql
!ll&&!rl&&r
000
MC/DC66%2/3
defined $sql and not ref $sql and $sql =~ /\S/
defined $sql not ref $sql $sql =~ /\S/
MC/DC0%0/3
defined $sql and not ref $sql and $sql =~ /\S/
defined $sql not ref $sql $sql =~ /\S/
Truth table75%3/4
A and not(B) and C
A= defined $sqlB= not ref $sqlC= $sql =~ /\S/
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and C
A= defined $sqlB= not ref $sqlC= $sql =~ /\S/
ABCresult
0XX0
10X0
1100
1111
138 0 return __PACKAGE__->new(kind => 'sql', value => $sql, binds => \@binds);
139 }
140  
141 =head1 ATTRIBUTES
142  
143 =over 4
144  
145 =item $kind = $marker->kind
146  
147 C<raw>, C<inflated>, C<binary>, or C<sql>.
148  
149 =item $value = $marker->value
150  
151 The value, or the SQL of an C<sql> marker.
152  
153 =item \@binds = $marker->binds
154  
155 The binds of an C<sql> marker.
156  
157 =back
158  
159 =head1 PRIVATE METHODS
160  
161 =over 4
162  
163 =item $marker = DBIx::QuickORM::Value->_mark($kind, $value)
164  
165 A marker of the kind for the value. Croaks unless given exactly one value.
166  
167 =back
168  
169 =cut
170  
171 84 ▶sub _mark {
Subroutine _mark: called CC=2 SCAR=9.2
172 0 ▶ my $class = shift;
Subroutine _mark: not called CC=2 SCAR=9.2
173 0 my ($kind, @args) = @_;
174 0 ▶ croak "$kind() takes exactly one value" unless @args == 1;
Branch: 2 branches
BranchTrueFalseTotal
unless @args == 1 3 81 84
unless @args == 1 0 0 0
175 0 return $class->new(kind => $kind, value => $args[0]);
176 }
177  
178 267 1;
179  
180 __END__
« blib/lib/DBIx/QuickORM/Dialect/SQLite.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm »