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/Type/DateTime.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm »
1 package DBIx::QuickORM::Value;
2 309 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 309 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 309 our $VERSION = '0.000032';
6  
7 309 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 309 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 309 our @EXPORT_OK = qw/raw inflated binary sql/;
11  
12 309 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
13 <kind
14 <value
15 <binds
16 309 };
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 =item $marker = inflated($value)
78  
79 Deflated through the field's type, even under C<< raw => 1 >> and even when
80 the type cannot tell the value is inflated.
81  
82 The type still decides: on a JSON field a plain value is JSON text, sent as
83 is (see L<DBIx::QuickORM::Type::JSON>).
84  
85 =item $marker = binary($value)
86  
87 Raw, and bound as binary whatever the field.
88  
89 =back
90  
91 =cut
92  
93 35 ▶sub raw { __PACKAGE__->_mark(raw => @_) }
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
94 21 ▶sub inflated { __PACKAGE__->_mark(inflated => @_) }
Subroutine inflated: called CC=1 SCAR=0.0
Pod documented
95 15 ▶sub binary { __PACKAGE__->_mark(binary => @_) }
Subroutine binary: called CC=1 SCAR=0.0
Pod documented
96  
97 =pod
98  
99 =over 4
100  
101 =item $marker = sql($sql, @binds)
102  
103 Literal SQL in place of the value, used as written: nothing in it is quoted,
104 and its binds are sent without types. Croaks unless the SQL is a non-blank
105 string.
106  
107 In inserted values and changes the field's new value then comes back through
108 C<RETURNING>, or is not fetched.
109  
110 It croaks in compare-and-swap values and primary key lookups. A primary key
111 set by it in an update without C<RETURNING> (MySQL, MariaDB) croaks too, since
112 the new key could not be known. An insert without C<RETURNING> falls back to
113 the last insert id for a single-field key, as C<insert> describes.
114  
115 =back
116  
117 =cut
118  
119 106 ▶sub sql {
Subroutine sql: called CC=4 SCAR=18.9
Pod documented
120 0 ▶ my ($sql, @binds) = @_;
Subroutine sql: not called CC=4 SCAR=18.9
Pod documented
121 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 103 106
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
12103
defined $sql and not ref $sql
!ll&&!rl&&r
01105
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
122 0 return __PACKAGE__->new(kind => 'sql', value => $sql, binds => \@binds);
123 }
124  
125 =head1 ATTRIBUTES
126  
127 =over 4
128  
129 =item $kind = $marker->kind
130  
131 C<raw>, C<inflated>, C<binary>, or C<sql>.
132  
133 =item $value = $marker->value
134  
135 The value, or the SQL of an C<sql> marker.
136  
137 =item \@binds = $marker->binds
138  
139 The binds of an C<sql> marker.
140  
141 =back
142  
143 =head1 PRIVATE METHODS
144  
145 =over 4
146  
147 =item $marker = DBIx::QuickORM::Value->_mark($kind, $value)
148  
149 A marker of the kind for the value. Croaks unless given exactly one value.
150  
151 =back
152  
153 =cut
154  
155 71 ▶sub _mark {
Subroutine _mark: called CC=2 SCAR=9.2
156 0 ▶ my $class = shift;
Subroutine _mark: not called CC=2 SCAR=9.2
157 0 my ($kind, @args) = @_;
158 0 ▶ croak "$kind() takes exactly one value" unless @args == 1;
Branch: 2 branches
BranchTrueFalseTotal
unless @args == 1 3 68 71
unless @args == 1 0 0 0
159 0 return $class->new(kind => $kind, value => $args[0]);
160 }
161  
162 309 1;
163  
164 __END__
« blib/lib/DBIx/QuickORM/Type/DateTime.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm »