[cgi-wiki-dev] C::W::Store::* diff

Jody Belka cgi-wiki-dev@earth.li
Thu, 4 Dec 2003 18:10:11 -0000 (GMT)


------=_20031204181011_45427
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

This is a diff against CGI::Wiki::Store::* that splits out the SQL
portions of some more of the methods in ::Database, and renames the one
existing sql-method that was there.

The purpose of this patch is to make changing some of the sql in
descendents possible without having to copy in the appropriate routine. I
haven't changed all of the methods, as some of them don't seem to make
much sense split out.

This came about as support for some other code I was writing, which does
some inheritence from various parts of CGI::Wiki and needed some changes
to the SQL statements. I'll post more about this at a later date.

*** Important Note ***: This diff is not made against current code, but
against the previous diff I posted to the list, which was against 0.50_01.
At the point i'm sending this mail, the previous one has not appeared yet,
so I will also mention that both diffs can be found at
http://perl.pimb.org/modules/

-- 
Jody Belka
knew (at) pimb (dot) org
------=_20031204181011_45427
Content-Type: text/plain; name="CGI-Wiki-Knew-Collab.diff"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="CGI-Wiki-Knew-Collab.diff"

diff -pubr CGI-Wiki-Knew/lib/CGI/Wiki/Store/Database.pm CGI-Wiki-Knew-Collab/lib/CGI/Wiki/Store/Database.pm
--- CGI-Wiki-Knew/lib/CGI/Wiki/Store/Database.pm	Mon Nov 24 20:49:15 2003
+++ CGI-Wiki-Knew-Collab/lib/CGI/Wiki/Store/Database.pm	Thu Dec  4 14:44:51 2003
@@ -270,12 +270,9 @@ sub list_backlinks {
     croak "Must supply a node name" unless $node;
     my $dbh = $self->dbh;
 
-    my $internal_links_table = $self->{_table_prefix} . "internal_links";
-
-    my $sql = "SELECT link_from FROM $internal_links_table WHERE link_to="
-            . $dbh->quote($node);
+    my $sql = $self->_get_list_backlinks_sql( $dbh );
     my $sth = $dbh->prepare($sql);
-    $sth->execute or croak $dbh->errstr;
+    $sth->execute( $node ) or croak $dbh->errstr;
     my @backlinks;
     while ( my $backlink = $sth->fetchrow_array ) {
         push @backlinks, $backlink;
@@ -283,6 +280,14 @@ sub list_backlinks {
     return @backlinks;
 }
 
+sub _get_list_backlinks_sql {
+    my ($self, $dbh) = @_;
+
+    my $internal_links_table = $self->{_table_prefix} . 'internal_links';
+
+    return "SELECT link_from FROM $internal_links_table WHERE link_to = ?";
+}
+
 =item B<list_dangling_links>
 
   # List all nodes that have been linked to from other nodes but don't
@@ -298,13 +303,7 @@ sub list_dangling_links {
     my $self = shift;
     my $dbh = $self->dbh;
 
-    my $node_table = $self->{_table_prefix} . "node";
-    my $internal_links_table = $self->{_table_prefix} . "internal_links";
-
-    my $sql = "SELECT DISTINCT $internal_links_table.link_to
-               FROM $internal_links_table LEFT JOIN $node_table
-                                   ON $node_table.name=$internal_links_table.link_to
-               WHERE $node_table.version IS NULL";
+    my $sql = $self->_get_list_dangling_links_sql( $dbh );
     my $sth = $dbh->prepare($sql);
     $sth->execute or croak $dbh->errstr;
     my @links;
@@ -314,6 +313,18 @@ sub list_dangling_links {
     return @links;
 }
 
+sub _get_list_dangling_links_sql {
+    my ($self, $dbh) = @_;
+
+    my $node_table = $self->{_table_prefix} . 'node';
+    my $internal_links_table = $self->{_table_prefix} . 'internal_links';
+
+    return "SELECT DISTINCT $internal_links_table.link_to
+            FROM $internal_links_table LEFT JOIN $node_table
+              ON $node_table.name = $internal_links_table.link_to
+            WHERE $node_table.version IS NULL";
+}
+
 =item B<write_node_post_locking>
 
   $store->write_node_post_locking( node     => $node,
@@ -763,13 +774,19 @@ sub list_all_nodes {
     my $self = shift;
     my $dbh = $self->dbh;
 
-    my $node_table = $self->{_table_prefix} . "node";
-
-    my $sql = "SELECT name FROM $node_table;";
+    my $sql = $self->_get_list_all_nodes_sql( $dbh );
     my $nodes = $dbh->selectall_arrayref($sql); 
     return ( map { $_->[0] } (@$nodes) );
 }
 
+sub _get_list_all_nodes_sql {
+    my ($self, $dbh) = @_;
+
+    my $node_table = $self->{_table_prefix} . 'node';
+
+    return "SELECT name FROM $node_table;";
+}
+
 =item B<list_nodes_by_metadata>
 
   # All documentation nodes.
@@ -818,7 +835,7 @@ sub list_nodes_by_metadata {
         $value = lc( $value );
     }
     my $sql =
-         $self->_get_list_by_metadata_sql( ignore_case => $args{ignore_case} );
+         $self->_get_list_nodes_by_metadata_sql( $dbh, ignore_case => $args{ignore_case} );
     my $sth = $dbh->prepare( $sql );
     $sth->execute( $type, $value );
     my @nodes;
@@ -828,8 +845,8 @@ sub list_nodes_by_metadata {
     return @nodes;
 }
 
-sub _get_list_by_metadata_sql {
-    my $self = shift;
+sub _get_list_nodes_by_metadata_sql {
+    my ($self, $dbh) = @_;
 
     my $metadata_table = $self->{_table_prefix} . "metadata";
     my $node_table = $self->{_table_prefix} . "node";
diff -pubr CGI-Wiki-Knew/lib/CGI/Wiki/Store/Pg.pm CGI-Wiki-Knew-Collab/lib/CGI/Wiki/Store/Pg.pm
--- CGI-Wiki-Knew/lib/CGI/Wiki/Store/Pg.pm	Mon Nov 24 20:49:15 2003
+++ CGI-Wiki-Knew-Collab/lib/CGI/Wiki/Store/Pg.pm	Thu Dec  4 17:38:01 2003
@@ -77,8 +77,8 @@ sub check_and_write_node {
     }
 }
 
-sub _get_list_by_metadata_sql {
-    my ($self, %args) = @_;
+sub _get_list_nodes_by_metadata_sql {
+    my ($self, $dbh, %args) = @_;
 
     my $metadata_table = $self->{_table_prefix} . "metadata";
     my $node_table = $self->{_table_prefix} . "node";
diff -pubr CGI-Wiki-Knew/lib/CGI/Wiki/Store/SQLite.pm CGI-Wiki-Knew-Collab/lib/CGI/Wiki/Store/SQLite.pm
--- CGI-Wiki-Knew/lib/CGI/Wiki/Store/SQLite.pm	Mon Nov 24 20:49:15 2003
+++ CGI-Wiki-Knew-Collab/lib/CGI/Wiki/Store/SQLite.pm	Thu Dec  4 17:38:13 2003
@@ -91,8 +91,8 @@ sub check_and_write_node {
     }
 }
 
-sub _get_list_by_metadata_sql {
-    my ($self, %args) = @_;
+sub _get_list_nodes_by_metadata_sql {
+    my ($self, $dbh, %args) = @_;
 
     my $metadata_table = $self->{_table_prefix} . "metadata";
     my $node_table = $self->{_table_prefix} . "node";
------=_20031204181011_45427--