DBIx::DBSchema::Index - Index objects


DBIx-DBSchema documentation Contained in the DBIx-DBSchema distribution.

Index


Code Index:

NAME

Top

DBIx::DBSchema::Index - Index objects

SYNOPSYS

Top

  use DBIx::DBSchema::Index;

  $index = new DBIx::DBSchema::Index (
    {
    }
  );

DESCRIPTION

Top

DBIx::DBSchema::Index objects represent a unique or non-unique database index.

METHODS

Top

new HASHREF | OPTION, VALUE, ...

Creates a new DBIx::DBschema::Index object.

Accepts either a hashref or a list of options and values.

Options are:

name - Index name
using - Optional index method
unique - Boolean indicating whether or not this is a unique index.
columns - List reference of column names (or expressions)

name [ INDEX_NAME ]

Returns or sets the index name.

using [ INDEX_METHOD ]

Returns or sets the optional index method.

unique [ BOOL ]

Returns or sets the unique flag.

columns [ LISTREF ]

Returns or sets the indexed columns (or expressions).

columns_sql

Returns a comma-joined list of columns, suitable for an SQL statement.

sql_create_index TABLENAME

Returns an SQL statment to create this index on the specified table.

cmp OTHER_INDEX_OBJECT

Compares this object to another supplied object. Returns true if they are identical, or false otherwise.

cmp_noname OTHER_INDEX_OBJECT

Compares this object to another supplied object. Returns true if they are identical, disregarding index name, or false otherwise.

AUTHOR

Top

Ivan Kohler <ivan-dbix-dbschema@420.am>

Copyright (c) 2007 Ivan Kohler Copyright (c) 2007 Freeside Internet Services, Inc. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

BUGS

Top

Is there any situation in which sql_create_index needs to return a list of multiple statements?

SEE ALSO

Top

DBIx::DBSchema::Table, DBIx::DBSchema, DBI


DBIx-DBSchema documentation Contained in the DBIx-DBSchema distribution.
package DBIx::DBSchema::Index;

use strict;
use vars qw($VERSION $DEBUG);

$VERSION = 0.1;
$DEBUG = 0;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my %opt = ref($_[0]) ? %{$_[0]} : @_; #want a new reference
  my $self = \%opt;
  bless($self, $class);
}

sub name {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{name} = $value;
  } else {
    $self->{name};
  }
}

sub using {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{using} = $value;
  } else {
    defined($self->{using})
      ? $self->{using}
      : '';
  }
}

sub unique {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{unique} = $value;
  } else {
    #$self->{unique};
    $self->{unique} ? 1 : 0;
  }
}

sub columns {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{columns} = $value;
  } else {
    $self->{columns};
  }
}

sub columns_sql {
  my $self = shift;
  join(', ', @{ $self->columns } );
}

sub sql_create_index {
  my( $self, $table ) = @_;

  my $unique = $self->unique ? 'UNIQUE' : '';
  my $name = $self->name;
  my $col_sql = $self->columns_sql;

  "CREATE $unique INDEX $name ON $table ( $col_sql )";
}

sub cmp {
  my( $self, $other ) = @_;

  $self->name eq $other->name and $self->cmp_noname($other);
}

sub cmp_noname {
  my( $self, $other ) = @_;

      $self->using       eq $other->using
  and $self->unique      == $other->unique
  and $self->columns_sql eq $other->columns_sql;

}

1;