| 1 | package EntityModel::Query::Insert; | ||||||||||
| 2 | { | ||||||||||
| 3 | 33 | $EntityModel::Query::Insert::VERSION = '0.102'; | |||||||||
| 4 | } | ||||||||||
| 5 | 33 | ▶ | use EntityModel::Class { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 33 | _isa => [qw{EntityModel::Query}], | |||||||||
| 7 | 'values' => { type => 'array', subclass => 'EntityModel::Query::InsertField' }, | ||||||||||
| 8 | 33 | }; | |||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | EntityModel::Query::Insert - support for INSERT SQL statement | ||||||||||
| 13 | |||||||||||
| 14 | =head1 VERSION | ||||||||||
| 15 | |||||||||||
| 16 | version 0.102 | ||||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | See L<Entitymodel::Query>. | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 24 | See L<Entitymodel::Query>. | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | =head1 METHODS | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | =head2 import | ||||||||||
| 33 | |||||||||||
| 34 | Register the parse handling for our 'insert' attribute. | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | 32 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 32 | my $class = shift; | |||||||||
| 40 | $class->register( | ||||||||||
| 41 | 2 | ▶ | 'insert_into' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 2 | my $self = shift; | |||||||||
| 43 | 2 | $self->upgradeTo('EntityModel::Query::Insert'); | |||||||||
| 44 | 2 | $self->parse_base( | |||||||||
| 45 | @_, | ||||||||||
| 46 | method => 'from', | ||||||||||
| 47 | type => 'EntityModel::Query::FromTable' | ||||||||||
| 48 | ); | ||||||||||
| 49 | } | ||||||||||
| 50 | 32 | ); | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | =head2 type | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | sub type { 'insert'; } | ||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 58 | |||||||||||
| 59 | =head2 parse_values | ||||||||||
| 60 | |||||||||||
| 61 | Populate the values for the insert statement. | ||||||||||
| 62 | |||||||||||
| 63 | =cut | ||||||||||
| 64 | |||||||||||
| 65 | 2 | ▶ | sub parse_values { | ||||||||
|
Subroutine
parse_values: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 66 | 2 | my $self = shift; | |||||||||
| 67 | 2 | my $spec = shift; | |||||||||
| 68 | 2 | foreach my $k (sort keys %$spec) { | |||||||||
| 69 | $self->parse_base({ | ||||||||||
| 70 | name => $k, | ||||||||||
| 71 | 4 | value => $spec->{$k}, | |||||||||
| 72 | }, | ||||||||||
| 73 | method => 'values', | ||||||||||
| 74 | type => 'EntityModel::Query::InsertField' | ||||||||||
| 75 | ); | ||||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | =head2 parse_fields | ||||||||||
| 80 | |||||||||||
| 81 | Populate the values for the insert statement. | ||||||||||
| 82 | |||||||||||
| 83 | =cut | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub parse_fields { | ||||||||
|
Subroutine
parse_fields: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 86 | 0 | my $self = shift; | |||||||||
| 87 | 0 | my $spec = shift; | |||||||||
| 88 | 0 | foreach my $k (sort keys %$spec) { | |||||||||
| 89 | $self->parse_base({ | ||||||||||
| 90 | name => $k, | ||||||||||
| 91 | 0 | value => $spec->{$k}, | |||||||||
| 92 | }, | ||||||||||
| 93 | method => 'values', | ||||||||||
| 94 | type => 'EntityModel::Query::InsertField' | ||||||||||
| 95 | ); | ||||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | =head2 fieldsSQL | ||||||||||
| 100 | |||||||||||
| 101 | =cut | ||||||||||
| 102 | |||||||||||
| 103 | 0 | ▶ | sub fieldsSQL { | ||||||||
|
Subroutine
fieldsSQL: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 104 | 0 | my $self = shift; | |||||||||
| 105 | 0 | logDebug("We have fields: [%s]", join(',', map { $_->field } $self->values->list)); | |||||||||
| 106 | 0 | my $fields = join(', ', grep { defined $_ } map { | |||||||||
| 107 | 0 | $_->asString | |||||||||
| 108 | } $self->values->list); | ||||||||||
| 109 | 0 | logDebug("Fields are [%s]", $fields); | |||||||||
| 110 | 0 | ▶ | return unless $fields; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | return '(' . $fields . ')'; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | =head2 valuesSQL | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | sub valuesSQL { | ||||||||
|
Subroutine
valuesSQL: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 119 | 0 | my $self = shift; | |||||||||
| 120 | 0 | my $sql = join(', ', grep { defined $_ } map { | |||||||||
| 121 | 0 | $_->quotedValue | |||||||||
| 122 | } $self->values->list); | ||||||||||
| 123 | 0 | ▶ | return unless $sql; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | return 'values (' . $sql . ')'; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | =head2 keyword_order | ||||||||||
| 128 | |||||||||||
| 129 | =cut | ||||||||||
| 130 | |||||||||||
| 131 | 0 | ▶ | sub keyword_order { qw{type from fields values returning}; } | ||||||||
|
Subroutine
keyword_order: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 132 | |||||||||||
| 133 | =head2 fromSQL | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||
| 136 | |||||||||||
| 137 | 0 | ▶ | sub fromSQL { | ||||||||
|
Subroutine
fromSQL: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 138 | 0 | my $self = shift; | |||||||||
| 139 | 0 | my $from = join(', ', map { $_->asString } $self->from->list); | |||||||||
| 140 | 0 | ▶ | return unless $from; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | return $from; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | 4 | ▶ | sub inlineSQL { | ||||||||
|
Subroutine
inlineSQL: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 145 | 4 | my $self = shift; | |||||||||
| 146 | my $data = [ | ||||||||||
| 147 | 'insert into ', | ||||||||||
| 148 | 4 | (map { $_->asString } $self->from->list), | |||||||||
| 149 | ' (', | ||||||||||
| 150 | 4 | join(', ', map { $_->name } $self->values->list), | |||||||||
| 151 | ') values (' | ||||||||||
| 152 | ]; | ||||||||||
| 153 | 4 | my @field; | |||||||||
| 154 | 4 | foreach ($self->values->list) { | |||||||||
| 155 | 8 | my $v = $_->value; | |||||||||
| 156 | 8 | ▶ | push @field, ', ' if @field; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 8 | push @field, \$v; | |||||||||
| 158 | } | ||||||||||
| 159 | 4 | push @$data, @field; | |||||||||
| 160 | 4 | push @$data, ')'; | |||||||||
| 161 | 4 | ▶ | if($self->returning->count) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | 2 | push @$data, ' returning '; | |||||||||
| 163 | 2 | push @$data, $_->asString for $self->returning->list; | |||||||||
| 164 | } | ||||||||||
| 165 | 4 | return $data; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | 33 | 1; | |||||||||
| 169 | |||||||||||
| 170 | __END__ | ||||||||||