Thursday, March 30, 2006

Perl 6 Now

ya, I has been at ZhengZhou for several days. and I'll be here in the next month I think.
I received a book from nomas today. It's "Perl 6 Now : The Core Ideas Illustrated with Perl 5" by Scott Walters. you can find more information at http://perl6now.com
I'll share u with my reading experience. :)
busy these days, see u later.
Thursday, March 23, 2006

Authentication and DBIC

因为正在搞的项目使用了多个数据源(说过好多次了。:) 所以只能在运行时指定 Catalyst::Plugin::Authentication::Store::DBIC 所采用的表所在的数据源。将这个 config 放到 Root.pm 下总是会报错。怎么改也改不好,大致原因是 Catalyst 一运行就载入所有的插件 Plugins, 而载入插件时插件就会读取它自己的 config ,所以在 __PACKAGE__->setup; 后修改插件的 config 是不起作用的。而我们获取表所在的数据源需要知道 $c->req->address 类似东西,所以一定会是在 setup 以后。
最后迫不得已到 Catalyst mailing list 发了个 [Catalyst] Catalyst::Plugin::Authentication::Store::DBIC and config in auto
邮件组里的人倒没给我解决,可能是时差关系。因为这是 $work, 所以就只好自己看源码了。最后的报错总是 Can't call method "search" on an undefined value at,研究了个把小时,总算搞定了。

写一个 Module 专门用于 Authentication:
package MyApp::Model::UserAuth;

use strict;
use warnings;
use base 'Catalyst::Model';

sub search {
my $self = shift;
my $user = MyApp->config->{dbic}{geo}->resultset('User')->search(@_);
return $user;
}

1;
这里的 search 方法是专门给 DBIC::User 用的。因为这是一个 Model, 所以可以得到存到 config 里的数据源所在的表的。最后设置 yml 文件。
authentication:
dbic:
user_class: "MyApp::Model::UserAuth"
user_field: "username"
password_field: "password"

:) I like $$$$$.
Tuesday, March 21, 2006

MySQL 首字母大写

MySQL 中有个将所有字母大写的函数,UPPER
但是没有将首字母大写的函数,今天要用到,所以写了一个。
mysql > update test set text = concat(upper(left(text, 1)), mid(text, 2, length(text) - 1));

test 是 table 表,text 为表的字段 field

concat 是用来连接字符串的。
upper 是将所有的字母大写
left(text, 1) 是取出 text 的第一个字符
mid(text, 2, length(text) - 1) 是取出从第二个字符到最后一个。

good luck

Update

OK, 这东西对 lin dao 这样的两个字段是不试用,它只能改成 Lin dao. 如果要改为 Lin Dao 的话那就比较复杂了。还好我们可以用 Perl 快速的写一段代码:
#!/usr/bin/perl

use strict;
use DBI;

my $dbh = DBI->connect("DBI:mysql:fayland:localhost",
'root', 'pass', { RaiseError => 1, PrintError => 1 }) or die $DBI::errstr;

my $sth = $dbh->prepare(
qq{SELECT * FROM test}
);
$sth->execute() or die $DBI::errstr;

my $records = $sth->fetchall_arrayref({});

$sth = $dbh->prepare(
qq{UPDATE test SET text = ? WHERE text = ?}
);

foreach my $record (@$records) {
my $old = $record->{text};
$record->{text} =~ s/(\w+)/\u\L$1/g;
$sth->execute($record->{text}, $old) or die $DBI::errstr;
}

print 'ok';
Monday, March 20, 2006

Marriage wassail of my sister

I need to engage in my one and only sister's marriage wassail this weekend. It means that I cann't join the PerlChina Beijing Conference even I'm willing to.

It's just a coincidence I cann't refuse. Any way, wish you guys have a good party.
Wish my sister happy.
Friday, March 17, 2006

DBIx::Class::Schema

* 为什么要选 DBIx::Class 而不是 Class::DBI ?
因为似乎 Class::DBI 只支持单个数据源的抽象层。而 DBIx::Class 是可以支持多个数据源。

* 为什么是 DBIx::Class::Schema ?
因为 DBIx::Class::Schema 可以先将数据库里的表抽象化而不管这个数据源是什么。你可以随便将不同的表抽象化,只要在后来的代码中指定数据源就可以。而对于不同的数据源可以构造不同的实例。

ok, 我得承认我说得可能很难理解。但是我们可以写一些代码加深我们的印象:
package Foorum::DBIC;

use strict;
use warnings;

use base qw/DBIx::Class::Schema/;

__PACKAGE__->load_classes(qw/User/);

package Foorum::DBIC::User;
use base qw/DBIx::Class/;

__PACKAGE__->load_components(qw/Core/);
__PACKAGE__->table('user');
__PACKAGE__->add_columns(qw/username password/);

上面这两段中我们没有指定这个数据库在哪个地方,而是假设有个数据库,里面会有个 user 表,表里有字段 username, password.
这种不指定数据源而是指定里面的模型结构的做法叫做 Schema, 这与以前的 Class::DBI 不同,Class::DBI 最基类中就要指定数据源。
做完了这个模型,我们就可以在实际代码中指定数据源而且使用了。
use Foorum::DBIC;

my $schema = Foorum::DBIC->connect(
'dbi:mysql:foorum1',
'root',
'pass',
{ AutoCommit => 1 },
);

my $result = $schema->resultset('User')->search( {username => 'fayland'} )->first;
print $result->password();

然后你可以在另外的数据源里使用这个 schema, 比如在上面接下来写:
my $schema2 = Foorum::DBIC->connect(
'dbi:mysql:foorum2',
'root',
'pass',
{ AutoCommit => 1 },
);

my $result2 = $schema->resultset('User')->search( {username => 'fayland'} )->first;
print $result2->password();

只要这个数据源 foorum2 里有个表是 user, 里面有字段 username, password 就可以。

这就是数据库抽象层的另一种做法,一种与 Class::DBI 不同的做法。
当然,我理解的可能有误,我得继续试下去,因为工作中是多个数据源的。非要用 DBIx::Class::Schema 不可。还得用到 Catalyst::Model::DBIC::Schema

* 参考
** perldoc DBIx::Class::Schema
** perldoc Catalyst::Model::DBIC::Schema
Thursday, March 16, 2006

Shanghai Day 4

I'll be back to HangZhou tomorrow. The job stuff is nearly finished, and I miss my bed very much. Getting up early makes me uncomfortable.

guys, see u later, I'm too tired to say anything.
Wednesday, March 15, 2006

Shanghai Day 3

ok, guys. At last, we decided to use DBIx::Class as our model. We planned to migrate to Catalyst within the coming month. I'll share my experience with you in the migration process. Of course, I'll not tell you anything related to the source code, which is not allowed. Yup, I'll be busy in the coming month, which is what I want. ;-)

cnhackTNT told me that he'll come to Perl Conf/BeiJing at 25th this month, yet I'm still not sure. I wish I could go. I bet I'll go if the work is going smoothly in the next week.

God bless me.
Tuesday, March 14, 2006

Shanghai Day 2

It's not a busy day since I don't get into Zorpia's source code too much. My boss Jeffrey want to migrate the old Zorpia platform to Catalyst.
We talked something about Catalyst, especially the Model part. Since the old Zorpia source code have some SQL sentences much more complex than you thought(and we have a much much more complex database structure, which likes load balance on different servers), they doubted whether the DBIx::Class can handle them all and whether there is any speed efficiency problem. Maybe they'll use pure DBI instead, but that's not what I like. I prefer DBIx::Class, which is more cleaner, more expansive. Any way, that's not up to me. My right is to express my idea before all is nailed down.

God bless me.
Monday, March 13, 2006

Shanghai

Hi, I'm at Shanghai now, in the East Asia Hotel at NanJing Eastern Road.
I'll be busy at day, but u guys can drop by after 7pm if u want a chat. :)

I met my fellow workers today, not my boss at this moment. We'll work together this week. to be honest, I'm a bit excited and upset. :)

The wireless of the hotel seems not to work, anyway, I'll query the information desk and try it tomorrow.

I'm just back from WaiTan, a bit tired. I'll update my blog tomorrow if I have free time. :)
Sunday, March 12, 2006

Doodle

ya, I'm a bit reluctant to move from Journal to here(doodle). but I have no choice. The Data Migration from MySQL 4.x to 5.x seems unsuccessful. There must be something wrong. but I have no time to search for the solution, I should go to Shanghai tomorrow.
And another problem, The Apache is hard to get long well with WinXP SP2. That's totally not my fault.
I like Wireless so I pick the WinXp up, and Apache is my coding box(I need modperl).
It's very weird that Apache can run at this moment but fail after reboot. and run when I change something, but doesn't work next time even I change the same thing. aha, she is killing me.

The error.log complains:

[Sun Mar 12 11:39:25 2006] [crit] (OS 10022)提供了一个无效的参数。  : Child 2744: setup_inherited_listeners(), WSASocket failed to open the inherited socket.
[Sun Mar 12 11:39:25 2006] [error] Parent: child process exited with status 3 -- Aborting.
It's not the fault of WinXP SP2 Firewall. I googled something, and did as what they told.
  • close my LMHOSTS lookup
  • Firewall -> Advanced??(高级) -> Local link?(本地连接) -> Services -> HTTP/HTTPS
Those don't help. I think there might be another software is unwilling to coexist with Apache2. yet I'm not sure which one it is. I'll uninstall it definitely when I find it.

God bless me. hooo, snowing here. so cold.

Saturday, March 11, 2006

数据迁移

既然新买了笔记本,就将台式机退休掉。不过里面的数据可一个也丢不得。有时候仅仅用 copy+paste 是行不通的,有些软件还是有其内在的迁移方法。

Subversion
根据中文版 SVN 指南,首先将老电脑里的 svn 数据导出,比如说我自己的 svn repos 在 E:/repos
e:
svnadmin dump repos > dumpfile
然后将 e:/dumpfile 拷贝到新的电脑中。我还是想在 E:/repos 里放自己的 svn repos.
于是在新的电脑里执行:
e:
svnadmin create repos
svnadmin load repos < dumpfile
Firefox
因为 Firefox 保存了我好的网页密码,有些网页密码我自己都不太记得起了。还有呢,n 多插件如果重新安装那不得累死呀。还好在 Mozilla Firefox Support 里找到自己想要的东西。
首先在老的电脑里,找到 C:\Documents and Settings\Administrator\Application Data\Mozilla\Firefox\Profiles (找不到就搜索) 里找到一个 kg06lpn3.default 文件夹,将这文件夹里的所有资料都拷贝到新的电脑里,比如说我就把它拷贝到 E:\Profiles\Firefox\kg06lpn3.default
然后在新的电脑里,找到 C:\Documents and Settings\fayland lam\Application Data\Mozilla\Firefox (找不到就搜索)下面的 profiles.ini, 编辑将两行改为:
IsRelative=0
Path=E:\Profiles\Firefox\kg06lpn3.default
MySQL and Thunderbird
MySQL 类似于 Subversion 的做法,或者用 phpMyAdmin 来搞。
不过我老电脑里用的是 4.x 而新的用的是 5.x 似乎转过来 utf8 的中文显示部分是乱码部分是正常的,弄得我也搞不清了。
而 Thunderbird 则类似于 Firefox, 参见:http://www.mozilla.org/support/thunderbird/profile
这样能将 Thunderbird 的所有邮件和新闻组,还有 filters 都会拷贝过来,啥都不会丢。

还有一些还没弄完,只好等明天再弄了。今天太累了。病也还没好,得早点去睡了。

Switch to Blogger

Ya, I'm doing something wrong within the mysql date migration, So I decide to move my journals to Blogger.

There are some benefits:

* Searchable for several blog search engine.
* comments? maybe not. China Great Wall blocks that.

ya, maybe see u later, it's just a test.