| 1 | package DBIx::Tree::NestedSet::MySQL; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | $DBIx::Tree::NestedSet::MySQL::VERSION='0.15'; | ||
| 6 | |||
| 7 | ################################################################################ | ||
| 8 | sub new{ | ||
| 9 | my $class=shift; | ||
| 10 | $class=ref($class)||$class; | ||
| 11 | my %params=@_; | ||
| 12 | my $self={ | ||
| 13 | dbh => $params{dbh}, | ||
| 14 | left_column_name => $params{left_column_name} || 'lft', | ||
| 15 | right_column_name => $params{right_column_name} || 'rght', | ||
| 16 | table_name => $params{table_name} || 'nested_set', | ||
| 17 | id_name => $params{id_name} || 'id', | ||
| 18 | no_alter_table => $params{no_alter_table} || undef, | ||
| 19 | no_locking => $params{no_locking} || undef | ||
| 20 | }; | ||
| 21 | bless $self, $class; | ||
| 22 | } | ||
| 23 | ######################################## | ||
| 24 | |||
| 25 | |||
| 26 | ################################################################################ | ||
| 27 | sub _lock_tables{ | ||
| 28 | my $self=shift; | ||
| 29 | if(! defined $self->{no_locking}){ | ||
| 30 | $self->{dbh}->do(qq|lock tables $self->{table_name} as n1 write, $self->{table_name} as n2 write, $self->{table_name} write|) | ||
| 31 | } | ||
| 32 | } | ||
| 33 | ######################################## | ||
| 34 | |||
| 35 | |||
| 36 | ################################################################################ | ||
| 37 | sub _unlock_tables{ | ||
| 38 | my $self=shift; | ||
| 39 | if(! defined $self->{no_locking}){ | ||
| 40 | $self->{dbh}->do(qq|unlock tables|) | ||
| 41 | } | ||
| 42 | } | ||
| 43 | ######################################## | ||
| 44 | |||
| 45 | |||
| 46 | ################################################################################ | ||
| 47 | sub _alter_table{ | ||
| 48 | my($self,$name)=@_; | ||
| 49 | my $table=$self->{table_name}; | ||
| 50 | $self->{dbh}->do("alter table $table add column $name varchar(255) not null default ''"); | ||
| 51 | } | ||
| 52 | ######################################## | ||
| 53 | |||
| 54 | |||
| 55 | ################################################################################ | ||
| 56 | sub _create_default_table{ | ||
| 57 | my $self=shift; | ||
| 58 | my $dbh=$self->{dbh}; | ||
| 59 | my $left=$self->{left_column_name}; | ||
| 60 | my $right=$self->{right_column_name}; | ||
| 61 | my $table=$self->{table_name}; | ||
| 62 | my $id=$self->{id_name}; | ||
| 63 | |||
| 64 | $dbh->do(_create_table_statement($table,$id,$left,$right)); | ||
| 65 | } | ||
| 66 | ######################################## | ||
| 67 | |||
| 68 | |||
| 69 | ################################################################################ | ||
| 70 | sub _get_default_create_table_statement{ | ||
| 71 | my $self=shift; | ||
| 72 | my $left=$self->{left_column_name}; | ||
| 73 | my $right=$self->{right_column_name}; | ||
| 74 | my $table=$self->{table_name}; | ||
| 75 | my $id=$self->{id_name}; | ||
| 76 | return _create_table_statement($table,$id,$left,$right); | ||
| 77 | } | ||
| 78 | ######################################## | ||
| 79 | |||
| 80 | |||
| 81 | ################################################################################ | ||
| 82 | sub _create_table_statement{ | ||
| 83 | my ($table,$id,$left,$right)=@_; | ||
| 84 | return qq| | ||
| 85 | CREATE TABLE $table ( | ||
| 86 | $id mediumint(9) NOT NULL auto_increment, | ||
| 87 | $left mediumint(9) NOT NULL default '0', | ||
| 88 | $right mediumint(9) NOT NULL default '0', | ||
| 89 | PRIMARY KEY ($id), | ||
| 90 | KEY $left ($left), | ||
| 91 | KEY $right ($right) | ||
| 92 | )|; | ||
| 93 | } | ||
| 94 | ######################################## | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | __END__ |