]> Frank Brehm's Git Trees - books.git/commitdiff
Korrektur Resultset von nativem SQL und anderes
authorFrank Brehm <frank@brehm-online.com>
Mon, 30 Mar 2009 22:36:25 +0000 (22:36 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 30 Mar 2009 22:36:25 +0000 (22:36 +0000)
db/create_v_buchliste.sql [new file with mode: 0644]
lib/FrBr/Books.pm
lib/FrBr/Books/Db.pm
lib/FrBr/Books/Db/BuchListe.pm [new file with mode: 0644]
lib/FrBr/Books/Db/Buecher.pm
lib/FrBr/Books/Model/Schema.pm
lib/FrBr/Books/Util/Author.pm
lib/FrBr/Books/Util/Book.pm

diff --git a/db/create_v_buchliste.sql b/db/create_v_buchliste.sql
new file mode 100644 (file)
index 0000000..5c69342
--- /dev/null
@@ -0,0 +1,75 @@
+
+-- $Id$
+-- $URL$
+
+
+CREATE OR REPLACE VIEW v_buchliste AS 
+SELECT `buch_id`,
+       `title`,
+       `title_original`,
+       `untertitel`,
+       `verlag`,
+       `isbn`,
+       `buch_nr`,
+       `ausgabejahr`,
+       `druckjahr`,
+       `seiten`,
+       `preis`,
+       `waehrungs_kuerzel`,
+       `waehrung`,
+       `ort`,
+       `bindungsart`,
+       GROUP_CONCAT( DISTINCT `autor_id`                            SEPARATOR ', ' ) AS `autor_ids`,
+       GROUP_CONCAT( DISTINCT `autor_name` ORDER BY `autor_ord_num` SEPARATOR ', ' ) AS `autoren`,
+       GROUP_CONCAT( DISTINCT `kategorie_id`                        SEPARATOR ', ' ) AS `kategorie_ids`,
+       GROUP_CONCAT( DISTINCT `kategorie`  ORDER BY `kategorie`     SEPARATOR ', ' ) AS `kategorien`,
+       GROUP_CONCAT( DISTINCT `serien_id`                           SEPARATOR ', ' ) AS `serien_ids`,
+       GROUP_CONCAT( DISTINCT `serie`      ORDER BY `serie`         SEPARATOR ', ' ) AS `serien`
+  FROM ( SELECT b.`id`                AS `buch_id`,
+                b.`title`             AS `title`,
+                b.`title_original`    AS `title_original`,
+                b.`untertitel`        AS `untertitel`,
+                b.`isbn`              AS `isbn`,
+                b.`buch_nr`           AS `buch_nr`,
+                b.`ausgabejahr`       AS `ausgabejahr`,
+                b.`druckjahr`         AS `druckjahr`,
+                b.`seiten`            AS `seiten`,
+                b.`preis`             AS `preis`,
+                w.`waehrungs_kuerzel` AS `waehrungs_kuerzel`,
+                w.`waehrungs_name`    AS `waehrung`,
+                CONCAT( '`', a.`nachname`,
+                        IF( ISNULL(a.`vorname`),     '', ' ' ), IFNULL( a.`vorname`,     '' ),
+                        IF( ISNULL(a.`mittelname`),  '', ' ' ), IFNULL( a.`mittelname`,  '' ),
+                        IF( ISNULL(a.`name_suffix`), '', ' ' ), IFNULL( a.`name_suffix`, '' ),
+                        '`'
+                )                     AS `autor_name`,
+                ab.`ord_num`          AS `autor_ord_num`,
+                CONCAT( '`', a.`id`, '`' ) AS `autor_id`,
+                a.`titel`             AS `autor_titel`,
+                a.`vorname`           AS `autor_vorname`,
+                a.`mittelname`        AS `autor_mittelname`,
+                a.`nachname`          AS `autor_nachname`,
+                a.`name_suffix`       AS `autor_name_suffix`,
+                v.`name_short`        AS `verlag`,
+                o.`name`              AS `ort`,
+                CONCAT( '`', k.`id`, '`' )             AS `kategorie_id`,
+                CONCAT( '`', k.`kategorie_name`, '`' ) AS `kategorie`,
+                CONCAT( '`', s.`id`, '`' )             AS `serien_id`,
+                CONCAT( '`', s.`serien_name`, '`' )    AS `serie`,
+                bi.`art_name`         AS `bindungsart`
+           FROM autor2buch AS ab
+           LEFT  JOIN `autoren`        a  ON ab.`autor_id` = a.`id`
+           RIGHT JOIN `buecher`        b  ON ab.`buch_id`  = b.`id`
+           LEFT  JOIN `bindungsarten`  bi ON b.`bindungsart_id` = bi.`id`
+           LEFT  JOIN `verlage`        v  ON b.`verlags_id` = v.`id`
+           LEFT  JOIN `orte`           o  ON b.`orts_id` = o.`id`
+           LEFT  JOIN `waehrungen`     w  ON b.`waehrungs_id` = w.`id`
+           LEFT  JOIN `buch2kategorie` bk ON b.`id` = bk.`buch_id`
+           LEFT  JOIN `kategorien`     k  ON k.`id` = bk.`kategorie_id`
+           LEFT  JOIN `buch2serie`     bs ON b.`id` = bs.`buch_id`
+           LEFT  JOIN `serien`         s  ON s.`id` = bs.`serien_id`
+  ) AS `a`
+GROUP BY `buch_id`;
+
+-- vim: noai : ts=4 fenc=utf-8 filetype=sql expandtab :
+
index c4508c72fea041265c8caf85c7c8a2884c77ef29..d0a5ec97df9c2d095efd8d23cf68c9939b6bdf8d 100644 (file)
@@ -136,6 +136,16 @@ sub auto : Private {
     $storage->sql_maker->quote_char( [qw/` `/] );
     $storage->sql_maker->name_sep('.');
 
+    my $SQL = "SET NAMES 'utf8'";
+    warn $SQL . ";\n" if $storage->debug();
+
+    $storage->dbh_do(
+        sub {
+            my ( $storage_a, $dbh ) = @_;
+            $dbh->do($SQL);
+        }
+    );
+
     $c->session->{'last_run'} = localtime();
 
     # Letzten und aktuellen Pfad behalten
index 003af1aec17aaca66d62390f080c5b6629879a49..1ceec654fc0fff1e54b70316a3fe352fe147ef76 100644 (file)
@@ -15,6 +15,34 @@ __PACKAGE__->load_classes;
 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:yMIjCo2VODRuy9VqS1AeQA
 
 # You can replace this text with custom content, and it will be preserved on regeneration
+#--------------------------------------------------------------------------
+
+sub connect {
+
+    my $schema = DBIx::Class::Schema::connect(@_);
+
+    return undef unless $schema;
+
+    $schema->storage->sql_maker->quote_char( [qw/` `/] );
+    $schema->storage->sql_maker->name_sep('.');
+
+    my $SQL = "SET NAMES 'utf-8'";
+    warn $SQL . ";\n" if $schema->storage->debug();
+    warn $SQL . ";\n";
+
+    $schema->storage->dbh_do(
+        sub {
+            my ( $storage, $dbh ) = @_;
+            $dbh->do($SQL);
+        }
+    );
+
+    return $schema;
+
+}
+
+#--------------------------------------------------------------------------
+
 1;
 
 __END__
diff --git a/lib/FrBr/Books/Db/BuchListe.pm b/lib/FrBr/Books/Db/BuchListe.pm
new file mode 100644 (file)
index 0000000..cd6d04c
--- /dev/null
@@ -0,0 +1,113 @@
+package FrBr::Books::Db::BuchListe;
+
+# $Id$
+# $URL$
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components("Core");
+
+__PACKAGE__->table("NONE");
+
+__PACKAGE__->add_columns(
+  "id",
+  "title",
+  "title_original",
+  "untertitel",
+  "verlag",
+  "isbn",
+  "buch_nr",
+  "ausgabejahr",
+  "druckjahr",
+  "seiten",
+  "preis",
+    "waehrungs_kuerzel",
+    "waehrung",
+    "ort",
+    "bindungsart",
+    "autor_ids",
+    "autoren",
+    "kategorie_ids",
+    "kategorien",
+    "serien_ids",
+    "serien",
+);
+
+__PACKAGE__->result_source_instance()->name( \<<SQL );
+( SELECT `id`,
+         `title`,
+         `title_original`,
+         `untertitel`,
+         `verlag`,
+         `isbn`,
+         `buch_nr`,
+         `ausgabejahr`,
+         `druckjahr`,
+         `seiten`,
+         `preis`,
+         `waehrungs_kuerzel`,
+         `waehrung`,
+         `ort`,
+         `bindungsart`,
+         GROUP_CONCAT( DISTINCT `autor_id`                            SEPARATOR ', ' ) AS `autor_ids`,
+         GROUP_CONCAT( DISTINCT `autor_name` ORDER BY `autor_ord_num` SEPARATOR ', ' ) AS `autoren`,
+         GROUP_CONCAT( DISTINCT `kategorie_id`                        SEPARATOR ', ' ) AS `kategorie_ids`,
+         GROUP_CONCAT( DISTINCT `kategorie`  ORDER BY `kategorie`     SEPARATOR ', ' ) AS `kategorien`,
+         GROUP_CONCAT( DISTINCT `serien_id`                           SEPARATOR ', ' ) AS `serien_ids`,
+         GROUP_CONCAT( DISTINCT `serie`      ORDER BY `serie`         SEPARATOR ', ' ) AS `serien`
+    FROM ( SELECT b.`id`                AS `id`,
+                  b.`title`             AS `title`,
+                  b.`title_original`    AS `title_original`,
+                  b.`untertitel`        AS `untertitel`,
+                  b.`isbn`              AS `isbn`,
+                  b.`buch_nr`           AS `buch_nr`,
+                  b.`ausgabejahr`       AS `ausgabejahr`,
+                  b.`druckjahr`         AS `druckjahr`,
+                  b.`seiten`            AS `seiten`,
+                  b.`preis`             AS `preis`,
+                  w.`waehrungs_kuerzel` AS `waehrungs_kuerzel`,
+                  w.`waehrungs_name`    AS `waehrung`,
+                  CONCAT( '`', a.`nachname`,
+                          IF( ISNULL(a.`vorname`),     '', ' ' ), IFNULL( a.`vorname`,     '' ),
+                          IF( ISNULL(a.`mittelname`),  '', ' ' ), IFNULL( a.`mittelname`,  '' ),
+                          IF( ISNULL(a.`name_suffix`), '', ' ' ), IFNULL( a.`name_suffix`, '' ),
+                          '`'
+                  )                     AS `autor_name`,
+                  ab.`ord_num`          AS `autor_ord_num`,
+                  CONCAT( '`', a.`id`, '`' ) AS `autor_id`,
+                  a.`titel`             AS `autor_titel`,
+                  a.`vorname`           AS `autor_vorname`,
+                  a.`mittelname`        AS `autor_mittelname`,
+                  a.`nachname`          AS `autor_nachname`,
+                  a.`name_suffix`       AS `autor_name_suffix`,
+                  v.`name_short`        AS `verlag`,
+                  o.`name`              AS `ort`,
+                  CONCAT( '`', k.`id`, '`' )             AS `kategorie_id`,
+                  CONCAT( '`', k.`kategorie_name`, '`' ) AS `kategorie`,
+                  CONCAT( '`', s.`id`, '`' )             AS `serien_id`,
+                  CONCAT( '`', s.`serien_name`, '`' )    AS `serie`,
+                  bi.`art_name`         AS `bindungsart`
+             FROM autor2buch AS ab
+             LEFT  JOIN `autoren`        a  ON ab.`autor_id` = a.`id`
+             RIGHT JOIN `buecher`        b  ON ab.`buch_id`  = b.`id`
+             LEFT  JOIN `bindungsarten`  bi ON b.`bindungsart_id` = bi.`id`
+             LEFT  JOIN `verlage`        v  ON b.`verlags_id` = v.`id`
+             LEFT  JOIN `orte`           o  ON b.`orts_id` = o.`id`
+             LEFT  JOIN `waehrungen`     w  ON b.`waehrungs_id` = w.`id`
+             LEFT  JOIN `buch2kategorie` bk ON b.`id` = bk.`buch_id`
+             LEFT  JOIN `kategorien`     k  ON k.`id` = bk.`kategorie_id`
+             LEFT  JOIN `buch2serie`     bs ON b.`id` = bs.`buch_id`
+             LEFT  JOIN `serien`         s  ON s.`id` = bs.`serien_id`
+    ) AS `a`
+  GROUP BY `id`
+)
+SQL
+
+1;
+
+__END__
+
+# vim: noai : ts=4 fenc=utf-8 filetype=perl expandtab :
index 70b81a852485201cebe9b57f1123568b18efa140..b880a7e3ce8e6307c472e9b876591107f1a33ee8 100644 (file)
@@ -43,16 +43,18 @@ __PACKAGE__->many_to_many( 'kategorien' => 'buch_kategorie', 'kategorie' );
 __PACKAGE__->has_many( 'buch_serie' => 'FrBr::Books::Db::Buch2serie', { 'foreign.buch_id' => 'self.id' } );
 __PACKAGE__->many_to_many( 'serien' => 'buch_serie', 'serie' );
 
+=pod
+
 # Make a new ResultSource based on the buecher class
 my $source = __PACKAGE__->result_source_instance();
 my $list_source = $source->new( $source );
-$list_source->source_name( 'BuecherListe' );
+$list_source->source_name( 'BuchListe' );
 
 # Hand in your query as a scalar reference
 # It will be added as a sub-select after FROM,
 # so pay attention to the surrounding brackets!
 $list_source->name( \<<SQL );
-( SELECT `buch_id`,
+( SELECT `id`,
          `title`,
          `title_original`,
          `untertitel`,
@@ -73,7 +75,7 @@ $list_source->name( \<<SQL );
          GROUP_CONCAT( DISTINCT `kategorie`  ORDER BY `kategorie`     SEPARATOR ', ' ) AS `kategorien`,
          GROUP_CONCAT( DISTINCT `serien_id`                           SEPARATOR ', ' ) AS `serien_ids`,
          GROUP_CONCAT( DISTINCT `serie`      ORDER BY `serie`         SEPARATOR ', ' ) AS `serien`
-    FROM ( SELECT b.`id`                AS `buch_id`,
+    FROM ( SELECT b.`id`                AS `id`,
                   b.`title`             AS `title`,
                   b.`title_original`    AS `title_original`,
                   b.`untertitel`        AS `untertitel`,
@@ -117,13 +119,14 @@ $list_source->name( \<<SQL );
              LEFT  JOIN `buch2serie`     bs ON b.`id` = bs.`buch_id`
              LEFT  JOIN `serien`         s  ON s.`id` = bs.`serien_id`
     ) AS `a`
-  GROUP BY `buch_id`
+  GROUP BY `id`
 )
 SQL
 
 # Finally, register your new ResultSource with your Schema
-FrBr::Books::Db->register_source( 'BuecherListe' => $list_source );
+FrBr::Books::Db->register_source( 'BuchListe' => $list_source );
 
+=cut
 
 1;
 
index b738794347b566e0b3d1c4fdb035038fd0d0288d..c2aa4e63fde4268ee1946810f73b8da6ffa66566 100644 (file)
@@ -4,6 +4,7 @@ package FrBr::Books::Model::Schema;
 # $URL$
 
 use strict;
+use warnings;
 use base 'Catalyst::Model::DBIC::Schema';
 
 __PACKAGE__->config(
@@ -19,17 +20,17 @@ __PACKAGE__->config(
     ],
 );
 
-sub setup {
-
-    my $c = shift;
-    my $K = ( caller(0) )[3] . "(): ";
-    $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
-    warn $K . "aufgerufen.\n";
-
-    $c->NEXT::setup(@_);
-
-    return $c;
-}
+#sub setup {
+#
+#    my $c = shift;
+#    my $K = ( caller(0) )[3] . "(): ";
+#    $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+#    warn $K . "aufgerufen.\n";
+#
+#    $c->NEXT::setup(@_);
+#
+#    return $c;
+#}
 
 
 =head1 NAME
index e2577c06e97141a44825ae55572fa3883ede790f..1b350a7cffa070579770403fa9ce595c16bc0016 100644 (file)
@@ -174,7 +174,12 @@ sub get_author_list {
                 'untertitel',
                 'isbn',
             ];
-            for my $ref ( $c->model('Schema::Autor2buch')->search( $search_params, $other_params )->all() ) {
+            $c->log->debug(  get_output_string( $K, "Suchparameter: ", $search_params ) ) if $c->stash->{'debug_level'} >= 2;
+            $c->log->debug(  get_output_string( $K, "Andere Parameter: ", $other_params ) ) if $c->stash->{'debug_level'} >= 2;
+            my $uhu = $c->model('Schema');
+            #$c->log->debug(  get_output_string( $K, "Datenquelle: ", $uhu ) ) if $c->stash->{'debug_level'} >= 2;
+            #for my $ref ( $c->model('Schema::Autor2buch')->search( $search_params, $other_params )->all() ) {
+            for my $ref ( $c->model('Schema')->resultset('Autor2buch')->search( $search_params, $other_params )->all() ) {
 
                 my $buch = {};
 
index 99f8ed8130429edd402bb4998f6ce6310c15d1e3..570bd947d386d574c2feed0b947ab4cd5c581735 100644 (file)
@@ -141,7 +141,7 @@ sub get_booklist {
     my $order = undef;
 
     if ( $params->{'buch_id'} ) {
-        $b_search_params->{'me.buch_id'} = $params->{'buch_id'};
+        $b_search_params->{'me.id'} = $params->{'buch_id'};
     }
     else {
         $order = collect_sort_criteria($c)
@@ -153,10 +153,10 @@ sub get_booklist {
     # Anzahl der Buecher insgesamt (ohne paginierung) ermitteln
     my $other_params = {};
     $other_params->{'order_by'} = $order;
-    $other_params->{'select'} = [ 'me.buch_id' ];
-    $other_params->{'as'}     = [ 'buch_id' ];
+    $other_params->{'select'} = [ 'me.id' ];
+    $other_params->{'as'}     = [ 'id' ];
 
-    $anzahl_buecher = $c->model('Schema::BuecherListe')->count( $b_search_params, $other_params );
+    $anzahl_buecher = $c->model('Schema::BuchListe')->count( $b_search_params, $other_params );
     $c->log->debug(  get_output_string( $K, "Anzahl gefundene Buecher: ", $anzahl_buecher ) ) if $c->stash->{'debug_level'} >= 2;
 
     $other_params->{'rows'} = $c->stash->{'list_length'} || 20;
@@ -164,8 +164,8 @@ sub get_booklist {
 
     my $book_ids = [];
 
-    for my $book ( $c->model('Schema::BuecherListe')->search( $b_search_params, $other_params )->all() ) {
-        push @$book_ids, $book->get_column('buch_id');
+    for my $book ( $c->model('Schema::BuchListe')->search( $b_search_params, $other_params )->all() ) {
+        push @$book_ids, $book->get_column('id');
     }
 
     return ( wantarray ? ( $book_ids, 0 ) : $book_ids ) unless scalar( @$book_ids );