DBD-SQLite

 view release on metacpan or  search on metacpan

lib/DBD/SQLite/VirtualTable.pm  view on Meta::CPAN

#======================================================================
package DBD::SQLite::VirtualTable;
#======================================================================
use strict;
use warnings;
use Scalar::Util    qw/weaken/;

our $VERSION = '1.78';
our @ISA;


#----------------------------------------------------------------------
# methods for registering/destroying the module
#----------------------------------------------------------------------

sub CREATE_MODULE  { my ($class, $mod_name) = @_; }
sub DESTROY_MODULE { my ($class, $mod_name) = @_; }

#----------------------------------------------------------------------
# methods for creating/destroying instances
#----------------------------------------------------------------------

sub CREATE         { my $class = shift; return $class->NEW(@_); }
sub CONNECT        { my $class = shift; return $class->NEW(@_); }

sub _PREPARE_SELF {
  my ($class, $dbh_ref, $module_name, $db_name, $vtab_name, @args) = @_;

  my @columns;
  my %options;

  # args containing '=' are options; others are column declarations
  foreach my $arg (@args) {
    if ($arg =~ /^([^=\s]+)\s*=\s*(.*)/) {
      my ($key, $val) = ($1, $2);
      $val =~ s/^"(.*)"$/$1/;
      $options{$key} = $val;
    }
    else {
      push @columns, $arg;
    }
  }

  # build $self
  my $self =  {
    dbh_ref     => $dbh_ref,
    module_name => $module_name,
    db_name     => $db_name,
    vtab_name   => $vtab_name,
    columns     => \@columns,
    options     => \%options,
   };
  weaken $self->{dbh_ref};

  return $self;
}

sub NEW {
  my $class = shift;

  my $self  = $class->_PREPARE_SELF(@_);
  bless $self, $class;
}


sub VTAB_TO_DECLARE {
  my $self = shift;

  local $" = ", ";
  my $sql = "CREATE TABLE $self->{vtab_name}(@{$self->{columns}})";

  return $sql;
}

sub DROP       { my $self = shift; }
sub DISCONNECT { my $self = shift; }


#----------------------------------------------------------------------
# methods for initiating a search
#----------------------------------------------------------------------

sub BEST_INDEX {
  my ($self, $constraints, $order_by) = @_;

  my $ix = 0;
  foreach my $constraint (grep {$_->{usable}} @$constraints) {
    $constraint->{argvIndex} = $ix++;
    $constraint->{omit}      = 0;
  }

  # stupid default values -- subclasses should put real values instead
  my $outputs = {
    idxNum           => 1,
    idxStr           => "",
    orderByConsumed  => 0,
    estimatedCost    => 1.0,
    estimatedRows    => undef,
   };

  return $outputs;
}


sub OPEN {
  my $self  = shift;
  my $class = ref $self;

  my $cursor_class = $class . "::Cursor";
  return $cursor_class->NEW($self, @_);
}




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