DBIx-PgLink

 view release on metacpan or  search on metacpan

lib/DBIx/PgLink/Accessor/Table.pm  view on Meta::CPAN

    params      => \@params,
  };
}


sub _delete_query {
  my $self = shift;

  my @params = ();
  my $where = $self->_where_clause(\@params);

  return {
    query_text  => <<END_OF_SQL,
DELETE FROM @{[ $self->remote_object_quoted ]}
WHERE $where
END_OF_SQL
    action      => 'D',
    params      => \@params,
  };

}


sub _where_clause {
  my ($self, $params) = @_;

  my @where;
  for my $f (grep { $_->{searchable} } $self->columns->metadata) {
    if ($f->{nullable}) {
      push @where, "($f->{remote_column_quoted} = ? OR ($f->{remote_column_quoted} IS NULL AND ? IS NULL))";
      push @{$params},
        ({ column_name => $f->{old_column_name}, meta => $f }) x 2;
    } else {
      push @where, "$f->{remote_column_quoted} = ?";
      push @{$params},
        { column_name => $f->{old_column_name}, meta => $f };
    }
  }
  return join("\n  AND ", @where);
};



sub create_local_objects {
  my $self = shift;

  $self->create_rowtype; # by HasColumns role
  $self->create_functions;
  $self->create_view;
  $self->create_shadow_table;
  $self->create_rules;

  return 1;
};


sub drop_local_objects {
  my $self = shift;

  for my $obj (
    ['VIEW',      $self->view_quoted],         # cascade to rules
    ['TABLE',     $self->shadow_table_quoted], # cascade to triggers
    ['FUNCTION',  $self->function0_quoted_sign],
    ['FUNCTION',  $self->function3_quoted_sign],
    ['FUNCTION',  $self->function_set_filter_quoted_sign],
    ['FUNCTION',  $self->function_reset_filter_quoted_sign],
    ['TYPE',      $self->rowtype_quoted],
  ) {
    pg_dbh->do("DROP $obj->[0] IF EXISTS $obj->[1]");
  }
};


sub _create_function {
  my ($self, $name, $query, $purpose) = @_;

  pg_dbh->do($query, {types=>[]});

  pg_dbh->do(<<END_OF_SQL);
REVOKE ALL ON FUNCTION $name FROM public;
END_OF_SQL

  $self->create_comment(
    type    => "FUNCTION",
    name    => $name,
    comment => $purpose . " for remote " . $self->remote_object_type . " " . $self->remote_object_quoted,
  );
  trace_msg('INFO', "Created function $name")
    if trace_level >= 1;
}


sub create_functions {
  my $self = shift;

  # (where,param_values,param_types)
  $self->_create_function($self->function3_quoted_sign, <<END_OF_SQL, 'SELECT function');
CREATE OR REPLACE FUNCTION @{[ $self->function3_quoted_sign ]}
RETURNS SETOF @{[ $self->rowtype_quoted ]}
SECURITY DEFINER
LANGUAGE plperlu
AS \$method_body\$
  use DBIx::PgLink;
  DBIx::PgLink->connect(
    @{[ $self->perl_quote($self->conn_name) ]}
  )->remote_accessor_query(
    object_id    => @{[ $self->object_id ]},
    where        => \$_[0],
    defined \$_[1] ? (param_values => \$_[1]) : (),
    defined \$_[2] ? (param_types  => \$_[2]) : (),
  );
\$method_body\$
END_OF_SQL

  # ()
  $self->_create_function($self->function0_quoted_sign, <<END_OF_SQL, 'SELECT function');
CREATE OR REPLACE FUNCTION @{[ $self->function0_quoted_sign ]}
RETURNS SETOF @{[ $self->rowtype_quoted ]}
SECURITY DEFINER
LANGUAGE sql
AS \$method_body\$
  SELECT * FROM @{[ $self->function_quoted ]}(''::text, NULL::text[], NULL::text[])



( run in 0.486 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )