<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>Aerialarts &#187; db</title>
	<atom:link href="http://aerial.st/tags/db/feed/" rel="self" type="application/rss+xml" />
	<link>http://aerial.st</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 01:27:51 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/tags/db/feed/" />
		<item>
		<title>Catalystでnamed_scope風実装</title>
		<link>http://aerial.st/archive/2011/05/13/named-scope-like-with-catalyst/</link>
		<comments>http://aerial.st/archive/2011/05/13/named-scope-like-with-catalyst/#comments</comments>
		<pubDate>Fri, 13 May 2011 10:49:28 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[catalyst]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=1042</guid>
		<description><![CDATA[泣きながらPerl/Catalystを書いている今日この頃です。慣れない言語は辛い。 PerlのORマッパーであるDBIx::Classを使ってるんですが、とりあえずDBIx::Class beginnersを見ると、使い方の指針やFat Modelっぽい書き方が載っていて大変良いです。ActiveRecord大好きです。 で、CatalystにはRailsのnamed_scopeのような便利な記法は標準では提供されていません。多分。でもDBIx::Class::ResultSetのサブクラスを使うようにすれば、それっぽいことはできるようになります。 まず アプリ名をMyAppとします。 my $rs = $c->("DBIC::Foo")が返すのはDBIx::Class::ResultSetのインスタンスで、これはクエリを投げて返ってきた集合に対して処理をするために用います。 一方、例えばmy $foo = $rs->firstが返すのはMyApp::Schema::Result::Fooのインスタンスで、これは個々のレコードに対して処理をするために用います。 なので、RailsのActiveRecordの雰囲気的にはDBIx::Class::ResultSetにモデルのクラスメソッド・named_scopeを実装して、MyApp::Schema::Result::Fooにモデルのインスタンスメソッドを実装する、という感じになります。 ::Result::Fooに::ResultSet::Fooを対応付ける デフォルトだとResultSetにはDBIx::Class::ResultSetが使われるため、全てのテーブルで共通の実装をすることになってしまい、あまり嬉しくありません（共通の処理を実装するなら別ですが）。 なのでまずはMyApp::Schema::Result::FooにMyApp::Schema::ResultSet::Fooを対応付ける必要があります。 この機能は各::Result::*で共通で利用したいので、lib/MyApp/Schema/Result/Base.pm を継承して使えるようにしておきます。 package MyApp::Schema::Result::Base; use strict; use warnings; # 継承されたときに呼ばれる、らしい sub import { my $caller = caller; # 継承の子のパッケージ # 継承の子側に対して、resultset_classに::ResultSet::Fooを設定する。 # クラス名はResultをResultSetに置換したもの。 $caller->resultset_class(sub { my $pkg = $caller; $pkg =~ s/Result/ResultSet/; return $pkg; }->()); } 1; さて、lib/MyApp/Schema/Result/Foo.pm ではBase.pmを継承するだけです。 package MyApp::Schema::Result::Foo; use strict; use warnings; use MyApp::Schema::Result::Base; 1; 以上で MyApp::Schema::ResultSet::Foo があれば$c->model("DBIC::Foo")はそのインスタンスを返し、無ければこれまでどおりDBIx::Class::ResulSetのインスタンスを返すようになります。 なお、MyApp::Schema::ResultSet::FooはDBIx::Class::ResulSetを継承する必要があります。 package MyApp::Schema::ResultSet::Foo; use base 'DBIx::Class::ResulSet'; 1; named_scope風のものを::ResultSet::Fooに実装する package MyApp::Schema::ResultSet::Foo; [...]]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/05/13/named-scope-like-with-catalyst/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/05/13/named-scope-like-with-catalyst/" />
	</item>
		<item>
		<title>SQL:group byとdistinctの同時使用でcount</title>
		<link>http://aerial.st/archive/2009/07/28/sql-group-by-and-distinct/</link>
		<comments>http://aerial.st/archive/2009/07/28/sql-group-by-and-distinct/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 17:02:52 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=514</guid>
		<description><![CDATA[MySQL 5.0.x 以下の例でcount(*)はcount(foo)と同じ。 全データ fooとbarの2カラムがあって、それらの値はいくつか重複している。 mysql> select * from hoges; +----+------+------+---------------------+---------------------+ &#124; id &#124; foo &#124; bar &#124; created_at &#124; updated_at &#124; +----+------+------+---------------------+---------------------+ &#124; 1 &#124; 1 &#124; 11 &#124; 2009-07-27 16:35:23 &#124; 2009-07-27 16:35:23 &#124; &#124; 2 &#124; 2 &#124; 12 &#124; 2009-07-27 16:35:23 &#124; 2009-07-27 16:35:23 &#124; +----+------+------+---------------------+---------------------+ &#124; 3 &#124; 1 &#124; 11 &#124; 2009-07-27 16:35:33 &#124; 2009-07-27 16:35:33 &#124; &#124; 4 &#124; 2 &#124; 12 &#124; 2009-07-27 16:35:33 &#124; 2009-07-27 16:35:33 &#124; +----+------+------+---------------------+---------------------+ &#124; 5 &#124; [...]]]></description>
		<wfw:commentRss>http://aerial.st/archive/2009/07/28/sql-group-by-and-distinct/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2009/07/28/sql-group-by-and-distinct/" />
	</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/


Served from: aerial.st @ 2012-05-23 01:25:55 -->
