NanoAでデータベースを使ってみる

2010年2月14日
| コメント(0) | トラックバック(1) NanoAでデータベースを使ってみる

小さな事からコツコツと。

ということで、nopasteのデータベース使用版とログイン不要の一行掲示板を作ってみた。

データベースを使うにあたって、はじめは素のDBIを使っていたのだが、折角なのでプラグインを作ってみた。
…と言っても、MENTAのプラグインをNanoA用に書き直しただけなんだけど。

やはりページネーションが便利ですよ。

package plugin::sql;
 
use strict;
use warnings;
use utf8;
 
use base qw(NanoA::Plugin);
 
sub init_plugin {
    my ($klass, $controller) = @_;
    no strict 'refs';
    no warnings 'redefine';
 
    # Usage: $app->sql_do($statement);
    *{$controller . '::sql_do'} = sub {
        my ($app, $sql) = @_;
        my $dbh = $app->db;
 
        $dbh->do($sql) or die $dbh->errstr;
    };
    # Usage: $app->sql_prepare_exec($statement, \@params);
    *{$controller . '::sql_prepare_exec'} = sub {
        my ($app, $sql, $params) = @_;
        my $dbh = $app->db;
        $params = [] unless ref $params eq 'ARRAY';
 
        my $sth = $dbh->prepare($sql) or die $dbh->errstr;
        $sth->execute(@{$params}) or die $dbh->errstr;
        $sth->finish;
        undef $sth;
    };
    # Usage: my $rows = $app->sql_select_all($statement, \@params);
    # $rows : foreach my $row (@{$rows}) { $row->{field} }
    *{$controller . '::sql_select_all'} = sub {
        my ($app, $sql, $params) = @_;
        my $dbh = $app->db;
        $params = [] unless ref $params eq 'ARRAY';
 
        my $sth = $dbh->prepare($sql) or die $dbh->errstr;
        $sth->execute(@{$params}) or die $dbh->errstr;
        my @res;
        while (my $row = $sth->fetchrow_hashref) {
            push @res, $row;
        }
        $sth->finish;
        undef $sth;
 
        return \@res;
    };
    # Usage: my ($rows, $pager) = $app->sql_select_paginate($statement, \@params, { page => num_of_page, rows => num_of_rows });
    # $rows             : foreach my $row (@{$rows}) { $row->{field} }
    # $pager->{page}    : page number
    # $pager->{has_next}: has next page
    # $pager->{has_prev}: has prev page
    *{$controller . '::sql_select_paginate'} = sub {
        my ($app, $sql, $params, $paging) = @_;
        my $dbh = $app->db;
        $params = [] unless ref $params eq 'ARRAY';
 
        $sql .= ' LIMIT ? OFFSET ?';
        my $sth = $dbh->prepare($sql) or die $dbh->errstr;
        $sth->execute(@{$params}, $paging->{rows}+1, ($paging->{page}-1)*$paging->{rows}) or die $dbh->errstr;
 
        my @res;
        while (my $row = $sth->fetchrow_hashref) {
            push @res, $row;
        }
        $sth->finish;
        undef $sth;
 
        my $has_next = 0;
        if ( @res == $paging->{rows} + 1 ) {
            pop @res;
            $has_next++;
        }
 
        return (\@res, {page => $paging->{page}, has_next => $has_next, has_prev => ($paging->{page} != 1) ? 1 : 0});
    };
}
 
# initialize myself
__PACKAGE__->init_plugin(__PACKAGE__);
 
1;

ついでに、それ用のテンプレートも作ってみた。
まあ、MENTAの(以下略

? my $pager = $c->{pager};
? my $action = $c->{action};
? my $page_n = $pager->{page};
? if ($pager->{has_prev}) {
<a href="<?= $app->uri_for($action, { page => $page_n - 1 }) ?>" rel="prev">前</a>
? } else {
<span class="disabled">前</span>
? }
|(現在:<?= $page_n ?>ページ)|
? if ($pager->{has_next}) {
<a href="<?= $app->uri_for($action, { page => $page_n + 1 }) ?>" rel="next">次</a>
? } else {
<span class="disabled">次</span>
? }


formプラグインを通じてHTML::AutoFormを使っているが、checkboxのcheckedが思ったように動かない。
checked=>1にしても、チェックが入っていない状態になる。
HTML::AutoFormのtディレクトリにあるコードと同じように書いているのだけど。
何故だ…。

トラックバック(1)

このブログ記事に対するトラックバックURL:

このブログ記事を参照しているブログ一覧:

未だにNanoAを触っているわけですが、ずっと悩んでいたchecked問題を、解... 続きを読む

コメントする

Google検索

Last.fm

このブログ記事について

このページは、のぶりんが2010年2月14日 21:30に書いたブログ記事です。

ひとつ前のブログ記事は「CRLFって重要!」です。

次のブログ記事は「NanoAでAuthenticationしたい」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

Creative Commons License
このブログのライセンスは クリエイティブ・コモンズライセンス.
Powered by Movable Type