首先,謝謝指點!部分答案如下....
我有什麼那麼遠,首先是一些腳手架:
# Connect to our db through DBIx::Class
my $schema = My::Schema->connect('dbi:SQLite:/home/me/accounts.db');
# See also BEGIN { $ENV{DBIC_TRACE} = 1 }
$schema->storage->debug(1);
# Create an instance of our subclassed (see below)
# DBIx::Class::Storage::Statistics class
my $stats = My::DBIx::Class::Storage::Statistics->new();
# Set the debugobj object on our schema's storage
$schema->storage->debugobj($stats);
和我:: DBIx ::類::存儲::統計被定義:
package My::DBIx::Class::Storage::Statistics;
use base qw<DBIx::Class::Storage::Statistics>;
use Data::Dumper qw<Dumper>;
use SQL::Statement;
use SQL::Parser;
sub query_start {
my ($self, $sql_query, @params) = @_;
print "The original sql query is\n$sql_query\n\n";
my $parser = SQL::Parser->new();
my $stmt = SQL::Statement->new($sql_query, $parser);
#printf "%s\n", $stmt->command;
print "The parameters for this query are:";
print Dumper \@params;
}
這解決了關於如何掛鉤以獲取SQL查詢以「可愛」的問題。
然後我運行一個查詢:
my $rs = $schema->resultset('SomeTable')->search(
{
'email' => $email,
'others.some_col' => 1,
},
{ join => 'others' }
);
$rs->count;
但是SQL ::分析器barfs由DBIx ::類生成的SQL:
The original sql query is
SELECT COUNT(*) FROM some_table me LEFT JOIN others other_table ON (others.some_col_id = me.id) WHERE (others.some_col_id = ? AND email = ?)
SQL ERROR: Bad table or column name '(others' has chars not alphanumeric or underscore!
SQL ERROR: No equijoin condition in WHERE or ON clause
所以...有沒有更好的解析器比SQL ::解析器的工作?
我使用這個模塊來解決兩個問題,1)通過跟蹤鏈接顯示的查詢與特定的子程序,2)漂亮的打印:https://gist.github.com/jar-o/25ba571709de15a83361 – jar 2016-01-31 01:16:53