| 1 | package Crop::Object::Warehouse::Lang::SQL::Query::Select; | ||
| 2 | use base qw/ Crop::Object::Warehouse::Lang::SQL::Query /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Warehouse::Lang::SQL::Query::Select | ||
| 6 | The SELECT query. | ||
| 7 | |||
| 8 | The point is to parse declaration to the inner presentation and to store it in the 'parsed' attribute. | ||
| 9 | =cut | ||
| 10 | |||
| 11 | use v5.14; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Crop::Error; | ||
| 15 | use Crop::Util 'load_class'; | ||
| 16 | use Crop::Object::Constants; | ||
| 17 | use Crop::Object::Collection; | ||
| 18 | use Crop::Object::Warehouse::Lang::SQL::Query::Select::Node; | ||
| 19 | use Crop::Object::Warehouse::Lang::SQL::Query::Select::Node::Root; | ||
| 20 | use Crop::Object::Warehouse::Lang::SQL::Clause; | ||
| 21 | |||
| 22 | use Crop::Debug; | ||
| 23 | |||
| 24 | =begin nd | ||
| 25 | Variable: our %Attributes | ||
| 26 | Class members: | ||
| 27 | |||
| 28 | clause - WHERE clause for initial part; | ||
| 29 | expression - raw expression to execute directly after 'SELECT' | ||
| 30 | ext - arrayref to EXT in form of [doc => {visible => 1} => ['file'] | ||
| 31 | limit - LIMIT restriction, such 'LIMIT 5'; optional string or undef | ||
| 32 | offset - SQL offset | ||
| 33 | order - optinal 'order by' clause including asc/desc directive; arrayref with item for each field | ||
| 34 | parsed - inner tree presents the Query; see <Crop::Object::Warehouse::Lang::SQL::Query::Select::Node>. | ||
| 35 | Value of 'parsed' is a top level Root node. | ||
| 36 | stack - parsed nodes; arrayref of <Crop::Object::Warehouse::Lang::SQL::Query::Select::Node> | ||
| 37 | start_class - class of the main table | ||
| 38 | table - hash of real tables used in query already; values is a number of privious use | ||
| 39 | value - array of values that uses 'WHERE' for their clauses; has personal getter 'val' | ||
| 40 | =cut | ||
| 41 | our %Attributes = ( | ||
| 42 | clause => undef, | ||
| 43 | expression => undef, | ||
| 44 | ext => {default => []}, | ||
| 45 | limit => undef, | ||
| 46 | offset => undef, | ||
| 47 | order => undef, | ||
| 48 | parsed => {mode => 'read'}, | ||
| 49 | stack => {default => []}, | ||
| 50 | start_class => undef, | ||
| 51 | table => undef, | ||
| 52 | value => {default => []}, # has a personal getter 'val' | ||
| 53 | ); | ||
| 54 | |||
| 55 | =begin nd | ||
| 56 | Method: build ( ) | ||
| 57 | Parse incomming structure to the inner presentation. | ||
| 58 | |||
| 59 | Returns: | ||
| 60 | $self; | ||
| 61 | =cut | ||
| 62 | sub build { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | return $self if defined $self->{expression}; | ||
| 66 | |||
| 67 | my $root = Crop::Object::Warehouse::Lang::SQL::Query::Select::Node::Root->new( | ||
| 68 | class => $self->{start_class}, | ||
| 69 | ); | ||
| 70 | |||
| 71 | ++$self->{table}{$root->table}; | ||
| 72 | |||
| 73 | $root->clause(Crop::Object::Warehouse::Lang::SQL::Clause->new($self->{clause})) if $self->{clause}; | ||
| 74 | |||
| 75 | $self->{parsed} = $root; | ||
| 76 | push @{$self->{stack}}, $root; | ||
| 77 | |||
| 78 | my $table = $root->table_effective; | ||
| 79 | if (defined $self->{order}) { | ||
| 80 | /\./ or $_ = "$table.$_" for @{$self->{order}}; | ||
| 81 | } | ||
| 82 | |||
| 83 | $self->_parse_ext($self->{ext}); | ||
| 84 | } | ||
| 85 | |||
| 86 | =begin nd | ||
| 87 | Method: DESTROY ( ) | ||
| 88 | Clean up recursive references. | ||
| 89 | =cut | ||
| 90 | sub DESRTOY { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | $self->foreach_prepared($self->{parsed}, sub { | ||
| 94 | my $node = shift; | ||
| 95 | |||
| 96 | $node->parent(undef); | ||
| 97 | }); | ||
| 98 | } | ||
| 99 | |||
| 100 | =begin nd | ||
| 101 | Method: foreach_prepared ($node, $cb) | ||
| 102 | Execute routine $cb for each $node. | ||
| 103 | =cut | ||
| 104 | sub foreach_prepared { | ||
| 105 | my ($self, $node, $cb) = @_; | ||
| 106 | |||
| 107 | $cb->($node); | ||
| 108 | $self->foreach_prepared($_, $cb) for $node->child->List; | ||
| 109 | } | ||
| 110 | |||
| 111 | =begin nd | ||
| 112 | Method: _parse_ext ($ext) | ||
| 113 | Parse EXT for current node. | ||
| 114 | |||
| 115 | Could be called recursively for each arrayref it the EXT-tree. | ||
| 116 | |||
| 117 | Parameters: | ||
| 118 | $ext - arrayref to the extended declaration in form of call. | ||
| 119 | > $ext = [doc => {visible => 1} => ['file'], qw/ picture units /] | ||
| 120 | |||
| 121 | Returns: | ||
| 122 | $self - if ok | ||
| 123 | undef - otherwise | ||
| 124 | =cut | ||
| 125 | sub _parse_ext { | ||
| 126 | my ($self, $ext) = @_; | ||
| 127 | |||
| 128 | return $self unless defined $ext; | ||
| 129 | |||
| 130 | my $i = 0; | ||
| 131 | while ($i < @$ext) { | ||
| 132 | return warn 'OBJECT: Extend of non-scalar name is prohibited' if ref $ext->[$i]; | ||
| 133 | |||
| 134 | my $parent = $self->{stack}[-1]; | ||
| 135 | my $extern = $parent->class->Attributes->extern($ext->[$i]); | ||
| 136 | |||
| 137 | ++$i; # next token | ||
| 138 | |||
| 139 | my $clause; | ||
| 140 | if ($i < @$ext and ref $ext->[$i] eq 'HASH') { | ||
| 141 | $clause = Crop::Object::Warehouse::Lang::SQL::Clause->new($ext->[$i]); | ||
| 142 | ++$i; | ||
| 143 | } | ||
| 144 | |||
| 145 | my $node = $extern->make_link($parent); | ||
| 146 | $node->clause($clause) if $clause; | ||
| 147 | |||
| 148 | my $child; | ||
| 149 | $child = $ext->[$i] if $i < @$ext and ref $ext->[$i] eq 'ARRAY'; | ||
| 150 | if ($child) { | ||
| 151 | push @{$self->{stack}}, $node; | ||
| 152 | $self->_parse_ext($child); | ||
| 153 | pop @{$self->{stack}}; | ||
| 154 | |||
| 155 | ++$i; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | $self; | ||
| 160 | } | ||
| 161 | |||
| 162 | =begin nd | ||
| 163 | Method: print_sql ( ) | ||
| 164 | Compose query string based on the inner presentation. | ||
| 165 | |||
| 166 | Returns: | ||
| 167 | SQL string + values | ||
| 168 | =cut | ||
| 169 | sub print_sql { | ||
| 170 | my $self = shift; | ||
| 171 | |||
| 172 | my $query = 'SELECT '; | ||
| 173 | |||
| 174 | if (defined $self->{expression}) { | ||
| 175 | $query .= $self->{expression}; | ||
| 176 | } else { | ||
| 177 | my @from; | ||
| 178 | my @field; | ||
| 179 | my @where; | ||
| 180 | $self->foreach_prepared($self->{parsed}, sub { | ||
| 181 | my $node = shift; | ||
| 182 | |||
| 183 | for (@{$node->attr}) { | ||
| 184 | push @field, $node->table_effective . '.' . $_->name . ' AS ' . $node->table_effective . '$' . $_->name; | ||
| 185 | } | ||
| 186 | |||
| 187 | my $table; | ||
| 188 | my $parent = $node->parent; | ||
| 189 | $table .= 'LEFT JOIN ' if $parent; | ||
| 190 | $table .= $node->table; | ||
| 191 | $table .= ' AS ' . $node->table_effective if $node->table_effective ne $node->table; | ||
| 192 | if ($parent) { | ||
| 193 | $table .= ' ON '; | ||
| 194 | |||
| 195 | my @link; | ||
| 196 | while (my ($src, $dst) = each %{$node->parent_link}) { | ||
| 197 | push @link, $parent->table_effective . '.' . $src . ' = ' . $node->table_effective . '.' . $dst; | ||
| 198 | } | ||
| 199 | $table .= join ' AND ', @link; | ||
| 200 | } | ||
| 201 | push @from, $table; | ||
| 202 | |||
| 203 | if ($node->clause) { | ||
| 204 | my ($sql, $val) = $node->clause->print_sql($node->table_effective); # clause->print_sql returns two-element array | ||
| 205 | |||
| 206 | if ($sql) { | ||
| 207 | push @where, $sql; | ||
| 208 | push @{$self->{value}}, @$val; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | }); | ||
| 212 | |||
| 213 | $query .= join ', ', @field; | ||
| 214 | $query .= " FROM @from"; | ||
| 215 | $query .= " WHERE " . join ' AND ', @where if @where; | ||
| 216 | $query .= ' ORDER BY ' . join ' AND ', @{$self->{order}} if defined $self->{order}; | ||
| 217 | $query .= " OFFSET $self->{offset} " if defined $self->{offset}; | ||
| 218 | $query .= " LIMIT $self->{limit}" if defined $self->{limit}; | ||
| 219 | } | ||
| 220 | |||
| 221 | ($query, $self->{value}); | ||
| 222 | } | ||
| 223 | |||
| 224 | 1; |