« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Session/State/Cookie.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/PHP/Session/Memcached.pm »
| 1 | package Dwarf::SQLBuilder::Query; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use Dwarf::Accessor { | ||
| 4 | ro => [qw/ | ||
| 5 | queries | ||
| 6 | binds | ||
| 7 | select_queries | ||
| 8 | from_queries | ||
| 9 | join_queries | ||
| 10 | where_queries | ||
| 11 | group_by_queries | ||
| 12 | having_queries | ||
| 13 | order_by_queries | ||
| 14 | limit_queries | ||
| 15 | offset_queries | ||
| 16 | /], | ||
| 17 | rw => [qw//] | ||
| 18 | }; | ||
| 19 | |||
| 20 | my %sort_key_map = ( | ||
| 21 | ); | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $class = shift; | ||
| 25 | my $self = bless { @_ }, $class; | ||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub new_query { | ||
| 30 | my ($self, $query) = @_; | ||
| 31 | $self->{queries} = []; | ||
| 32 | |||
| 33 | $self->new_binds; | ||
| 34 | $self->new_select_queries; | ||
| 35 | $self->new_from_queries; | ||
| 36 | $self->new_join_queries; | ||
| 37 | $self->new_where_queries; | ||
| 38 | $self->new_group_by_queries; | ||
| 39 | $self->new_having_queries; | ||
| 40 | $self->new_order_by_queries; | ||
| 41 | $self->new_limit_queries; | ||
| 42 | $self->new_offset_queries; | ||
| 43 | |||
| 44 | # 古いバージョンへの互換性のためのハック | ||
| 45 | if (defined $query) { | ||
| 46 | if ($query =~ /WHERE (.+)$/) { | ||
| 47 | $self->where($1); | ||
| 48 | $query =~ s/WHERE .+$//; | ||
| 49 | } | ||
| 50 | push @{ $self->{queries} }, $query; | ||
| 51 | } | ||
| 52 | |||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub new_binds { $_[0]->{binds} = [] }; | ||
| 57 | sub new_select_queries { $_[0]->{select_queries} = [] }; | ||
| 58 | sub new_from_queries { $_[0]->{from_queries} = [] }; | ||
| 59 | sub new_join_queries { $_[0]->{join_queries} = [] }; | ||
| 60 | sub new_where_queries { $_[0]->{where_queries} = [] }; | ||
| 61 | sub new_group_by_queries { $_[0]->{group_by_queries} = [] }; | ||
| 62 | sub new_having_queries { $_[0]->{having_queries} = [] }; | ||
| 63 | sub new_order_by_queries { $_[0]->{order_by_queries} = [] }; | ||
| 64 | sub new_limit_queries { $_[0]->{limit_queries} = [] }; | ||
| 65 | sub new_offset_queries { $_[0]->{offset_queries} = [] }; | ||
| 66 | |||
| 67 | sub add_query { | ||
| 68 | my $self = shift; | ||
| 69 | push @{ $self->{queries} }, @_; | ||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub add_binds { | ||
| 74 | my ($self, @params) = @_; | ||
| 75 | push @{ $self->{binds} }, @params; | ||
| 76 | return $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub to_teng { | ||
| 80 | my ($self) = @_; | ||
| 81 | return ($self->sql, $self->binds); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub sql { | ||
| 85 | my ($self) = @_; | ||
| 86 | |||
| 87 | my $query = join ' ', @{ $self->queries }; | ||
| 88 | my $select_query = join ', ', @{ $self->select_queries }; | ||
| 89 | my $from_query = join ', ', @{ $self->from_queries }; | ||
| 90 | my $join_query = join ' ', @{ $self->join_queries }; | ||
| 91 | my $where_query = join ' ', @{ $self->where_queries }; | ||
| 92 | my $group_by_query = join ', ', @{ $self->group_by_queries }; | ||
| 93 | my $having_query = join ', ', @{ $self->having_queries }; | ||
| 94 | my $order_by_query = join ', ', @{ $self->order_by_queries }; | ||
| 95 | my $limit_query = join ' ', @{ $self->limit_queries }; | ||
| 96 | my $offset_query = join ' ', @{ $self->offset_queries }; | ||
| 97 | |||
| 98 | $query .= qq{ SELECT $select_query} if $select_query; | ||
| 99 | $query .= qq{ FROM $from_query} if $from_query; | ||
| 100 | $query .= qq{ $join_query} if $join_query; | ||
| 101 | $query .= qq{ WHERE $where_query} if $where_query; | ||
| 102 | $query .= qq{ GROUP BY $group_by_query} if $group_by_query; | ||
| 103 | $query .= qq{ HAVING $having_query} if $having_query; | ||
| 104 | $query .= qq{ ORDER BY $order_by_query} if $order_by_query; | ||
| 105 | $query .= qq{ LIMIT $limit_query} if $limit_query; | ||
| 106 | $query .= qq{ OFFSET $offset_query } if $offset_query; | ||
| 107 | |||
| 108 | return $query; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub select { | ||
| 112 | my $self = shift; | ||
| 113 | return unless @_; | ||
| 114 | return $self->select_with_hash(@_) if @_ > 1; | ||
| 115 | return $self->select_with_hashref(@_) if ref $_[0] eq 'HASH'; | ||
| 116 | push @{ $self->{select_queries} }, $_[0]; | ||
| 117 | return $self; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub select_with_hashref { | ||
| 121 | my ($self, $data) = @_; | ||
| 122 | |||
| 123 | my @list; | ||
| 124 | for my $k (keys %$data) { | ||
| 125 | my $table_name = $k; | ||
| 126 | |||
| 127 | if (ref $data->{$k} eq 'ARRAY') { | ||
| 128 | for my $row (@{ $data->{$k} }) { | ||
| 129 | my $column_name = $row; | ||
| 130 | my $label = $row; | ||
| 131 | |||
| 132 | if (ref $row eq 'ARRAY') { | ||
| 133 | $column_name = $row->[0]; | ||
| 134 | $label = $row->[1]; | ||
| 135 | } | ||
| 136 | |||
| 137 | push @list, $table_name . '.' . $column_name . ' AS ' . $label; | ||
| 138 | } | ||
| 139 | } else { | ||
| 140 | my $column_name = $data->{$k}; | ||
| 141 | my $label = $data->{$k}; | ||
| 142 | push @list, $table_name . '.' . $column_name . ' AS ' . $label; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | push @{ $self->{select_queries} }, @list; | ||
| 147 | return $self; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub select_with_hash { | ||
| 151 | my ($self, %data) = @_; | ||
| 152 | |||
| 153 | my @list; | ||
| 154 | for my $k (keys %data) { | ||
| 155 | my $sql = $k; | ||
| 156 | my $label = $data{$k}; | ||
| 157 | push @list, $sql . ' AS ' . $label; | ||
| 158 | } | ||
| 159 | |||
| 160 | push @{ $self->{select_queries} }, @list; | ||
| 161 | return $self; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub from { | ||
| 165 | my $self = shift; | ||
| 166 | return $self->from_with_array(@_) if @_ > 1; | ||
| 167 | push @{ $self->{from_queries} }, $_[0]; | ||
| 168 | return $self; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub from_with_array { | ||
| 172 | my ($self, @data) = @_; | ||
| 173 | |||
| 174 | my @list; | ||
| 175 | while (@data) { | ||
| 176 | my $sql = shift @data; | ||
| 177 | my $label = shift @data; | ||
| 178 | push @list, $sql . ' ' . $label; | ||
| 179 | } | ||
| 180 | |||
| 181 | push @{ $self->{from_queries} }, @list; | ||
| 182 | return $self; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub add_join { | ||
| 186 | my $self = shift; | ||
| 187 | push @{ $self->{join_queries} }, @_; | ||
| 188 | return $self; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub where { | ||
| 192 | my $self = shift; | ||
| 193 | push @{ $self->{where_queries} }, $_[0]; | ||
| 194 | return $self; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub add_where_if_defined { | ||
| 198 | my ($self, $key, $value, $glue) = @_; | ||
| 199 | return $self unless defined $value; | ||
| 200 | $glue //= @{ $self->{where_queries} } == 0 ? "" : "AND"; | ||
| 201 | push @{ $self->{where_queries} }, qq{ $glue $key = ? }; | ||
| 202 | push @{ $self->{binds} }, $value; | ||
| 203 | return $self; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub add_where_if_not_blank { | ||
| 207 | my ($self, $key, $value, $glue) = @_; | ||
| 208 | return $self if not defined $value or $value eq ''; | ||
| 209 | $glue //= @{ $self->{where_queries} } == 0 ? "" : "AND"; | ||
| 210 | push @{ $self->{where_queries} }, qq{ $glue $key = ? }; | ||
| 211 | push @{ $self->{binds} }, $value; | ||
| 212 | return $self; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub add_where_as_like_if_defined { | ||
| 216 | my ($self, $key, $value, $glue) = @_; | ||
| 217 | return $self unless defined $value; | ||
| 218 | $glue //= @{ $self->{where_queries} } == 0 ? "" : "AND"; | ||
| 219 | push @{ $self->{where_queries} }, qq{ $glue $key LIKE '%' || ? || '%' }; | ||
| 220 | push @{ $self->{binds} }, $value; | ||
| 221 | return $self; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub group_by { | ||
| 225 | my $self = shift; | ||
| 226 | push @{ $self->{group_by_queries} }, @_; | ||
| 227 | return $self; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub having { | ||
| 231 | my $self = shift; | ||
| 232 | push @{ $self->{having_queries} }, @_; | ||
| 233 | return $self; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub order_by { | ||
| 237 | my $self = shift; | ||
| 238 | if (@_ == 1) { | ||
| 239 | push @{ $self->{order_by_queries} }, @_; | ||
| 240 | } else { | ||
| 241 | push @{ $self->{order_by_queries} }, $self->_order_by(@_); | ||
| 242 | } | ||
| 243 | return $self; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub _order_by { | ||
| 247 | my ($self, $sort_key, $sort_order) = @_; | ||
| 248 | my @k = split '\.', $sort_key; | ||
| 249 | $sort_key = pop @k; | ||
| 250 | $sort_key = $sort_key_map{$sort_key} if exists $sort_key_map{$sort_key}; | ||
| 251 | $sort_key = $k[0] . '.' . $sort_key if @k; | ||
| 252 | |||
| 253 | my $order_by = join ' ', $sort_key, $sort_order; | ||
| 254 | return $order_by; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub limit { | ||
| 258 | my $self = shift; | ||
| 259 | push @{ $self->{limit_queries} }, @_; | ||
| 260 | return $self; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub offset { | ||
| 264 | my $self = shift; | ||
| 265 | push @{ $self->{offset_queries} }, @_; | ||
| 266 | return $self; | ||
| 267 | } | ||
| 268 | |||
| 269 | 1; |