line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: JsonSQL::Param::Table object. Stores a Perl representation of an SQL table expression for use in JsonSQL::Query objects. |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
46
|
|
6
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
38
|
|
7
|
2
|
|
|
2
|
|
24
|
use 5.014; |
|
2
|
|
|
|
|
5
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package JsonSQL::Param::Table; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.4'; # VERSION |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
17
|
use JsonSQL::Error; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
493
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
18
|
13
|
|
|
13
|
1
|
33
|
my ( $class, $tablehashref, $queryObj ) = @_; |
19
|
|
|
|
|
|
|
|
20
|
13
|
|
|
|
|
27
|
my $tableName = $tablehashref->{table}; |
21
|
13
|
50
|
33
|
|
|
109
|
if ( defined $tableName and $tableName =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ ) { |
22
|
13
|
|
|
|
|
38
|
my $self = { |
23
|
|
|
|
|
|
|
_tableName => $tableName |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
## Schema is an optional parameter. Store it if it has been provided. |
27
|
13
|
|
100
|
|
|
53
|
my $tableSchema = $tablehashref->{schema} || $queryObj->{_defaultSchema}; |
28
|
13
|
100
|
66
|
|
|
64
|
if ( defined $tableSchema and $tableSchema =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ ) { |
29
|
9
|
|
|
|
|
26
|
$self->{_tableSchema} = $tableSchema; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
## Check if table access is allowed based on $queryObj rule sets. |
33
|
13
|
|
|
|
|
21
|
my $validator = $queryObj->{_validator}; |
34
|
13
|
|
|
|
|
65
|
my $table_rules = $validator->check_table_allowed({ schema => $self->{_tableSchema}, table => $self->{_tableName} }); |
35
|
13
|
100
|
|
|
|
34
|
if ( eval { $table_rules->is_error } ) { |
|
13
|
|
|
|
|
109
|
|
36
|
2
|
|
|
|
|
10
|
return JsonSQL::Error->new("invalid_tables", "Error validating table $self->{_tableName}: $table_rules->{message}"); |
37
|
|
|
|
|
|
|
} else { |
38
|
|
|
|
|
|
|
## Save a reference to the $table_rules for future column processing. |
39
|
11
|
|
|
|
|
328
|
$self->{_tableRules} = $table_rules; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
## This is for the future. |
43
|
|
|
|
|
|
|
# The current SQL::Maker code doesn't support aliases for tables, but most DB backends allow it. |
44
|
|
|
|
|
|
|
# Will be able to enable this when the SQL::Maker code gets replaced. |
45
|
|
|
|
|
|
|
#my $tableAlias = $tablehashref->{alias}; |
46
|
|
|
|
|
|
|
#if (defined $tableAlias and $tableAlias =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) { |
47
|
|
|
|
|
|
|
# $self->{_tableAlias} = $tableAlias; |
48
|
|
|
|
|
|
|
#} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
## Return the blessed reference. |
51
|
11
|
|
|
|
|
25
|
bless $self, $class; |
52
|
11
|
|
|
|
|
37
|
return $self; |
53
|
|
|
|
|
|
|
} else { |
54
|
0
|
|
|
|
|
0
|
return JsonSQL::Error->new("invalid_tables", "Invalid table name $tableName."); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub get_table_param { |
60
|
9
|
|
|
9
|
1
|
22
|
my ( $self, $queryObj ) = @_; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
## Format the table string as schema.table if a schema has been defined. |
63
|
9
|
|
|
|
|
13
|
my $tableString; |
64
|
|
|
|
|
|
|
|
65
|
9
|
100
|
|
|
|
28
|
if (defined $self->{_tableSchema}) { |
66
|
6
|
|
|
|
|
21
|
$tableString = $queryObj->quote_identifier($self->{_tableSchema}) . '.'; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
## Now add the table |
70
|
9
|
|
|
|
|
29
|
$tableString .= $queryObj->quote_identifier($self->{_tableName}); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
## Return a scalar ref |
73
|
9
|
|
|
|
|
24
|
return $tableString; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
## When table aliases become possible... |
76
|
|
|
|
|
|
|
# if (defined $self->{_tableAlias}) { |
77
|
|
|
|
|
|
|
# return { $tableString => $self->{_tableAlias} }; |
78
|
|
|
|
|
|
|
# } else { |
79
|
|
|
|
|
|
|
# return $tableString; |
80
|
|
|
|
|
|
|
# } |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=pod |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=encoding UTF-8 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 NAME |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
JsonSQL::Param::Table - JsonSQL::Param::Table object. Stores a Perl representation of an SQL table expression for use in JsonSQL::Query objects. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 VERSION |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
version 0.4 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 SYNOPSIS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
This module constructs a Perl object representing a table identifier for use in SQL queries. It has a method for |
103
|
|
|
|
|
|
|
extracting the parameters to generate the appropriate SQL string. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 DESCRIPTION |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head3 Object properties: |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=over |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item _tableName => <string> |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item _tableSchema => <string> |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=back |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head3 Generated parameters: |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=over |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item $tableString => <string> |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=back |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 METHODS |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 Constructor new($tablehashref, $queryObj) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Instantiates and returns a new JsonSQL::Param::Table object. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
$tablehashref => A hashref of table/schema properties used to construct the object. |
132
|
|
|
|
|
|
|
$queryObj => A reference to the JsonSQL::Query object that will own this object. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Returns a JsonSQL::Error object on failure. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 ObjectMethod get_table_param -> $tableString |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Generates parameters represented by the object for the SQL statement. Returns: |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
$tableString => The SQL table identifier as a quoted string. Includes schema as appropriate. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 AUTHOR |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Chris Hoefler <bhoefler@draper.com> |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Chris Hoefler. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
151
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |