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