line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Types::SQL; |
2
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
997086
|
use v5.14; |
|
9
|
|
|
|
|
97
|
|
4
|
9
|
|
|
9
|
|
46
|
use warnings; |
|
9
|
|
|
|
|
20
|
|
|
9
|
|
|
|
|
516
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Type::Library |
7
|
9
|
|
|
|
|
99
|
-base, |
8
|
9
|
|
|
9
|
|
3915
|
-declare => qw/ BigInt Char Integer Numeric Serial SmallInt Text Varchar /; |
|
9
|
|
|
|
|
211992
|
|
9
|
|
|
|
|
|
|
|
10
|
9
|
|
|
9
|
|
21509
|
use Ref::Util qw/ is_arrayref /; |
|
9
|
|
|
|
|
14332
|
|
|
9
|
|
|
|
|
795
|
|
11
|
9
|
|
|
9
|
|
4117
|
use Type::Utils 0.44 -all; |
|
9
|
|
|
|
|
38930
|
|
|
9
|
|
|
|
|
62
|
|
12
|
9
|
|
|
9
|
|
35493
|
use Types::Standard -types; |
|
9
|
|
|
|
|
406529
|
|
|
9
|
|
|
|
|
89
|
|
13
|
9
|
|
|
9
|
|
46126
|
use PerlX::Maybe qw/ maybe /; |
|
9
|
|
|
|
|
21349
|
|
|
9
|
|
|
|
|
42
|
|
14
|
|
|
|
|
|
|
|
15
|
9
|
|
|
9
|
|
4890
|
use namespace::autoclean; |
|
9
|
|
|
|
|
155494
|
|
|
9
|
|
|
|
|
39
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# RECOMMEND PREREQ: PerlX::Maybe::XS |
18
|
|
|
|
|
|
|
# RECOMMEND PREREQ: Ref::Util::XS |
19
|
|
|
|
|
|
|
# RECOMMEND PREREQ: Type::Tiny::XS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# ABSTRACT: a library of SQL types |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $VERSION = 'v0.7.0'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $Blob = _generate_type( |
27
|
|
|
|
|
|
|
name => 'Blob', |
28
|
|
|
|
|
|
|
parent => Str, |
29
|
|
|
|
|
|
|
dbic_column_info => sub { |
30
|
|
|
|
|
|
|
my ($self) = @_; |
31
|
|
|
|
|
|
|
return ( |
32
|
|
|
|
|
|
|
is_numeric => 0, |
33
|
|
|
|
|
|
|
data_type => 'blob', |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
}, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our $Text = _generate_type( |
40
|
|
|
|
|
|
|
name => 'Text', |
41
|
|
|
|
|
|
|
parent => Str, |
42
|
|
|
|
|
|
|
dbic_column_info => sub { |
43
|
|
|
|
|
|
|
my ($self) = @_; |
44
|
|
|
|
|
|
|
return ( |
45
|
|
|
|
|
|
|
is_numeric => 0, |
46
|
|
|
|
|
|
|
data_type => 'text', |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
our $Varchar = _generate_type( |
53
|
|
|
|
|
|
|
name => 'Varchar', |
54
|
|
|
|
|
|
|
parent => $Text, |
55
|
|
|
|
|
|
|
constraint_generator => \&_size_constraint_generator, |
56
|
|
|
|
|
|
|
dbic_column_info => sub { |
57
|
|
|
|
|
|
|
my ( $self, $size ) = @_; |
58
|
|
|
|
|
|
|
my $parent = $self->parent->my_methods->{dbic_column_info}; |
59
|
|
|
|
|
|
|
return ( |
60
|
|
|
|
|
|
|
$parent->( $self->parent, $size // $self->type_parameter ), |
61
|
|
|
|
|
|
|
data_type => 'varchar', |
62
|
|
|
|
|
|
|
maybe size => $size // $self->type_parameter, |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
}, |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
our $Char = _generate_type( |
69
|
|
|
|
|
|
|
name => 'Char', |
70
|
|
|
|
|
|
|
parent => $Text, |
71
|
|
|
|
|
|
|
constraint_generator => \&_size_constraint_generator, |
72
|
|
|
|
|
|
|
dbic_column_info => sub { |
73
|
|
|
|
|
|
|
my ( $self, $size ) = @_; |
74
|
|
|
|
|
|
|
my $parent = $self->parent->my_methods->{dbic_column_info}; |
75
|
|
|
|
|
|
|
return ( |
76
|
|
|
|
|
|
|
$parent->( $self->parent, $size // $self->type_parameter // 1 ), |
77
|
|
|
|
|
|
|
data_type => 'char', |
78
|
|
|
|
|
|
|
size => $size // $self->type_parameter // 1, |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
}, |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
our $Integer = _generate_type( |
85
|
|
|
|
|
|
|
name => 'Integer', |
86
|
|
|
|
|
|
|
parent => Int, |
87
|
|
|
|
|
|
|
constraint_generator => \&_size_constraint_generator, |
88
|
|
|
|
|
|
|
dbic_column_info => sub { |
89
|
|
|
|
|
|
|
my ( $self, $size ) = @_; |
90
|
|
|
|
|
|
|
return ( |
91
|
|
|
|
|
|
|
data_type => 'integer', |
92
|
|
|
|
|
|
|
is_numeric => 1, |
93
|
|
|
|
|
|
|
maybe size => $size // $self->type_parameter, |
94
|
|
|
|
|
|
|
); |
95
|
|
|
|
|
|
|
}, |
96
|
|
|
|
|
|
|
); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
declare SmallInt, as Integer[5]; |
100
|
|
|
|
|
|
|
declare BigInt, as Integer[19]; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
our $Serial = _generate_type( |
104
|
|
|
|
|
|
|
name => 'Serial', |
105
|
|
|
|
|
|
|
parent => $Integer, |
106
|
|
|
|
|
|
|
constraint_generator => \&_size_constraint_generator, |
107
|
|
|
|
|
|
|
dbic_column_info => sub { |
108
|
|
|
|
|
|
|
my ( $self, $size ) = @_; |
109
|
|
|
|
|
|
|
my $parent = $self->parent->my_methods->{dbic_column_info}; |
110
|
|
|
|
|
|
|
return ( |
111
|
|
|
|
|
|
|
$parent->( $self->parent, $size // $self->type_parameter ), |
112
|
|
|
|
|
|
|
data_type => 'serial', |
113
|
|
|
|
|
|
|
is_auto_increment => 1, |
114
|
|
|
|
|
|
|
); |
115
|
|
|
|
|
|
|
}, |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
our $Numeric = _generate_type( |
120
|
|
|
|
|
|
|
name => 'Numeric', |
121
|
|
|
|
|
|
|
parent => Num, |
122
|
|
|
|
|
|
|
constraint_generator => \&_size_range_constraint_generator, |
123
|
|
|
|
|
|
|
dbic_column_info => sub { |
124
|
|
|
|
|
|
|
my ( $self, $size ) = @_; |
125
|
|
|
|
|
|
|
return ( |
126
|
|
|
|
|
|
|
data_type => 'numeric', |
127
|
|
|
|
|
|
|
is_numeric => 1, |
128
|
|
|
|
|
|
|
maybe size => $size // $self->parameters, |
129
|
|
|
|
|
|
|
); |
130
|
|
|
|
|
|
|
}, |
131
|
|
|
|
|
|
|
); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub _size_constraint_generator { |
134
|
29
|
50
|
|
29
|
|
31141
|
if (@_) { |
135
|
29
|
|
|
|
|
77
|
my ($size) = @_; |
136
|
29
|
100
|
|
|
|
207
|
die "Size must be a positive integer" unless $size =~ /^[1-9]\d*$/a; |
137
|
25
|
|
|
|
|
522
|
my $re = qr/^0*\d{1,$size}$/a; |
138
|
25
|
|
|
10
|
|
177
|
return sub { $_ =~ $re }; |
|
10
|
|
|
|
|
8829
|
|
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
else { |
141
|
0
|
|
|
0
|
|
0
|
return sub { $_ =~ /^\d+$/a }; |
|
0
|
|
|
|
|
0
|
|
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub _size_range_constraint_generator { |
146
|
3
|
50
|
|
3
|
|
8805
|
if (@_) { |
147
|
3
|
|
|
|
|
7
|
my ( $prec, $scale ) = @_; |
148
|
3
|
|
100
|
|
|
13
|
$scale //= 0; |
149
|
|
|
|
|
|
|
|
150
|
3
|
100
|
|
|
|
27
|
die "Precision must be a positive integer" unless $prec =~ /^[1-9]\d*$/a; |
151
|
2
|
50
|
|
|
|
8
|
die "Scale must be a positive integer" unless $scale =~ /^\d+$/a; |
152
|
|
|
|
|
|
|
|
153
|
2
|
|
|
|
|
5
|
my $left = $prec - $scale; |
154
|
2
|
50
|
|
|
|
5
|
die "Scale must be less than the precision" if ( $left < 0 ); |
155
|
|
|
|
|
|
|
|
156
|
2
|
|
|
|
|
75
|
my $re = qr/^0*\d{0,$left}([.]\d{0,$scale}0*)?$/a; |
157
|
2
|
|
|
9
|
|
19
|
return sub { $_ =~ $re }; |
|
9
|
|
|
|
|
6666
|
|
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
else { |
160
|
0
|
|
|
0
|
|
0
|
return sub { $_ =~ /^\d+$/a }; |
|
0
|
|
|
|
|
0
|
|
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub _generate_type { |
165
|
63
|
|
|
63
|
|
400
|
my %args = @_; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
$args{my_methods} = |
168
|
63
|
|
|
|
|
273
|
{ maybe dbic_column_info => delete $args{dbic_column_info}, }; |
169
|
|
|
|
|
|
|
|
170
|
63
|
|
|
|
|
378
|
my $type = Type::Tiny->new(%args); |
171
|
63
|
|
|
|
|
6859
|
__PACKAGE__->meta->add_type($type); |
172
|
63
|
|
|
|
|
34298
|
return $type; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
__END__ |