| 1 | package SQL::Engine::Grammar; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | use SQL::Engine::Collection; | ||
| 15 | use SQL::Engine::Operation; | ||
| 16 | use SQL::Validator; | ||
| 17 | |||
| 18 | use Scalar::Util (); | ||
| 19 | |||
| 20 | our $VERSION = '0.01'; # VERSION | ||
| 21 | |||
| 22 | # ATTRIBUTES | ||
| 23 | |||
| 24 | has operations => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'InstanceOf["SQL::Engine::Collection"]', | ||
| 27 | new => 1 | ||
| 28 | ); | ||
| 29 | |||
| 30 | fun new_operations($self) { | ||
| 31 | |||
| 32 | SQL::Engine::Collection->new; | ||
| 33 | } | ||
| 34 | |||
| 35 | has schema => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'HashRef', | ||
| 38 | req => 1 | ||
| 39 | ); | ||
| 40 | |||
| 41 | has validator => ( | ||
| 42 | is => 'ro', | ||
| 43 | isa => 'Maybe[InstanceOf["SQL::Validator"]]', | ||
| 44 | new => 1 | ||
| 45 | ); | ||
| 46 | |||
| 47 | fun new_validator($self) { | ||
| 48 | SQL::Validator->new( | ||
| 49 | $ENV{SQL_ENGINE_SCHEMA} | ||
| 50 | ? (schema => $ENV{SQL_ENGINE_SCHEMA}) | ||
| 51 | : (version => '0.0') | ||
| 52 | ) | ||
| 53 | } | ||
| 54 | |||
| 55 | # METHODS | ||
| 56 | |||
| 57 | method binding(Str $name) { | ||
| 58 | $self->{bindings}{int(keys(%{$self->{bindings}}))} = $name; | ||
| 59 | |||
| 60 | return '?'; | ||
| 61 | } | ||
| 62 | |||
| 63 | method column_change(HashRef $data) { | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | method column_create(HashRef $data) { | ||
| 69 | my $sql = []; | ||
| 70 | |||
| 71 | # alter table | ||
| 72 | push @$sql, $self->term(qw(alter table)); | ||
| 73 | |||
| 74 | # safe | ||
| 75 | push @$sql, $self->term(qw(if exists)) if $data->{safe}; | ||
| 76 | |||
| 77 | # for | ||
| 78 | push @$sql, $self->table($data->{for}); | ||
| 79 | |||
| 80 | # column | ||
| 81 | push @$sql, $self->term(qw(add column)); | ||
| 82 | |||
| 83 | # column specification | ||
| 84 | push @$sql, $self->column_specification($data->{column}); | ||
| 85 | |||
| 86 | # sql statement | ||
| 87 | my $result = join ' ', @$sql; | ||
| 88 | |||
| 89 | return $self->operation($result); | ||
| 90 | } | ||
| 91 | |||
| 92 | method column_definition(HashRef $data) { | ||
| 93 | my $def = {}; | ||
| 94 | |||
| 95 | if ($data->{name}) { | ||
| 96 | $def->{name} = $self->name($data->{name}); | ||
| 97 | } | ||
| 98 | |||
| 99 | if ($data->{type}) { | ||
| 100 | $def->{type} = $self->type($data); | ||
| 101 | } | ||
| 102 | |||
| 103 | if (exists $data->{default}) { | ||
| 104 | $def->{default} = join ' ', $self->term('default'), | ||
| 105 | $self->expression($data->{default}); | ||
| 106 | } | ||
| 107 | |||
| 108 | if (exists $data->{nullable}) { | ||
| 109 | $def->{nullable} | ||
| 110 | = $data->{nullable} ? $self->term('null') : $self->term(qw(not null)); | ||
| 111 | } | ||
| 112 | |||
| 113 | if ($data->{primary}) { | ||
| 114 | $def->{primary} = $self->term(qw(primary key)); | ||
| 115 | } | ||
| 116 | |||
| 117 | return $def; | ||
| 118 | } | ||
| 119 | |||
| 120 | method column_drop(HashRef $data) { | ||
| 121 | my $sql = []; | ||
| 122 | |||
| 123 | # alter table | ||
| 124 | push @$sql, $self->term(qw(alter table)); | ||
| 125 | |||
| 126 | # safe | ||
| 127 | push @$sql, $self->term(qw(if exists)) if $data->{safe}; | ||
| 128 | |||
| 129 | # table name | ||
| 130 | push @$sql, $self->table($data); | ||
| 131 | |||
| 132 | # drop column | ||
| 133 | push @$sql, $self->term(qw(drop column)); | ||
| 134 | |||
| 135 | # safe | ||
| 136 | push @$sql, $self->term(qw(if exists)) if $data->{safe}; | ||
| 137 | |||
| 138 | # column name | ||
| 139 | push @$sql, $self->name($data->{column}); | ||
| 140 | |||
| 141 | # sql statement | ||
| 142 | my $result = join ' ', @$sql; | ||
| 143 | |||
| 144 | return $self->operation($result); | ||
| 145 | } | ||
| 146 | |||
| 147 | method column_specification(HashRef $data) { | ||
| 148 | my $sql = []; | ||
| 149 | |||
| 150 | my $column = $self->column_definition($data); | ||
| 151 | |||
| 152 | # name | ||
| 153 | push @$sql, $column->{name}; | ||
| 154 | |||
| 155 | # type | ||
| 156 | push @$sql, $column->{type}; | ||
| 157 | |||
| 158 | # nullable | ||
| 159 | push @$sql, $column->{nullable} if $column->{nullable}; | ||
| 160 | |||
| 161 | # default | ||
| 162 | push @$sql, $column->{default} if $column->{default}; | ||
| 163 | |||
| 164 | # primary | ||
| 165 | push @$sql, $column->{primary} if $column->{primary}; | ||
| 166 | |||
| 167 | # increments | ||
| 168 | push @$sql, $column->{increment} if $column->{increment}; | ||
| 169 | |||
| 170 | # sql statement | ||
| 171 | return join ' ', @$sql; | ||
| 172 | } | ||
| 173 | |||
| 174 | method column_rename(HashRef $data) { | ||
| 175 | my $sql = []; | ||
| 176 | |||
| 177 | # alter table | ||
| 178 | push @$sql, $self->term(qw(alter table)); | ||
| 179 | |||
| 180 | # safe | ||
| 181 | push @$sql, $self->term(qw(if exists)) if $data->{safe}; | ||
| 182 | |||
| 183 | # table name | ||
| 184 | push @$sql, $self->table($data->{"for"}); | ||
| 185 | |||
| 186 | # rename column | ||
| 187 | push @$sql, $self->term(qw(rename column)), | ||
| 188 | $self->name($data->{name}{old}), | ||
| 189 | $self->term('to'), | ||
| 190 | $self->name($data->{name}{new}); | ||
| 191 | |||
| 192 | # sql statement | ||
| 193 | my $result = join ' ', @$sql; | ||
| 194 | |||
| 195 | return $self->operation($result); | ||
| 196 | } | ||
| 197 | |||
| 198 | method constraint_create(HashRef $data) { | ||
| 199 | my $sql = []; | ||
| 200 | |||
| 201 | # alter table | ||
| 202 | push @$sql, $self->term(qw(alter table)); | ||
| 203 | |||
| 204 | # safe | ||
| 205 | push @$sql, $self->term(qw(if exists)) if $data->{safe}; | ||
| 206 | |||
| 207 | # table name | ||
| 208 | push @$sql, $self->table($data->{source}); | ||
| 209 | |||
| 210 | # add constraint | ||
| 211 | push @$sql, $self->term(qw(add constraint)); | ||
| 212 | |||
| 213 | # constraint name | ||
| 214 | push @$sql, $self->name($self->constraint_name($data)); | ||
| 215 | |||
| 216 | # foreign key | ||
| 217 | push @$sql, $self->term(qw(foreign key)); | ||
| 218 | |||
| 219 | # column name | ||
| 220 | push @$sql, sprintf('(%s)', $self->name($data->{source}{column})); | ||
| 221 | |||
| 222 | # references | ||
| 223 | push @$sql, $self->term('references'); | ||
| 224 | |||
| 225 | # foreign table and column name | ||
| 226 | push @$sql, sprintf('%s (%s)', $self->table($data->{target}), | ||
| 227 | $self->name($data->{target}{column})); | ||
| 228 | |||
| 229 | # reference option (on delete) | ||
| 230 | if ($data->{on}{delete}) { | ||
| 231 | push @$sql, $self->term(qw(on delete)), | ||
| 232 | $self->constraint_option($data->{on}{delete}); | ||
| 233 | } | ||
| 234 | |||
| 235 | # reference option (on update) | ||
| 236 | if ($data->{on}{update}) { | ||
| 237 | push @$sql, $self->term(qw(on update)), | ||
| 238 | $self->constraint_option($data->{on}{update}); | ||
| 239 | } | ||
| 240 | |||
| 241 | # sql statement | ||
| 242 | my $result = join ' ', @$sql; | ||
| 243 | |||
| 244 | return $self->operation($result); | ||
| 245 | } | ||
| 246 | |||
| 247 | method constraint_drop(HashRef $data) { | ||
| 248 | my $sql = []; | ||
| 249 | |||
| 250 | # alter table | ||
| 251 | push @$sql, $self->term(qw(alter table)); | ||
| 252 | |||
| 253 | # safe | ||
| 254 | push @$sql, $self->term(qw(if exists)) if $data->{safe}; | ||
| 255 | |||
| 256 | # table name | ||
| 257 | push @$sql, $self->table($data->{source}); | ||
| 258 | |||
| 259 | # drop constraint | ||
| 260 | push @$sql, $self->term(qw(drop constraint)); | ||
| 261 | |||
| 262 | # constraint name | ||
| 263 | push @$sql, $self->name($self->constraint_name($data)); | ||
| 264 | |||
| 265 | # sql statement | ||
| 266 | my $result = join ' ', @$sql; | ||
| 267 | |||
| 268 | return $self->operation($result); | ||
| 269 | } | ||
| 270 | |||
| 271 | method constraint_name(HashRef $data) { | ||
| 272 | |||
| 273 | return $data->{name} || join('_', 'foreign', | ||
| 274 | join('_', grep {defined} @{$data->{source}}{qw(schema table column)}), | ||
| 275 | join('_', grep {defined} @{$data->{target}}{qw(schema table column)}) | ||
| 276 | ); | ||
| 277 | } | ||
| 278 | |||
| 279 | method constraint_option(Str $name) { | ||
| 280 | if (lc($name) eq "cascade") { | ||
| 281 | return $self->term('cascade'); | ||
| 282 | } | ||
| 283 | elsif (lc($name) eq "no-action") { | ||
| 284 | return $self->term(qw(no action)); | ||
| 285 | } | ||
| 286 | elsif (lc($name) eq "restrict") { | ||
| 287 | return $self->term('restrict'); | ||
| 288 | } | ||
| 289 | elsif (lc($name) eq "set-default") { | ||
| 290 | return $self->term(qw(set default)); | ||
| 291 | } | ||
| 292 | elsif (lc($name) eq "set-null") { | ||
| 293 | return $self->term(qw(set null)); | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | return $self->term(qw(no action)); | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | method criteria(ArrayRef $data) { | ||
| 301 | |||
| 302 | return [map $self->criterion($_), @$data]; | ||
| 303 | } | ||
| 304 | |||
| 305 | method criterion(HashRef $data) { | ||
| 306 | if (my $cond = $data->{"and"}) { | ||
| 307 | return sprintf('(%s)', | ||
| 308 | join(sprintf(' %s ', $self->term('and')), @{$self->criteria($cond)})); | ||
| 309 | } | ||
| 310 | |||
| 311 | if (my $cond = $data->{"eq"}) { | ||
| 312 | return sprintf '%s = %s', map $self->expression($_), @$cond; | ||
| 313 | } | ||
| 314 | |||
| 315 | if (my $cond = $data->{"glob"}) { | ||
| 316 | return sprintf '%s %s %s', $self->expression($cond->[0]), | ||
| 317 | $self->term('glob'), $self->expression($cond->[1]); | ||
| 318 | } | ||
| 319 | |||
| 320 | if (my $cond = $data->{"gt"}) { | ||
| 321 | return sprintf '%s > %s', map $self->expression($_), @$cond; | ||
| 322 | } | ||
| 323 | |||
| 324 | if (my $cond = $data->{"gte"}) { | ||
| 325 | return sprintf '%s >= %s', map $self->expression($_), @$cond; | ||
| 326 | } | ||
| 327 | |||
| 328 | if (my $cond = $data->{"in"}) { | ||
| 329 | return sprintf '%s %s %s', $self->expression($cond->[0]), | ||
| 330 | $self->term('in'), join ', ', map $self->expression($_), | ||
| 331 | @$cond[1 .. $#$cond]; | ||
| 332 | } | ||
| 333 | |||
| 334 | if (my $cond = $data->{"is-null"}) { | ||
| 335 | return sprintf '%s IS NULL', $self->expression($cond); | ||
| 336 | } | ||
| 337 | |||
| 338 | if (my $cond = $data->{"like"}) { | ||
| 339 | return sprintf '%s %s %s', $self->expression($cond->[0]), | ||
| 340 | $self->term('like'), $self->expression($cond->[1]); | ||
| 341 | } | ||
| 342 | |||
| 343 | if (my $cond = $data->{"lt"}) { | ||
| 344 | return sprintf '%s < %s', map $self->expression($_), @$cond; | ||
| 345 | } | ||
| 346 | |||
| 347 | if (my $cond = $data->{"lte"}) { | ||
| 348 | return sprintf '%s <= %s', map $self->expression($_), @$cond; | ||
| 349 | } | ||
| 350 | |||
| 351 | if (my $cond = $data->{"ne"}) { | ||
| 352 | return sprintf '%s != %s', map $self->expression($_), @$cond; | ||
| 353 | } | ||
| 354 | |||
| 355 | if (my $cond = $data->{"not-null"}) { | ||
| 356 | return sprintf '%s IS NOT NULL', $self->expression($cond); | ||
| 357 | } | ||
| 358 | |||
| 359 | if (my $cond = $data->{"or"}) { | ||
| 360 | return sprintf('(%s)', | ||
| 361 | join(sprintf(' %s ', $self->term('or')), @{$self->criteria($cond)})); | ||
| 362 | } | ||
| 363 | |||
| 364 | if (my $cond = $data->{"regexp"}) { | ||
| 365 | return sprintf '%s %s %s', $self->expression($cond->[0]), | ||
| 366 | $self->term('regexp'), $self->expression($cond->[1]); | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | method delete(HashRef $data) { | ||
| 371 | my $sql = []; | ||
| 372 | |||
| 373 | # delete | ||
| 374 | push @$sql, $self->term(qw(delete from)); | ||
| 375 | |||
| 376 | # from | ||
| 377 | push @$sql, $self->table($data->{from}); | ||
| 378 | |||
| 379 | # where | ||
| 380 | if (my $where = $data->{where}) { | ||
| 381 | push @$sql, $self->term('where'), | ||
| 382 | join(sprintf(' %s ', $self->term('and')), @{$self->criteria($where)}); | ||
| 383 | } | ||
| 384 | |||
| 385 | # sql statement | ||
| 386 | my $result = join ' ', @$sql; | ||
| 387 | |||
| 388 | return $self->operation($result); | ||
| 389 | } | ||
| 390 | |||
| 391 | method database_create(HashRef $data) { | ||
| 392 | my $sql = []; | ||
| 393 | |||
| 394 | # create database | ||
| 395 | push @$sql, $self->term(qw(create database)), | ||
| 396 | ($data->{safe} ? $self->term(qw(if not exists)) : ()), | ||
| 397 | $self->name($data->{name}); | ||
| 398 | |||
| 399 | # sql statement | ||
| 400 | my $result = join ' ', @$sql; | ||
| 401 | |||
| 402 | return $self->operation($result); | ||
| 403 | } | ||
| 404 | |||
| 405 | method database_drop(HashRef $data) { | ||
| 406 | my $sql = []; | ||
| 407 | |||
| 408 | # drop database | ||
| 409 | push @$sql, $self->term(qw(drop database)), | ||
| 410 | ($data->{safe} ? $self->term(qw(if exists)) : ()), | ||
| 411 | $self->name($data->{name}); | ||
| 412 | |||
| 413 | # sql statement | ||
| 414 | my $result = join ' ', @$sql; | ||
| 415 | |||
| 416 | return $self->operation($result); | ||
| 417 | } | ||
| 418 | |||
| 419 | method execute() { | ||
| 420 | if ($self->validator and not $self->validate) { | ||
| 421 | die $self->validator->error; | ||
| 422 | } | ||
| 423 | |||
| 424 | return $self->process; | ||
| 425 | } | ||
| 426 | |||
| 427 | method expression(Any $data) { | ||
| 428 | if (!ref $data) { | ||
| 429 | return $self->value($data); # literal | ||
| 430 | } | ||
| 431 | |||
| 432 | if (UNIVERSAL::isa($data, 'SCALAR')) { | ||
| 433 | if ($$data eq '1') { | ||
| 434 | return $self->term('true'); | ||
| 435 | } | ||
| 436 | if ($$data eq '0') { | ||
| 437 | return $self->term('false'); | ||
| 438 | } | ||
| 439 | } | ||
| 440 | |||
| 441 | if (my $expr = $data->{"as"}) { | ||
| 442 | my ($alias, $other) = @$expr; | ||
| 443 | return sprintf '%s %s %s', | ||
| 444 | $self->expression($other), | ||
| 445 | $self->term('as'), | ||
| 446 | $alias; | ||
| 447 | } | ||
| 448 | |||
| 449 | if (my $expr = $data->{"binary"}) { | ||
| 450 | if ($expr->{plus}) { | ||
| 451 | return sprintf '(%s + %s)', | ||
| 452 | map $self->expression($_), @{$expr->{plus}}; | ||
| 453 | } | ||
| 454 | if ($expr->{minus}) { | ||
| 455 | return sprintf '(%s - %s)', | ||
| 456 | map $self->expression($_), @{$expr->{minus}}; | ||
| 457 | } | ||
| 458 | if ($expr->{multiply}) { | ||
| 459 | return sprintf '(%s * %s)', | ||
| 460 | map $self->expression($_), @{$expr->{multiply}}; | ||
| 461 | } | ||
| 462 | if ($expr->{divide}) { | ||
| 463 | return sprintf '(%s / %s)', | ||
| 464 | map $self->expression($_), @{$expr->{divide}}; | ||
| 465 | } | ||
| 466 | if ($expr->{modulo}) { | ||
| 467 | return sprintf '(%s % %s)', | ||
| 468 | map $self->expression($_), @{$expr->{modulo}}; | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | if ($data->{"binding"}) { | ||
| 473 | return $self->binding($data->{"binding"}); | ||
| 474 | } | ||
| 475 | |||
| 476 | if (my $expr = $data->{"case"}) { | ||
| 477 | return sprintf( | ||
| 478 | '%s %s %s %s %s', | ||
| 479 | $self->term('case'), | ||
| 480 | join( | ||
| 481 | ' ', | ||
| 482 | map { | ||
| 483 | sprintf '%s %s %s %s', $self->term('when'), | ||
| 484 | ($self->expression($$_{cond}) || $self->criterion($$_{cond})), | ||
| 485 | $self->term('then'), $self->expression($$_{then}); | ||
| 486 | } @{$expr->{"when"}} | ||
| 487 | ), | ||
| 488 | $self->term('else'), | ||
| 489 | $self->expression($expr->{"else"}), | ||
| 490 | $self->term('end') | ||
| 491 | ); | ||
| 492 | } | ||
| 493 | |||
| 494 | if ($data->{"cast"}) { | ||
| 495 | return sprintf( | ||
| 496 | '%s(%s)', | ||
| 497 | $self->term('cast'), | ||
| 498 | join( | ||
| 499 | sprintf(' %s ', $self->term('as')), | ||
| 500 | map $self->expression($_), | ||
| 501 | @{$data->{"cast"}} | ||
| 502 | ) | ||
| 503 | ); | ||
| 504 | } | ||
| 505 | |||
| 506 | if ($data->{"column"}) { | ||
| 507 | return $self->name($data->{"alias"}, $data->{"column"}); | ||
| 508 | } | ||
| 509 | |||
| 510 | if ($data->{"function"}) { | ||
| 511 | my ($name, @args) = @{$data->{"function"}}; | ||
| 512 | return sprintf('%s(%s)', | ||
| 513 | $name, join(', ', @args ? (map $self->expression($_), @args) : '')); | ||
| 514 | } | ||
| 515 | |||
| 516 | if (my $expr = $data->{"subquery"}) { | ||
| 517 | $self->select($expr->{select}); | ||
| 518 | my $operation = $self->operations->pop; | ||
| 519 | $self->{bindings} = $operation->bindings; | ||
| 520 | return sprintf('(%s)', $operation->statement); | ||
| 521 | } | ||
| 522 | |||
| 523 | if (my $expr = $data->{"unary"}) { | ||
| 524 | if ($expr->{"plus"}) { | ||
| 525 | return sprintf '+%s', $self->expression($expr->{"plus"}); | ||
| 526 | } | ||
| 527 | if ($expr->{"minus"}) { | ||
| 528 | return sprintf '-%s', $self->expression($expr->{"minus"}); | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 532 | if (my $expr = $data->{"verbatim"}) { | ||
| 533 | my @verbatim = @{$data->{"verbatim"}}; | ||
| 534 | return join(' ', $verbatim[0], | ||
| 535 | join(', ', map $self->expression($_), @verbatim[1..$#verbatim])); | ||
| 536 | } | ||
| 537 | } | ||
| 538 | |||
| 539 | method name(Any @args) { | ||
| 540 | |||
| 541 | return join '.', map { /\W/ ? $_ : $self->wrap($_) } grep {defined} @args; | ||
| 542 | } | ||
| 543 | |||
| 544 | method operation(Str $statement) { | ||
| 545 | $self->operations->push( | ||
| 546 | my $operation = SQL::Engine::Operation->new( | ||
| 547 | bindings => delete $self->{bindings} || {}, | ||
| 548 | statement => $statement, | ||
| 549 | ) | ||
| 550 | ); | ||
| 551 | |||
| 552 | return $operation; | ||
| 553 | } | ||
| 554 | |||
| 555 | method process(HashRef $schema = $self->schema) { | ||
| 556 | if ($schema->{"select"}) { | ||
| 557 | $self->select($schema->{"select"}); | ||
| 558 | |||
| 559 | return $self; | ||
| 560 | } | ||
| 561 | |||
| 562 | if ($schema->{"insert"}) { | ||
| 563 | $self->insert($schema->{"insert"}); | ||
| 564 | |||
| 565 | return $self; | ||
| 566 | } | ||
| 567 | |||
| 568 | if ($schema->{"update"}) { | ||
| 569 | $self->update($schema->{"update"}); | ||
| 570 | |||
| 571 | return $self; | ||
| 572 | } | ||
| 573 | |||
| 574 | if ($schema->{"delete"}) { | ||
| 575 | $self->delete($schema->{"delete"}); | ||
| 576 | |||
| 577 | return $self; | ||
| 578 | } | ||
| 579 | |||
| 580 | if ($schema->{"column-change"}) { | ||
| 581 | $self->column_change($schema->{"column-change"}); | ||
| 582 | |||
| 583 | return $self; | ||
| 584 | } | ||
| 585 | |||
| 586 | if ($schema->{"column-create"}) { | ||
| 587 | $self->column_create($schema->{"column-create"}); | ||
| 588 | |||
| 589 | return $self; | ||
| 590 | } | ||
| 591 | |||
| 592 | if ($schema->{"column-drop"}) { | ||
| 593 | $self->column_drop($schema->{"column-drop"}); | ||
| 594 | |||
| 595 | return $self; | ||
| 596 | } | ||
| 597 | |||
| 598 | if ($schema->{"column-rename"}) { | ||
| 599 | $self->column_rename($schema->{"column-rename"}); | ||
| 600 | |||
| 601 | return $self; | ||
| 602 | } | ||
| 603 | |||
| 604 | if ($schema->{"constraint-create"}) { | ||
| 605 | $self->constraint_create($schema->{"constraint-create"}); | ||
| 606 | |||
| 607 | return $self; | ||
| 608 | } | ||
| 609 | |||
| 610 | if ($schema->{"constraint-drop"}) { | ||
| 611 | $self->constraint_drop($schema->{"constraint-drop"}); | ||
| 612 | |||
| 613 | return $self; | ||
| 614 | } | ||
| 615 | |||
| 616 | if ($schema->{"database-create"}) { | ||
| 617 | $self->database_create($schema->{"database-create"}); | ||
| 618 | |||
| 619 | return $self; | ||
| 620 | } | ||
| 621 | |||
| 622 | if ($schema->{"database-drop"}) { | ||
| 623 | $self->database_drop($schema->{"database-drop"}); | ||
| 624 | |||
| 625 | return $self; | ||
| 626 | } | ||
| 627 | |||
| 628 | if ($schema->{"index-create"}) { | ||
| 629 | $self->index_create($schema->{"index-create"}); | ||
| 630 | |||
| 631 | return $self; | ||
| 632 | } | ||
| 633 | |||
| 634 | if ($schema->{"index-drop"}) { | ||
| 635 | $self->index_drop($schema->{"index-drop"}); | ||
| 636 | |||
| 637 | return $self; | ||
| 638 | } | ||
| 639 | |||
| 640 | if ($schema->{"schema-create"}) { | ||
| 641 | $self->schema_create($schema->{"schema-create"}); | ||
| 642 | |||
| 643 | return $self; | ||
| 644 | } | ||
| 645 | |||
| 646 | if ($schema->{"schema-drop"}) { | ||
| 647 | $self->schema_drop($schema->{"schema-drop"}); | ||
| 648 | |||
| 649 | return $self; | ||
| 650 | } | ||
| 651 | |||
| 652 | if ($schema->{"schema-rename"}) { | ||
| 653 | $self->schema_rename($schema->{"schema-rename"}); | ||
| 654 | |||
| 655 | return $self; | ||
| 656 | } | ||
| 657 | |||
| 658 | if ($schema->{"table-create"}) { | ||
| 659 | $self->table_create($schema->{"table-create"}); | ||
| 660 | |||
| 661 | return $self; | ||
| 662 | } | ||
| 663 | |||
| 664 | if ($schema->{"table-drop"}) { | ||
| 665 | $self->table_drop($schema->{"table-drop"}); | ||
| 666 | |||
| 667 | return $self; | ||
| 668 | } | ||
| 669 | |||
| 670 | if ($schema->{"transaction"}) { | ||
| 671 | $self->transaction($schema->{"transaction"}); | ||
| 672 | |||
| 673 | return $self; | ||
| 674 | } | ||
| 675 | |||
| 676 | if ($schema->{"table-rename"}) { | ||
| 677 | $self->table_rename($schema->{"table-rename"}); | ||
| 678 | |||
| 679 | return $self; | ||
| 680 | } | ||
| 681 | |||
| 682 | if ($schema->{"view-create"}) { | ||
| 683 | $self->view_create($schema->{"view-create"}); | ||
| 684 | |||
| 685 | return $self; | ||
| 686 | } | ||
| 687 | |||
| 688 | if ($schema->{"view-drop"}) { | ||
| 689 | $self->view_drop($schema->{"view-drop"}); | ||
| 690 | |||
| 691 | return $self; | ||
| 692 | } | ||
| 693 | |||
| 694 | return $self; | ||
| 695 | } | ||
| 696 | |||
| 697 | method index_create(HashRef $data) { | ||
| 698 | my $sql = []; | ||
| 699 | |||
| 700 | # create | ||
| 701 | push @$sql, $self->term('create'); | ||
| 702 | |||
| 703 | # unique | ||
| 704 | push @$sql, $self->term('unique') if $data->{unique}; | ||
| 705 | |||
| 706 | # index | ||
| 707 | push @$sql, $self->term('index'); | ||
| 708 | |||
| 709 | # safe | ||
| 710 | push @$sql, $self->term(qw(if not exists)) if $data->{safe}; | ||
| 711 | |||
| 712 | # index name | ||
| 713 | push @$sql, $self->wrap($self->index_name($data)); | ||
| 714 | |||
| 715 | # on table | ||
| 716 | push @$sql, $self->term('on'), $self->table($data->{for}); | ||
| 717 | |||
| 718 | # columns | ||
| 719 | push @$sql, sprintf('(%s)', | ||
| 720 | join(', ', map $self->name($$_{alias}, $$_{column}), @{$data->{columns}})); | ||
| 721 | |||
| 722 | # sql statement | ||
| 723 | my $result = join ' ', @$sql; | ||
| 724 | |||
| 725 | return $self->operation($result); | ||
| 726 | } | ||
| 727 | |||
| 728 | method index_drop(HashRef $data) { | ||
| 729 | my $sql = []; | ||
| 730 | |||
| 731 | # drop | ||
| 732 | push @$sql, $self->term('drop'); | ||
| 733 | |||
| 734 | # index | ||
| 735 | push @$sql, $self->term('index'); | ||
| 736 | |||
| 737 | # safe | ||
| 738 | push @$sql, $self->term(qw(if exists)) if $data->{safe}; | ||
| 739 | |||
| 740 | # index name | ||
| 741 | push @$sql, $self->wrap($self->index_name($data)); | ||
| 742 | |||
| 743 | # sql statement | ||
| 744 | my $result = join ' ', @$sql; | ||
| 745 | |||
| 746 | return $self->operation($result); | ||
| 747 | } | ||
| 748 | |||
| 749 | method index_name(HashRef $data) { | ||
| 750 | return $data->{name} || join('_', | ||
| 751 | ($data->{unique} ? 'unique' : 'index'), | ||
| 752 | grep {defined} $data->{for}{schema}, $data->{for}{table}, | ||
| 753 | map $$_{column}, @{$data->{columns}} | ||
| 754 | ); | ||
| 755 | } | ||
| 756 | |||
| 757 | method insert(HashRef $data) { | ||
| 758 | my $sql = []; | ||
| 759 | |||
| 760 | # insert | ||
| 761 | push @$sql, $self->term('insert'); | ||
| 762 | |||
| 763 | # into | ||
| 764 | push @$sql, $self->term('into'), $self->table($data->{into}); | ||
| 765 | |||
| 766 | # columns | ||
| 767 | if (my $columns = $data->{columns}) { | ||
| 768 | push @$sql, | ||
| 769 | sprintf('(%s)', join(', ', map $self->expression($_), @$columns)); | ||
| 770 | } | ||
| 771 | |||
| 772 | # values | ||
| 773 | if (my $values = $data->{values}) { | ||
| 774 | push @$sql, | ||
| 775 | sprintf('%s (%s)', | ||
| 776 | $self->term('values'), | ||
| 777 | join(', ', map $self->expression($$_{value}), @$values)); | ||
| 778 | } | ||
| 779 | |||
| 780 | # query | ||
| 781 | if (my $query = $data->{query}) { | ||
| 782 | $self->select($query->{select}); | ||
| 783 | my $operation = $self->operations->pop; | ||
| 784 | $self->{bindings} = $operation->bindings; | ||
| 785 | push @$sql, $operation->statement; | ||
| 786 | } | ||
| 787 | |||
| 788 | # default | ||
| 789 | if ($data->{default} && !$data->{values} && !$data->{values}) { | ||
| 790 | push @$sql, $self->term('default'), $self->term('values'); | ||
| 791 | } | ||
| 792 | |||
| 793 | # sql statement | ||
| 794 | my $result = join ' ', @$sql; | ||
| 795 | |||
| 796 | return $self->operation($result); | ||
| 797 | } | ||
| 798 | |||
| 799 | method schema_create(HashRef $data) { | ||
| 800 | my $sql = []; | ||
| 801 | |||
| 802 | # create schema | ||
| 803 | push @$sql, $self->term(qw(create schema)), | ||
| 804 | ($data->{safe} ? $self->term(qw(if not exists)) : ()), | ||
| 805 | $self->name($data->{name}); | ||
| 806 | |||
| 807 | # sql statement | ||
| 808 | my $result = join ' ', @$sql; | ||
| 809 | |||
| 810 | return $self->operation($result); | ||
| 811 | } | ||
| 812 | |||
| 813 | method schema_drop(HashRef $data) { | ||
| 814 | my $sql = []; | ||
| 815 | |||
| 816 | # drop schema | ||
| 817 | push @$sql, $self->term(qw(drop schema)), | ||
| 818 | ($data->{safe} ? $self->term(qw(if exists)) : ()), | ||
| 819 | $self->name($data->{name}); | ||
| 820 | |||
| 821 | # sql statement | ||
| 822 | my $result = join ' ', @$sql; | ||
| 823 | |||
| 824 | return $self->operation($result); | ||
| 825 | } | ||
| 826 | |||
| 827 | method schema_rename(HashRef $data) { | ||
| 828 | my $sql = []; | ||
| 829 | |||
| 830 | # rename schema | ||
| 831 | push @$sql, $self->term(qw(alter schema)), | ||
| 832 | $self->name($data->{name}{old}), | ||
| 833 | $self->term(qw(rename to)), | ||
| 834 | $self->name($data->{name}{new}); | ||
| 835 | |||
| 836 | # sql statement | ||
| 837 | my $result = join ' ', @$sql; | ||
| 838 | |||
| 839 | return $self->operation($result); | ||
| 840 | } | ||
| 841 | |||
| 842 | method select(HashRef $data) { | ||
| 843 | my $sql = []; | ||
| 844 | |||
| 845 | # select | ||
| 846 | push @$sql, $self->term('select'); | ||
| 847 | |||
| 848 | # columns | ||
| 849 | if (my $columns = $data->{columns}) { | ||
| 850 | push @$sql, join(', ', map $self->expression($_), @$columns); | ||
| 851 | } | ||
| 852 | |||
| 853 | # from | ||
| 854 | push @$sql, $self->term('from'), | ||
| 855 | ref($data->{from}) eq 'ARRAY' | ||
| 856 | ? join(', ', map $self->table($_), @{$data->{from}}) | ||
| 857 | : $self->table($data->{from}); | ||
| 858 | |||
| 859 | # joins | ||
| 860 | if (my $joins = $data->{joins}) { | ||
| 861 | for my $join (@$joins) { | ||
| 862 | push @$sql, $self->term('join'), $self->table($join->{with}); | ||
| 863 | push @$sql, $self->term('on'), | ||
| 864 | join( | ||
| 865 | sprintf(' %s ', $self->term('and')), | ||
| 866 | @{$self->criteria($join->{having})} | ||
| 867 | ); | ||
| 868 | } | ||
| 869 | } | ||
| 870 | |||
| 871 | # where | ||
| 872 | if (my $where = $data->{where}) { | ||
| 873 | push @$sql, $self->term('where'), | ||
| 874 | join(sprintf(' %s ', $self->term('and')), @{$self->criteria($where)}); | ||
| 875 | } | ||
| 876 | |||
| 877 | # group-by | ||
| 878 | if (my $group_by = $data->{"group-by"}) { | ||
| 879 | push @$sql, $self->term(qw(group by)); | ||
| 880 | push @$sql, join ', ', map $self->expression($_), @$group_by; | ||
| 881 | |||
| 882 | # having | ||
| 883 | if (my $having = $data->{"having"}) { | ||
| 884 | push @$sql, $self->term('having'), | ||
| 885 | join(sprintf(' %s ', $self->term('and')), @{$self->criteria($having)}); | ||
| 886 | } | ||
| 887 | } | ||
| 888 | |||
| 889 | # order-by | ||
| 890 | if (my $orders = $data->{"order-by"}) { | ||
| 891 | my @orders; | ||
| 892 | push @$sql, $self->term(qw(order by)); | ||
| 893 | for my $order (@$orders) { | ||
| 894 | if ($order->{sort} | ||
| 895 | && ($order->{sort} eq 'asc' || $order->{sort} eq 'ascending')) | ||
| 896 | { | ||
| 897 | push @orders, sprintf '%s ASC', | ||
| 898 | $self->name($order->{"alias"}, $order->{"column"}); | ||
| 899 | } | ||
| 900 | elsif ($order->{sort} | ||
| 901 | && ($order->{sort} eq 'desc' || $order->{sort} eq 'descending')) | ||
| 902 | { | ||
| 903 | push @orders, sprintf '%s DESC', | ||
| 904 | $self->name($order->{"alias"}, $order->{"column"}); | ||
| 905 | } | ||
| 906 | else { | ||
| 907 | push @orders, $self->name($order->{"alias"}, $order->{"column"}); | ||
| 908 | } | ||
| 909 | } | ||
| 910 | push @$sql, join ', ', @orders; | ||
| 911 | } | ||
| 912 | |||
| 913 | # rows | ||
| 914 | if (my $rows = $data->{rows}) { | ||
| 915 | if ($rows->{limit} && $rows->{offset}) { | ||
| 916 | push @$sql, sprintf '%s %d %s %d', $self->term('limit'), $rows->{limit}, | ||
| 917 | $self->term('offset'), $rows->{offset}; | ||
| 918 | } | ||
| 919 | if ($rows->{limit} && !$rows->{offset}) { | ||
| 920 | push @$sql, sprintf '%s %d', $self->term('limit'), $rows->{limit}; | ||
| 921 | } | ||
| 922 | } | ||
| 923 | |||
| 924 | # sql statement | ||
| 925 | my $result = join ' ', @$sql; | ||
| 926 | |||
| 927 | return $self->operation($result); | ||
| 928 | } | ||
| 929 | |||
| 930 | method table(HashRef $data) { | ||
| 931 | my $name; | ||
| 932 | |||
| 933 | my $table = $data->{table}; | ||
| 934 | my $schema = $data->{schema}; | ||
| 935 | my $alias = $data->{alias}; | ||
| 936 | |||
| 937 | $name = $self->name($schema, $table); | ||
| 938 | $name = join ' ', $name, $self->wrap($alias) if $alias; | ||
| 939 | |||
| 940 | return $name; | ||
| 941 | } | ||
| 942 | |||
| 943 | method table_create(HashRef $data) { | ||
| 944 | my $sql = []; | ||
| 945 | |||
| 946 | # create | ||
| 947 | push @$sql, $self->term('create'); | ||
| 948 | |||
| 949 | # temporary | ||
| 950 | if ($data->{temp}) { | ||
| 951 | push @$sql, $self->term('temporary'); | ||
| 952 | } | ||
| 953 | |||
| 954 | # table | ||
| 955 | push @$sql, $self->term('table'), | ||
| 956 | ($data->{safe} ? $self->term(qw(if not exists)) : ()), | ||
| 957 | $self->name($data->{name}); | ||
| 958 | |||
| 959 | # columns | ||
| 960 | if (my $columns = $data->{columns}) { | ||
| 961 | push @$sql, sprintf('(%s)', join ', ', | ||
| 962 | map $self->column_specification($_), @$columns); | ||
| 963 | } | ||
| 964 | |||
| 965 | # query | ||
| 966 | if (my $query = $data->{query}) { | ||
| 967 | $self->select($query->{select}); | ||
| 968 | my $operation = $self->operations->pop; | ||
| 969 | $self->{bindings} = $operation->bindings; | ||
| 970 | push @$sql, $self->term('as'); | ||
| 971 | push @$sql, $operation->statement; | ||
| 972 | } | ||
| 973 | |||
| 974 | # sql statement | ||
| 975 | my $result = join ' ', @$sql; | ||
| 976 | |||
| 977 | return $self->operation($result); | ||
| 978 | } | ||
| 979 | |||
| 980 | method table_drop(HashRef $data) { | ||
| 981 | my $sql = []; | ||
| 982 | |||
| 983 | # drop table | ||
| 984 | push @$sql, $self->term(qw(drop table)), | ||
| 985 | ($data->{safe} ? $self->term(qw(if exists)) : ()), | ||
| 986 | $self->name($data->{name}); | ||
| 987 | |||
| 988 | # with condition | ||
| 989 | if (my $condition = $data->{condition}) { | ||
| 990 | push @$sql, $self->term($data->{condition}); | ||
| 991 | } | ||
| 992 | |||
| 993 | # sql statement | ||
| 994 | my $result = join ' ', @$sql; | ||
| 995 | |||
| 996 | return $self->operation($result); | ||
| 997 | } | ||
| 998 | |||
| 999 | method table_rename(HashRef $data) { | ||
| 1000 | my $sql = []; | ||
| 1001 | |||
| 1002 | # rename table | ||
| 1003 | push @$sql, $self->term(qw(alter table)), | ||
| 1004 | $self->name($data->{name}{old}), | ||
| 1005 | $self->term(qw(rename to)), | ||
| 1006 | $self->name($data->{name}{new}); | ||
| 1007 | |||
| 1008 | # sql statement | ||
| 1009 | my $result = join ' ', @$sql; | ||
| 1010 | |||
| 1011 | return $self->operation($result); | ||
| 1012 | } | ||
| 1013 | |||
| 1014 | method term(Str @args) { | ||
| 1015 | my $method = join '_', 'term', map lc, map {split /\s/} @args; | ||
| 1016 | |||
| 1017 | if ($self->can($method)) { | ||
| 1018 | return $self->$method; | ||
| 1019 | } | ||
| 1020 | else { | ||
| 1021 | return join ' ', map uc, @args; | ||
| 1022 | } | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | method transaction(HashRef $data) { | ||
| 1026 | $self->operation($self->term('begin', 'transaction')); | ||
| 1027 | $self->process($_) for @{$data->{queries}}; | ||
| 1028 | $self->operation($self->term('commit')); | ||
| 1029 | |||
| 1030 | return $self; | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | method type(HashRef $data) { | ||
| 1034 | if ($data->{type} eq 'binary') { | ||
| 1035 | return $self->type_binary($data); | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | if ($data->{type} eq 'boolean') { | ||
| 1039 | return $self->type_boolean($data); | ||
| 1040 | } | ||
| 1041 | |||
| 1042 | if ($data->{type} eq 'char') { | ||
| 1043 | return $self->type_char($data); | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | if ($data->{type} eq 'date') { | ||
| 1047 | return $self->type_date($data); | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | if ($data->{type} eq 'datetime') { | ||
| 1051 | return $self->type_datetime($data); | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | if ($data->{type} eq 'datetime-wtz') { | ||
| 1055 | return $self->type_datetime_wtz($data); | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | if ($data->{type} eq 'decimal') { | ||
| 1059 | return $self->type_decimal($data); | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | if ($data->{type} eq 'double') { | ||
| 1063 | return $self->type_double($data); | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | if ($data->{type} eq 'enum') { | ||
| 1067 | return $self->type_enum($data); | ||
| 1068 | } | ||
| 1069 | |||
| 1070 | if ($data->{type} eq 'float') { | ||
| 1071 | return $self->type_float($data); | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | if ($data->{type} eq 'integer') { | ||
| 1075 | return $self->type_integer($data); | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | if ($data->{type} eq 'integer-big') { | ||
| 1079 | return $self->type_integer_big($data); | ||
| 1080 | } | ||
| 1081 | |||
| 1082 | if ($data->{type} eq 'integer-big-unsigned') { | ||
| 1083 | return $self->type_integer_big_unsigned($data); | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | if ($data->{type} eq 'integer-medium') { | ||
| 1087 | return $self->type_integer_medium($data); | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | if ($data->{type} eq 'integer-medium-unsigned') { | ||
| 1091 | return $self->type_integer_medium_unsigned($data); | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | if ($data->{type} eq 'integer-small') { | ||
| 1095 | return $self->type_integer_small($data); | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | if ($data->{type} eq 'integer-small-unsigned') { | ||
| 1099 | return $self->type_integer_small_unsigned($data); | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | if ($data->{type} eq 'integer-tiny') { | ||
| 1103 | return $self->type_integer_tiny($data); | ||
| 1104 | } | ||
| 1105 | |||
| 1106 | if ($data->{type} eq 'integer-tiny-unsigned') { | ||
| 1107 | return $self->type_integer_tiny_unsigned($data); | ||
| 1108 | } | ||
| 1109 | |||
| 1110 | if ($data->{type} eq 'integer-unsigned') { | ||
| 1111 | return $self->type_integer_unsigned($data); | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | if ($data->{type} eq 'json') { | ||
| 1115 | return $self->type_json($data); | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | if ($data->{type} eq 'number') { | ||
| 1119 | return $self->type_number($data); | ||
| 1120 | } | ||
| 1121 | |||
| 1122 | if ($data->{type} eq 'string') { | ||
| 1123 | return $self->type_string($data); | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | if ($data->{type} eq 'text') { | ||
| 1127 | return $self->type_text($data); | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | if ($data->{type} eq 'text-long') { | ||
| 1131 | return $self->type_text_long($data); | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | if ($data->{type} eq 'text-medium') { | ||
| 1135 | return $self->type_text_medium($data); | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | if ($data->{type} eq 'time') { | ||
| 1139 | return $self->type_time($data); | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | if ($data->{type} eq 'time-wtz') { | ||
| 1143 | return $self->type_time_wtz($data); | ||
| 1144 | } | ||
| 1145 | |||
| 1146 | if ($data->{type} eq 'timestamp') { | ||
| 1147 | return $self->type_timestamp($data); | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | if ($data->{type} eq 'timestamp-wtz') { | ||
| 1151 | return $self->type_timestamp_wtz($data); | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | if ($data->{type} eq 'uuid') { | ||
| 1155 | return $self->type_uuid($data); | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | return $data->{type}; | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | method type_binary(HashRef $data) { | ||
| 1162 | |||
| 1163 | return 'blob'; | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | method type_boolean(HashRef $data) { | ||
| 1167 | |||
| 1168 | return 'tinyint(1)'; | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | method type_char(HashRef $data) { | ||
| 1172 | |||
| 1173 | return 'varchar'; | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | method type_date(HashRef $data) { | ||
| 1177 | |||
| 1178 | return 'date'; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | method type_datetime(HashRef $data) { | ||
| 1182 | |||
| 1183 | return 'datetime'; | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | method type_datetime_wtz(HashRef $data) { | ||
| 1187 | |||
| 1188 | return 'datetime'; | ||
| 1189 | } | ||
| 1190 | |||
| 1191 | method type_decimal(HashRef $data) { | ||
| 1192 | |||
| 1193 | return 'numeric'; | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | method type_double(HashRef $data) { | ||
| 1197 | |||
| 1198 | return 'float'; | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | method type_enum(HashRef $data) { | ||
| 1202 | |||
| 1203 | return 'varchar'; | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | method type_float(HashRef $data) { | ||
| 1207 | |||
| 1208 | return 'float'; | ||
| 1209 | } | ||
| 1210 | |||
| 1211 | method type_integer(HashRef $data) { | ||
| 1212 | |||
| 1213 | return 'integer'; | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | method type_integer_big(HashRef $data) { | ||
| 1217 | |||
| 1218 | return 'integer'; | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | method type_integer_big_unsigned(HashRef $data) { | ||
| 1222 | |||
| 1223 | return 'integer'; | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | method type_integer_medium(HashRef $data) { | ||
| 1227 | |||
| 1228 | return 'integer'; | ||
| 1229 | } | ||
| 1230 | |||
| 1231 | method type_integer_medium_unsigned(HashRef $data) { | ||
| 1232 | |||
| 1233 | return 'integer'; | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | method type_integer_small(HashRef $data) { | ||
| 1237 | |||
| 1238 | return 'integer'; | ||
| 1239 | } | ||
| 1240 | |||
| 1241 | method type_integer_small_unsigned(HashRef $data) { | ||
| 1242 | |||
| 1243 | return 'integer'; | ||
| 1244 | } | ||
| 1245 | |||
| 1246 | method type_integer_tiny(HashRef $data) { | ||
| 1247 | |||
| 1248 | return 'integer'; | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | method type_integer_tiny_unsigned(HashRef $data) { | ||
| 1252 | |||
| 1253 | return 'integer'; | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | method type_integer_unsigned(HashRef $data) { | ||
| 1257 | |||
| 1258 | return 'integer'; | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | method type_json(HashRef $data) { | ||
| 1262 | |||
| 1263 | return 'text'; | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | method type_number(HashRef $data) { | ||
| 1267 | |||
| 1268 | return 'integer'; | ||
| 1269 | } | ||
| 1270 | |||
| 1271 | method type_string(HashRef $data) { | ||
| 1272 | |||
| 1273 | return 'varchar'; | ||
| 1274 | } | ||
| 1275 | |||
| 1276 | method type_text(HashRef $data) { | ||
| 1277 | |||
| 1278 | return 'text'; | ||
| 1279 | } | ||
| 1280 | |||
| 1281 | method type_text_long(HashRef $data) { | ||
| 1282 | |||
| 1283 | return 'text'; | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | method type_text_medium(HashRef $data) { | ||
| 1287 | |||
| 1288 | return 'text'; | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | method type_time(HashRef $data) { | ||
| 1292 | |||
| 1293 | return 'time'; | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | method type_time_wtz(HashRef $data) { | ||
| 1297 | |||
| 1298 | return 'time'; | ||
| 1299 | } | ||
| 1300 | |||
| 1301 | method type_timestamp(HashRef $data) { | ||
| 1302 | |||
| 1303 | return 'datetime'; | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | method type_timestamp_wtz(HashRef $data) { | ||
| 1307 | |||
| 1308 | return 'datetime'; | ||
| 1309 | } | ||
| 1310 | |||
| 1311 | method type_uuid(HashRef $data) { | ||
| 1312 | |||
| 1313 | return 'varchar'; | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | method update(HashRef $data) { | ||
| 1317 | my $sql = []; | ||
| 1318 | |||
| 1319 | # update | ||
| 1320 | push @$sql, $self->term('update'); | ||
| 1321 | |||
| 1322 | # for | ||
| 1323 | push @$sql, $self->table($data->{for}); | ||
| 1324 | |||
| 1325 | # columns | ||
| 1326 | if (my $columns = $data->{columns}) { | ||
| 1327 | push @$sql, $self->term('set'); | ||
| 1328 | push @$sql, join( | ||
| 1329 | ', ', | ||
| 1330 | map { | ||
| 1331 | sprintf('%s = %s', | ||
| 1332 | $self->name($$_{alias}, $$_{column}), | ||
| 1333 | $self->expression($$_{value})) | ||
| 1334 | } @$columns | ||
| 1335 | ); | ||
| 1336 | } | ||
| 1337 | |||
| 1338 | # where | ||
| 1339 | if (my $where = $data->{where}) { | ||
| 1340 | push @$sql, $self->term('where'), | ||
| 1341 | join(sprintf(' %s ', $self->term('and')), @{$self->criteria($where)}); | ||
| 1342 | } | ||
| 1343 | |||
| 1344 | # sql statement | ||
| 1345 | my $result = join ' ', @$sql; | ||
| 1346 | |||
| 1347 | return $self->operation($result); | ||
| 1348 | } | ||
| 1349 | |||
| 1350 | method validate() { | ||
| 1351 | |||
| 1352 | return $self->validator->validate($self->schema); | ||
| 1353 | } | ||
| 1354 | |||
| 1355 | method value(Any $value) { | ||
| 1356 | |||
| 1357 | return !defined $value ? $self->term('null') : ( | ||
| 1358 | Scalar::Util::looks_like_number($value) ? $value : do { | ||
| 1359 | $value =~ s/\'/\\'/g; | ||
| 1360 | "'$value'" | ||
| 1361 | } | ||
| 1362 | ); | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | method view_create(HashRef $data) { | ||
| 1366 | my $sql = []; | ||
| 1367 | |||
| 1368 | # create | ||
| 1369 | push @$sql, $self->term('create'); | ||
| 1370 | |||
| 1371 | # temporary | ||
| 1372 | if ($data->{temp}) { | ||
| 1373 | push @$sql, $self->term('temporary'); | ||
| 1374 | } | ||
| 1375 | |||
| 1376 | # view | ||
| 1377 | push @$sql, $self->term('view'); | ||
| 1378 | |||
| 1379 | # safe | ||
| 1380 | if ($data->{safe}) { | ||
| 1381 | push @$sql, $self->term(qw(if not exists)); | ||
| 1382 | } | ||
| 1383 | |||
| 1384 | # view name | ||
| 1385 | push @$sql, $self->name($data->{name}); | ||
| 1386 | |||
| 1387 | # columns | ||
| 1388 | if (my $columns = $data->{columns}) { | ||
| 1389 | push @$sql, | ||
| 1390 | sprintf('(%s)', join(', ', map $self->expression($_), @$columns)); | ||
| 1391 | } | ||
| 1392 | |||
| 1393 | # query | ||
| 1394 | if (my $query = $data->{query}) { | ||
| 1395 | $self->select($query->{select}); | ||
| 1396 | my $operation = $self->operations->pop; | ||
| 1397 | $self->{bindings} = $operation->bindings; | ||
| 1398 | push @$sql, $self->term('as'); | ||
| 1399 | push @$sql, $operation->statement; | ||
| 1400 | } | ||
| 1401 | |||
| 1402 | # sql statement | ||
| 1403 | my $result = join ' ', @$sql; | ||
| 1404 | |||
| 1405 | return $self->operation($result); | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | method view_drop(HashRef $data) { | ||
| 1409 | my $sql = []; | ||
| 1410 | |||
| 1411 | # drop view | ||
| 1412 | push @$sql, $self->term(qw(drop view)), | ||
| 1413 | ($data->{safe} ? $self->term(qw(if exists)) : ()), | ||
| 1414 | $self->name($data->{name}); | ||
| 1415 | |||
| 1416 | # sql statement | ||
| 1417 | my $result = join ' ', @$sql; | ||
| 1418 | |||
| 1419 | return $self->operation($result); | ||
| 1420 | } | ||
| 1421 | |||
| 1422 | method wrap(Str $name) { | ||
| 1423 | |||
| 1424 | return qq("$name"); | ||
| 1425 | } | ||
| 1426 | |||
| 1427 | 1; | ||
| 1428 | |||
| 1429 | =encoding utf8 | ||
| 1430 | |||
| 1431 | =head1 NAME | ||
| 1432 | |||
| 1433 | SQL::Engine::Grammar - Standard Grammar | ||
| 1434 | |||
| 1435 | =cut | ||
| 1436 | |||
| 1437 | =head1 ABSTRACT | ||
| 1438 | |||
| 1439 | SQL::Engine Standard Grammar | ||
| 1440 | |||
| 1441 | =cut | ||
| 1442 | |||
| 1443 | =head1 SYNOPSIS | ||
| 1444 | |||
| 1445 | use SQL::Engine::Grammar; | ||
| 1446 | |||
| 1447 | my $grammar = SQL::Engine::Grammar->new( | ||
| 1448 | schema => { | ||
| 1449 | select => { | ||
| 1450 | from => { | ||
| 1451 | table => 'users' | ||
| 1452 | }, | ||
| 1453 | columns => [ | ||
| 1454 | { | ||
| 1455 | column => '*' | ||
| 1456 | } | ||
| 1457 | ] | ||
| 1458 | } | ||
| 1459 | } | ||
| 1460 | ); | ||
| 1461 | |||
| 1462 | # $grammar->execute; | ||
| 1463 | |||
| 1464 | =cut | ||
| 1465 | |||
| 1466 | =head1 DESCRIPTION | ||
| 1467 | |||
| 1468 | This package provides methods for converting | ||
| 1469 | L<json-sql|https://github.com/iamalnewkirk/json-sql> data structures into | ||
| 1470 | SQL statements. | ||
| 1471 | |||
| 1472 | =cut | ||
| 1473 | |||
| 1474 | =head1 LIBRARIES | ||
| 1475 | |||
| 1476 | This package uses type constraints from: | ||
| 1477 | |||
| 1478 | L<Types::Standard> | ||
| 1479 | |||
| 1480 | =cut | ||
| 1481 | |||
| 1482 | =head1 ATTRIBUTES | ||
| 1483 | |||
| 1484 | This package has the following attributes: | ||
| 1485 | |||
| 1486 | =cut | ||
| 1487 | |||
| 1488 | =head2 operations | ||
| 1489 | |||
| 1490 | operations(InstanceOf["SQL::Engine::Collection"]) | ||
| 1491 | |||
| 1492 | This attribute is read-only, accepts C<(InstanceOf["SQL::Engine::Collection"])> values, and is optional. | ||
| 1493 | |||
| 1494 | =cut | ||
| 1495 | |||
| 1496 | =head2 schema | ||
| 1497 | |||
| 1498 | schema(HashRef) | ||
| 1499 | |||
| 1500 | This attribute is read-only, accepts C<(HashRef)> values, and is required. | ||
| 1501 | |||
| 1502 | =cut | ||
| 1503 | |||
| 1504 | =head2 validator | ||
| 1505 | |||
| 1506 | validator(Maybe[InstanceOf["SQL::Validator"]]) | ||
| 1507 | |||
| 1508 | This attribute is read-only, accepts C<(Maybe[InstanceOf["SQL::Validator"]])> values, and is optional. | ||
| 1509 | |||
| 1510 | =cut | ||
| 1511 | |||
| 1512 | =head1 METHODS | ||
| 1513 | |||
| 1514 | This package implements the following methods: | ||
| 1515 | |||
| 1516 | =cut | ||
| 1517 | |||
| 1518 | =head2 binding | ||
| 1519 | |||
| 1520 | binding(Str $name) : Str | ||
| 1521 | |||
| 1522 | The binding method registers a SQL statement binding (or placeholder). | ||
| 1523 | |||
| 1524 | =over 4 | ||
| 1525 | |||
| 1526 | =item binding example #1 | ||
| 1527 | |||
| 1528 | # given: synopsis | ||
| 1529 | |||
| 1530 | $grammar->binding('user_id'); | ||
| 1531 | $grammar->binding('user_id'); | ||
| 1532 | $grammar->binding('user_id'); | ||
| 1533 | $grammar->binding('user_id'); | ||
| 1534 | $grammar->binding('user_id'); | ||
| 1535 | |||
| 1536 | =back | ||
| 1537 | |||
| 1538 | =cut | ||
| 1539 | |||
| 1540 | =head2 column_change | ||
| 1541 | |||
| 1542 | column_change(HashRef $data) : Object | ||
| 1543 | |||
| 1544 | The column_change method generates SQL statements to change a column | ||
| 1545 | definition. | ||
| 1546 | |||
| 1547 | =over 4 | ||
| 1548 | |||
| 1549 | =item column_change example #1 | ||
| 1550 | |||
| 1551 | my $grammar = SQL::Engine::Grammar->new({ | ||
| 1552 | schema => { | ||
| 1553 | 'column-change' => { | ||
| 1554 | for => { | ||
| 1555 | table => 'users' | ||
| 1556 | }, | ||
| 1557 | column => { | ||
| 1558 | name => 'accessed', | ||
| 1559 | type => 'datetime', | ||
| 1560 | nullable => 1 | ||
| 1561 | } | ||
| 1562 | } | ||
| 1563 | } | ||
| 1564 | }); | ||
| 1565 | |||
| 1566 | $grammar->column_change($grammar->schema->{'column-change'}); | ||
| 1567 | |||
| 1568 | =back | ||
| 1569 | |||
| 1570 | =cut | ||
| 1571 | |||
| 1572 | =head2 column_create | ||
| 1573 | |||
| 1574 | column_create(HashRef $data) : Object | ||
| 1575 | |||
| 1576 | The column_create method generates SQL statements to add a new table column. | ||
| 1577 | |||
| 1578 | =over 4 | ||
| 1579 | |||
| 1580 | =item column_create example #1 | ||
| 1581 | |||
| 1582 | # given: synopsis | ||
| 1583 | |||
| 1584 | $grammar->column_create({ | ||
| 1585 | for => { | ||
| 1586 | table => 'users' | ||
| 1587 | }, | ||
| 1588 | column => { | ||
| 1589 | name => 'accessed', | ||
| 1590 | type => 'datetime' | ||
| 1591 | } | ||
| 1592 | }); | ||
| 1593 | |||
| 1594 | =back | ||
| 1595 | |||
| 1596 | =cut | ||
| 1597 | |||
| 1598 | =head2 column_definition | ||
| 1599 | |||
| 1600 | column_definition(HashRef $data) : HashRef | ||
| 1601 | |||
| 1602 | The column_definition method column definition SQL statement fragments. | ||
| 1603 | |||
| 1604 | =over 4 | ||
| 1605 | |||
| 1606 | =item column_definition example #1 | ||
| 1607 | |||
| 1608 | # given: synopsis | ||
| 1609 | |||
| 1610 | my $column_definition = $grammar->column_definition({ | ||
| 1611 | name => 'id', | ||
| 1612 | type => 'number', | ||
| 1613 | primary => 1 | ||
| 1614 | }); | ||
| 1615 | |||
| 1616 | =back | ||
| 1617 | |||
| 1618 | =cut | ||
| 1619 | |||
| 1620 | =head2 column_drop | ||
| 1621 | |||
| 1622 | column_drop(HashRef $data) : Object | ||
| 1623 | |||
| 1624 | The column_drop method generates SQL statements to remove a table column. | ||
| 1625 | |||
| 1626 | =over 4 | ||
| 1627 | |||
| 1628 | =item column_drop example #1 | ||
| 1629 | |||
| 1630 | # given: synopsis | ||
| 1631 | |||
| 1632 | $grammar->column_drop({ | ||
| 1633 | table => 'users', | ||
| 1634 | column => 'accessed' | ||
| 1635 | }); | ||
| 1636 | |||
| 1637 | =back | ||
| 1638 | |||
| 1639 | =cut | ||
| 1640 | |||
| 1641 | =head2 column_rename | ||
| 1642 | |||
| 1643 | column_rename(HashRef $data) : Object | ||
| 1644 | |||
| 1645 | The column_rename method generates SQL statements to rename a table column. | ||
| 1646 | |||
| 1647 | =over 4 | ||
| 1648 | |||
| 1649 | =item column_rename example #1 | ||
| 1650 | |||
| 1651 | # given: synopsis | ||
| 1652 | |||
| 1653 | $grammar->column_rename({ | ||
| 1654 | for => { | ||
| 1655 | table => 'users' | ||
| 1656 | }, | ||
| 1657 | name => { | ||
| 1658 | old => 'accessed', | ||
| 1659 | new => 'accessed_at' | ||
| 1660 | } | ||
| 1661 | }); | ||
| 1662 | |||
| 1663 | =back | ||
| 1664 | |||
| 1665 | =cut | ||
| 1666 | |||
| 1667 | =head2 column_specification | ||
| 1668 | |||
| 1669 | column_specification(HashRef $data) : Str | ||
| 1670 | |||
| 1671 | The column_specification method a column definition SQL statment partial. | ||
| 1672 | |||
| 1673 | =over 4 | ||
| 1674 | |||
| 1675 | =item column_specification example #1 | ||
| 1676 | |||
| 1677 | # given: synopsis | ||
| 1678 | |||
| 1679 | my $column_specification = $grammar->column_specification({ | ||
| 1680 | name => 'id', | ||
| 1681 | type => 'number', | ||
| 1682 | primary => 1 | ||
| 1683 | }); | ||
| 1684 | |||
| 1685 | =back | ||
| 1686 | |||
| 1687 | =cut | ||
| 1688 | |||
| 1689 | =head2 constraint_create | ||
| 1690 | |||
| 1691 | constraint_create(HashRef $data) : Object | ||
| 1692 | |||
| 1693 | The constraint_create method generates SQL statements to create a table | ||
| 1694 | constraint. | ||
| 1695 | |||
| 1696 | =over 4 | ||
| 1697 | |||
| 1698 | =item constraint_create example #1 | ||
| 1699 | |||
| 1700 | # given: synopsis | ||
| 1701 | |||
| 1702 | $grammar->constraint_create({ | ||
| 1703 | source => { | ||
| 1704 | table => 'users', | ||
| 1705 | column => 'profile_id' | ||
| 1706 | }, | ||
| 1707 | target => { | ||
| 1708 | table => 'profiles', | ||
| 1709 | column => 'id' | ||
| 1710 | } | ||
| 1711 | }); | ||
| 1712 | |||
| 1713 | =back | ||
| 1714 | |||
| 1715 | =cut | ||
| 1716 | |||
| 1717 | =head2 constraint_drop | ||
| 1718 | |||
| 1719 | constraint_drop(HashRef $data) : Object | ||
| 1720 | |||
| 1721 | The constraint_drop method generates SQL statements to remove a table | ||
| 1722 | constraint. | ||
| 1723 | |||
| 1724 | =over 4 | ||
| 1725 | |||
| 1726 | =item constraint_drop example #1 | ||
| 1727 | |||
| 1728 | # given: synopsis | ||
| 1729 | |||
| 1730 | $grammar->constraint_drop({ | ||
| 1731 | source => { | ||
| 1732 | table => 'users', | ||
| 1733 | column => 'profile_id' | ||
| 1734 | }, | ||
| 1735 | target => { | ||
| 1736 | table => 'profiles', | ||
| 1737 | column => 'id' | ||
| 1738 | } | ||
| 1739 | }); | ||
| 1740 | |||
| 1741 | =back | ||
| 1742 | |||
| 1743 | =cut | ||
| 1744 | |||
| 1745 | =head2 constraint_name | ||
| 1746 | |||
| 1747 | constraint_name(HashRef $data) : Str | ||
| 1748 | |||
| 1749 | The constraint_name method returns the generated constraint name. | ||
| 1750 | |||
| 1751 | =over 4 | ||
| 1752 | |||
| 1753 | =item constraint_name example #1 | ||
| 1754 | |||
| 1755 | # given: synopsis | ||
| 1756 | |||
| 1757 | my $constraint_name = $grammar->constraint_name({ | ||
| 1758 | source => { | ||
| 1759 | table => 'users', | ||
| 1760 | column => 'profile_id' | ||
| 1761 | }, | ||
| 1762 | target => { | ||
| 1763 | table => 'profiles', | ||
| 1764 | column => 'id' | ||
| 1765 | } | ||
| 1766 | }); | ||
| 1767 | |||
| 1768 | =back | ||
| 1769 | |||
| 1770 | =cut | ||
| 1771 | |||
| 1772 | =head2 constraint_option | ||
| 1773 | |||
| 1774 | constraint_option(Str $name) : Str | ||
| 1775 | |||
| 1776 | The constraint_option method returns a SQL expression for the constraint option | ||
| 1777 | provided. | ||
| 1778 | |||
| 1779 | =over 4 | ||
| 1780 | |||
| 1781 | =item constraint_option example #1 | ||
| 1782 | |||
| 1783 | # given: synopsis | ||
| 1784 | |||
| 1785 | $grammar->constraint_option('no-action'); | ||
| 1786 | |||
| 1787 | =back | ||
| 1788 | |||
| 1789 | =cut | ||
| 1790 | |||
| 1791 | =head2 criteria | ||
| 1792 | |||
| 1793 | criteria(ArrayRef $data) : ArrayRef[Str] | ||
| 1794 | |||
| 1795 | The criteria method returns a list of SQL expressions. | ||
| 1796 | |||
| 1797 | =over 4 | ||
| 1798 | |||
| 1799 | =item criteria example #1 | ||
| 1800 | |||
| 1801 | # given: synopsis | ||
| 1802 | |||
| 1803 | my $criteria = $grammar->criteria([ | ||
| 1804 | { | ||
| 1805 | eq => [{ column => 'id' }, 123] | ||
| 1806 | }, | ||
| 1807 | { | ||
| 1808 | 'not-null' => { column => 'deleted' } | ||
| 1809 | } | ||
| 1810 | ]); | ||
| 1811 | |||
| 1812 | =back | ||
| 1813 | |||
| 1814 | =cut | ||
| 1815 | |||
| 1816 | =head2 criterion | ||
| 1817 | |||
| 1818 | criterion(HashRef $data) : Str | ||
| 1819 | |||
| 1820 | The criterion method returns a SQL expression. | ||
| 1821 | |||
| 1822 | =over 4 | ||
| 1823 | |||
| 1824 | =item criterion example #1 | ||
| 1825 | |||
| 1826 | # given: synopsis | ||
| 1827 | |||
| 1828 | my $criterion = $grammar->criterion({ | ||
| 1829 | in => [{ column => 'theme' }, 'light', 'dark'] | ||
| 1830 | }); | ||
| 1831 | |||
| 1832 | =back | ||
| 1833 | |||
| 1834 | =cut | ||
| 1835 | |||
| 1836 | =head2 database_create | ||
| 1837 | |||
| 1838 | database_create(HashRef $data) : Object | ||
| 1839 | |||
| 1840 | The database_create method generates SQL statements to create a database. | ||
| 1841 | |||
| 1842 | =over 4 | ||
| 1843 | |||
| 1844 | =item database_create example #1 | ||
| 1845 | |||
| 1846 | # given: synopsis | ||
| 1847 | |||
| 1848 | $grammar->database_create({ | ||
| 1849 | name => 'todoapp' | ||
| 1850 | }); | ||
| 1851 | |||
| 1852 | =back | ||
| 1853 | |||
| 1854 | =cut | ||
| 1855 | |||
| 1856 | =head2 database_drop | ||
| 1857 | |||
| 1858 | database_drop(HashRef $data) : Object | ||
| 1859 | |||
| 1860 | The database_drop method generates SQL statements to remove a database. | ||
| 1861 | |||
| 1862 | =over 4 | ||
| 1863 | |||
| 1864 | =item database_drop example #1 | ||
| 1865 | |||
| 1866 | # given: synopsis | ||
| 1867 | |||
| 1868 | $grammar->database_drop({ | ||
| 1869 | name => 'todoapp' | ||
| 1870 | }); | ||
| 1871 | |||
| 1872 | =back | ||
| 1873 | |||
| 1874 | =cut | ||
| 1875 | |||
| 1876 | =head2 delete | ||
| 1877 | |||
| 1878 | delete(HashRef $data) : Object | ||
| 1879 | |||
| 1880 | The delete method generates SQL statements to delete table rows. | ||
| 1881 | |||
| 1882 | =over 4 | ||
| 1883 | |||
| 1884 | =item delete example #1 | ||
| 1885 | |||
| 1886 | # given: synopsis | ||
| 1887 | |||
| 1888 | $grammar->delete({ | ||
| 1889 | from => { | ||
| 1890 | table => 'tasklists' | ||
| 1891 | } | ||
| 1892 | }); | ||
| 1893 | |||
| 1894 | =back | ||
| 1895 | |||
| 1896 | =cut | ||
| 1897 | |||
| 1898 | =head2 execute | ||
| 1899 | |||
| 1900 | execute() : Object | ||
| 1901 | |||
| 1902 | The execute method validates and processes the object instruction. | ||
| 1903 | |||
| 1904 | =over 4 | ||
| 1905 | |||
| 1906 | =item execute example #1 | ||
| 1907 | |||
| 1908 | # given: synopsis | ||
| 1909 | |||
| 1910 | $grammar->operations->clear; | ||
| 1911 | |||
| 1912 | $grammar->execute; | ||
| 1913 | |||
| 1914 | =back | ||
| 1915 | |||
| 1916 | =cut | ||
| 1917 | |||
| 1918 | =head2 expression | ||
| 1919 | |||
| 1920 | expression(Any $data) : Any | ||
| 1921 | |||
| 1922 | The expression method returns a SQL expression representing the data provided. | ||
| 1923 | |||
| 1924 | =over 4 | ||
| 1925 | |||
| 1926 | =item expression example #1 | ||
| 1927 | |||
| 1928 | # given: synopsis | ||
| 1929 | |||
| 1930 | $grammar->expression(undef); | ||
| 1931 | |||
| 1932 | # NULL | ||
| 1933 | |||
| 1934 | =back | ||
| 1935 | |||
| 1936 | =cut | ||
| 1937 | |||
| 1938 | =head2 index_create | ||
| 1939 | |||
| 1940 | index_create(HashRef $data) : Object | ||
| 1941 | |||
| 1942 | The index_create method generates SQL statements to create a table index. | ||
| 1943 | |||
| 1944 | =over 4 | ||
| 1945 | |||
| 1946 | =item index_create example #1 | ||
| 1947 | |||
| 1948 | # given: synopsis | ||
| 1949 | |||
| 1950 | $grammar->index_create({ | ||
| 1951 | for => { | ||
| 1952 | table => 'users' | ||
| 1953 | }, | ||
| 1954 | columns => [ | ||
| 1955 | { | ||
| 1956 | column => 'name' | ||
| 1957 | } | ||
| 1958 | ] | ||
| 1959 | }); | ||
| 1960 | |||
| 1961 | =back | ||
| 1962 | |||
| 1963 | =cut | ||
| 1964 | |||
| 1965 | =head2 index_drop | ||
| 1966 | |||
| 1967 | index_drop(HashRef $data) : Object | ||
| 1968 | |||
| 1969 | The index_drop method generates SQL statements to remove a table index. | ||
| 1970 | |||
| 1971 | =over 4 | ||
| 1972 | |||
| 1973 | =item index_drop example #1 | ||
| 1974 | |||
| 1975 | # given: synopsis | ||
| 1976 | |||
| 1977 | $grammar->index_drop({ | ||
| 1978 | for => { | ||
| 1979 | table => 'users' | ||
| 1980 | }, | ||
| 1981 | columns => [ | ||
| 1982 | { | ||
| 1983 | column => 'name' | ||
| 1984 | } | ||
| 1985 | ] | ||
| 1986 | }); | ||
| 1987 | |||
| 1988 | =back | ||
| 1989 | |||
| 1990 | =cut | ||
| 1991 | |||
| 1992 | =head2 index_name | ||
| 1993 | |||
| 1994 | index_name(HashRef $data) : Str | ||
| 1995 | |||
| 1996 | The index_name method returns the generated index name. | ||
| 1997 | |||
| 1998 | =over 4 | ||
| 1999 | |||
| 2000 | =item index_name example #1 | ||
| 2001 | |||
| 2002 | # given: synopsis | ||
| 2003 | |||
| 2004 | my $index_name = $grammar->index_name({ | ||
| 2005 | for => { | ||
| 2006 | table => 'users' | ||
| 2007 | }, | ||
| 2008 | columns => [ | ||
| 2009 | { | ||
| 2010 | column => 'email' | ||
| 2011 | } | ||
| 2012 | ], | ||
| 2013 | unique => 1 | ||
| 2014 | }); | ||
| 2015 | |||
| 2016 | =back | ||
| 2017 | |||
| 2018 | =cut | ||
| 2019 | |||
| 2020 | =head2 insert | ||
| 2021 | |||
| 2022 | insert(HashRef $data) : Object | ||
| 2023 | |||
| 2024 | The insert method generates SQL statements to insert table rows. | ||
| 2025 | |||
| 2026 | =over 4 | ||
| 2027 | |||
| 2028 | =item insert example #1 | ||
| 2029 | |||
| 2030 | # given: synopsis | ||
| 2031 | |||
| 2032 | $grammar->insert({ | ||
| 2033 | into => { | ||
| 2034 | table => 'users' | ||
| 2035 | }, | ||
| 2036 | values => [ | ||
| 2037 | { | ||
| 2038 | value => undef | ||
| 2039 | }, | ||
| 2040 | { | ||
| 2041 | value => 'Rob Zombie' | ||
| 2042 | }, | ||
| 2043 | { | ||
| 2044 | value => { | ||
| 2045 | function => ['now'] | ||
| 2046 | } | ||
| 2047 | }, | ||
| 2048 | { | ||
| 2049 | value => { | ||
| 2050 | function => ['now'] | ||
| 2051 | } | ||
| 2052 | }, | ||
| 2053 | { | ||
| 2054 | value => { | ||
| 2055 | function => ['now'] | ||
| 2056 | } | ||
| 2057 | } | ||
| 2058 | ] | ||
| 2059 | }); | ||
| 2060 | |||
| 2061 | =back | ||
| 2062 | |||
| 2063 | =cut | ||
| 2064 | |||
| 2065 | =head2 name | ||
| 2066 | |||
| 2067 | name(Any @args) : Str | ||
| 2068 | |||
| 2069 | The name method returns a qualified quoted object name. | ||
| 2070 | |||
| 2071 | =over 4 | ||
| 2072 | |||
| 2073 | =item name example #1 | ||
| 2074 | |||
| 2075 | # given: synopsis | ||
| 2076 | |||
| 2077 | my $name = $grammar->name(undef, 'public', 'users'); | ||
| 2078 | |||
| 2079 | # "public"."users" | ||
| 2080 | |||
| 2081 | =back | ||
| 2082 | |||
| 2083 | =cut | ||
| 2084 | |||
| 2085 | =head2 operation | ||
| 2086 | |||
| 2087 | operation(Str $statement) : InstanceOf["SQL::Engine::Operation"] | ||
| 2088 | |||
| 2089 | The operation method creates and appends an operation to the I<"operations"> | ||
| 2090 | collection. | ||
| 2091 | |||
| 2092 | =over 4 | ||
| 2093 | |||
| 2094 | =item operation example #1 | ||
| 2095 | |||
| 2096 | # given: synopsis | ||
| 2097 | |||
| 2098 | $grammar->operation('SELECT TRUE'); | ||
| 2099 | |||
| 2100 | =back | ||
| 2101 | |||
| 2102 | =cut | ||
| 2103 | |||
| 2104 | =head2 process | ||
| 2105 | |||
| 2106 | process(Mayb[HashRef] $schema) : Object | ||
| 2107 | |||
| 2108 | The process method processes the object instructions. | ||
| 2109 | |||
| 2110 | =over 4 | ||
| 2111 | |||
| 2112 | =item process example #1 | ||
| 2113 | |||
| 2114 | # given: synopsis | ||
| 2115 | |||
| 2116 | $grammar->process; | ||
| 2117 | |||
| 2118 | =back | ||
| 2119 | |||
| 2120 | =cut | ||
| 2121 | |||
| 2122 | =head2 schema_create | ||
| 2123 | |||
| 2124 | schema_create(HashRef $data) : Object | ||
| 2125 | |||
| 2126 | The schema_create method generates SQL statements to create a schema. | ||
| 2127 | |||
| 2128 | =over 4 | ||
| 2129 | |||
| 2130 | =item schema_create example #1 | ||
| 2131 | |||
| 2132 | # given: synopsis | ||
| 2133 | |||
| 2134 | $grammar->schema_create({ | ||
| 2135 | name => 'private', | ||
| 2136 | }); | ||
| 2137 | |||
| 2138 | =back | ||
| 2139 | |||
| 2140 | =cut | ||
| 2141 | |||
| 2142 | =head2 schema_drop | ||
| 2143 | |||
| 2144 | schema_drop(HashRef $data) : Object | ||
| 2145 | |||
| 2146 | The schema_drop method generates SQL statements to remove a schema. | ||
| 2147 | |||
| 2148 | =over 4 | ||
| 2149 | |||
| 2150 | =item schema_drop example #1 | ||
| 2151 | |||
| 2152 | # given: synopsis | ||
| 2153 | |||
| 2154 | $grammar->schema_drop({ | ||
| 2155 | name => 'private', | ||
| 2156 | }); | ||
| 2157 | |||
| 2158 | =back | ||
| 2159 | |||
| 2160 | =cut | ||
| 2161 | |||
| 2162 | =head2 schema_rename | ||
| 2163 | |||
| 2164 | schema_rename(HashRef $data) : Object | ||
| 2165 | |||
| 2166 | The schema_rename method generates SQL statements to rename a schema. | ||
| 2167 | |||
| 2168 | =over 4 | ||
| 2169 | |||
| 2170 | =item schema_rename example #1 | ||
| 2171 | |||
| 2172 | # given: synopsis | ||
| 2173 | |||
| 2174 | $grammar->schema_rename({ | ||
| 2175 | name => { | ||
| 2176 | old => 'private', | ||
| 2177 | new => 'restricted' | ||
| 2178 | } | ||
| 2179 | }); | ||
| 2180 | |||
| 2181 | =back | ||
| 2182 | |||
| 2183 | =cut | ||
| 2184 | |||
| 2185 | =head2 select | ||
| 2186 | |||
| 2187 | select(HashRef $data) : Object | ||
| 2188 | |||
| 2189 | The select method generates SQL statements to select table rows. | ||
| 2190 | |||
| 2191 | =over 4 | ||
| 2192 | |||
| 2193 | =item select example #1 | ||
| 2194 | |||
| 2195 | # given: synopsis | ||
| 2196 | |||
| 2197 | $grammar->select({ | ||
| 2198 | from => { | ||
| 2199 | table => 'people' | ||
| 2200 | }, | ||
| 2201 | columns => [ | ||
| 2202 | { column => 'name' } | ||
| 2203 | ] | ||
| 2204 | }); | ||
| 2205 | |||
| 2206 | =back | ||
| 2207 | |||
| 2208 | =cut | ||
| 2209 | |||
| 2210 | =head2 table | ||
| 2211 | |||
| 2212 | table(HashRef $data) : Str | ||
| 2213 | |||
| 2214 | The table method returns a qualified quoted table name. | ||
| 2215 | |||
| 2216 | =over 4 | ||
| 2217 | |||
| 2218 | =item table example #1 | ||
| 2219 | |||
| 2220 | # given: synopsis | ||
| 2221 | |||
| 2222 | my $table = $grammar->table({ | ||
| 2223 | schema => 'public', | ||
| 2224 | table => 'users', | ||
| 2225 | alias => 'u' | ||
| 2226 | }); | ||
| 2227 | |||
| 2228 | =back | ||
| 2229 | |||
| 2230 | =cut | ||
| 2231 | |||
| 2232 | =head2 table_create | ||
| 2233 | |||
| 2234 | table_create(HashRef $data) : Object | ||
| 2235 | |||
| 2236 | The table_create method generates SQL statements to create a table. | ||
| 2237 | |||
| 2238 | =over 4 | ||
| 2239 | |||
| 2240 | =item table_create example #1 | ||
| 2241 | |||
| 2242 | # given: synopsis | ||
| 2243 | |||
| 2244 | $grammar->table_create({ | ||
| 2245 | name => 'users', | ||
| 2246 | columns => [ | ||
| 2247 | { | ||
| 2248 | name => 'id', | ||
| 2249 | type => 'integer', | ||
| 2250 | primary => 1 | ||
| 2251 | } | ||
| 2252 | ] | ||
| 2253 | }); | ||
| 2254 | |||
| 2255 | =back | ||
| 2256 | |||
| 2257 | =cut | ||
| 2258 | |||
| 2259 | =head2 table_drop | ||
| 2260 | |||
| 2261 | table_drop(HashRef $data) : Object | ||
| 2262 | |||
| 2263 | The table_drop method generates SQL statements to remove a table. | ||
| 2264 | |||
| 2265 | =over 4 | ||
| 2266 | |||
| 2267 | =item table_drop example #1 | ||
| 2268 | |||
| 2269 | # given: synopsis | ||
| 2270 | |||
| 2271 | $grammar->table_drop({ | ||
| 2272 | name => 'people' | ||
| 2273 | }); | ||
| 2274 | |||
| 2275 | =back | ||
| 2276 | |||
| 2277 | =cut | ||
| 2278 | |||
| 2279 | =head2 table_rename | ||
| 2280 | |||
| 2281 | table_rename(HashRef $data) : Object | ||
| 2282 | |||
| 2283 | The table_rename method generates SQL statements to rename a table. | ||
| 2284 | |||
| 2285 | =over 4 | ||
| 2286 | |||
| 2287 | =item table_rename example #1 | ||
| 2288 | |||
| 2289 | # given: synopsis | ||
| 2290 | |||
| 2291 | $grammar->table_rename({ | ||
| 2292 | name => { | ||
| 2293 | old => 'peoples', | ||
| 2294 | new => 'people' | ||
| 2295 | } | ||
| 2296 | }); | ||
| 2297 | |||
| 2298 | =back | ||
| 2299 | |||
| 2300 | =cut | ||
| 2301 | |||
| 2302 | =head2 term | ||
| 2303 | |||
| 2304 | term(Str @args) : Str | ||
| 2305 | |||
| 2306 | The term method returns a SQL keyword. | ||
| 2307 | |||
| 2308 | =over 4 | ||
| 2309 | |||
| 2310 | =item term example #1 | ||
| 2311 | |||
| 2312 | # given: synopsis | ||
| 2313 | |||
| 2314 | $grammar->term('end'); | ||
| 2315 | |||
| 2316 | =back | ||
| 2317 | |||
| 2318 | =cut | ||
| 2319 | |||
| 2320 | =head2 transaction | ||
| 2321 | |||
| 2322 | transaction(HashRef $data) : Object | ||
| 2323 | |||
| 2324 | The transaction method generates SQL statements to commit an atomic database | ||
| 2325 | transaction. | ||
| 2326 | |||
| 2327 | =over 4 | ||
| 2328 | |||
| 2329 | =item transaction example #1 | ||
| 2330 | |||
| 2331 | my $grammar = SQL::Engine::Grammar->new({ | ||
| 2332 | schema => { | ||
| 2333 | 'transaction' => { | ||
| 2334 | queries => [ | ||
| 2335 | { | ||
| 2336 | 'table-create' => { | ||
| 2337 | name => 'users', | ||
| 2338 | columns => [ | ||
| 2339 | { | ||
| 2340 | name => 'id', | ||
| 2341 | type => 'integer', | ||
| 2342 | primary => 1 | ||
| 2343 | } | ||
| 2344 | ] | ||
| 2345 | } | ||
| 2346 | } | ||
| 2347 | ] | ||
| 2348 | } | ||
| 2349 | } | ||
| 2350 | }); | ||
| 2351 | |||
| 2352 | $grammar->column_change($grammar->schema->{'transaction'}); | ||
| 2353 | |||
| 2354 | =back | ||
| 2355 | |||
| 2356 | =cut | ||
| 2357 | |||
| 2358 | =head2 type | ||
| 2359 | |||
| 2360 | type(HashRef $data) : Str | ||
| 2361 | |||
| 2362 | The type method return the SQL representation for a data type. | ||
| 2363 | |||
| 2364 | =over 4 | ||
| 2365 | |||
| 2366 | =item type example #1 | ||
| 2367 | |||
| 2368 | # given: synopsis | ||
| 2369 | |||
| 2370 | $grammar->type({ | ||
| 2371 | type => 'datetime-wtz' | ||
| 2372 | }); | ||
| 2373 | |||
| 2374 | # datetime | ||
| 2375 | |||
| 2376 | =back | ||
| 2377 | |||
| 2378 | =cut | ||
| 2379 | |||
| 2380 | =head2 type_binary | ||
| 2381 | |||
| 2382 | type_binary(HashRef $data) : Str | ||
| 2383 | |||
| 2384 | The type_binary method returns the SQL expression representing a binary data | ||
| 2385 | type. | ||
| 2386 | |||
| 2387 | =over 4 | ||
| 2388 | |||
| 2389 | =item type_binary example #1 | ||
| 2390 | |||
| 2391 | # given: synopsis | ||
| 2392 | |||
| 2393 | $grammar->type_binary({}); | ||
| 2394 | |||
| 2395 | # blob | ||
| 2396 | |||
| 2397 | =back | ||
| 2398 | |||
| 2399 | =cut | ||
| 2400 | |||
| 2401 | =head2 type_boolean | ||
| 2402 | |||
| 2403 | type_boolean(HashRef $data) : Str | ||
| 2404 | |||
| 2405 | The type_boolean method returns the SQL expression representing a boolean data | ||
| 2406 | type. | ||
| 2407 | |||
| 2408 | =over 4 | ||
| 2409 | |||
| 2410 | =item type_boolean example #1 | ||
| 2411 | |||
| 2412 | # given: synopsis | ||
| 2413 | |||
| 2414 | $grammar->type_boolean({}); | ||
| 2415 | |||
| 2416 | # tinyint(1) | ||
| 2417 | |||
| 2418 | =back | ||
| 2419 | |||
| 2420 | =cut | ||
| 2421 | |||
| 2422 | =head2 type_char | ||
| 2423 | |||
| 2424 | type_char(HashRef $data) : Str | ||
| 2425 | |||
| 2426 | The type_char method returns the SQL expression representing a char data type. | ||
| 2427 | |||
| 2428 | =over 4 | ||
| 2429 | |||
| 2430 | =item type_char example #1 | ||
| 2431 | |||
| 2432 | # given: synopsis | ||
| 2433 | |||
| 2434 | $grammar->type_char({}); | ||
| 2435 | |||
| 2436 | # varchar | ||
| 2437 | |||
| 2438 | =back | ||
| 2439 | |||
| 2440 | =cut | ||
| 2441 | |||
| 2442 | =head2 type_date | ||
| 2443 | |||
| 2444 | type_date(HashRef $data) : Str | ||
| 2445 | |||
| 2446 | The type_date method returns the SQL expression representing a date data type. | ||
| 2447 | |||
| 2448 | =over 4 | ||
| 2449 | |||
| 2450 | =item type_date example #1 | ||
| 2451 | |||
| 2452 | # given: synopsis | ||
| 2453 | |||
| 2454 | $grammar->type_date({}); | ||
| 2455 | |||
| 2456 | # date | ||
| 2457 | |||
| 2458 | =back | ||
| 2459 | |||
| 2460 | =cut | ||
| 2461 | |||
| 2462 | =head2 type_datetime | ||
| 2463 | |||
| 2464 | type_datetime(HashRef $data) : Str | ||
| 2465 | |||
| 2466 | The type_datetime method returns the SQL expression representing a datetime | ||
| 2467 | data type. | ||
| 2468 | |||
| 2469 | =over 4 | ||
| 2470 | |||
| 2471 | =item type_datetime example #1 | ||
| 2472 | |||
| 2473 | # given: synopsis | ||
| 2474 | |||
| 2475 | $grammar->type_datetime({}); | ||
| 2476 | |||
| 2477 | # datetime | ||
| 2478 | |||
| 2479 | =back | ||
| 2480 | |||
| 2481 | =cut | ||
| 2482 | |||
| 2483 | =head2 type_datetime_wtz | ||
| 2484 | |||
| 2485 | type_datetime_wtz(HashRef $data) : Str | ||
| 2486 | |||
| 2487 | The type_datetime_wtz method returns the SQL expression representing a datetime | ||
| 2488 | (and timezone) data type. | ||
| 2489 | |||
| 2490 | =over 4 | ||
| 2491 | |||
| 2492 | =item type_datetime_wtz example #1 | ||
| 2493 | |||
| 2494 | # given: synopsis | ||
| 2495 | |||
| 2496 | $grammar->type_datetime_wtz({}); | ||
| 2497 | |||
| 2498 | # datetime | ||
| 2499 | |||
| 2500 | =back | ||
| 2501 | |||
| 2502 | =cut | ||
| 2503 | |||
| 2504 | =head2 type_decimal | ||
| 2505 | |||
| 2506 | type_decimal(HashRef $data) : Str | ||
| 2507 | |||
| 2508 | The type_decimal method returns the SQL expression representing a decimal data | ||
| 2509 | type. | ||
| 2510 | |||
| 2511 | =over 4 | ||
| 2512 | |||
| 2513 | =item type_decimal example #1 | ||
| 2514 | |||
| 2515 | # given: synopsis | ||
| 2516 | |||
| 2517 | $grammar->type_decimal({}); | ||
| 2518 | |||
| 2519 | # numeric | ||
| 2520 | |||
| 2521 | =back | ||
| 2522 | |||
| 2523 | =cut | ||
| 2524 | |||
| 2525 | =head2 type_double | ||
| 2526 | |||
| 2527 | type_double(HashRef $data) : Str | ||
| 2528 | |||
| 2529 | The type_double method returns the SQL expression representing a double data | ||
| 2530 | type. | ||
| 2531 | |||
| 2532 | =over 4 | ||
| 2533 | |||
| 2534 | =item type_double example #1 | ||
| 2535 | |||
| 2536 | # given: synopsis | ||
| 2537 | |||
| 2538 | $grammar->type_double({}); | ||
| 2539 | |||
| 2540 | # float | ||
| 2541 | |||
| 2542 | =back | ||
| 2543 | |||
| 2544 | =cut | ||
| 2545 | |||
| 2546 | =head2 type_enum | ||
| 2547 | |||
| 2548 | type_enum(HashRef $data) : Str | ||
| 2549 | |||
| 2550 | The type_enum method returns the SQL expression representing a enum data type. | ||
| 2551 | |||
| 2552 | =over 4 | ||
| 2553 | |||
| 2554 | =item type_enum example #1 | ||
| 2555 | |||
| 2556 | # given: synopsis | ||
| 2557 | |||
| 2558 | $grammar->type_enum({}); | ||
| 2559 | |||
| 2560 | # varchar | ||
| 2561 | |||
| 2562 | =back | ||
| 2563 | |||
| 2564 | =cut | ||
| 2565 | |||
| 2566 | =head2 type_float | ||
| 2567 | |||
| 2568 | type_float(HashRef $data) : Str | ||
| 2569 | |||
| 2570 | The type_float method returns the SQL expression representing a float data | ||
| 2571 | type. | ||
| 2572 | |||
| 2573 | =over 4 | ||
| 2574 | |||
| 2575 | =item type_float example #1 | ||
| 2576 | |||
| 2577 | # given: synopsis | ||
| 2578 | |||
| 2579 | $grammar->type_float({}); | ||
| 2580 | |||
| 2581 | # float | ||
| 2582 | |||
| 2583 | =back | ||
| 2584 | |||
| 2585 | =cut | ||
| 2586 | |||
| 2587 | =head2 type_integer | ||
| 2588 | |||
| 2589 | type_integer(HashRef $data) : Str | ||
| 2590 | |||
| 2591 | The type_integer method returns the SQL expression representing a integer data | ||
| 2592 | type. | ||
| 2593 | |||
| 2594 | =over 4 | ||
| 2595 | |||
| 2596 | =item type_integer example #1 | ||
| 2597 | |||
| 2598 | # given: synopsis | ||
| 2599 | |||
| 2600 | $grammar->type_integer({}); | ||
| 2601 | |||
| 2602 | # integer | ||
| 2603 | |||
| 2604 | =back | ||
| 2605 | |||
| 2606 | =cut | ||
| 2607 | |||
| 2608 | =head2 type_integer_big | ||
| 2609 | |||
| 2610 | type_integer_big(HashRef $data) : Str | ||
| 2611 | |||
| 2612 | The type_integer_big method returns the SQL expression representing a | ||
| 2613 | big-integer data type. | ||
| 2614 | |||
| 2615 | =over 4 | ||
| 2616 | |||
| 2617 | =item type_integer_big example #1 | ||
| 2618 | |||
| 2619 | # given: synopsis | ||
| 2620 | |||
| 2621 | $grammar->type_integer_big({}); | ||
| 2622 | |||
| 2623 | # integer | ||
| 2624 | |||
| 2625 | =back | ||
| 2626 | |||
| 2627 | =cut | ||
| 2628 | |||
| 2629 | =head2 type_integer_big_unsigned | ||
| 2630 | |||
| 2631 | type_integer_big_unsigned(HashRef $data) : Str | ||
| 2632 | |||
| 2633 | The type_integer_big_unsigned method returns the SQL expression representing a | ||
| 2634 | big unsigned integer data type. | ||
| 2635 | |||
| 2636 | =over 4 | ||
| 2637 | |||
| 2638 | =item type_integer_big_unsigned example #1 | ||
| 2639 | |||
| 2640 | # given: synopsis | ||
| 2641 | |||
| 2642 | $grammar->type_integer_big_unsigned({}); | ||
| 2643 | |||
| 2644 | # integer | ||
| 2645 | |||
| 2646 | =back | ||
| 2647 | |||
| 2648 | =cut | ||
| 2649 | |||
| 2650 | =head2 type_integer_medium | ||
| 2651 | |||
| 2652 | type_integer_medium(HashRef $data) : Str | ||
| 2653 | |||
| 2654 | The type_integer_medium method returns the SQL expression representing a medium | ||
| 2655 | integer data type. | ||
| 2656 | |||
| 2657 | =over 4 | ||
| 2658 | |||
| 2659 | =item type_integer_medium example #1 | ||
| 2660 | |||
| 2661 | # given: synopsis | ||
| 2662 | |||
| 2663 | $grammar->type_integer_medium({}); | ||
| 2664 | |||
| 2665 | # integer | ||
| 2666 | |||
| 2667 | =back | ||
| 2668 | |||
| 2669 | =cut | ||
| 2670 | |||
| 2671 | =head2 type_integer_medium_unsigned | ||
| 2672 | |||
| 2673 | type_integer_medium_unsigned(HashRef $data) : Str | ||
| 2674 | |||
| 2675 | The type_integer_medium_unsigned method returns the SQL expression representing | ||
| 2676 | a unsigned medium integer data type. | ||
| 2677 | |||
| 2678 | =over 4 | ||
| 2679 | |||
| 2680 | =item type_integer_medium_unsigned example #1 | ||
| 2681 | |||
| 2682 | # given: synopsis | ||
| 2683 | |||
| 2684 | $grammar->type_integer_medium_unsigned({}); | ||
| 2685 | |||
| 2686 | # integer | ||
| 2687 | |||
| 2688 | =back | ||
| 2689 | |||
| 2690 | =cut | ||
| 2691 | |||
| 2692 | =head2 type_integer_small | ||
| 2693 | |||
| 2694 | type_integer_small(HashRef $data) : Str | ||
| 2695 | |||
| 2696 | The type_integer_small method returns the SQL expression representing a small | ||
| 2697 | integer data type. | ||
| 2698 | |||
| 2699 | =over 4 | ||
| 2700 | |||
| 2701 | =item type_integer_small example #1 | ||
| 2702 | |||
| 2703 | # given: synopsis | ||
| 2704 | |||
| 2705 | $grammar->type_integer_small({}); | ||
| 2706 | |||
| 2707 | # integer | ||
| 2708 | |||
| 2709 | =back | ||
| 2710 | |||
| 2711 | =cut | ||
| 2712 | |||
| 2713 | =head2 type_integer_small_unsigned | ||
| 2714 | |||
| 2715 | type_integer_small_unsigned(HashRef $data) : Str | ||
| 2716 | |||
| 2717 | The type_integer_small_unsigned method returns the SQL expression representing | ||
| 2718 | a unsigned small integer data type. | ||
| 2719 | |||
| 2720 | =over 4 | ||
| 2721 | |||
| 2722 | =item type_integer_small_unsigned example #1 | ||
| 2723 | |||
| 2724 | # given: synopsis | ||
| 2725 | |||
| 2726 | $grammar->type_integer_small_unsigned({}); | ||
| 2727 | |||
| 2728 | # integer | ||
| 2729 | |||
| 2730 | =back | ||
| 2731 | |||
| 2732 | =cut | ||
| 2733 | |||
| 2734 | =head2 type_integer_tiny | ||
| 2735 | |||
| 2736 | type_integer_tiny(HashRef $data) : Str | ||
| 2737 | |||
| 2738 | The type_integer_tiny method returns the SQL expression representing a tiny | ||
| 2739 | integer data type. | ||
| 2740 | |||
| 2741 | =over 4 | ||
| 2742 | |||
| 2743 | =item type_integer_tiny example #1 | ||
| 2744 | |||
| 2745 | # given: synopsis | ||
| 2746 | |||
| 2747 | $grammar->type_integer_tiny({}); | ||
| 2748 | |||
| 2749 | # integer | ||
| 2750 | |||
| 2751 | =back | ||
| 2752 | |||
| 2753 | =cut | ||
| 2754 | |||
| 2755 | =head2 type_integer_tiny_unsigned | ||
| 2756 | |||
| 2757 | type_integer_tiny_unsigned(HashRef $data) : Str | ||
| 2758 | |||
| 2759 | The type_integer_tiny_unsigned method returns the SQL expression representing a | ||
| 2760 | unsigned tiny integer data type. | ||
| 2761 | |||
| 2762 | =over 4 | ||
| 2763 | |||
| 2764 | =item type_integer_tiny_unsigned example #1 | ||
| 2765 | |||
| 2766 | # given: synopsis | ||
| 2767 | |||
| 2768 | $grammar->type_integer_tiny_unsigned({}); | ||
| 2769 | |||
| 2770 | # integer | ||
| 2771 | |||
| 2772 | =back | ||
| 2773 | |||
| 2774 | =cut | ||
| 2775 | |||
| 2776 | =head2 type_integer_unsigned | ||
| 2777 | |||
| 2778 | type_integer_unsigned(HashRef $data) : Str | ||
| 2779 | |||
| 2780 | The type_integer_unsigned method returns the SQL expression representing a | ||
| 2781 | unsigned integer data type. | ||
| 2782 | |||
| 2783 | =over 4 | ||
| 2784 | |||
| 2785 | =item type_integer_unsigned example #1 | ||
| 2786 | |||
| 2787 | # given: synopsis | ||
| 2788 | |||
| 2789 | $grammar->type_integer_unsigned({}); | ||
| 2790 | |||
| 2791 | # integer | ||
| 2792 | |||
| 2793 | =back | ||
| 2794 | |||
| 2795 | =cut | ||
| 2796 | |||
| 2797 | =head2 type_json | ||
| 2798 | |||
| 2799 | type_json(HashRef $data) : Str | ||
| 2800 | |||
| 2801 | The type_json method returns the SQL expression representing a json data type. | ||
| 2802 | |||
| 2803 | =over 4 | ||
| 2804 | |||
| 2805 | =item type_json example #1 | ||
| 2806 | |||
| 2807 | # given: synopsis | ||
| 2808 | |||
| 2809 | $grammar->type_json({}); | ||
| 2810 | |||
| 2811 | # text | ||
| 2812 | |||
| 2813 | =back | ||
| 2814 | |||
| 2815 | =cut | ||
| 2816 | |||
| 2817 | =head2 type_number | ||
| 2818 | |||
| 2819 | type_number(HashRef $data) : Str | ||
| 2820 | |||
| 2821 | The type_number method returns the SQL expression representing a number data | ||
| 2822 | type. | ||
| 2823 | |||
| 2824 | =over 4 | ||
| 2825 | |||
| 2826 | =item type_number example #1 | ||
| 2827 | |||
| 2828 | # given: synopsis | ||
| 2829 | |||
| 2830 | $grammar->type_number({}); | ||
| 2831 | |||
| 2832 | # integer | ||
| 2833 | |||
| 2834 | =back | ||
| 2835 | |||
| 2836 | =cut | ||
| 2837 | |||
| 2838 | =head2 type_string | ||
| 2839 | |||
| 2840 | type_string(HashRef $data) : Str | ||
| 2841 | |||
| 2842 | The type_string method returns the SQL expression representing a string data | ||
| 2843 | type. | ||
| 2844 | |||
| 2845 | =over 4 | ||
| 2846 | |||
| 2847 | =item type_string example #1 | ||
| 2848 | |||
| 2849 | # given: synopsis | ||
| 2850 | |||
| 2851 | $grammar->type_string({}); | ||
| 2852 | |||
| 2853 | # varchar | ||
| 2854 | |||
| 2855 | =back | ||
| 2856 | |||
| 2857 | =cut | ||
| 2858 | |||
| 2859 | =head2 type_text | ||
| 2860 | |||
| 2861 | type_text(HashRef $data) : Str | ||
| 2862 | |||
| 2863 | The type_text method returns the SQL expression representing a text data type. | ||
| 2864 | |||
| 2865 | =over 4 | ||
| 2866 | |||
| 2867 | =item type_text example #1 | ||
| 2868 | |||
| 2869 | # given: synopsis | ||
| 2870 | |||
| 2871 | $grammar->type_text({}); | ||
| 2872 | |||
| 2873 | # text | ||
| 2874 | |||
| 2875 | =back | ||
| 2876 | |||
| 2877 | =cut | ||
| 2878 | |||
| 2879 | =head2 type_text_long | ||
| 2880 | |||
| 2881 | type_text_long(HashRef $data) : Str | ||
| 2882 | |||
| 2883 | The type_text_long method returns the SQL expression representing a long text | ||
| 2884 | data type. | ||
| 2885 | |||
| 2886 | =over 4 | ||
| 2887 | |||
| 2888 | =item type_text_long example #1 | ||
| 2889 | |||
| 2890 | # given: synopsis | ||
| 2891 | |||
| 2892 | $grammar->type_text_long({}); | ||
| 2893 | |||
| 2894 | # text | ||
| 2895 | |||
| 2896 | =back | ||
| 2897 | |||
| 2898 | =cut | ||
| 2899 | |||
| 2900 | =head2 type_text_medium | ||
| 2901 | |||
| 2902 | type_text_medium(HashRef $data) : Str | ||
| 2903 | |||
| 2904 | The type_text_medium method returns the SQL expression representing a medium | ||
| 2905 | text data type. | ||
| 2906 | |||
| 2907 | =over 4 | ||
| 2908 | |||
| 2909 | =item type_text_medium example #1 | ||
| 2910 | |||
| 2911 | # given: synopsis | ||
| 2912 | |||
| 2913 | $grammar->type_text_medium({}); | ||
| 2914 | |||
| 2915 | # text | ||
| 2916 | |||
| 2917 | =back | ||
| 2918 | |||
| 2919 | =cut | ||
| 2920 | |||
| 2921 | =head2 type_time | ||
| 2922 | |||
| 2923 | type_time(HashRef $data) : Str | ||
| 2924 | |||
| 2925 | The type_time method returns the SQL expression representing a time data type. | ||
| 2926 | |||
| 2927 | =over 4 | ||
| 2928 | |||
| 2929 | =item type_time example #1 | ||
| 2930 | |||
| 2931 | # given: synopsis | ||
| 2932 | |||
| 2933 | $grammar->type_time({}); | ||
| 2934 | |||
| 2935 | # time | ||
| 2936 | |||
| 2937 | =back | ||
| 2938 | |||
| 2939 | =cut | ||
| 2940 | |||
| 2941 | =head2 type_time_wtz | ||
| 2942 | |||
| 2943 | type_time_wtz(HashRef $data) : Str | ||
| 2944 | |||
| 2945 | The type_time_wtz method returns the SQL expression representing a time (and | ||
| 2946 | timezone) data type. | ||
| 2947 | |||
| 2948 | =over 4 | ||
| 2949 | |||
| 2950 | =item type_time_wtz example #1 | ||
| 2951 | |||
| 2952 | # given: synopsis | ||
| 2953 | |||
| 2954 | $grammar->type_time_wtz({}); | ||
| 2955 | |||
| 2956 | # time | ||
| 2957 | |||
| 2958 | =back | ||
| 2959 | |||
| 2960 | =cut | ||
| 2961 | |||
| 2962 | =head2 type_timestamp | ||
| 2963 | |||
| 2964 | type_timestamp(HashRef $data) : Str | ||
| 2965 | |||
| 2966 | The type_timestamp method returns the SQL expression representing a timestamp | ||
| 2967 | data type. | ||
| 2968 | |||
| 2969 | =over 4 | ||
| 2970 | |||
| 2971 | =item type_timestamp example #1 | ||
| 2972 | |||
| 2973 | # given: synopsis | ||
| 2974 | |||
| 2975 | $grammar->type_timestamp({}); | ||
| 2976 | |||
| 2977 | # datetime | ||
| 2978 | |||
| 2979 | =back | ||
| 2980 | |||
| 2981 | =cut | ||
| 2982 | |||
| 2983 | =head2 type_timestamp_wtz | ||
| 2984 | |||
| 2985 | type_timestamp_wtz(HashRef $data) : Str | ||
| 2986 | |||
| 2987 | The type_timestamp_wtz method returns the SQL expression representing a | ||
| 2988 | timestamp (and timezone) data type. | ||
| 2989 | |||
| 2990 | =over 4 | ||
| 2991 | |||
| 2992 | =item type_timestamp_wtz example #1 | ||
| 2993 | |||
| 2994 | # given: synopsis | ||
| 2995 | |||
| 2996 | $grammar->type_timestamp_wtz({}); | ||
| 2997 | |||
| 2998 | # datetime | ||
| 2999 | |||
| 3000 | =back | ||
| 3001 | |||
| 3002 | =cut | ||
| 3003 | |||
| 3004 | =head2 type_uuid | ||
| 3005 | |||
| 3006 | type_uuid(HashRef $data) : Str | ||
| 3007 | |||
| 3008 | The type_uuid method returns the SQL expression representing a uuid data type. | ||
| 3009 | |||
| 3010 | =over 4 | ||
| 3011 | |||
| 3012 | =item type_uuid example #1 | ||
| 3013 | |||
| 3014 | # given: synopsis | ||
| 3015 | |||
| 3016 | $grammar->type_uuid({}); | ||
| 3017 | |||
| 3018 | # varchar | ||
| 3019 | |||
| 3020 | =back | ||
| 3021 | |||
| 3022 | =cut | ||
| 3023 | |||
| 3024 | =head2 update | ||
| 3025 | |||
| 3026 | update(HashRef $data) : Object | ||
| 3027 | |||
| 3028 | The update method generates SQL statements to update table rows. | ||
| 3029 | |||
| 3030 | =over 4 | ||
| 3031 | |||
| 3032 | =item update example #1 | ||
| 3033 | |||
| 3034 | # given: synopsis | ||
| 3035 | |||
| 3036 | $grammar->update({ | ||
| 3037 | for => { | ||
| 3038 | table => 'users' | ||
| 3039 | }, | ||
| 3040 | columns => [ | ||
| 3041 | { | ||
| 3042 | column => 'updated', | ||
| 3043 | value => { function => ['now'] } | ||
| 3044 | } | ||
| 3045 | ] | ||
| 3046 | }); | ||
| 3047 | |||
| 3048 | =back | ||
| 3049 | |||
| 3050 | =cut | ||
| 3051 | |||
| 3052 | =head2 validate | ||
| 3053 | |||
| 3054 | validate() : Bool | ||
| 3055 | |||
| 3056 | The validate method validates the data structure defined in the I<"schema"> | ||
| 3057 | property. | ||
| 3058 | |||
| 3059 | =over 4 | ||
| 3060 | |||
| 3061 | =item validate example #1 | ||
| 3062 | |||
| 3063 | # given: synopsis | ||
| 3064 | |||
| 3065 | my $valid = $grammar->validate; | ||
| 3066 | |||
| 3067 | =back | ||
| 3068 | |||
| 3069 | =cut | ||
| 3070 | |||
| 3071 | =head2 value | ||
| 3072 | |||
| 3073 | value(Any $value) : Str | ||
| 3074 | |||
| 3075 | The value method returns the SQL representation of a value. | ||
| 3076 | |||
| 3077 | =over 4 | ||
| 3078 | |||
| 3079 | =item value example #1 | ||
| 3080 | |||
| 3081 | # given: synopsis | ||
| 3082 | |||
| 3083 | $grammar->value(undef); | ||
| 3084 | |||
| 3085 | # NULL | ||
| 3086 | |||
| 3087 | =back | ||
| 3088 | |||
| 3089 | =cut | ||
| 3090 | |||
| 3091 | =head2 view_create | ||
| 3092 | |||
| 3093 | view_create(HashRef $data) : Object | ||
| 3094 | |||
| 3095 | The view_create method generates SQL statements to create a table view. | ||
| 3096 | |||
| 3097 | =over 4 | ||
| 3098 | |||
| 3099 | =item view_create example #1 | ||
| 3100 | |||
| 3101 | # given: synopsis | ||
| 3102 | |||
| 3103 | $grammar->view_create({ | ||
| 3104 | name => 'active_users', | ||
| 3105 | query => { | ||
| 3106 | select => { | ||
| 3107 | from => { | ||
| 3108 | table => 'users' | ||
| 3109 | }, | ||
| 3110 | columns => [ | ||
| 3111 | { | ||
| 3112 | column => '*' | ||
| 3113 | } | ||
| 3114 | ], | ||
| 3115 | where => [ | ||
| 3116 | { | ||
| 3117 | 'not-null' => { | ||
| 3118 | column => 'deleted' | ||
| 3119 | } | ||
| 3120 | } | ||
| 3121 | ] | ||
| 3122 | } | ||
| 3123 | } | ||
| 3124 | }); | ||
| 3125 | |||
| 3126 | =back | ||
| 3127 | |||
| 3128 | =cut | ||
| 3129 | |||
| 3130 | =head2 view_drop | ||
| 3131 | |||
| 3132 | view_drop(HashRef $data) : Object | ||
| 3133 | |||
| 3134 | The view_drop method generates SQL statements to remove a table view. | ||
| 3135 | |||
| 3136 | =over 4 | ||
| 3137 | |||
| 3138 | =item view_drop example #1 | ||
| 3139 | |||
| 3140 | # given: synopsis | ||
| 3141 | |||
| 3142 | $grammar->view_drop({ | ||
| 3143 | name => 'active_users' | ||
| 3144 | }); | ||
| 3145 | |||
| 3146 | =back | ||
| 3147 | |||
| 3148 | =cut | ||
| 3149 | |||
| 3150 | =head2 wrap | ||
| 3151 | |||
| 3152 | wrap(Str $name) : Str | ||
| 3153 | |||
| 3154 | The wrap method returns a SQL-escaped string. | ||
| 3155 | |||
| 3156 | =over 4 | ||
| 3157 | |||
| 3158 | =item wrap example #1 | ||
| 3159 | |||
| 3160 | # given: synopsis | ||
| 3161 | |||
| 3162 | $grammar->wrap('field'); | ||
| 3163 | |||
| 3164 | # "field" | ||
| 3165 | |||
| 3166 | =back | ||
| 3167 | |||
| 3168 | =cut | ||
| 3169 | |||
| 3170 | =head1 AUTHOR | ||
| 3171 | |||
| 3172 | Al Newkirk, C<awncorp@cpan.org> | ||
| 3173 | |||
| 3174 | =head1 LICENSE | ||
| 3175 | |||
| 3176 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 3177 | |||
| 3178 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 3179 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 3180 | file"|https://github.com/iamalnewkirk/sql-engine/blob/master/LICENSE>. | ||
| 3181 | |||
| 3182 | =head1 PROJECT | ||
| 3183 | |||
| 3184 | L<Wiki|https://github.com/iamalnewkirk/sql-engine/wiki> | ||
| 3185 | |||
| 3186 | L<Project|https://github.com/iamalnewkirk/sql-engine> | ||
| 3187 | |||
| 3188 | L<Initiatives|https://github.com/iamalnewkirk/sql-engine/projects> | ||
| 3189 | |||
| 3190 | L<Milestones|https://github.com/iamalnewkirk/sql-engine/milestones> | ||
| 3191 | |||
| 3192 | L<Contributing|https://github.com/iamalnewkirk/sql-engine/blob/master/CONTRIBUTE.md> | ||
| 3193 | |||
| 3194 | L<Issues|https://github.com/iamalnewkirk/sql-engine/issues> | ||
| 3195 | |||
| 3196 | =cut |