add examples and DBIC::Storage::PrettyPrinter
Arthur Axel "fREW" Schmidt [Mon, 6 Sep 2010 23:29:38 +0000 (23:29 +0000)]
Makefile.PL
examples/console.pl [new file with mode: 0644]
lib/DBIx/Class/Storage/PrettyPrinter.pm [new file with mode: 0644]

index 3c71b5d..6d14747 100644 (file)
@@ -18,6 +18,9 @@ test_requires "Test::Exception" => 0;
 test_requires "Test::Warn"      => 0;
 test_requires "Storable"        => 0;   # for cloning in tests
 
+no_index package => 'DBIx::Class::Storage::PrettyPrinter';
+no_index directory => 'examples';
+
 tests_recursive 't';
 
 WriteAll();
diff --git a/examples/console.pl b/examples/console.pl
new file mode 100644 (file)
index 0000000..8f4f770
--- /dev/null
@@ -0,0 +1,20 @@
+#!/sur/bin/env perl
+
+use SQL::Abstract::Tree;
+
+my $sqlat = SQL::Abstract::Tree->new({ profile => 'console' });
+
+my @sql = (
+   "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'",
+   "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'",
+   "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'",
+   "UPDATE session SET expires = ?  WHERE (id = ?)",
+   "INSERT INTO Request (creation_date, is_private, owner_id, request) VALUES (? , ? , ? , ?)",
+   "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] FROM [users_roles] [me] JOIN [roles] [role] ON [role].[id] = [me].[role_id] JOIN [roles_permissions] [role_permissions] ON [role_permissions].[role_id] = [role].[id] JOIN [permissions] [permission] ON [permission].[id] = [role_permissions].[permission_id] JOIN [permissionscreens] [permission_screens] ON [permission_screens].[permission_id] = [permission].[id] JOIN [screens] [screen] ON [screen].[id] = [permission_screens].[screen_id] WHERE ( [me].[user_id] = ? ) GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype]",
+   "SELECT [status], [supplier_id], [ship_to_supplier_id], [request_by_user_id], [is_printed], [creation_date], [id], [date], [fob_state], [is_confirmed], [is_outside_process], [ship_via], [special_instructions], [when_shipped] FROM ( SELECT [status], [supplier_id], [ship_to_supplier_id], [request_by_user_id], [is_printed], [creation_date], [id], [date], [fob_state], [is_confirmed], [is_outside_process], [ship_via], [special_instructions], [when_shipped], ROW_NUMBER() OVER(  ORDER BY [me].[id] DESC ) AS [rno__row__index] FROM ( SELECT [me].[status], [me].[supplier_id], [me].[ship_to_supplier_id], [me].[request_by_user_id], [me].[is_printed], [me].[creation_date], [me].[id], [me].[date], [me].[fob_state], [me].[is_confirmed], [me].[is_outside_process], [me].[ship_via], [me].[special_instructions], [me].[when_shipped] FROM [PurchaseOrders] [me] WHERE ( [me].[status] = ? ) ) [me] ) [me] WHERE [rno__row__index] BETWEEN 1 AND 25",
+   "SELECT me.id, me.name, me.creator_id, group_users.group_id, group_users.user_id, user.id, user.first_name, user.last_name, user.nickname, user.email, user.password, user.is_active, user.logins FROM Group me LEFT JOIN GroupUser group_users ON group_users.group_id = me.id LEFT JOIN User user ON user.id = group_users.user_id WHERE (me.creator_id = ?) ORDER BY name, group_users.group_id",
+
+);
+
+print "\n\n" . $sqlat->format($_) . "\n" for @sql;
+
diff --git a/lib/DBIx/Class/Storage/PrettyPrinter.pm b/lib/DBIx/Class/Storage/PrettyPrinter.pm
new file mode 100644 (file)
index 0000000..58a64a4
--- /dev/null
@@ -0,0 +1,29 @@
+package DBIx::Class::Storage::PrettyPrinter;
+
+use base 'DBIx::Class::Storage::Statistics';
+
+use SQL::Abstract::Tree;
+
+__PACKAGE__->mk_group_accessors( simple => '_sqlat' );
+
+sub new {
+       my $class = shift;
+
+       my $sqlat = SQL::Abstract::Tree->new(shift @_);
+       my $self = $class->next::method(@_);
+
+       $self->_sqlat($sqlat);
+
+       return $self
+}
+
+sub query_start {
+  my $self = shift;
+  my $string = shift;
+
+  my $formatted = $self->_sqlat->format($string);
+
+  $self->next::method($formatted, @_);
+}
+
+1;