ObjectDBI

 view release on metacpan or  search on metacpan

lib/ObjectDBI.pm  view on Meta::CPAN

#      $id, $pid, (defined($gpid) ? $gpid : $id), $name, $type, $value
      $id, $pid, (length($gpid) ? $gpid : $id), $name, $type, $value
    );
    if ($self->{debug}) {
      print STDERR "SQL '$sql' with params '" . join("', '", @params) . "'\n";
    }
    if ($self->{dbh}->do($sql, undef, @params)) {
      return $id;
    } else {
      return undef;
    }
  }
}

sub __object_del {
  my $self = shift;
  my $id = int(shift());
  my $sql = "delete from $self->{objtable} where obj_gpid=?";
  if ($self->{debug}) {
    print STDERR "SQL '$sql' with params '$id'\n";
  }
  $self->{dbh}->do($sql, undef, $id);
} 

my $count = 0;

sub __new_id {
  my $self = shift;
  if ($self->{sequence}) {
    my $sql = "select nextval('$self->{sequence}')";
    my $type = $self->{dbtype};
    if ($type =~ /oracle/i) {
      $sql = "SELECT $self->{sequence}.NEXTVAL FROM DUAL";
    } elsif ($type =~ /pg/i || $type =~ /postgres/i) {
      $sql = "select nextval('$self->{sequence}')";
    }
    my $id = $self->{dbh}->selectrow_array($sql);
    return $id;
  } elsif ($self->{sequencesql}) {
    my $id = $self->{dbh}->selectrow_array($self->{sequencesql});
    return $id;
  } elsif ($self->{sequencefnc}) {
    my $fnc = $self->{sequencefnc};
    my $id = &$fnc();
    return $id;
  } else {
    my $id = int(sprintf("%d%.4d", time(), ++$count));
    return $id;
  }
}

sub cursor {
  my $self = shift;
  return ObjectDBI::Cursor->new($self, @_);
}

package ObjectDBI::Cursor;

=head1 CURSORS

Cursors are there to obtain lists of objects in a 'streaming' (as opposed
to 'buffered') fashion.  When the list of objects is (potentially) too long
to retrieve all at once, you'd use a cursor and iterate through it.

=head2 B<my $cursor = $objectdbi-E<gt>cursor([query], [type]);>

or

=head2 B<my $cursor = ObjectDBI::Cursor-E<gt>new($objectdbi, [query], [type]);>

Usage:

  my $cursor = $objectdbi->cursor("foo");
  while (my $ref = $cursor->next()) {
    print Dumper($ref);
  }

Bear in mind that the query given when initializing the cursor is optional.
When no query is given, you simply iterate through the entire set of objects.

=cut

sub new {
  my $class = shift;
  my $objectdbi = shift;
  if (!UNIVERSAL::isa($objectdbi, 'ObjectDBI')) {
    die "Need ObjectDBI reference as argument";
  }
  my $classname = ref($class) || $class; 
  my $self = {
    OBJECTDBI => $objectdbi,
  };
  bless $self, $classname;
  my $sql =
    "SELECT DISTINCT(TABLE1.obj_gpid)" .
    " from $objectdbi->{objtable} as TABLE1 where 1=1";
  my @params;
  $self->{PARAMS} = \@params;
  if (defined($_[0])) {
    my $query = $_[0];
    my @tokens = ObjectDBI::__tokenize_query($query);
    my $parsetree = ObjectDBI::__parse_query(@tokens) || return undef;
    ($sql, @params) = $self->{OBJECTDBI}->__tree_to_sql($parsetree);
    $self->{QUERY} = $query;
  }
  if (defined($_[1])) {
    my $type = $_[1];
    $sql =
      "SELECT DISTINCT(obj_gpid)" .
      " FROM $objectdbi->{objtable}" .
      " WHERE obj_type=?" .
      " AND obj_pid is null" .
      " INTERSECT " .
      $sql;
    unshift @params, $type;
  }
  $self->{SQL} = $sql;
#  $sql =~ s/^SELECT DISTINCT/SELECT MIN/;
  $sql = "SELECT MIN(obj_gpid) FROM ($sql) FOO";
  if ($self->{OBJECTDBI}{debug}) {
    print STDERR "SQL: '$sql' with params: '" . join("', '", @params) . "\n";



( run in 0.635 second using v1.01-cache-2.11-cpan-39bf76dae61 )