| 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 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | package JsonSQL::Param::Table; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.4'; # VERSION | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use JsonSQL::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub new { | ||||||||||||||||||||||||||||
| 18 | 13 | ▶ | my ( $class, $tablehashref, $queryObj ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 13 | my $tableName = $tablehashref->{table}; | |||||||||||||||||||||||||||
| 21 | 13 | ▶ | if ( defined $tableName and $tableName =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $tableName and $tableName =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
MC/DC0%0/2
defined $tableName and $tableName =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
defined $tableName
$tableName =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
Truth table33%1/3
A and B
A= defined $tableNameB= $tableName =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
| |||||||||||||||||||||||||||||
| 22 | 13 | my $self = { | |||||||||||||||||||||||||||
| 23 | _tableName => $tableName | ||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | ## Schema is an optional parameter. Store it if it has been provided. | ||||||||||||||||||||||||||||
| 27 | 13 | ▶ | my $tableSchema = $tablehashref->{schema} || $queryObj->{_defaultSchema}; | ||||||||||||||||||||||||||
|
Condition100%3/3
$tablehashref->{'schema'} || $queryObj->{'_defaultSchema'}
MC/DC100%2/2
$tablehashref->{'schema'} || $queryObj->{'_defaultSchema'}
$tablehashref->{'schema'}
$queryObj->{'_defaultSchema'}
Truth table100%3/3
A || B
A= $tablehashref->{'schema'}B= $queryObj->{'_defaultSchema'}
| |||||||||||||||||||||||||||||
| 28 | 13 | ▶ | if ( defined $tableSchema and $tableSchema =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $tableSchema and $tableSchema =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
MC/DC50%1/2
defined $tableSchema and $tableSchema =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
defined $tableSchema
$tableSchema =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
Truth table66%2/3
A and B
A= defined $tableSchemaB= $tableSchema =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
| |||||||||||||||||||||||||||||
| 29 | 9 | $self->{_tableSchema} = $tableSchema; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | ## Check if table access is allowed based on $queryObj rule sets. | ||||||||||||||||||||||||||||
| 33 | 13 | my $validator = $queryObj->{_validator}; | |||||||||||||||||||||||||||
| 34 | 13 | my $table_rules = $validator->check_table_allowed({ schema => $self->{_tableSchema}, table => $self->{_tableName} }); | |||||||||||||||||||||||||||
| 35 | 13 | ▶ | if ( eval { $table_rules->is_error } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | 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 | $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 | bless $self, $class; | |||||||||||||||||||||||||||
| 52 | 11 | return $self; | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | 0 | return JsonSQL::Error->new("invalid_tables", "Invalid table name $tableName."); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub get_table_param { | ||||||||||||||||||||||||||||
| 60 | 9 | ▶ | my ( $self, $queryObj ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_table_param: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | ## Format the table string as schema.table if a schema has been defined. | ||||||||||||||||||||||||||||
| 63 | 9 | my $tableString; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 9 | ▶ | if (defined $self->{_tableSchema}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 6 | $tableString = $queryObj->quote_identifier($self->{_tableSchema}) . '.'; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ## Now add the table | ||||||||||||||||||||||||||||
| 70 | 9 | $tableString .= $queryObj->quote_identifier($self->{_tableName}); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | ## Return a scalar ref | ||||||||||||||||||||||||||||
| 73 | 9 | 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 | 2 | 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 | ||||||||||||||||||||||||||||