| 1 | package DBIx::EAV::Table; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use SQL::Abstract; | ||
| 5 | use constant { | ||
| 6 | SQL_DEBUG => $ENV{DBIX_EAV_TRACE} | ||
| 7 | }; | ||
| 8 | |||
| 9 | my $sql = SQL::Abstract->new; | ||
| 10 | |||
| 11 | has '_dbh', is => 'ro', required => 1, init_arg => 'dbh'; | ||
| 12 | has 'name', is => 'ro', required => 1; | ||
| 13 | has 'columns', is => 'ro', required => 1; | ||
| 14 | has 'tenant_id', is => 'ro'; | ||
| 15 | |||
| 16 | |||
| 17 | sub BUILD { | ||
| 18 | my $self = shift; | ||
| 19 | |||
| 20 | die sprintf "Error instantiating table '%s': tenant_id is required!" | ||
| 21 | if $self->has_column('tenant_id') && !defined $self->tenant_id; | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | sub has_column { | ||
| 27 | my ($self, $name) = @_; | ||
| 28 | foreach (@{$self->columns}) { | ||
| 29 | return 1 if $_ eq $name; | ||
| 30 | } | ||
| 31 | 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub select { | ||
| 35 | my ($self, $where) = @_; | ||
| 36 | $where //= {}; | ||
| 37 | $where = $self->_mangle_where($where); | ||
| 38 | |||
| 39 | my ($stmt, @bind) = $sql->select($self->name.' AS me', $self->columns, $where); | ||
| 40 | my ($rv, $sth) = $self->_do($stmt, \@bind); | ||
| 41 | $sth; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub select_one { | ||
| 45 | my ($self, $where) = @_; | ||
| 46 | $self->select($where)->fetchrow_hashref; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub insert { | ||
| 50 | my ($self, $data) = @_; | ||
| 51 | |||
| 52 | $data->{tenant_id} = $self->tenant_id | ||
| 53 | if $self->has_column('tenant_id'); | ||
| 54 | |||
| 55 | my ($stmt, @bind) = $sql->insert($self->name, $data); | ||
| 56 | my ($rv, $sth) = $self->_do($stmt, \@bind); | ||
| 57 | |||
| 58 | if ($rv == 1) { | ||
| 59 | return $self->_dbh->last_insert_id(undef, undef, undef, undef) || 1; | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | $rv; | ||
| 63 | } | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | sub update { | ||
| 68 | my ($self, $data, $where) = @_; | ||
| 69 | $where = $self->_mangle_where($where); | ||
| 70 | |||
| 71 | my ($stmt, @bind) = $sql->update($self->name, $data, $where); | ||
| 72 | my ($rv, $sth) = $self->_do($stmt, \@bind); | ||
| 73 | $rv; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub delete { | ||
| 77 | my ($self, $where, $opts) = @_; | ||
| 78 | $opts //= {}; | ||
| 79 | |||
| 80 | my $stmt = $opts->{join} ? sprintf("DELETE me FROM %s AS me", $self->name) | ||
| 81 | : sprintf("DELETE FROM %s", $self->name); | ||
| 82 | |||
| 83 | # JOIN | ||
| 84 | while (my ($table, $spec) = each %{ $opts->{join} || {} }) { | ||
| 85 | |||
| 86 | my ($join_criteria, @bind) = $sql->where($spec); | ||
| 87 | while ( (my $offset = index($join_criteria, '?')) > -1) { | ||
| 88 | my $val = shift @bind; | ||
| 89 | substr($join_criteria, $offset, 1, $val); | ||
| 90 | } | ||
| 91 | $join_criteria =~ s/^\s*WHERE//; | ||
| 92 | $join_criteria =~ s/\btheir\./$table./g; | ||
| 93 | $stmt .= " INNER JOIN $table ON $join_criteria"; | ||
| 94 | } | ||
| 95 | |||
| 96 | # WHERE | ||
| 97 | my ($where_part, @bind); | ||
| 98 | if ($where) { | ||
| 99 | $where = $self->_mangle_where($where); | ||
| 100 | ($where_part, @bind) = $sql->where($where); | ||
| 101 | $stmt .= " $where_part"; | ||
| 102 | } | ||
| 103 | |||
| 104 | my ($rv, $sth) = $self->_do($stmt, \@bind); | ||
| 105 | $rv; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _mangle_where { | ||
| 109 | my ($self, $where) = @_; | ||
| 110 | |||
| 111 | return $where unless $self->has_column('tenant_id'); | ||
| 112 | |||
| 113 | if (ref $where eq 'HASH') { | ||
| 114 | $where->{tenant_id} = $self->tenant_id; | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | $where = { -and => [ tenant_id => $self->tenant_id, $where ] }; | ||
| 118 | } | ||
| 119 | |||
| 120 | $where; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | sub _do { | ||
| 126 | my ($self, $stmt, $bind) = @_; | ||
| 127 | |||
| 128 | if (SQL_DEBUG) { | ||
| 129 | my $i = 0; | ||
| 130 | printf STDERR "$stmt: %s\n", | ||
| 131 | join(' ', map { $i++.'='.$_ } @$bind); | ||
| 132 | } | ||
| 133 | |||
| 134 | my $sth = $self->_dbh->prepare($stmt); | ||
| 135 | my $rv = $sth->execute(ref $bind eq 'ARRAY' ? @$bind : ()); | ||
| 136 | die $sth->errstr unless defined $rv; | ||
| 137 | |||
| 138 | return ($rv, $sth); | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | 1; | ||
| 146 | |||
| 147 | __END__ |