NestedSet/SQLite.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran n/a Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 83 CC 1 SCAR 6.9
CC
1
Cov
0%
new
2.0
_lock_tables
2.0
_unlock_tables
2.0
CRAP 2.0
1 package DBIx::Tree::NestedSet::SQLite;
2  
3 use strict;
4 use Carp;
5 $DBIx::Tree::NestedSet::SQLite::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  
29 #Transactions are automatically created by SQLite, according to the docs.
30  
31 # my $self=shift;
32 # if(! defined $self->{no_locking}){
33 # $self->{dbh}->do(qq|lock tables $self->{table_name} as n1 write, $self->{table_name} as n2 write, $self->{table_name} write|)
34 # }
35 }
36 ########################################
37  
38  
39 ################################################################################
40 sub _unlock_tables{
41  
42 #Transactions are automatically created by SQLite, according to the docs.
43  
44 # my $self=shift;
45 # if(! defined $self->{no_locking}){
46 # $self->{dbh}->do(qq|unlock tables|)
47 # }
48 }
49 ########################################
50  
51  
52 ################################################################################
53 sub _alter_table{
54 my($self,$name)=@_;
55 my $table=$self->{table_name};
56 my $left=$self->{left_column_name};
57 my $right=$self->{right_column_name};
58 my $dbh=$self->{dbh};
59  
60 my ($base_create)=$dbh->selectrow_array('select sql from sqlite_master where tbl_name = ? and type="table"',undef,($table));
61 $base_create =~ s/^\s?create\s+table\s+$table\s?(.+)/$1/gim;
62 $dbh->do('create temporary table '.$table.'_temp'.$base_create);
63 $dbh->do("insert into ${table}_temp select * from $table");
64 my $recreate=$base_create;
65 $recreate =~ s/(.+)\)$/$1/gim;
66 $recreate .= ", $name text not null)";
67 my $indeces=$dbh->selectcol_arrayref('select sql from sqlite_master where tbl_name=? and type="index"',undef,($table));
68 $dbh->do("drop table $table");
69 $dbh->do("create table $table ".$recreate);
70 foreach (@$indeces) {
71 $dbh->do($_);
72 }
73 $dbh->do("insert into $table select *,'' from ${table}_temp");
74 $dbh->do("drop table ".$table."_temp");
75 }
76 ########################################
77  
78  
79 ################################################################################
80 sub _create_default_table{
81 my $self=shift;
82 my $dbh=$self->{dbh};
83 my $left=$self->{left_column_name};
84 my $right=$self->{right_column_name};
85 my $table=$self->{table_name};
86 my $id=$self->{id_name};
87 my ($create_table,$index1,$index2)=_create_table_statements($table,$id,$left,$right);
88 $dbh->do($create_table);
89 $dbh->do($index1);
90 $dbh->do($index2);
91 }
92 ########################################
93  
94  
95 ################################################################################
96 sub _create_table_statements{
97 my ($table,$id,$left,$right)=@_;
98 return(qq|CREATE TABLE $table ($id integer primary key,
99 $left mediumint(9) NOT NULL,
100 $right mediumint(9) NOT NULL)|,
101 qq|CREATE INDEX $left on $table($left)|,
102 qq|CREATE INDEX $right on $table($right)|);
103 }
104 ########################################
105  
106  
107 ################################################################################
108 sub _get_default_create_table_statement{
109 my $self=shift;
110 my $left=$self->{left_column_name};
111 my $right=$self->{right_column_name};
112 my $table=$self->{table_name};
113 my $id=$self->{id_name};
114 return(join(";\n",_create_table_statements($table,$id,$left,$right))).";\n";
115 }
116 ########################################
117  
118 1;
119  
120 =pod
121  
122 =head1 NAME
123  
124 DBIx::Tree::NestedSet::SQLite
125  
126 =head1 SYNOPSIS
127  
128 A driver class for L<DBIx::Tree::NestedSet> that implements an SQLite interface. There are no publicly available methods in this class.
129  
130 =head1 WARNING
131  
132 You should use this class and L<DBIx::Tree::NestedSet> to create your default table: The way the create table is done in this class is pretty tightly tied to how the "automatic alteration" is done.
133  
134 =cut
135