Merge 'trunk' into 'sybase'
Rafael Kitover [Sat, 23 May 2009 20:48:06 +0000 (20:48 +0000)]
225 files changed:
.gitignore
Changes
MANIFEST.SKIP
Makefile.PL
examples/Schema/MyDatabase/Main.pm
examples/Schema/MyDatabase/Main/Result/Artist.pm [moved from examples/Schema/MyDatabase/Main/Artist.pm with 65% similarity]
examples/Schema/MyDatabase/Main/Result/Cd.pm [moved from examples/Schema/MyDatabase/Main/Cd.pm with 51% similarity]
examples/Schema/MyDatabase/Main/Result/Track.pm [moved from examples/Schema/MyDatabase/Main/Track.pm with 65% similarity]
examples/Schema/testdb.pl
lib/DBIx/Class.pm
lib/DBIx/Class/CDBICompat/AbstractSearch.pm
lib/DBIx/Class/CDBICompat/ColumnGroups.pm
lib/DBIx/Class/CDBICompat/ColumnsAsHash.pm
lib/DBIx/Class/CDBICompat/Copy.pm
lib/DBIx/Class/CDBICompat/ImaDBI.pm
lib/DBIx/Class/CDBICompat/Iterator.pm
lib/DBIx/Class/CDBICompat/LiveObjectIndex.pm
lib/DBIx/Class/CDBICompat/NoObjectIndex.pm
lib/DBIx/Class/CDBICompat/Relationship.pm
lib/DBIx/Class/CDBICompat/Relationships.pm
lib/DBIx/Class/CDBICompat/Retrieve.pm
lib/DBIx/Class/Componentised.pm
lib/DBIx/Class/DB.pm
lib/DBIx/Class/InflateColumn/DateTime.pm
lib/DBIx/Class/Manual/Cookbook.pod
lib/DBIx/Class/Manual/FAQ.pod
lib/DBIx/Class/Manual/Glossary.pod
lib/DBIx/Class/Manual/Reading.pod
lib/DBIx/Class/Manual/Troubleshooting.pod
lib/DBIx/Class/Ordered.pm
lib/DBIx/Class/Relationship/Accessor.pm
lib/DBIx/Class/Relationship/Base.pm
lib/DBIx/Class/Relationship/BelongsTo.pm
lib/DBIx/Class/Relationship/ManyToMany.pm
lib/DBIx/Class/ResultClass/HashRefInflator.pm
lib/DBIx/Class/ResultSet.pm
lib/DBIx/Class/ResultSetColumn.pm
lib/DBIx/Class/ResultSource.pm
lib/DBIx/Class/ResultSource/View.pm
lib/DBIx/Class/ResultSourceHandle.pm
lib/DBIx/Class/Row.pm
lib/DBIx/Class/SQLAHacks.pm [new file with mode: 0644]
lib/DBIx/Class/SQLAHacks/OracleJoins.pm [new file with mode: 0644]
lib/DBIx/Class/Schema.pm
lib/DBIx/Class/Schema/Versioned.pm
lib/DBIx/Class/Storage.pm
lib/DBIx/Class/Storage/DBI.pm
lib/DBIx/Class/Storage/DBI/Cursor.pm
lib/DBIx/Class/Storage/DBI/MultiColumnIn.pm [new file with mode: 0644]
lib/DBIx/Class/Storage/DBI/MultiDistinctEmulation.pm [deleted file]
lib/DBIx/Class/Storage/DBI/NoBindVars.pm
lib/DBIx/Class/Storage/DBI/ODBC.pm
lib/DBIx/Class/Storage/DBI/ODBC/ACCESS.pm
lib/DBIx/Class/Storage/DBI/Oracle.pm
lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
lib/DBIx/Class/Storage/DBI/Oracle/WhereJoins.pm
lib/DBIx/Class/Storage/DBI/Pg.pm
lib/DBIx/Class/Storage/DBI/Replicated.pm
lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm
lib/DBIx/Class/Storage/DBI/Replicated/Balancer/First.pm
lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm
lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm
lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm
lib/DBIx/Class/Storage/DBI/Replicated/Types.pm [new file with mode: 0644]
lib/DBIx/Class/Storage/DBI/Replicated/WithDSN.pm [new file with mode: 0644]
lib/DBIx/Class/Storage/DBI/SQLite.pm
lib/DBIx/Class/Storage/DBI/Sybase.pm
lib/DBIx/Class/Storage/DBI/Sybase/MSSQL.pm
lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm [new file with mode: 0644]
lib/DBIx/Class/Storage/DBI/mysql.pm
lib/DBIx/Class/Storage/Statistics.pm
lib/DBIx/Class/Storage/TxnScopeGuard.pm
lib/SQL/Translator/Parser/DBIx/Class.pm
lib/SQL/Translator/Producer/DBIx/Class/File.pm
t/03podcoverage.t
t/04dont_break_c3.t
t/100extra_source.t
t/103many_to_many_warning.t
t/19quotes.t
t/19quotes_newstyle.t
t/33storage_reconnect.t
t/39load_namespaces_1.t
t/39load_namespaces_2.t
t/39load_namespaces_3.t
t/39load_namespaces_4.t
t/39load_namespaces_rt41083.t
t/41orrible.t
t/42toplimit.t
t/47bind_attribute.t
t/53delete_chained.t [deleted file]
t/53delete_related.t [deleted file]
t/54taint.t
t/60core.t
t/63register_class.t
t/63register_source.t [new file with mode: 0644]
t/64db.t
t/66relationship.t
t/67pager.t
t/71mysql.t
t/72pg.t
t/73oracle.t
t/73oracle_inflate.t
t/745db2.t
t/746sybase.t [new file with mode: 0644]
t/74mssql.t
t/76joins.t
t/76select.t
t/77prefetch.t [deleted file]
t/80unique.t
t/81transactions.t
t/82cascade_copy.t
t/83cache.t
t/89dbicadmin.t
t/89inflate_datetime.t
t/90join_torture.t
t/91debug.t
t/92storage_on_connect_do.t
t/93nobindvars.t
t/93storage_replication.t
t/94versioning.t
t/95sql_maker.t
t/95sql_maker_quote.t
t/96multi_create.t
t/98rows_prefetch.t [deleted file]
t/99dbic_sqlt_parser.t
t/cdbi/01-columns.t
t/cdbi/02-Film.t
t/cdbi/04-lazy.t
t/cdbi/15-accessor.t
t/cdbi/22-deflate_order.t
t/cdbi/columns_dont_override_custom_accessors.t
t/cdbi/copy.t
t/cdbi/multi_column_set.t
t/cdbi/set_to_undef.t
t/cdbi/set_vs_DateTime.t
t/cdbi/testlib/Actor.pm
t/cdbi/testlib/ActorAlias.pm
t/cdbi/testlib/Blurb.pm
t/cdbi/testlib/CDBase.pm
t/cdbi/testlib/DBIC/Test/SQLite.pm [moved from lib/DBIx/Class/Test/SQLite.pm with 97% similarity]
t/cdbi/testlib/Director.pm
t/cdbi/testlib/Film.pm
t/cdbi/testlib/Lazy.pm
t/cdbi/testlib/MyBase.pm
t/cdbi/testlib/Order.pm
t/cdbi/testlib/OtherThing.pm
t/cdbi/testlib/Thing.pm
t/count/distinct.t [new file with mode: 0644]
t/count/grouped_pager.t [new file with mode: 0644]
t/count/in_subquery.t [new file with mode: 0644]
t/count/joined.t [new file with mode: 0644]
t/count/prefetch.t [new file with mode: 0644]
t/delete/m2m.t [moved from t/deleting_many_to_many.t with 100% similarity]
t/delete/related.t [new file with mode: 0644]
t/from_subquery.t [new file with mode: 0644]
t/lib/DBIC/SqlMakerTest.pm
t/lib/DBICNGTest/Schema.pm [deleted file]
t/lib/DBICNGTest/Schema/Result.pm [deleted file]
t/lib/DBICNGTest/Schema/Result/FriendList.pm [deleted file]
t/lib/DBICNGTest/Schema/Result/Gender.pm [deleted file]
t/lib/DBICNGTest/Schema/Result/Person.pm [deleted file]
t/lib/DBICNGTest/Schema/ResultSet.pm [deleted file]
t/lib/DBICNGTest/Schema/ResultSet/Person.pm [deleted file]
t/lib/DBICTest.pm [changed mode: 0755->0644]
t/lib/DBICTest/AuthorCheck.pm [new file with mode: 0644]
t/lib/DBICTest/BaseResult.pm [new file with mode: 0644]
t/lib/DBICTest/BaseResultSet.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Artist.pm
t/lib/DBICTest/Schema/ArtistUndirectedMap.pm
t/lib/DBICTest/Schema/Artwork.pm
t/lib/DBICTest/Schema/Artwork_to_Artist.pm
t/lib/DBICTest/Schema/BindType.pm
t/lib/DBICTest/Schema/Bookmark.pm
t/lib/DBICTest/Schema/BooksInLibrary.pm
t/lib/DBICTest/Schema/CD.pm
t/lib/DBICTest/Schema/CD_to_Producer.pm
t/lib/DBICTest/Schema/Collection.pm
t/lib/DBICTest/Schema/CollectionObject.pm
t/lib/DBICTest/Schema/Dummy.pm
t/lib/DBICTest/Schema/Employee.pm
t/lib/DBICTest/Schema/Encoded.pm
t/lib/DBICTest/Schema/Event.pm
t/lib/DBICTest/Schema/EventTZ.pm
t/lib/DBICTest/Schema/EventTZDeprecated.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/FileColumn.pm
t/lib/DBICTest/Schema/ForceForeign.pm
t/lib/DBICTest/Schema/FourKeys.pm
t/lib/DBICTest/Schema/FourKeys_to_TwoKeys.pm
t/lib/DBICTest/Schema/Genre.pm
t/lib/DBICTest/Schema/Image.pm
t/lib/DBICTest/Schema/LinerNotes.pm
t/lib/DBICTest/Schema/Link.pm
t/lib/DBICTest/Schema/LyricVersion.pm
t/lib/DBICTest/Schema/Lyrics.pm
t/lib/DBICTest/Schema/NoPrimaryKey.pm
t/lib/DBICTest/Schema/OneKey.pm
t/lib/DBICTest/Schema/Owners.pm
t/lib/DBICTest/Schema/Producer.pm
t/lib/DBICTest/Schema/SelfRef.pm
t/lib/DBICTest/Schema/SelfRefAlias.pm
t/lib/DBICTest/Schema/SequenceTest.pm
t/lib/DBICTest/Schema/Serialized.pm
t/lib/DBICTest/Schema/Tag.pm
t/lib/DBICTest/Schema/Track.pm
t/lib/DBICTest/Schema/TreeLike.pm
t/lib/DBICTest/Schema/TwoKeyTreeLike.pm
t/lib/DBICTest/Schema/TwoKeys.pm
t/lib/DBICTest/Schema/TypedObject.pm
t/lib/DBICTest/Schema/Year1999CDs.pm
t/lib/DBICTest/Schema/Year2000CDs.pm
t/lib/DBICTest/Taint/Classes/Auto.pm [new file with mode: 0644]
t/lib/DBICTest/Taint/Classes/Manual.pm [new file with mode: 0644]
t/lib/DBICTest/Taint/Namespaces/Result/Test.pm [new file with mode: 0644]
t/lib/sqlite.sql
t/prefetch/attrs_untouched.t [new file with mode: 0644]
t/prefetch/diamond.t [new file with mode: 0644]
t/prefetch/multiple_hasmany.t [new file with mode: 0644]
t/prefetch/pollute_already_joined.t [new file with mode: 0644]
t/prefetch/rows_bug.t [new file with mode: 0644]
t/prefetch/standard.t [new file with mode: 0644]
t/resultset/as_query.t
t/resultset/update_delete.t [new file with mode: 0644]
t/resultset_class.t
t/search/subquery.t
t/zzzzzzz_perl_perf_bug.t [moved from t/99rh_perl_perf_bug.t with 98% similarity]

index 4d98a49..ebae942 100644 (file)
@@ -1,6 +1,7 @@
 Build
 Build.bat
 MANIFEST
+MANIFEST.bak
 META.yml
 Makefile
 Makefile.old
diff --git a/Changes b/Changes
index 61f5831..5ce2d89 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,4 +1,58 @@
 Revision history for DBIx::Class
+
+        - don't pass SQL functions into GROUP BY
+        - Remove MultiDistinctEmulation.pm, effectively deprecating
+          { select => { distinct => [ qw/col1 col2/ ] } }
+        - Change ->count code to work correctly with DISTINCT (distinct => 1)
+          via GROUP BY
+        - remove interpolation of bind vars for as_query
+        - update Cookbook entry for "SELECT DISTINCT with multiple columns"
+        - update Cookbook entry for "SELECT COUNT(DISTINCT colname)"
+        - Refactor DBIx::Class::Storage::DBI::Sybase to automatically 
+          load a subclass, namely Microsoft_SQL_Server.pm
+          (similar to DBIx::Class::Storage::DBI::ODBC)
+        - Proper support for update/delete of joined resultsets
+          (using IN => $sub_rs->as_query)
+        - Refactor InflateColumn::DateTime to allow components to
+          circumvent DateTime parsing
+        - Support inflation of timestamp datatype
+        - Support BLOB and CLOB datatypes on Oracle
+        - Storage::DBI::Replicated::Balancer::Random:
+          added master_read_weight
+        - Storage::DBI::Replicated: storage opts from connect_info,
+          connect_info merging to replicants, hashref connect_info support,
+          improved trace output, other bug fixes/cleanups
+
+0.08102 2009-04-30 08:29:00 (UTC)
+        - Fixed two subtle bugs when using columns or select/as
+          paired with a join (limited prefetch)
+        - Fixed breakage of cdbi tests (RT#45551)
+        - Some POD improvements
+
+0.08101 2009-04-27 09:45:00 (UTC)
+        - Fix +select, +as, +columns and include_columns being stripped
+          by $rs->get_column
+        - move load_optional_class from DBIx::Class::Componentised to
+          Class::C3::Componentised, bump dependency
+        - register_extra_source() now *really* fixed wrt subclassing
+        - Added missing POD descriptions (RT#45195)
+        - Fix insert() to not store_column() every present object column
+        - Multiple Makefile.PL fixes
+
+0.08100 2009-04-19 11:39:35 (UTC)
+        - Todo out the register_extra_source test until after shipping
+
+0.08099_08 2009-03-30 00:00:00 (UTC)
+        - Fixed taint mode with load_namespaces
+        - Putting IC::DateTime locale, timezone or floating_tz_ok attributes into
+          extra => {} has been deprecated. The new way is to put these things
+          directly into the columns definition
+        - Switched MI code to MRO::Compat
+        - Document db-side default_value caveats
+        - Search_like() now warns to indicate deprecation in 0.09.
+        - TxnScopeGuard left experimental state
+
+0.08099_07 2009-02-27 02:00:00 (UTC)
         - multi-create using find_or_create rather than _related for post-insert
         - fix get_inflated_columns to check has_column_loaded
         - Add DBIC_MULTICREATE_DEBUG env var (undocumented, quasi-internal)
@@ -7,15 +61,17 @@ Revision history for DBIx::Class
           - not try and insert things tagged on via new_related unless required
         - Possible to set locale in IC::DateTime extra => {} config
         - Calling the accessor of a belongs_to when the foreign_key
-          was NULL and the row was not stored would unexpectedly fail (groditi)
+          was NULL and the row was not stored would unexpectedly fail 
         - Split sql statements for deploy only if SQLT::Producer returned a scalar
           containing all statements to be executed
         - Add as_query() for ResultSet and ResultSetColumn. This makes subqueries
-          possible. See the Cookbook for details. (robkinyon, michaelr)
+          possible. See the Cookbook for details.
         - Massive rewrite of Ordered to properly handle position constraints and
           to make it more matpath-friendly
         - deploy_statements called ddl_filename with the $version and $dir arguments
-          in the wrong order. (groditi)
+          in the wrong order.
+        - columns/+columns attributes now support { as => select } hahsrefs
+        - support for views both in DBIC and via deploy() in SQLT
 
 0.08099_06 2009-01-23 07:30:00 (UTC)
         - Allow a scalarref to be supplied to the 'from' resultset attribute
@@ -30,19 +86,18 @@ Revision history for DBIx::Class
           (http://msdn.microsoft.com/en-us/library/ms190315.aspx)
         - an sqlt_deploy_hook can now be shared between result sources using
           a configurable callback trigger
-        - new order_by => { -desc => 'colname' } syntax supported with
-          SQLA >= 1.50
-        - PG array datatype supported with SQLA >= 1.50
+        - new order_by => { -desc => 'colname' } syntax supported
+        - PG array datatype supported
         - insert should use store_column, not set_column to avoid marking
-          clean just-stored values as dirty. New test for this (groditi)
-        - regression test for source_name (groditi)
+          clean just-stored values as dirty. New test for this 
+        - regression test for source_name 
 
 0.08099_05 2008-10-30 21:30:00 (UTC)
-        - Rewritte of Storage::DBI::connect_info(), extended with an
+        - Rewrite of Storage::DBI::connect_info(), extended with an
           additional argument format type
         - InflateColumn::DateTime: add warning about floating timezone
         - InflateColumn::DateTime: possible to enforce/skip inflation
-        - delete throws exception if passed arguments to prevent drunken mishaps. (purge)
+        - delete throws exception if passed arguments to prevent drunken mishaps.
         - Fix storage to copy scalar conds before regexping to avoid
           trying to modify a constant in odd edge cases
         - Related resultsets on uninserted objects are now empty
index 07a5968..1e9b295 100644 (file)
@@ -1,3 +1,6 @@
+^(?!script/|examples/|lib/|inc/|t/|Makefile.PL$|README$|MANIFEST$|Changes$|META.yml$)
+
+
 # Avoid version control files.
 \bRCS\b
 \bCVS\b
index 4e279a6..7d8175f 100644 (file)
@@ -1,4 +1,4 @@
-use inc::Module::Install 0.67;
+use inc::Module::Install 0.79;
 use strict;
 use warnings;
 use POSIX ();
@@ -9,31 +9,35 @@ name     'DBIx-Class';
 perl_version '5.006001';
 all_from 'lib/DBIx/Class.pm';
 
+# configure_requires so _check_sqlite() below can run
+# remove once test deprecated
+configure_requires 'DBD::SQLite';
+
+requires 'DBD::SQLite'              => 1.25;
 requires 'Data::Page'               => 2.00;
-requires 'Scalar::Util'             => 0;
-requires 'SQL::Abstract'            => 1.49;
+requires 'SQL::Abstract'            => 1.55;
 requires 'SQL::Abstract::Limit'     => 0.13;
-requires 'Class::C3'                => 0.20;
-requires 'Class::C3::Componentised' => 0;
-requires 'Storable'                 => 0;
-requires 'Carp::Clan'               => 0;
-requires 'DBI'                      => 1.40;
-requires 'Module::Find'             => 0;
-requires 'Class::Inspector'         => 0;
-requires 'Class::Accessor::Grouped' => 0.08002;
-requires 'JSON::Any'                => 1.17;
+requires 'Class::C3::Componentised' => 1.0005;
+requires 'Carp::Clan'               => 6.0;
+requires 'DBI'                      => 1.605;
+requires 'Module::Find'             => 0.06;
+requires 'Class::Inspector'         => 1.24;
+requires 'Class::Accessor::Grouped' => 0.08003;
+requires 'JSON::Any'                => 1.18;
 requires 'Scope::Guard'             => 0.03;
-requires 'Path::Class'              => 0;
-requires 'List::Util'               => 1.19;
+requires 'Path::Class'              => 0.16;
 requires 'Sub::Name'                => 0.04;
-requires 'namespace::clean'         => 0.09;
+requires 'MRO::Compat'              => 0.09;
 
-# Perl 5.8.0 doesn't have utf8::is_utf8()
-requires 'Encode'                   => 0 if ($] <= 5.008000);  
+# Core
+requires 'List::Util'               => 0;
+requires 'Scalar::Util'             => 0;
+requires 'Storable'                 => 0;
 
-# configure_requires so the sanity check below can run
-configure_requires 'DBD::SQLite'    => 1.14;
+# Perl 5.8.0 doesn't have utf8::is_utf8()
+requires 'Encode'                   => 0 if ($] <= 5.008000);
 
+test_requires 'Test::More'          => 0.82;
 test_requires 'Test::Builder'       => 0.33;
 test_requires 'Test::Warn'          => 0.11;
 test_requires 'Test::Exception'     => 0;
@@ -41,34 +45,56 @@ test_requires 'Test::Deep'          => 0;
 
 recommends 'SQL::Translator'        => 0.09004;
 
-install_script 'script/dbicadmin';
+install_script (qw|
+    script/dbicadmin
+|);
 
-tests_recursive 't';
+tests_recursive (qw|
+    t
+|);
 
-# re-build README and require CDBI modules for testing if we're in a checkout
+# re-build README and require extra modules for testing if we're in a checkout
 
 my %force_requires_if_author = (
-  'Test::Pod::Coverage'       => 0,
+  'Test::Pod::Coverage'       => 1.04,
   'SQL::Translator'           => 0.09004,
 
   # CDBI-compat related
   'DBIx::ContextualFetch'     => 0,
+  'Class::DBI::Plugin::DeepAbstractSearch' => 0,
   'Class::Trigger'            => 0,
-  'Time::Piece'               => 0,
+  'Time::Piece::MySQL'        => 0,
   'Clone'                     => 0,
+  'Date::Simple'              => 0,
 
   # t/52cycle.t
   'Test::Memory::Cycle'       => 0,
 
+  # t/60core.t
+  'DateTime::Format::MySQL'   => 0,
+
+  # t/72pg.t
+  $ENV{DBICTEST_PG_DSN}
+    ? ('Sys::SigAction'=> 0)
+    : ()
+  ,
+
   # t/93storage_replication.t
-  'Moose',                    => 0,
-  'MooseX::AttributeHelpers'  => 0.12,
+  'Moose',                        => 0.77,
+  'MooseX::AttributeHelpers'      => 0.12,
+  'MooseX::Types',                => 0.10,
+  'namespace::clean'              => 0.11,
+  'Hash::Merge',                  => 0.11,
+
+  # t/96_is_deteministic_value.t
+  # t/746sybase.t
+  'DateTime::Format::Strptime' => 0,
 );
 
 if ($Module::Install::AUTHOR) {
 
   foreach my $module (keys %force_requires_if_author) {
-    requires ($module => $force_requires_if_author{$module});
+    build_requires ($module => $force_requires_if_author{$module});
   }
 
   system('pod2text lib/DBIx/Class.pm > README');
@@ -76,62 +102,65 @@ if ($Module::Install::AUTHOR) {
 
 auto_provides;
 
+if ($Module::Install::AUTHOR) {
+  warn <<'EOW';
+******************************************************************************
+******************************************************************************
+***                                                                        ***
+*** AUTHOR MODE: all optional test dependencies converted to hard requires ***
+***                                                                        ***
+******************************************************************************
+******************************************************************************
+
+EOW
+}
 auto_install;
 
 # Have all prerequisites, check DBD::SQLite sanity
-if (! $ENV{DBICTEST_NO_SQLITE_CHECK} ) {
+_check_sqlite() if (! $ENV{DBICTEST_NO_SQLITE_CHECK} );
 
-  my $pid = fork();
-  if (not defined $pid) {
-      die "Unable to fork(): $!";
-  }
-  elsif (! $pid) {
+WriteAll();
 
-      # Win32 does not have real fork()s so a segfault will bring
-      # everything down. Warn about it.
-      if ($^O eq 'MSWin32') {
-        print <<'EOW';
+if ($Module::Install::AUTHOR) {
+  # Need to do this _after_ WriteAll else it loses track of them
+  Meta->{values}{build_requires} = [ grep {
+    my $ok = 1;
+    foreach my $module (keys %force_requires_if_author) {
+      if ($_->[0] =~ /$module/) {
+        $ok = 0;
+        last;
+      }
+    }
+    $ok;
+  } @{Meta->{values}{build_requires}} ];
 
-######################################################################
-#                                                                    #
-# A short stress-testing of DBD::SQLite will follow. If you have a   #
-# buggy library this might very well be the last text you will see   #
-# before the installation silently terminates. If this happens it    #
-# would mean that you are running a buggy version of DBD::SQLite     #
-# known to randomly segfault on errors. Even if you have the latest  #
-# CPAN module version, the system sqlite3 dynamic library might have #
-# been compiled against an older buggy sqlite3 dev library (oddly    #
-# DBD::SQLite will prefer the system library against the one bundled #
-# with it). You are strongly advised to resolve this issue before    #
-# proceeding.                                                        #
-#                                                                    #
-# If this happens to you (this text is the last thing you see), and  #
-# you just want to install this module without worrying about the    #
-# tests (which will almost certainly fail) - set the environment     #
-# variable DBICTEST_NO_SQLITE_CHECK to a true value and try again.   #
-#                                                                    #
-######################################################################
+  Meta->{values}{resources} = [ 
+    [ 'MailingList', 'http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class' ],
+    [ 'IRC', 'irc://irc.perl.org/#dbix-class' ],
+    [ 'license', 'http://dev.perl.org/licenses/' ],
+    [ 'repository', 'http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/' ],
+  ];
+  Meta->write;
+}
 
-EOW
-      }
 
-      require DBI;
-      for (1 .. 100) {
-          my $dbh;
-          $dbh = DBI->connect ('dbi:SQLite::memory:', undef, undef, {
-              AutoCommit => 1,
-              RaiseError => 0,
-              PrintError => 0,
-          })
-              or die "Unable to connect to database: $@";
-          $dbh->do ('CREATE TABLE name_with_no_columns');   # a subtle syntax error
-          $dbh->do ('COMMIT');                              # followed by commit
-          $dbh->disconnect;
-      }
+# This is legacy code. Latest DBD::SQLite developments fixed all known bugs
+# in this area. Remove before some arbitrary next version
+sub _check_sqlite {
+
+  # Win32 does not have real fork()s so a segfault will bring
+  # everything down. Warn about it below, and don't try fork()
+  if ($^O ne 'MSWin32') {
 
+    my $pid = fork();
+    if (not defined $pid) {
+        die "Unable to fork(): $!";
+    }
+    elsif (! $pid) {
+      _torture_sqlite();
       exit 0;
-  }
-  else {
+    }
+    else {
       eval {
           local $SIG{ALRM} = sub { die "timeout\n" };
           alarm 5;
@@ -142,7 +171,7 @@ EOW
 
       my $sig = $? & 127;
 
-# make sure process actually dies
+      # make sure process actually dies
       $exception && kill POSIX::SIGKILL(), $pid;
 
       if ($exception || $sig == POSIX::SIGSEGV() || $sig == POSIX::SIGABRT()
@@ -166,41 +195,53 @@ EOE
             . 'Do you really want to continue?',
             'no',
           );
+
           exit 0 unless ($ans =~ /^y(es)?$/i);
       }
+    }
   }
-}
 
+  else {  # the win32 version
 
-WriteAll();
+    print <<'EOW';
+######################################################################
+#                                                                    #
+# A short stress-testing of DBD::SQLite will follow. If you have a   #
+# buggy library this might very well be the last text you will see   #
+# before the installation silently terminates. If this happens it    #
+# would mean that you are running a buggy version of DBD::SQLite     #
+# known to randomly segfault on errors. Even if you have the latest  #
+# CPAN module version, the system sqlite3 dynamic library might have #
+# been compiled against an older buggy sqlite3 dev library (oddly    #
+# DBD::SQLite will prefer the system library against the one bundled #
+# with it). You are strongly advised to resolve this issue before    #
+# proceeding.                                                        #
+#                                                                    #
+# If this happens to you (this text is the last thing you see), and  #
+# you just want to install this module without worrying about the    #
+# tests (which will almost certainly fail) - set the environment     #
+# variable DBICTEST_NO_SQLITE_CHECK to a true value and try again.   #
+#                                                                    #
+######################################################################
 
+EOW
 
-if ($Module::Install::AUTHOR) {
-  # Need to do this _after_ WriteAll else it looses track of them
-  Meta->{values}{build_requires} = [ grep {
-    my $ok = 1;
-    foreach my $module (keys %force_requires_if_author) {
-      if ($_->[0] =~ /$module/) {
-        $ok = 0;
-        last;
-      }
-    }
-    $ok;
-  } @{Meta->{values}{build_requires}} ];
+    _torture_sqlite();
+  }
+}
 
-  my @scalar_keys = Module::Install::Metadata::Meta_TupleKeys();
-  my $cr = Module::Install::Metadata->can("Meta_TupleKeys");
-  {
-    no warnings 'redefine';
-    *Module::Install::Metadata::Meta_TupleKeys = sub {
-      return $cr->(@_), 'resources';
-    };
+sub _torture_sqlite {
+  require DBI;
+
+  for (1 .. 100) {
+    my $dbh = DBI->connect ('dbi:SQLite::memory:', undef, undef, {
+      AutoCommit => 1,
+      RaiseError => 0,
+      PrintError => 0,
+    }) or die "Unable to connect to database: $@";
+
+    $dbh->do ('CREATE TABLE name_with_no_columns');   # a subtle syntax error
+    $dbh->do ('COMMIT');                              # followed by commit
+    $dbh->disconnect;
   }
-  Meta->{values}{resources} = [ 
-    [ 'MailingList', 'http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class' ],
-    [ 'IRC', 'irc://irc.perl.org/#dbix-class' ],
-    [ 'license', 'http://dev.perl.org/licenses/' ],
-    [ 'repository', 'http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/' ],
-  ];
-  Meta->write;
 }
index 6b9eef7..42fae1b 100644 (file)
@@ -1,5 +1,5 @@
 package MyDatabase::Main;
 use base qw/DBIx::Class::Schema/;
-__PACKAGE__->load_classes(qw/Artist Cd Track/);
+__PACKAGE__->load_namespaces;
 
 1;
similarity index 65%
rename from examples/Schema/MyDatabase/Main/Artist.pm
rename to examples/Schema/MyDatabase/Main/Result/Artist.pm
index 5f039e6..ec78501 100644 (file)
@@ -1,10 +1,10 @@
-package MyDatabase::Main::Artist;
+package MyDatabase::Main::Result::Artist;
 use base qw/DBIx::Class/;
 __PACKAGE__->load_components(qw/PK::Auto Core/);
 __PACKAGE__->table('artist');
 __PACKAGE__->add_columns(qw/ artistid name /);
 __PACKAGE__->set_primary_key('artistid');
-__PACKAGE__->has_many('cds' => 'MyDatabase::Main::Cd');
+__PACKAGE__->has_many('cds' => 'MyDatabase::Main::Result::Cd');
 
 1;
 
similarity index 51%
rename from examples/Schema/MyDatabase/Main/Cd.pm
rename to examples/Schema/MyDatabase/Main/Result/Cd.pm
index 4579823..83fd21e 100644 (file)
@@ -1,10 +1,10 @@
-package MyDatabase::Main::Cd;
+package MyDatabase::Main::Result::Cd;
 use base qw/DBIx::Class/;
 __PACKAGE__->load_components(qw/PK::Auto Core/);
 __PACKAGE__->table('cd');
 __PACKAGE__->add_columns(qw/ cdid artist title/);
 __PACKAGE__->set_primary_key('cdid');
-__PACKAGE__->belongs_to('artist' => 'MyDatabase::Main::Artist');
-__PACKAGE__->has_many('tracks' => 'MyDatabase::Main::Track');
+__PACKAGE__->belongs_to('artist' => 'MyDatabase::Main::Result::Artist');
+__PACKAGE__->has_many('tracks' => 'MyDatabase::Main::Result::Track');
 
 1;
similarity index 65%
rename from examples/Schema/MyDatabase/Main/Track.pm
rename to examples/Schema/MyDatabase/Main/Result/Track.pm
index 3710406..23877bb 100644 (file)
@@ -1,9 +1,9 @@
-package MyDatabase::Main::Track;
+package MyDatabase::Main::Result::Track;
 use base qw/DBIx::Class/;
 __PACKAGE__->load_components(qw/PK::Auto Core/);
 __PACKAGE__->table('track');
 __PACKAGE__->add_columns(qw/ trackid cd title/);
 __PACKAGE__->set_primary_key('trackid');
-__PACKAGE__->belongs_to('cd' => 'MyDatabase::Main::Cd');
+__PACKAGE__->belongs_to('cd' => 'MyDatabase::Main::Result::Cd');
 
 1;
index b31773d..9ca3e39 100644 (file)
@@ -26,7 +26,6 @@ sub get_tracks_by_cd {
         },
         {
             join     => [qw/ cd /],
-            prefetch => [qw/ cd /]
         }
     );
     while (my $track = $rs->next) {
@@ -79,7 +78,6 @@ sub get_cds_by_artist {
         },
         {
             join     => [qw/ artist /],
-            prefetch => [qw/ artist /]
         }
     );
     while (my $cd = $rs->next) {
index d698719..3c7933f 100644 (file)
@@ -8,13 +8,13 @@ use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/;
 use DBIx::Class::StartupCheck;
 
 
-sub mk_classdata { 
+sub mk_classdata {
   shift->mk_classaccessor(@_);
 }
 
 sub mk_classaccessor {
   my $self = shift;
-  $self->mk_group_accessors('inherited', $_[0]); 
+  $self->mk_group_accessors('inherited', $_[0]);
   $self->set_inherited(@_) if @_ > 1;
 }
 
@@ -24,7 +24,7 @@ sub component_base_class { 'DBIx::Class' }
 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
 # brain damage and presumably various other packaging systems too
 
-$VERSION = '0.08099_06';
+$VERSION = '0.08102';
 
 $VERSION = eval $VERSION; # numify for warning-free dev releases
 
@@ -207,6 +207,8 @@ andyg: Andy Grundman <andy@hybridized.org>
 
 ank: Andres Kievsky
 
+arcanez: Justin Hunter <justin.d.hunter@gmail.com>
+
 ash: Ash Berlin <ash@cpan.org>
 
 bert: Norbert Csongradi <bert@cpan.org>
@@ -219,8 +221,6 @@ bricas: Brian Cassidy <bricas@cpan.org>
 
 caelum: Rafael Kitover <rkitover@cpan.org>
 
-captainL: Luke Saunders <luke.saunders@gmail.com>
-
 castaway: Jess Robinson
 
 claco: Christopher H. Laco
@@ -239,10 +239,16 @@ dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
 
 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
 
+frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
+
 gphat: Cory G Watson <gphat@cpan.org>
 
 groditi: Guillermo Roditi <groditi@cpan.org>
 
+ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
+
+jasonmay: Jason May <jason.a.may@gmail.com>
+
 jesper: Jesper Krogh
 
 jgoulah: John Goulah <jgoulah@cpan.org>
@@ -257,6 +263,8 @@ jshirley: J. Shirley <jshirley@gmail.com>
 
 konobi: Scott McWhirter
 
+lukes: Luke Saunders <luke.saunders@gmail.com>
+
 marcus: Marcus Ramberg <mramberg@cpan.org>
 
 mattlaw: Matt Lawrence
@@ -269,6 +277,10 @@ nigel: Nigel Metheringham <nigelm@cpan.org>
 
 ningu: David Kamholz <dkamholz@cpan.org>
 
+Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
+
+norbi: Norbert Buchmuller <norbi@nix.hu>
+
 Numa: Dan Sully <daniel@cpan.org>
 
 oyse: Ã˜ystein Torget <oystein.torget@dnv.com>
@@ -279,6 +291,8 @@ penguin: K J Cheetham
 
 perigrin: Chris Prather <chris@prather.org>
 
+peter: Peter Collingbourne <peter@pcc.me.uk>
+
 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
 
 plu: Johannes Plunien <plu@cpan.org>
@@ -301,6 +315,8 @@ scotty: Scotty Allen <scotty@scottyallen.com>
 
 semifor: Marc Mims <marc@questright.com>
 
+solomon: Jared Johnson <jaredj@nmgi.com>
+
 sszabo: Stephan Szabo <sszabo@bigpanda.com>
 
 teejay : Aaron Trevena <teejay@cpan.org>
@@ -317,9 +333,9 @@ wdh: Will Hawes
 
 willert: Sebastian Willert <willert@cpan.org>
 
-zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
+wreis: Wallace Reis <wreis@cpan.org>
 
-norbi: Norbert Buchmuller <norbi@nix.hu>
+zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
 
 =head1 LICENSE
 
index 948dcd9..c02f4f7 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 
 =head1 NAME
 
-DBIx::Class::CDBICompat::AbstractSearch
+DBIx::Class::CDBICompat::AbstractSearch - Emulates Class::DBI::AbstractSearch
 
 =head1 SYNOPSIS
 
index 2dcd878..cbc1124 100644 (file)
@@ -155,7 +155,8 @@ sub _find_columns {
   return map { $class->find_column($_) } @col;
 }
 
-package DBIx::Class::CDBICompat::ColumnGroups::GrouperShim;
+package # hide from PAUSE (should be harmless, no POD no Version)
+    DBIx::Class::CDBICompat::ColumnGroups::GrouperShim;
 
 sub groups_for {
   my ($self, @cols) = @_;
@@ -167,6 +168,5 @@ sub groups_for {
   }
   return keys %groups;
 }
-    
 
 1;
index b5f1168..7b81f09 100644 (file)
@@ -7,7 +7,7 @@ use warnings;
 
 =head1 NAME
 
-DBIx::Class::CDBICompat::ColumnsAsHash
+DBIx::Class::CDBICompat::ColumnsAsHash - Emulates the behavior of Class::DBI where the object can be accessed as a hash of columns.
 
 =head1 SYNOPSIS
 
index 414cbd6..ed42d95 100644 (file)
@@ -8,7 +8,7 @@ use Carp;
 
 =head1 NAME
 
-DBIx::Class::CDBICompat::Copy
+DBIx::Class::CDBICompat::Copy - Emulates Class::DBI->copy($new_id)
 
 =head1 SYNOPSIS
 
index 7a3b4f0..db844e8 100644 (file)
@@ -55,7 +55,7 @@ __PACKAGE__->mk_classdata('_transform_sql_handlers' =>
         $self->throw_exception( "No relationship to JOIN from ${from_class} to ${to_class}" )
           unless $rel_obj;
         my $join = $from_class->storage->sql_maker->_join_condition(
-          $from_class->result_source_instance->resolve_condition(
+          $from_class->result_source_instance->_resolve_condition(
             $rel_obj->{cond}, $to, $from) );
         return $join;
       }
index 3466769..80e788c 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 
 =head1 NAME
 
-DBIx::Class::CDBICompat::Iterator
+DBIx::Class::CDBICompat::Iterator - Emulates the extra behaviors of the Class::DBI search iterator.
 
 =head1 SYNOPSIS
 
index 445282c..a461a13 100644 (file)
@@ -51,15 +51,15 @@ sub clear_object_index {
 sub insert {
   my ($self, @rest) = @_;
   $self->next::method(@rest);
-  
+
   return $self if $self->nocache;
 
-    # Because the insert will die() if it can't insert into the db (or should)
-    # we can be sure the object *was* inserted if we got this far. In which
-    # case, given primary keys are unique and ID only returns a
-    # value if the object has all its primary keys, we can be sure there
-    # isn't a real one in the object index already because such a record
-    # cannot have existed without the insert failing.
+  # Because the insert will die() if it can't insert into the db (or should)
+  # we can be sure the object *was* inserted if we got this far. In which
+  # case, given primary keys are unique and ID only returns a
+  # value if the object has all its primary keys, we can be sure there
+  # isn't a real one in the object index already because such a record
+  # cannot have existed without the insert failing.
   if (my $key = $self->ID) {
     my $live = $self->live_object_index;
     weaken($live->{$key} = $self);
@@ -67,7 +67,7 @@ sub insert {
       if ++$self->live_object_init_count->{count}
               % $self->purge_object_index_every == 0;
   }
-  #use Data::Dumper; warn Dumper($self);
+
   return $self;
 }
 
index 003c875..5dd6268 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 
 =head1 NAME
 
-DBIx::Class::CDBICompat::NoObjectIndex
+DBIx::Class::CDBICompat::NoObjectIndex - Defines empty methods for object indexing. They do nothing
 
 =head1 SYNOPSIS
 
index 880ffc2..5d71924 100644 (file)
@@ -7,7 +7,7 @@ use Sub::Name ();
 
 =head1 NAME
 
-DBIx::Class::CDBICompat::Relationship
+DBIx::Class::CDBICompat::Relationship - Emulate the Class::DBI::Relationship object returned from meta_info()
 
 =head1 DESCRIPTION
 
index d6120bc..7572870 100644 (file)
@@ -14,7 +14,7 @@ __PACKAGE__->mk_classdata('__meta_info' => {});
 
 =head1 NAME
 
-DBIx::Class::CDBICompat::Relationships
+DBIx::Class::CDBICompat::Relationships - Emulate has_a(), has_many(), might_have() and meta_info()
 
 =head1 DESCRIPTION
 
index 4c36887..e701cfc 100644 (file)
@@ -50,11 +50,24 @@ sub retrieve_from_sql {
 
   $cond =~ s/^\s*WHERE//i;
 
-  if( $cond =~ s/\bLIMIT (\d+)\s*$//i ) {
-      push @rest, { rows => $1 };
+  # Need to parse the SQL clauses after WHERE in reverse
+  # order of appearance.
+
+  my %attrs;
+
+  if( $cond =~ s/\bLIMIT\s+(\d+)\s*$//i ) {
+      $attrs{rows} = $1;
+  }
+
+  if ( $cond =~ s/\bORDER\s+BY\s+(.*)\s*$//i ) {
+      $attrs{order_by} = $1;
+  }
+
+  if( $cond =~ s/\bGROUP\s+BY\s+(.*)\s*$//i ) {
+      $attrs{group_by} = $1;
   }
 
-  return $class->search_literal($cond, @rest);
+  return $class->search_literal($cond, @rest, ( %attrs ? \%attrs : () ) );
 }
 
 sub construct {
index a438c06..7b4cb1f 100644 (file)
@@ -17,7 +17,7 @@ sub inject_base {
       foreach my $first_comp (@comps) {
         if ($to eq 'DBIx::Class::Core' &&
             $target->isa("DBIx::Class::${first_comp}")) {
-          warn "Possible incorrect order of components in ".
+          carp "Possible incorrect order of components in ".
                "${target}::load_components($first_comp) call: Core loaded ".
                "before $first_comp. See the documentation for ".
                "DBIx::Class::$first_comp for more information";
@@ -31,25 +31,4 @@ sub inject_base {
   $class->next::method($target, @to_inject);
 }
 
-# Returns a true value if the specified class is installed and loaded
-# successfully, throws an exception if the class is found but not loaded
-# successfully, and false if the class is not installed
-sub load_optional_class {
-  my ($class, $f_class) = @_;
-  eval { $class->ensure_class_loaded($f_class) };
-  my $err = $@;   # so we don't lose it
-  if (! $err) {
-    return 1;
-  }
-  else {
-    my $fn = (join ('/', split ('::', $f_class) ) ) . '.pm';
-    if ($err =~ /Can't locate ${fn} in \@INC/ ) {
-      return 0;
-    }
-    else {
-      die $err;
-    }
-  }
-}
-
 1;
index 1f1ffd8..57381d0 100644 (file)
@@ -162,11 +162,12 @@ __PACKAGE__->mk_classdata('_result_source_instance' => []);
 sub _maybe_attach_source_to_schema {
   my ($class, $source) = @_;
   if (my $meth = $class->can('schema_instance')) {
-    my $schema = $class->$meth;
-    $schema->register_class($class, $class);
-    my $new_source = $schema->source($class);
-    %$source = %$new_source;
-    $schema->source_registrations->{$class} = $source;
+    if (my $schema = $class->$meth) {
+      $schema->register_class($class, $class);
+      my $new_source = $schema->source($class);
+      %$source = %$new_source;
+      $schema->source_registrations->{$class} = $source;
+    }
   }
 }
 
index 3024241..5b1b313 100644 (file)
@@ -3,6 +3,7 @@ package DBIx::Class::InflateColumn::DateTime;
 use strict;
 use warnings;
 use base qw/DBIx::Class/;
+use Carp::Clan qw/^DBIx::Class/;
 
 =head1 NAME
 
@@ -30,7 +31,7 @@ Then you can treat the specified column as a L<DateTime> object.
 If you want to set a specific timezone and locale for that field, use:
 
   __PACKAGE__->add_columns(
-    starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago", locale => "de_DE" } }
+    starts_when => { data_type => 'datetime', timezone => "America/Chicago", locale => "de_DE" }
   );
 
 If you want to inflate no matter what data_type your column is,
@@ -94,7 +95,7 @@ sub register_column {
 
   my $type;
 
-  for (qw/date datetime/) {
+  for (qw/date datetime timestamp/) {
     my $key = "inflate_${_}";
 
     next unless exists $info->{$key};
@@ -106,46 +107,45 @@ sub register_column {
 
   unless ($type) {
     $type = lc($info->{data_type});
-    $type = 'datetime' if ($type =~ /^timestamp/);
   }
 
   my $timezone;
   if ( defined $info->{extra}{timezone} ) {
+    carp "Putting timezone into extra => { timezone => '...' } has been deprecated, ".
+         "please put it directly into the columns definition.";
     $timezone = $info->{extra}{timezone};
   }
 
   my $locale;
   if ( defined $info->{extra}{locale} ) {
+    carp "Putting locale into extra => { locale => '...' } has been deprecated, ".
+         "please put it directly into the columns definition.";
     $locale = $info->{extra}{locale};
   }
+  
+  $locale   = $info->{locale}   if defined $info->{locale};
+  $timezone = $info->{timezone} if defined $info->{timezone};
 
   my $undef_if_invalid = $info->{datetime_undef_if_invalid};
 
-  if ($type eq 'datetime' || $type eq 'date') {
-    my ($parse, $format) = ("parse_${type}", "format_${type}");
-
-    # This assignment must happen here, otherwise Devel::Cycle treats
-    # the resulting deflator as a circular reference (go figure):
-    #
-    # Cycle #1
-    #     DBICTest::Schema A->{source_registrations} => %B
-    #     %B->{Event} => DBIx::Class::ResultSource::Table C
-    #     DBIx::Class::ResultSource::Table C->{_columns} => %D
-    #     %D->{created_on} => %E
-    #     %E->{_inflate_info} => %F
-    #     %F->{deflate} => &G
-    #     closure &G, $info => $H
-    #     $H => %E
-    #
-    my $floating_tz_ok = $info->{extra}{floating_tz_ok};
+  if ($type eq 'datetime' || $type eq 'date' || $type eq 'timestamp') {
+    # This shallow copy of %info avoids t/52_cycle.t treating
+    # the resulting deflator as a circular reference.
+    my %info = ( '_ic_dt_method' => $type , %{ $info } );
+
+    if (defined $info->{extra}{floating_tz_ok}) {
+      carp "Putting floating_tz_ok into extra => { floating_tz_ok => 1 } has been deprecated, ".
+           "please put it directly into the columns definition.";
+      $info{floating_tz_ok} = $info->{extra}{floating_tz_ok};
+    }
 
     $self->inflate_column(
       $column =>
         {
           inflate => sub {
             my ($value, $obj) = @_;
-            my $dt = eval { $obj->_datetime_parser->$parse($value); };
-            die "Error while inflating ${value} for ${column} on ${self}: $@"
+            my $dt = eval { $obj->_inflate_to_datetime( $value, \%info ) };
+            $self->throw_exception ("Error while inflating ${value} for ${column} on ${self}: $@")
               if $@ and not $undef_if_invalid;
             $dt->set_time_zone($timezone) if $timezone;
             $dt->set_locale($locale) if $locale;
@@ -154,21 +154,41 @@ sub register_column {
           deflate => sub {
             my ($value, $obj) = @_;
             if ($timezone) {
-                warn "You're using a floating timezone, please see the documentation of"
+                carp "You're using a floating timezone, please see the documentation of"
                   . " DBIx::Class::InflateColumn::DateTime for an explanation"
                   if ref( $value->time_zone ) eq 'DateTime::TimeZone::Floating'
-                      and not $floating_tz_ok
+                      and not $info{floating_tz_ok}
                       and not $ENV{DBIC_FLOATING_TZ_OK};
                 $value->set_time_zone($timezone);
                 $value->set_locale($locale) if $locale;
             }
-            $obj->_datetime_parser->$format($value);
+            $obj->_deflate_from_datetime( $value, \%info );
           },
         }
     );
   }
 }
 
+sub _flate_or_fallback
+{
+  my( $self, $value, $info, $method_fmt ) = @_;
+
+  my $parser = $self->_datetime_parser;
+  my $preferred_method = sprintf($method_fmt, $info->{ _ic_dt_method });
+  my $method = $parser->can($preferred_method) ? $preferred_method : sprintf($method_fmt, 'datetime');
+  return $parser->$method($value);
+}
+
+sub _inflate_to_datetime {
+  my( $self, $value, $info ) = @_;
+  return $self->_flate_or_fallback( $value, $info, 'parse_%s' );
+}
+
+sub _deflate_from_datetime {
+  my( $self, $value, $info ) = @_;
+  return $self->_flate_or_fallback( $value, $info, 'format_%s' );
+}
+
 sub _datetime_parser {
   my $self = shift;
   if (my $parser = $self->__datetime_parser) {
@@ -189,7 +209,7 @@ timezone, you will get a warning (as there is a very good chance this will not h
 result you expect). For example:
 
   __PACKAGE__->add_columns(
-    starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
+    starts_when => { data_type => 'datetime', timezone => "America/Chicago" }
   );
 
   my $event = $schema->resultset('EventTZ')->create({
@@ -213,7 +233,7 @@ to be supply explicit times to the database:
 =item Suppress the check on per-column basis
 
   __PACKAGE__->add_columns(
-    starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago", floating_tz_ok => 1 } }
+    starts_when => { data_type => 'datetime', timezone => "America/Chicago", floating_tz_ok => 1 }
   );
 
 =item Suppress the check globally
@@ -222,7 +242,10 @@ Set the environment variable DBIC_FLOATING_TZ_OK to some true value.
 
 =back
 
-
+Putting extra attributes like timezone, locale or floating_tz_ok into extra => {} has been
+B<DEPRECATED> because this gets you into trouble using L<DBIx::Class::Schema::Versioned>.
+Instead put it directly into the columns definition like in the examples above. If you still
+use the old way you'll see a warning - please fix your code then!
 
 =head1 SEE ALSO
 
index 00c28e9..574a8e1 100644 (file)
@@ -19,19 +19,8 @@ paged resultset, which will fetch only a defined number of records at a time:
 
   return $rs->all(); # all records for page 1
 
-The C<page> attribute does not have to be specified in your search:
-
-  my $rs = $schema->resultset('Artist')->search(
-    undef,
-    {
-      rows => 10,
-    }
-  );
-
-  return $rs->page(1); # DBIx::Class::ResultSet containing first 10 records
-
-In either of the above cases, you can get a L<Data::Page> object for the
-resultset (suitable for use in e.g. a template) using the C<pager> method:
+You can get a L<Data::Page> object for the resultset (suitable for use
+in e.g. a template) using the C<pager> method:
 
   return $rs->pager();
 
@@ -116,7 +105,7 @@ reference (this is a feature of L<SQL::Abstract>).
 Say you want to run a complex custom query on your user data, here's what
 you have to add to your User class:
 
-  package My::Schema::User;
+  package My::Schema::Result::User;
   
   use base qw/DBIx::Class/;
   
@@ -160,10 +149,10 @@ files (instead of stuffing all of them into the same resultset class), you can
 achieve the same with subclassing the resultset class and defining the
 ResultSource there:
 
-  package My::Schema::UserFriendsComplex;
+  package My::Schema::Result::UserFriendsComplex;
 
-  use My::Schema::User;
-  use base qw/My::Schema::User/;
+  use My::Schema::Result::User;
+  use base qw/My::Schema::Result::User/;
 
   __PACKAGE__->table('dummy');  # currently must be called before anything else
 
@@ -248,29 +237,49 @@ any of your aliases using either of these:
 
 =head2 SELECT DISTINCT with multiple columns
 
-  my $rs = $schema->resultset('Foo')->search(
+  my $rs = $schema->resultset('Artist')->search(
     {},
     {
-      select => [
-        { distinct => [ $source->columns ] }
-      ],
-      as => [ $source->columns ] # remember 'as' is not the same as SQL AS :-)
+      columns => [ qw/artistid name rank/ ],
+      distinct => 1
+    } 
+  );
+
+  my $rs = $schema->resultset('Artist')->search(
+    {},
+    {
+      columns => [ qw/artistid name rank/ ],
+      group_by => [ qw/artistid name rank/ ],
     }
   );
 
+  # Equivalent SQL:
+  # SELECT me.artistid, me.name, me.rank
+  # FROM artist me
+  # GROUP BY artistid, name, rank
+
 =head2 SELECT COUNT(DISTINCT colname)
 
-  my $rs = $schema->resultset('Foo')->search(
+  my $rs = $schema->resultset('Artist')->search(
     {},
     {
-      select => [
-        { count => { distinct => 'colname' } }
-      ],
-      as => [ 'count' ]
+      columns => [ qw/name/ ],
+      distinct => 1
+    }
+  );
+
+  my $rs = $schema->resultset('Artist')->search(
+    {},
+    {
+      columns => [ qw/name/ ],
+      group_by => [ qw/name/ ],
     }
   );
 
-  my $count = $rs->next->get_column('count');
+  my $count = $rs->count;
+
+  # Equivalent SQL:
+  # SELECT COUNT( DISTINCT( me.name ) ) FROM artist me 
 
 =head2 Grouping results
 
@@ -295,7 +304,7 @@ Please see L<DBIx::Class::ResultSet/ATTRIBUTES> documentation if you
 are in any way unsure about the use of the attributes above (C< join
 >, C< select >, C< as > and C< group_by >).
 
-=head2 Subqueries
+=head2 Subqueries (EXPERIMENTAL)
 
 You can write subqueries relatively easily in DBIC.
 
@@ -343,6 +352,10 @@ That creates the following SQL:
        WHERE artistid = me.artistid
       )
 
+=head3 EXPERIMENTAL
+
+Please note that subqueries are considered an experimental feature.
+
 =head2 Predefined searches
 
 You can write your own L<DBIx::Class::ResultSet> class by inheriting from it
@@ -504,9 +517,6 @@ L<DBIx::Class> has now prefetched all matching data from the C<artist> table,
 so no additional SQL statements are executed. You now have a much more
 efficient query.
 
-Note that as of L<DBIx::Class> 0.05999_01, C<prefetch> I<can> be used with
-C<has_many> relationships.
-
 Also note that C<prefetch> should only be used when you know you will
 definitely use data from a related table. Pre-fetching related tables when you
 only need columns from the main table will make performance worse!
@@ -624,7 +634,7 @@ CD and Concert, and join CD to LinerNotes:
 
 =head2 Multi-step prefetch
 
-From 0.04999_05 onwards, C<prefetch> can be nested more than one relationship
+C<prefetch> can be nested more than one relationship
 deep using the same syntax as a multi-step join:
 
   my $rs = $schema->resultset('Tag')->search(
@@ -664,8 +674,7 @@ method.
 
 AKA getting last_insert_id
 
-If you are using PK::Auto (which is a core component as of 0.07), this is 
-straightforward:
+Thanks to the core component PK::Auto, this is straightforward:
 
   my $foo = $rs->create(\%blah);
   # do more stuff
@@ -680,7 +689,7 @@ Employ the standard stringification technique by using the C<overload>
 module.
 
 To make an object stringify itself as a single column, use something
-like this (replace C<foo> with the column/method of your choice):
+like this (replace C<name> with the column/method of your choice):
 
   use overload '""' => sub { shift->name}, fallback => 1;
 
@@ -724,6 +733,48 @@ Just use C<find_or_new> instead, then check C<in_storage>:
     # do whatever else you wanted if it was a new row
   }
 
+=head2 Static sub-classing DBIx::Class result classes 
+
+AKA adding additional relationships/methods/etc. to a model for a
+specific usage of the (shared) model.
+
+B<Schema definition> 
+    package My::App::Schema; 
+     
+    use base DBIx::Class::Schema; 
+
+    # load subclassed classes from My::App::Schema::Result/ResultSet
+    __PACKAGE__->load_namespaces;
+
+    # load classes from shared model
+    load_classes({
+        'My::Shared::Model::Result' => [qw/
+            Foo
+            Bar
+        /]});
+
+    1;
+B<Result-Subclass definition> 
+    package My::App::Schema::Result::Baz;
+     
+    use strict; 
+    use warnings; 
+    use base My::Shared::Model::Result::Baz; 
+    
+    # WARNING: Make sure you call table() again in your subclass,
+    # otherwise DBIx::Class::ResultSourceProxy::Table will not be called
+    # and the class name is not correctly registered as a source
+    __PACKAGE__->table('baz'); 
+     
+    sub additional_method { 
+        return "I'm an additional method only needed by this app"; 
+    }
+
+    1;
+     
 =head2 Dynamic Sub-classing DBIx::Class proxy classes 
 
 AKA multi-class object inflation from one table
@@ -747,16 +798,18 @@ below:
  
 B<Schema Definition> 
  
-    package DB::Schema; 
+    package My::Schema; 
      
     use base qw/DBIx::Class::Schema/; 
  
-    __PACKAGE__->load_classes(qw/User/); 
+    __PACKAGE__->load_namespaces;
+
+    1;
  
  
 B<Proxy-Class definitions> 
  
-    package DB::Schema::User; 
+    package My::Schema::Result::User; 
      
     use strict; 
     use warnings; 
@@ -789,13 +842,15 @@ B<Proxy-Class definitions>
         print "I am a regular user.\n"; 
         return ; 
     } 
+    
+    1;
+
      
-     
-    package DB::Schema::User::Admin; 
+    package My::Schema::Result::User::Admin; 
      
     use strict; 
     use warnings; 
-    use base qw/DB::Schema::User/; 
+    use base qw/My::Schema::Result::User/; 
      
     sub hello 
     { 
@@ -807,13 +862,15 @@ B<Proxy-Class definitions>
     { 
         print "I am doing admin stuff\n"; 
         return ; 
-    } 
+    }
+
+    1;
  
 B<Test File> test.pl 
  
     use warnings; 
     use strict; 
-    use DB::Schema; 
+    use My::Schema; 
      
     my $user_data = { email    => 'someguy@place.com',  
                       password => 'pass1',  
@@ -823,7 +880,7 @@ B<Test File> test.pl
                        password => 'pass2',  
                        admin    => 1 }; 
                            
-    my $schema = DB::Schema->connection('dbi:Pg:dbname=test'); 
+    my $schema = My::Schema->connection('dbi:Pg:dbname=test'); 
      
     $schema->resultset('User')->create( $user_data ); 
     $schema->resultset('User')->create( $admin_data ); 
@@ -861,11 +918,16 @@ To do this simply use L<DBIx::Class::ResultClass::HashRefInflator>.
 
 Wasn't that easy?
 
+Beware, changing the Result class using
+L<DBIx::Class::ResultSet/result_class> will replace any existing class
+completely including any special components loaded using
+load_components, eg L<DBIx::Class::InflateColumn::DateTime>.
+
 =head2 Get raw data for blindingly fast results
 
 If the L<HashRefInflator|DBIx::Class::ResultClass::HashRefInflator> solution
 above is not fast enough for you, you can use a DBIx::Class to return values
-exactly as they come out of the data base with none of the convenience methods
+exactly as they come out of the database with none of the convenience methods
 wrapped round them.
 
 This is used like so:
@@ -876,13 +938,13 @@ This is used like so:
   }
 
 You will need to map the array offsets to particular columns (you can
-use the I<select> attribute of C<search()> to force ordering).
+use the L<DBIx::Class::ResultSet/select> attribute of L<DBIx::Class::ResultSet/search> to force ordering).
 
 =head1 RESULTSET OPERATIONS
 
 =head2 Getting Schema from a ResultSet
 
-To get the schema object from a result set, do the following:
+To get the L<DBIx::Class::Schema> object from a ResultSet, do the following:
 
  $rs->result_source->schema
 
@@ -1022,6 +1084,98 @@ This is straightforward using L<ManyToMany|DBIx::Class::Relationship/many_to_man
   $rs = $user->addresses(); # get all addresses for a user
   $rs = $address->users(); # get all users for an address
 
+=head2 Relationships across DB schemas
+
+Mapping relationships across L<DB schemas|DBIx::Class::Manual::Glossary/DB schema>
+is easy as long as the schemas themselves are all accessible via the same DBI
+connection. In most cases, this means that they are on the same database host
+as each other and your connecting database user has the proper permissions to them.
+
+To accomplish this one only needs to specify the DB schema name in the table
+declaration, like so...
+
+  package MyDatabase::Main::Artist;
+  use base qw/DBIx::Class/;
+  __PACKAGE__->load_components(qw/PK::Auto Core/);
+  
+  __PACKAGE__->table('database1.artist'); # will use "database1.artist" in FROM clause
+  
+  __PACKAGE__->add_columns(qw/ artistid name /);
+  __PACKAGE__->set_primary_key('artistid');
+  __PACKAGE__->has_many('cds' => 'MyDatabase::Main::Cd');
+
+  1;
+
+Whatever string you specify there will be used to build the "FROM" clause in SQL
+queries.
+
+The big drawback to this is you now have DB schema names hardcoded in your
+class files. This becomes especially troublesome if you have multiple instances
+of your application to support a change lifecycle (e.g. DEV, TEST, PROD) and
+the DB schemas are named based on the environment (e.g. database1_dev).
+
+However, one can dynamically "map" to the proper DB schema by overriding the
+L<connection|DBIx::Class::Schama/connection> method in your Schema class and
+building a renaming facility, like so:
+
+  package MyDatabase::Schema;
+  use Moose;
+  
+  extends 'DBIx::Class::Schema';
+  
+  around connection => sub {
+    my ( $inner, $self, $dsn, $username, $pass, $attr ) = ( shift, @_ );
+   
+    my $postfix = delete $attr->{schema_name_postfix};
+    
+    $inner->(@_);
+    
+    if ( $postfix ) {
+        $self->append_db_name($postfix);
+    }
+  };
+
+  sub append_db_name {
+    my ( $self, $postfix ) = @_;
+    
+    my @sources_with_db 
+        = grep 
+            { $_->name =~ /^\w+\./mx } 
+            map 
+                { $self->source($_) } 
+                $self->sources;
+    
+    foreach my $source (@sources_with_db) {
+        my $name = $source->name;
+        $name =~ s{^(\w+)\.}{${1}${postfix}\.}mx;
+        
+        $source->name($name);
+    }
+  }
+
+  1;
+
+By overridding the L<connection|DBIx::Class::Schama/connection>
+method and extracting a custom option from the provided \%attr hashref one can
+then simply iterate over all the Schema's ResultSources, renaming them as
+needed.
+
+To use this facility, simply add or modify the \%attr hashref that is passed to 
+L<connection|DBIx::Class::Schama/connect>, as follows:
+
+  my $schema 
+    = MyDatabase::Schema->connect(
+      $dsn, 
+      $user, 
+      $pass,
+      {
+        schema_name_postfix => '_dev'
+        # ... Other options as desired ... 
+      })
+
+Obviously, one could accomplish even more advanced mapping via a hash map or a
+callback routine.
+
 =head1 TRANSACTIONS
 
 As of version 0.04001, there is improved transaction support in
@@ -1174,6 +1328,18 @@ Or use C<cursor>
   while (my @vals = $cursor->next) {
     print $vals[0]."\n";
   }
+
+In case you're going to use this "trick" together with L<DBIx::Class::Schema/deploy> or
+L<DBIx::Class::Schema/create_ddl_dir> a table called "dual" will be created in your
+current schema. This would overlap "sys.dual" and you could not fetch "sysdate" or
+"sequence.nextval" anymore from dual. To avoid this problem, just tell
+L<SQL::Translator> to not create table dual:
+
+    my $sqlt_args = {
+        add_drop_table => 1,
+        parser_args    => { sources => [ grep $_ ne 'Dual', schema->sources ] },
+    };
+    $schema->create_ddl_dir( [qw/Oracle/], undef, './sql', undef, $sqlt_args );
  
 Or use L<DBIx::Class::ResultClass::HashRefInflator>
  
@@ -1219,7 +1385,7 @@ class (refer to the advanced
 L<callback system|DBIx::Class::ResultSource/sqlt_deploy_callback> if you wish
 to share a hook between multiple sources):
 
- package My::Schema::Artist;
+ package My::Schema::Result::Artist;
 
  __PACKAGE__->table('artist');
  __PACKAGE__->add_columns(id => { ... }, name => { ... })
@@ -1381,10 +1547,10 @@ C<limit_dialect> key in the final hash as shown above.
 
 =head2 Working with PostgreSQL array types
 
-If your SQL::Abstract version (>= 1.50) supports it, you can assign to
-PostgreSQL array values by passing array references in the C<\%columns>
-(C<\%vals>) hashref of the L<DBIx::Class::ResultSet/create> and
-L<DBIx::Class::Row/update> family of methods:
+You can also assign values to PostgreSQL array columns by passing array
+references in the C<\%columns> (C<\%vals>) hashref of the
+L<DBIx::Class::ResultSet/create> and L<DBIx::Class::Row/update> family of
+methods:
 
   $resultset->create({
     numbers => [1, 2, 3]
@@ -1733,4 +1899,33 @@ If you are instead using the L<load_namespaces|DBIx::Class::Schema/load_namespac
 syntax to load the appropriate classes there is not a direct alternative
 avoiding L<Module::Find|Module::Find>.
 
+=head1 MEMORY USAGE
+
+=head2 Cached statements
+
+L<DBIx::Class> normally caches all statements with L<< prepare_cached()|DBI/prepare_cached >>.
+This is normally a good idea, but if too many statements are cached, the database may use too much
+memory and may eventually run out and fail entirely.  If you suspect this may be the case, you may want
+to examine DBI's L<< CachedKids|DBI/CachedKidsCachedKids_(hash_ref) >> hash:
+
+    # print all currently cached prepared statements
+    print for keys %{$schema->storage->dbh->{CachedKids}};
+    # get a count of currently cached prepared statements
+    my $count = scalar keys %{$schema->storage->dbh->{CachedKids}};
+
+If it's appropriate, you can simply clear these statements, automatically deallocating them in the
+database:
+
+    my $kids = $schema->storage->dbh->{CachedKids};
+    delete @{$kids}{keys %$kids} if scalar keys %$kids > 100;
+
+But what you probably want is to expire unused statements and not those that are used frequently.
+You can accomplish this with L<Tie::Cache> or L<Tie::Cache::LRU>:
+
+    use Tie::Cache;
+    use DB::Main;
+    my $schema = DB::Main->connect($dbi_dsn, $user, $pass, {
+        on_connect_do => sub { tie %{shift->_dbh->{CachedKids}}, 'Tie::Cache', 100 },
+    });
+
 =cut
index 273397a..d635342 100644 (file)
@@ -274,7 +274,7 @@ replaced with the following.)
 
 Or, if you have quoting off:
 
- ->search({ 'YEAR(date_of_birth' => 1979 });
+ ->search({ 'YEAR(date_of_birth)' => 1979 });
 
 =item .. find more help on constructing searches?
 
@@ -353,6 +353,20 @@ to get a new, fresh copy of the row, just re-fetch the row from storage.
 L<DBIx::Class::PK/discard_changes> does just that by re-fetching the row from storage
 using the row's primary key.
 
+=item .. fetch my data a "page" at a time?
+
+Pass the C<rows> and C<page> attributes to your search, eg:
+
+  ->search({}, { rows => 10, page => 1});
+
+=item .. get a count of all rows even when paging?
+
+Call C<pager> on the paged resultset, it will return a L<Data::Page>
+object. Calling C<total_entries> on the pager will return the correct
+total.
+
+C<count> on the resultset will only return the total number in the page.
+
 =back
 
 =head2 Inserting and updating data
@@ -538,3 +552,38 @@ group, or stringify_self method) ?
 See L<DBIx::Class::Manual::Cookbook/Stringification>
 
 =back
+
+=head2 Troubleshooting
+
+=over 4
+
+=item Help, I can't connect to postgresql!
+
+If you get an error such as:
+
+  DBI connect('dbname=dbic','user',...) failed: could not connect to server:
+  No such file or directory Is the server running locally and accepting
+  connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
+
+Likely you have/had two copies of postgresql installed simultaneously, the
+second one will use a default port of 5433, while L<DBD::Pg> is compiled with a
+default port of 5432.
+
+You can chance the port setting in C<postgresql.conf>.
+
+=item I've lost or forgotten my mysql password
+
+Stop mysqld and restart it with the --skip-grant-tables option.
+
+Issue the following statements in the mysql client.
+
+  UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
+  FLUSH PRIVILEGES;
+
+Restart mysql.
+
+Taken from:
+
+L<http://dev.mysql.com/doc/refman/5.1/en/resetting-permissions.html>.
+
+=back
index a70ffa1..b245dc9 100644 (file)
@@ -9,6 +9,17 @@ explain them.
 
 =head1 TERMS
 
+=head2 DB schema
+
+Refers to a single physical schema within an RDBMS. Synonymous with the terms
+'database', for MySQL; and 'schema', for most other RDBMS(s).
+
+In other words, it's the 'xyz' _thing_ you're connecting to when using any of
+the following L<DSN|DBI/connect>(s):
+
+  dbi:DriverName:xyz@hostname:port
+  dbi:DriverName:database=xyz;host=hostname;port=port
+
 =head2 Inflation
 
 The act of turning database row data into objects in
index 6aa4830..02b6dcd 100644 (file)
@@ -34,13 +34,18 @@ object that they will be called on.
 
 =item *
 
-Each method starts with a "head2" statement of it's name.
+Each method starts with a "head2" statement of its name.
+
+Just the plain method name, not an example of how to call it, or a link.
+This is to ensure easy linking to method documentation from other POD.
 
 =item *
 
-The header is followed by a one-item list.
+The header is followed by a two-item list. This contains a description
+of the arguments the method is expected to take, and an indication of
+what the method returns.
 
-The single item provides a list of all possible values for the
+The first item provides a list of all possible values for the
 arguments of the method in order, separated by C<, >, preceeded by the
 text "Arguments: "
 
@@ -70,23 +75,59 @@ $var - A scalar (string or numeric) variable.
 
 =item *
 
+%var - A hashref variable (list of key/value pairs) - rarely used in DBIx::Class.
+
+Reading an argument as a hash variable will consume all subsequent
+method arguments, use with caution.
+
+=item *
+
+@var - An array variable (list of values).
+
+Reading an argument as a array variable will consume all subsequent
+method arguments, use with caution.
+
+=item *
+
 ? - Optional, should be placed after the argument type and name.
 
+  ## Correct
+  \%myhashref|\@myarrayref?
+
+  ## Wrong
+  \%myhashref?|\@myarrayref
+
+Applies to the entire argument.
+
+Optional arguments can be left out of method calls, unless the caller
+needs to pass in any of the following arguments. In which case the
+caller should pass C<undef> in place of the missing argument.
+
 =item *
 
-| - Alternate argument types.
+| - Alternate argument content types. 
+
+At least one of these must be supplied unless the argument is also
+marked optional.
 
 =back
 
-NOTES:
+The second item starts with the text "Return value:". The remainder of
+the line is either the text "undefined", a text describing the result of
+the method, or a variable with a descriptive name.
 
-If several arguments are optional, it is always possible to pass
-C<undef> as one optional argument in order to skip it and provide a
-value for the following ones. This does not need to be indicated in
-the Arguments line, it is assumed.
+  ## Good examples
+  =item Return value: undefined
+  =item Return value: A schema object
+  =item Return value: $classname
 
-The C<?> for optional arguments always applies to the entire argument
-value, not a particular type or argument.
+  ## Bad examples
+  =item Return value: The names
+
+"undefined" means the method does not deliberately return a value, and
+the caller should not use or rely on anything it does return. (Perl
+functions always return something, usually the result of the last code
+statement, if there is no explicit return statement.)
 
 =item *
 
@@ -98,6 +139,9 @@ the method does.
 The description paragraph is followed by another list. Each item in
 the list explains one of the possible argument/type combinations.
 
+This list may be omitted if the author feels that the variable names are
+self-explanatory enough to not require it. Use best judgement.
+
 =item *
 
 The argument list is followed by some examples of how to use the
index b870b3b..2a19c50 100644 (file)
@@ -47,7 +47,7 @@ correctly.
 
 L<DBI> version 1.50 and L<DBD::Pg> 1.43 are known to work.
 
-=head2 ... Can't locate object method "source_name" via package ...
+=head2 Can't locate object method "source_name" via package
 
 There's likely a syntax error in the table class referred to elsewhere
 in this error message.  In particular make sure that the package
index dc51856..b3fd102 100644 (file)
@@ -272,6 +272,20 @@ sub last_sibling {
     return defined $lsib ? $lsib : 0;
 }
 
+# an optimised method to get the last sibling position without inflating a row object
+sub _last_sibling_pos {
+    my $self = shift;
+    my $position_column = $self->position_column;
+
+    my $cursor = $self->next_siblings->search(
+        {},
+        { rows => 1, order_by => { '-desc' => $position_column }, columns => $position_column },
+    )->cursor;
+
+    my ($pos) = $cursor->next;
+    return $pos;
+}
+
 =head2 move_previous
 
   $item->move_previous();
@@ -427,7 +441,7 @@ sub move_to_group {
         if ( not defined($to_position) or $to_position > $new_group_count) {
             $self->set_column(
                 $position_column => $new_group_count
-                    ? $self->_next_position_value ( $self->last_sibling->get_column ($position_column) )    # FIXME - no need to inflate last_sibling
+                    ? $self->_next_position_value ( $self->_last_sibling_pos )
                     : $self->_initial_position_value
             );
         }
@@ -459,10 +473,10 @@ sub insert {
     my $position_column = $self->position_column;
 
     unless ($self->get_column($position_column)) {
-        my $lsib = $self->last_sibling;     # FIXME - no need to inflate last_sibling
+        my $lsib_pos = $self->_last_sibling_pos;
         $self->set_column(
-            $position_column => ($lsib
-                ? $self->_next_position_value ( $lsib->get_column ($position_column) )
+            $position_column => (defined $lsib_pos
+                ? $self->_next_position_value ( $lsib_pos )
                 : $self->_initial_position_value
             )
         );
@@ -692,12 +706,22 @@ sub _shift_siblings {
     # position column is part of a unique constraint, and do a
     # one-by-one update if this is the case
 
-    if (grep { $_ eq $position_column } ( map { @$_ } (values %{{ $self->result_source->unique_constraints }} ) ) ) {
+    my $rsrc = $self->result_source;
+
+    if (grep { $_ eq $position_column } ( map { @$_ } (values %{{ $rsrc->unique_constraints }} ) ) ) {
+
+        my @pcols = $rsrc->primary_columns;
+        my $cursor = $shift_rs->search ({}, { order_by => { "-$ord", $position_column }, columns => \@pcols } )->cursor;
+        my $rs = $self->result_source->resultset;
+
+        while (my @pks = $cursor->next ) {
+
+          my $cond;
+          for my $i (0.. $#pcols) {
+            $cond->{$pcols[$i]} = $pks[$i];
+          }
 
-        my $rs = $shift_rs->search ({}, { order_by => { "-$ord", $position_column } } );
-        # FIXME - no need to inflate each row
-        while (my $r = $rs->next) {
-            $r->_ordered_internal_update ({ $position_column => \ "$position_column $op 1" } );
+          $rs->search($cond)->update ({ $position_column => \ "$position_column $op 1" } );
         }
     }
     else {
index 6ec2f25..c10d4e4 100644 (file)
@@ -27,15 +27,16 @@ sub add_relationship_accessor {
       } elsif (exists $self->{_relationship_data}{$rel}) {
         return $self->{_relationship_data}{$rel};
       } else {
-        my $cond = $self->result_source->resolve_condition(
+        my $cond = $self->result_source->_resolve_condition(
           $rel_info->{cond}, $rel, $self
         );
         if ($rel_info->{attrs}->{undef_on_null_fk}){
-          return unless ref($cond) eq 'HASH';
-          return if grep { not defined } values %$cond;
+          return undef unless ref($cond) eq 'HASH';
+          return undef if grep { not defined $_ } values %$cond;
         }
         my $val = $self->find_related($rel, {}, {});
-        return unless $val;
+        return $val unless $val;  # $val instead of undef so that null-objects can go through
+
         return $self->{_relationship_data}{$rel} = $val;
       }
     };
index 23df27e..8c2e4fd 100644 (file)
@@ -189,7 +189,7 @@ sub related_resultset {
     my $query = ((@_ > 1) ? {@_} : shift);
 
     my $source = $self->result_source;
-    my $cond = $source->resolve_condition(
+    my $cond = $source->_resolve_condition(
       $rel_obj->{cond}, $rel, $self
     );
     if ($cond eq $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION) {
@@ -404,7 +404,7 @@ sub set_from_related {
       unless Scalar::Util::blessed($f_obj) and $f_obj->isa($f_class);
   }
   $self->set_columns(
-    $self->result_source->resolve_condition(
+    $self->result_source->_resolve_condition(
        $rel_obj->{cond}, $f_obj, $rel));
   return 1;
 }
@@ -470,7 +470,7 @@ B<Currently only available for C<many-to-many> relationships.>
 
 =over 4
 
-=item Arguments: (\@hashrefs | \@objs)
+=item Arguments: (\@hashrefs | \@objs), $link_vals?
 
 =back
 
@@ -481,6 +481,10 @@ B<Currently only available for C<many-to-many> relationships.>
   $actor->set_roles(\@roles);
      # Replaces all of $actor's previous roles with the two named
 
+  $actor->set_roles(\@roles, { salary => 15_000_000 });
+     # Sets a column in the link table for all roles
+
+
 Replace all the related objects with the given reference to a list of
 objects. This does a C<delete> B<on the link table resultset> to remove the
 association between the current object and all related objects, then calls
index eb10752..a4dce6e 100644 (file)
@@ -79,12 +79,14 @@ sub belongs_to {
   return 1;
 }
 
-=head1 AUTHORS
+# Attempt to remove the POD so it (maybe) falls off the indexer
 
-Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
-
-Matt S. Trout <mst@shadowcatsystems.co.uk>
-
-=cut
+#=head1 AUTHORS
+#
+#Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
+#
+#Matt S. Trout <mst@shadowcatsystems.co.uk>
+#
+#=cut
 
 1;
index 163ac36..e953a44 100644 (file)
@@ -3,7 +3,8 @@ package # hide from PAUSE
 
 use strict;
 use warnings;
-use warnings::register;
+
+use Carp::Clan qw/^DBIx::Class/;
 use Sub::Name ();
 
 sub many_to_many {
@@ -28,16 +29,20 @@ sub many_to_many {
 
     for ($add_meth, $remove_meth, $set_meth, $rs_meth) {
       if ( $class->can ($_) ) {
-        warnings::warnif(<<"EOW")
+        carp (<<"EOW") unless $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK};
+
 ***************************************************************************
-The many-to-many relationship $meth is trying to create a utility method called
-$_. This will overwrite the existing method on $class. You almost certainly
-want to rename your method or the many-to-many relationship, as your method
-will not be callable (it will use the one from the relationship instead.)
+The many-to-many relationship '$meth' is trying to create a utility method
+called $_.
+This will completely overwrite one such already existing method on class
+$class.
 
-To disable this warning add the following to $class
+You almost certainly want to rename your method or the many-to-many
+relationship, as the functionality of the original method will not be
+accessible anymore.
 
-  no warnings 'DBIx::Class::Relationship::ManyToMany';
+To disable this warning set to a true value the environment variable
+DBIC_OVERWRITE_HELPER_METHODS_OK
 
 ***************************************************************************
 EOW
@@ -103,7 +108,7 @@ EOW
       );
       my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_);
       $self->search_related($rel, {})->delete;
-      $self->$add_meth($_) for (@to_set);
+      $self->$add_meth($_, ref($_[1]) ? $_[1] : {}) for (@to_set);
     };
 
     my $remove_meth_name = join '::', $class, $remove_meth;
@@ -114,7 +119,7 @@ EOW
       my $obj = shift;
       my $rel_source = $self->search_related($rel)->result_source;
       my $cond = $rel_source->relationship_info($f_rel)->{cond};
-      my $link_cond = $rel_source->resolve_condition(
+      my $link_cond = $rel_source->_resolve_condition(
         $cond, $obj, $f_rel
       );
       $self->search_related($rel, $link_cond)->delete;
index 5fffad4..45251fa 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 
 =head1 NAME
 
-DBIx::Class::ResultClass::HashRefInflator
+DBIx::Class::ResultClass::HashRefInflator - Get raw hashrefs from a resultset
 
 =head1 SYNOPSIS
 
index 233597e..b48e7b4 100644 (file)
@@ -46,29 +46,15 @@ A new ResultSet is returned from calling L</search> on an existing
 ResultSet. The new one will contain all the conditions of the
 original, plus any new conditions added in the C<search> call.
 
-A ResultSet is also an iterator. L</next> is used to return all the
-L<DBIx::Class::Row>s the ResultSet represents.
+A ResultSet also incorporates an implicit iterator. L</next> and L</reset>
+can be used to walk through all the L<DBIx::Class::Row>s the ResultSet
+represents.
 
 The query that the ResultSet represents is B<only> executed against
 the database when these methods are called:
+L</find> L</next> L</all> L</first> L</single> L</count>
 
-=over
-
-=item L</find>
-
-=item L</next>
-
-=item L</all>
-
-=item L</count>
-
-=item L</single>
-
-=item L</first>
-
-=back
-
-=head1 EXAMPLES 
+=head1 EXAMPLES
 
 =head2 Chaining resultsets
 
@@ -102,6 +88,21 @@ another.
     });
   }
 
+=head3 Resolving conditions and attributes
+
+When a resultset is chained from another resultset, conditions and
+attributes with the same keys need resolving.
+
+L</join>, L</prefetch>, L</+select>, L</+as> attributes are merged
+into the existing ones from the original resultset.
+
+The L</where>, L</having> attribute, and any search conditions are
+merged with an SQL C<AND> to the existing condition from the original
+resultset.
+
+All other attributes are overridden by any new ones supplied in the
+search attributes.
+
 =head2 Multiple queries
 
 Since a resultset just defines a query, you can do all sorts of
@@ -178,7 +179,7 @@ sub new {
   return $class->new_result(@_) if ref $class;
 
   my ($source, $attrs) = @_;
-  $source = $source->handle 
+  $source = $source->handle
     unless $source->isa('DBIx::Class::ResultSourceHandle');
   $attrs = { %{$attrs||{}} };
 
@@ -264,6 +265,11 @@ always return a resultset, even in list context.
 sub search_rs {
   my $self = shift;
 
+  # Special-case handling for (undef, undef).
+  if ( @_ == 2 && !defined $_[1] && !defined $_[0] ) {
+    pop(@_); pop(@_);
+  }
+
   my $attrs = {};
   $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
   my $our_attrs = { %{$self->{attrs}} };
@@ -276,7 +282,7 @@ sub search_rs {
 
   unless (
     (@_ && defined($_[0])) # @_ == () or (undef)
-    || 
+    ||
     (keys %$attrs # empty attrs or only 'safe' attrs
     && List::Util::first { !$safe{$_} } keys %$attrs)
   ) {
@@ -287,7 +293,7 @@ sub search_rs {
   my $new_attrs = { %{$our_attrs}, %{$attrs} };
 
   # merge new attrs into inherited
-  foreach my $key (qw/join prefetch +select +as/) {
+  foreach my $key (qw/join prefetch +select +as bind/) {
     next unless exists $attrs->{$key};
     $new_attrs->{$key} = $self->_merge_attr($our_attrs->{$key}, $attrs->{$key});
   }
@@ -373,19 +379,29 @@ Pass a literal chunk of SQL to be added to the conditional part of the
 resultset query.
 
 CAVEAT: C<search_literal> is provided for Class::DBI compatibility and should
-only be used in that context. There are known problems using C<search_literal>
-in chained queries; it can result in bind values in the wrong order.  See
-L<DBIx::Class::Manual::Cookbook/Searching> and
+only be used in that context. C<search_literal> is a convenience method.
+It is equivalent to calling $schema->search(\[]), but if you want to ensure
+columns are bound correctly, use C<search>.
+
+Example of how to use C<search> instead of C<search_literal>
+
+  my @cds = $cd_rs->search_literal('cdid = ? AND (artist = ? OR artist = ?)', (2, 1, 2));
+  my @cds = $cd_rs->search(\[ 'cdid = ? AND (artist = ? OR artist = ?)', [ 'cdid', 2 ], [ 'artist', 1 ], [ 'artist', 2 ] ]);
+
+
+See L<DBIx::Class::Manual::Cookbook/Searching> and
 L<DBIx::Class::Manual::FAQ/Searching> for searching techniques that do not
 require C<search_literal>.
 
 =cut
 
 sub search_literal {
-  my ($self, $cond, @vals) = @_;
-  my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
-  $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
-  return $self->search(\$cond, $attrs);
+  my ($self, $sql, @bind) = @_;
+  my $attr;
+  if ( @bind && ref($bind[-1]) eq 'HASH' ) {
+    $attr = pop @bind;
+  }
+  return $self->search(\[ $sql, map [ __DUMMY__ => $_ ], @bind ], ($attr || () ));
 }
 
 =head2 find
@@ -475,7 +491,7 @@ sub find {
         && ($info = $self->result_source->relationship_info($key))) {
       my $val = delete $input_query->{$key};
       next KEY if (ref($val) eq 'ARRAY'); # has_many for multi_create
-      my $rel_q = $self->result_source->resolve_condition(
+      my $rel_q = $self->result_source->_resolve_condition(
                     $info->{cond}, $val, $key
                   );
       die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY';
@@ -644,7 +660,7 @@ L<DBIx::Class::Cursor> for more information.
 sub cursor {
   my ($self) = @_;
 
-  my $attrs = { %{$self->_resolved_attrs} };
+  my $attrs = $self->_resolved_attrs_copy;
   return $self->{cursor}
     ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
           $attrs->{where},$attrs);
@@ -682,7 +698,7 @@ a warning:
   Query returned more than one row
 
 In this case, you should be using L</first> or L</find> instead, or if you really
-know what you are doing, use the L</rows> attribute to explicitly limit the size 
+know what you are doing, use the L</rows> attribute to explicitly limit the size
 of the resultset.
 
 =back
@@ -695,7 +711,7 @@ sub single {
       $self->throw_exception('single() only takes search conditions, no attributes. You want ->search( $cond, $attrs )->single()');
   }
 
-  my $attrs = { %{$self->_resolved_attrs} };
+  my $attrs = $self->_resolved_attrs_copy;
   if ($where) {
     if (defined $attrs->{where}) {
       $attrs->{where} = {
@@ -766,19 +782,16 @@ sub _collapse_query {
   if (ref $query eq 'ARRAY') {
     foreach my $subquery (@$query) {
       next unless ref $subquery;  # -or
-#      warn "ARRAY: " . Dumper $subquery;
       $collapsed = $self->_collapse_query($subquery, $collapsed);
     }
   }
   elsif (ref $query eq 'HASH') {
     if (keys %$query and (keys %$query)[0] eq '-and') {
       foreach my $subquery (@{$query->{-and}}) {
-#        warn "HASH: " . Dumper $subquery;
         $collapsed = $self->_collapse_query($subquery, $collapsed);
       }
     }
     else {
-#      warn "LEAF: " . Dumper $query;
       foreach my $col (keys %$query) {
         my $value = $query->{$col};
         $collapsed->{$col}{$value}++;
@@ -830,10 +843,24 @@ You most likely want to use L</search> with specific operators.
 
 For more information, see L<DBIx::Class::Manual::Cookbook>.
 
+This method is deprecated and will be removed in 0.09. Use L</search()>
+instead. An example conversion is:
+
+  ->search_like({ foo => 'bar' });
+
+  # Becomes
+
+  ->search({ foo => { like => 'bar' } });
+
 =cut
 
 sub search_like {
   my $class = shift;
+  carp join ("\n",
+    'search_like() is deprecated and will be removed in 0.09.',
+    'Instead use ->search({ x => { -like => "y%" } })',
+    '(note the outer pair of {}s - they are important!)'
+  );
   my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
   my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
   $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
@@ -975,7 +1002,7 @@ sub _collapse_result {
   do { # no need to check anything at the front, we always want the first row
 
     my %const;
-  
+
     foreach my $this_as (@construct_as) {
       $const{$this_as->[0]||''}{$this_as->[1]} = shift(@copy);
     }
@@ -1022,7 +1049,7 @@ sub _collapse_result {
         foreach my $p (@parts) {
           $target = $target->[1]->{$p} ||= [];
           $cur .= ".${p}";
-          if ($cur eq ".${key}" && (my @ckey = @{$collapse{$cur}||[]})) { 
+          if ($cur eq ".${key}" && (my @ckey = @{$collapse{$cur}||[]})) {
             # collapsing at this point and on final part
             my $pos = $collapse_pos{$cur};
             CK: foreach my $ck (@ckey) {
@@ -1073,10 +1100,15 @@ is derived.
 
 =back
 
-An accessor for the class to use when creating row objects. Defaults to 
-C<< result_source->result_class >> - which in most cases is the name of the 
+An accessor for the class to use when creating row objects. Defaults to
+C<< result_source->result_class >> - which in most cases is the name of the
 L<"table"|DBIx::Class::Manual::Glossary/"ResultSource"> class.
 
+Note that changing the result_class will also remove any components
+that were originally loaded in the source class via
+L<DBIx::Class::ResultSource/load_components>. Any overloaded methods
+in the original source class will not run.
+
 =cut
 
 sub result_class {
@@ -1099,14 +1131,8 @@ sub result_class {
 =back
 
 Performs an SQL C<COUNT> with the same query as the resultset was built
-with to find the number of elements. If passed arguments, does a search
-on the resultset and counts the results of that.
-
-Note: When using C<count> with C<group_by>, L<DBIx::Class> emulates C<GROUP BY>
-using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
-not support C<DISTINCT> with multiple columns. If you are using such a
-database, you should only use columns from the main table in your C<group_by>
-clause.
+with to find the number of elements. Passing arguments is equivalent to
+C<< $rs->search ($cond, \%attrs)->count >>
 
 =cut
 
@@ -1114,49 +1140,80 @@ sub count {
   my $self = shift;
   return $self->search(@_)->count if @_ and defined $_[0];
   return scalar @{ $self->get_cache } if $self->get_cache;
-  my $count = $self->_count;
+
+  my @grouped_subq_attrs = qw/prefetch collapse distinct group_by having/;
+  my @subq_attrs = ();
+  
+  my $attrs = $self->_resolved_attrs;
+  # if we are not paged - we are simply asking for a limit
+  if (not $attrs->{page} and not $attrs->{software_limit}) {
+    push @subq_attrs, qw/rows offset/;
+  }
+
+  my $need_subq = $self->_has_attr (@subq_attrs);
+  my $need_group_subq = $self->_has_attr (@grouped_subq_attrs);
+
+  return ($need_subq || $need_group_subq)
+    ? $self->_count_subq ($need_group_subq)
+    : $self->_count_simple
+}
+
+sub _count_subq {
+  my ($self, $add_group_by) = @_;
+
+  my $attrs = $self->_resolved_attrs_copy;
+
+  # copy for the subquery, we need to do some adjustments to it too
+  my $sub_attrs = { %$attrs };
+
+  # these can not go in the subquery, and there is no point of ordering it
+  delete $sub_attrs->{$_} for qw/prefetch collapse select +select as +as columns +columns order_by/;
+
+  # if needed force a group_by and the same set of columns (most databases require this)
+  if ($add_group_by) {
+    $sub_attrs->{columns} = $sub_attrs->{group_by} ||= [ map { "$attrs->{alias}.$_" } ($self->result_source->primary_columns) ];
+  }
+
+  $attrs->{from} = [{
+    count_subq => (ref $self)->new ($self->result_source, $sub_attrs )->as_query
+  }];
+
+  # the subquery replaces this
+  delete $attrs->{$_} for qw/where bind prefetch collapse distinct group_by having having_bind/;
+
+  return $self->__count ($attrs);
+}
+
+sub _count_simple {
+  my $self = shift;
+
+  my $count = $self->__count;
   return 0 unless $count;
 
   # need to take offset from resolved attrs
 
-  $count -= $self->{_attrs}{offset} if $self->{_attrs}{offset};
-  $count = $self->{attrs}{rows} if
-    $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
+  my $attrs = $self->_resolved_attrs;
+
+  $count -= $attrs->{offset} if $attrs->{offset};
+  $count = $attrs->{rows} if $attrs->{rows} and $attrs->{rows} < $count;
   $count = 0 if ($count < 0);
   return $count;
 }
 
-sub _count { # Separated out so pager can get the full count
-  my $self = shift;
-  my $select = { count => '*' };
-
-  my $attrs = { %{$self->_resolved_attrs} };
-  if (my $group_by = delete $attrs->{group_by}) {
-    delete $attrs->{having};
-    my @distinct = (ref $group_by ?  @$group_by : ($group_by));
-    # todo: try CONCAT for multi-column pk
-    my @pk = $self->result_source->primary_columns;
-    if (@pk == 1) {
-      my $alias = $attrs->{alias};
-      foreach my $column (@distinct) {
-        if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) {
-          @distinct = ($column);
-          last;
-        }
-      }
-    }
+sub __count {
+  my ($self, $attrs) = @_;
 
-    $select = { count => { distinct => \@distinct } };
-  }
+  $attrs ||= $self->_resolved_attrs_copy;
 
-  $attrs->{select} = $select;
-  $attrs->{as} = [qw/count/];
+  # take off any column specs, any pagers, record_filter is cdbi, and no point of ordering a count
+  delete $attrs->{$_} for (qw/columns +columns select +select as +as rows offset page pager order_by record_filter/); 
 
-  # offset, order by and page are not needed to count. record_filter is cdbi
-  delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
+  $attrs->{select} = { count => '*' };
+  $attrs->{as} = [qw/count/];
 
   my $tmp_rs = (ref $self)->new($self->result_source, $attrs);
   my ($count) = $tmp_rs->cursor->next;
+
   return $count;
 }
 
@@ -1269,6 +1326,72 @@ sub first {
   return $_[0]->reset->next;
 }
 
+
+# _rs_update_delete
+#
+# Determines whether and what type of subquery is required for the $rs operation.
+# If grouping is necessary either supplies its own, or verifies the current one
+# After all is done delegates to the proper storage method.
+
+sub _rs_update_delete {
+  my ($self, $op, $values) = @_;
+
+  my $rsrc = $self->result_source;
+
+  my $needs_group_by_subq = $self->_has_attr (qw/prefetch distinct join seen_join group_by/);
+  my $needs_subq = $self->_has_attr (qw/row offset page/);
+
+  if ($needs_group_by_subq or $needs_subq) {
+
+    # make a new $rs selecting only the PKs (that's all we really need)
+    my $attrs = $self->_resolved_attrs_copy;
+
+    delete $attrs->{$_} for qw/prefetch collapse select +select as +as columns +columns/;
+    $attrs->{columns} = [ map { "$attrs->{alias}.$_" } ($self->result_source->primary_columns) ];
+
+    if ($needs_group_by_subq) {
+      # make sure no group_by was supplied, or if there is one - make sure it matches
+      # the columns compiled above perfectly. Anything else can not be sanely executed
+      # on most databases so croak right then and there
+
+      if (my $g = $attrs->{group_by}) {
+        my @current_group_by = map
+          { $_ =~ /\./ ? $_ : "$attrs->{alias}.$_" }
+          (ref $g eq 'ARRAY' ? @$g : $g );
+
+        if (
+          join ("\x00", sort @current_group_by)
+            ne
+          join ("\x00", sort @{$attrs->{columns}} )
+        ) {
+          $self->throw_exception (
+            "You have just attempted a $op operation on a resultset which does group_by"
+            . ' on columns other than the primary keys, while DBIC internally needs to retrieve'
+            . ' the primary keys in a subselect. All sane RDBMS engines do not support this'
+            . ' kind of queries. Please retry the operation with a modified group_by or'
+            . ' without using one at all.'
+          );
+        }
+      }
+      else {
+        $attrs->{group_by} = $attrs->{columns};
+      }
+    }
+
+    my $subrs = (ref $self)->new($rsrc, $attrs);
+
+    return $self->result_source->storage->subq_update_delete($subrs, $op, $values);
+  }
+  else {
+    return $rsrc->storage->$op(
+      $rsrc,
+      $op eq 'update' ? $values : (),
+      $self->_cond_for_update_delete,
+    );
+  }
+}
+
+
 # _cond_for_update_delete
 #
 # update/delete require the condition to be modified to handle
@@ -1298,11 +1421,9 @@ sub _cond_for_update_delete {
   elsif (ref $full_cond eq 'HASH') {
     if ((keys %{$full_cond})[0] eq '-and') {
       $cond->{-and} = [];
-
       my @cond = @{$full_cond->{-and}};
-      for (my $i = 0; $i < @cond; $i++) {
+       for (my $i = 0; $i < @cond; $i++) {
         my $entry = $cond[$i];
-
         my $hash;
         if (ref $entry eq 'HASH') {
           $hash = $self->_cond_for_update_delete($entry);
@@ -1311,7 +1432,6 @@ sub _cond_for_update_delete {
           $entry =~ /([^.]+)$/;
           $hash->{$1} = $cond[++$i];
         }
-
         push @{$cond->{-and}}, $hash;
       }
     }
@@ -1323,9 +1443,7 @@ sub _cond_for_update_delete {
     }
   }
   else {
-    $self->throw_exception(
-      "Can't update/delete on resultset with condition unless hash or array"
-    );
+    $self->throw_exception("Can't update/delete on resultset with condition unless hash or array");
   }
 
   return $cond;
@@ -1350,19 +1468,10 @@ if no records were updated; exact type of success value is storage-dependent.
 
 sub update {
   my ($self, $values) = @_;
-  $self->throw_exception("Values for update must be a hash")
+  $self->throw_exception('Values for update must be a hash')
     unless ref $values eq 'HASH';
 
-  carp(   'WARNING! Currently $rs->update() does not generate proper SQL'
-        . ' on joined resultsets, and may affect rows well outside of the'
-        . ' contents of $rs. Use at your own risk' )
-    if ( $self->{attrs}{seen_join} );
-
-  my $cond = $self->_cond_for_update_delete;
-   
-  return $self->result_source->storage->update(
-    $self->result_source, $values, $cond
-  );
+  return $self->_rs_update_delete ('update', $values);
 }
 
 =head2 update_all
@@ -1382,7 +1491,7 @@ will run DBIC cascade triggers, while L</update> will not.
 
 sub update_all {
   my ($self, $values) = @_;
-  $self->throw_exception("Values for update must be a hash")
+  $self->throw_exception('Values for update_all must be a hash')
     unless ref $values eq 'HASH';
   foreach my $obj ($self->all) {
     $obj->set_columns($values)->update;
@@ -1407,27 +1516,17 @@ to run. See also L<DBIx::Class::Row/delete>.
 delete may not generate correct SQL for a query with joins or a resultset
 chained from a related resultset.  In this case it will generate a warning:-
 
-  WARNING! Currently $rs->delete() does not generate proper SQL on
-  joined resultsets, and may delete rows well outside of the contents
-  of $rs. Use at your own risk
-
 In these cases you may find that delete_all is more appropriate, or you
 need to respecify your query in a way that can be expressed without a join.
 
 =cut
 
 sub delete {
-  my ($self) = @_;
-  $self->throw_exception("Delete should not be passed any arguments")
-    if $_[1];
-  carp(   'WARNING! Currently $rs->delete() does not generate proper SQL'
-        . ' on joined resultsets, and may delete rows well outside of the'
-        . ' contents of $rs. Use at your own risk' )
-    if ( $self->{attrs}{seen_join} );
-  my $cond = $self->_cond_for_update_delete;
-
-  $self->result_source->storage->delete($self->result_source, $cond);
-  return 1;
+  my $self = shift;
+  $self->throw_exception('delete does not accept any arguments')
+    if @_;
+
+  return $self->_rs_update_delete ('delete');
 }
 
 =head2 delete_all
@@ -1446,7 +1545,10 @@ will run DBIC cascade triggers, while L</delete> will not.
 =cut
 
 sub delete_all {
-  my ($self) = @_;
+  my $self = shift;
+  $self->throw_exception('delete_all does not accept any arguments')
+    if @_;
+
   $_->delete for $self->all;
   return 1;
 }
@@ -1464,7 +1566,7 @@ For the arrayref of hashrefs style each hashref should be a structure suitable
 forsubmitting to a $resultset->create(...) method.
 
 In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
-to insert the data, as this is a faster method.  
+to insert the data, as this is a faster method.
 
 Otherwise, each set of data is inserted into the database using
 L<DBIx::Class::ResultSet/create>, and a arrayref of the resulting row
@@ -1473,10 +1575,10 @@ objects is returned.
 Example:  Assuming an Artist Class that has many CDs Classes relating:
 
   my $Artist_rs = $schema->resultset("Artist");
-  
-  ## Void Context Example 
+
+  ## Void Context Example
   $Artist_rs->populate([
-     { artistid => 4, name => 'Manufactured Crap', cds => [ 
+     { artistid => 4, name => 'Manufactured Crap', cds => [
         { title => 'My First CD', year => 2006 },
         { title => 'Yet More Tweeny-Pop crap', year => 2007 },
       ],
@@ -1488,7 +1590,7 @@ Example:  Assuming an Artist Class that has many CDs Classes relating:
       ],
      },
   ]);
-  
+
   ## Array Context Example
   my ($ArtistOne, $ArtistTwo, $ArtistThree) = $Artist_rs->populate([
     { name => "Artist One"},
@@ -1498,7 +1600,7 @@ Example:  Assuming an Artist Class that has many CDs Classes relating:
     { title => "Second CD", year => 2008},
   ]}
   ]);
-  
+
   print $ArtistOne->name; ## response is 'Artist One'
   print $ArtistThree->cds->count ## reponse is '2'
 
@@ -1514,11 +1616,11 @@ example:
   ]);
 
 Please note an important effect on your data when choosing between void and
-wantarray context. Since void context goes straight to C<insert_bulk> in 
+wantarray context. Since void context goes straight to C<insert_bulk> in
 L<DBIx::Class::Storage::DBI> this will skip any component that is overriding
-c<insert>.  So if you are using something like L<DBIx-Class-UUIDColumns> to 
-create primary keys for you, you will find that your PKs are empty.  In this 
-case you will have to use the wantarray context in order to create those 
+C<insert>.  So if you are using something like L<DBIx-Class-UUIDColumns> to
+create primary keys for you, you will find that your PKs are empty.  In this
+case you will have to use the wantarray context in order to create those
 values.
 
 =cut
@@ -1528,7 +1630,7 @@ sub populate {
   my $data = ref $_[0][0] eq 'HASH'
     ? $_[0] : ref $_[0][0] eq 'ARRAY' ? $self->_normalize_populate_args($_[0]) :
     $self->throw_exception('Populate expects an arrayref of hashes or arrayref of arrayrefs');
-  
+
   if(defined wantarray) {
     my @created;
     foreach my $item (@$data) {
@@ -1540,28 +1642,28 @@ sub populate {
 
     my @names = grep {!ref $first->{$_}} keys %$first;
     my @rels = grep { $self->result_source->has_relationship($_) } keys %$first;
-    my @pks = $self->result_source->primary_columns;  
+    my @pks = $self->result_source->primary_columns;
 
-    ## do the belongs_to relationships  
+    ## do the belongs_to relationships
     foreach my $index (0..$#$data) {
       if( grep { !defined $data->[$index]->{$_} } @pks ) {
         my @ret = $self->populate($data);
         return;
       }
-    
+
       foreach my $rel (@rels) {
         next unless $data->[$index]->{$rel} && ref $data->[$index]->{$rel} eq "HASH";
         my $result = $self->related_resultset($rel)->create($data->[$index]->{$rel});
         my ($reverse) = keys %{$self->result_source->reverse_relationship_info($rel)};
-        my $related = $result->result_source->resolve_condition(
+        my $related = $result->result_source->_resolve_condition(
           $result->result_source->relationship_info($reverse)->{cond},
-          $self,        
-          $result,        
+          $self,
+          $result,
         );
 
         delete $data->[$index]->{$rel};
         $data->[$index] = {%{$data->[$index]}, %$related};
-      
+
         push @names, keys %$related if $index == 0;
       }
     }
@@ -1570,8 +1672,8 @@ sub populate {
     my @values = map { [ @$_{@names} ] } @$data;
 
     $self->result_source->storage->insert_bulk(
-      $self->result_source, 
-      \@names, 
+      $self->result_source,
+      \@names,
       \@values,
     );
 
@@ -1581,12 +1683,12 @@ sub populate {
       foreach my $rel (@rels) {
         next unless $item->{$rel} && ref $item->{$rel} eq "ARRAY";
 
-        my $parent = $self->find(map {{$_=>$item->{$_}} } @pks) 
+        my $parent = $self->find(map {{$_=>$item->{$_}} } @pks)
      || $self->throw_exception('Cannot find the relating object.');
-     
+
         my $child = $parent->$rel;
-    
-        my $related = $child->result_source->resolve_condition(
+
+        my $related = $child->result_source->_resolve_condition(
           $parent->result_source->relationship_info($rel)->{cond},
           $child,
           $parent,
@@ -1618,7 +1720,7 @@ sub _normalize_populate_args {
     foreach my $index (0..$#names) {
       $result_to_create{$names[$index]} = $$datum[$index];
     }
-    push @results_to_create, \%result_to_create;    
+    push @results_to_create, \%result_to_create;
   }
   return \@results_to_create;
 }
@@ -1636,16 +1738,32 @@ sub _normalize_populate_args {
 Return Value a L<Data::Page> object for the current resultset. Only makes
 sense for queries with a C<page> attribute.
 
+To get the full count of entries for a paged resultset, call
+C<total_entries> on the L<Data::Page> object.
+
 =cut
 
 sub pager {
   my ($self) = @_;
+
+  return $self->{pager} if $self->{pager};
+
   my $attrs = $self->{attrs};
   $self->throw_exception("Can't create pager for non-paged rs")
     unless $self->{attrs}{page};
   $attrs->{rows} ||= 10;
-  return $self->{pager} ||= Data::Page->new(
-    $self->_count, $attrs->{rows}, $self->{attrs}{page});
+
+  # throw away the paging flags and re-run the count (possibly
+  # with a subselect) to get the real total count
+  my $count_attrs = { %$attrs };
+  delete $count_attrs->{$_} for qw/rows offset page pager/;
+  my $total_count = (ref $self)->new($self->result_source, $count_attrs)->count;
+
+  return $self->{pager} = Data::Page->new(
+    $total_count,
+    $attrs->{rows},
+    $self->{attrs}{page}
+  );
 }
 
 =head2 page
@@ -1706,13 +1824,13 @@ sub new_result {
     $self->throw_exception(
       "Can't abstract implicit construct, condition not a hash"
     ) if ($self->{cond} && !(ref $self->{cond} eq 'HASH'));
-  
+
     my $collapsed_cond = (
       $self->{cond}
         ? $self->_collapse_cond($self->{cond})
         : {}
     );
-  
+
     # precendence must be given to passed values over values inherited from
     # the cond, so the order here is important.
     my %implied =  %{$self->_remove_alias($collapsed_cond, $alias)};
@@ -1737,7 +1855,7 @@ sub new_result {
 
 # _is_deterministic_value
 #
-# Make an effor to strip non-deterministic values from the condition, 
+# Make an effor to strip non-deterministic values from the condition,
 # to make sure new_result chokes less
 
 sub _is_deterministic_value {
@@ -1749,6 +1867,50 @@ sub _is_deterministic_value {
   return 0;
 }
 
+# _has_attr
+#
+# determines if the resultset defines at least one
+# of the attributes supplied
+#
+# used to determine if a subquery is neccessary
+
+sub _has_attr {
+  my ($self, @attr_names) = @_;
+
+  my $attrs = $self->_resolved_attrs;
+
+  my $join_check_req;
+
+  for my $n (@attr_names) {
+    ++$join_check_req if $n =~ /join/;
+
+    my $attr =  $attrs->{$n};
+
+    next if not defined $attr;
+
+    if (ref $attr eq 'HASH') {
+      return 1 if keys %$attr;
+    }
+    elsif (ref $attr eq 'ARRAY') {
+      return 1 if @$attr;
+    }
+    else {
+      return 1 if $attr;
+    }
+  }
+
+  # a join can be expressed as a multi-level from
+  return 1 if (
+    $join_check_req
+      and
+    ref $attrs->{from} eq 'ARRAY'
+      and
+    @{$attrs->{from}} > 1
+  );
+
+  return 0;
+}
+
 # _collapse_cond
 #
 # Recursively collapse the condition.
@@ -1761,19 +1923,16 @@ sub _collapse_cond {
   if (ref $cond eq 'ARRAY') {
     foreach my $subcond (@$cond) {
       next unless ref $subcond;  # -or
-#      warn "ARRAY: " . Dumper $subcond;
       $collapsed = $self->_collapse_cond($subcond, $collapsed);
     }
   }
   elsif (ref $cond eq 'HASH') {
     if (keys %$cond and (keys %$cond)[0] eq '-and') {
       foreach my $subcond (@{$cond->{-and}}) {
-#        warn "HASH: " . Dumper $subcond;
         $collapsed = $self->_collapse_cond($subcond, $collapsed);
       }
     }
     else {
-#      warn "LEAF: " . Dumper $cond;
       foreach my $col (keys %$cond) {
         my $value = $cond->{$col};
         $collapsed->{$col} = $value;
@@ -1807,7 +1966,7 @@ sub _remove_alias {
   return \%unaliased;
 }
 
-=head2 as_query
+=head2 as_query (EXPERIMENTAL)
 
 =over 4
 
@@ -1821,6 +1980,8 @@ Returns the SQL query and bind vars associated with the invocant.
 
 This is generally used as the RHS for a subquery.
 
+B<NOTE>: This feature is still experimental.
+
 =cut
 
 sub as_query { return shift->cursor->as_query(@_) }
@@ -1908,12 +2069,12 @@ Example of creating a new row.
     name=>"Some Person",
     email=>"somebody@someplace.com"
   });
-  
+
 Example of creating a new row and also creating rows in a related C<has_many>
 or C<has_one> resultset.  Note Arrayref.
 
   $artist_rs->create(
-     { artistid => 4, name => 'Manufactured Crap', cds => [ 
+     { artistid => 4, name => 'Manufactured Crap', cds => [
         { title => 'My First CD', year => 2006 },
         { title => 'Yet More Tweeny-Pop crap', year => 2007 },
       ],
@@ -2029,10 +2190,10 @@ For example:
     { key => 'cd_artist_title' }
   );
 
-  $cd->cd_to_producer->update_or_create({ 
-    producer => $producer, 
+  $cd->cd_to_producer->update_or_create({
+    producer => $producer,
     name => 'harry',
-  }, { 
+  }, {
     key => 'primary,
   });
 
@@ -2067,6 +2228,63 @@ sub update_or_create {
   return $self->create($cond);
 }
 
+=head2 update_or_new
+
+=over 4
+
+=item Arguments: \%col_values, { key => $unique_constraint }?
+
+=item Return Value: $rowobject
+
+=back
+
+  $resultset->update_or_new({ col => $val, ... });
+
+First, searches for an existing row matching one of the unique constraints
+(including the primary key) on the source of this resultset. If a row is
+found, updates it with the other given column values. Otherwise, instantiate
+a new result object and return it. The object will not be saved into your storage
+until you call L<DBIx::Class::Row/insert> on it.
+
+Takes an optional C<key> attribute to search on a specific unique constraint.
+For example:
+
+  # In your application
+  my $cd = $schema->resultset('CD')->update_or_new(
+    {
+      artist => 'Massive Attack',
+      title  => 'Mezzanine',
+      year   => 1998,
+    },
+    { key => 'cd_artist_title' }
+  );
+
+  if ($cd->in_storage) {
+      # the cd was updated
+  }
+  else {
+      # the cd is not yet in the database, let's insert it
+      $cd->insert;
+  }
+
+See also L</find>, L</find_or_create> and L<find_or_new>.
+
+=cut
+
+sub update_or_new {
+    my $self  = shift;
+    my $attrs = ( @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {} );
+    my $cond  = ref $_[0] eq 'HASH' ? shift : {@_};
+
+    my $row = $self->find( $cond, $attrs );
+    if ( defined $row ) {
+        $row->update($cond);
+        return $row;
+    }
+
+    return $self->new_result($cond);
+}
+
 =head2 get_cache
 
 =over 4
@@ -2160,7 +2378,7 @@ sub related_resultset {
       "search_related: result source '" . $self->result_source->source_name .
         "' has no such relationship $rel")
       unless $rel_obj;
-    
+
     my ($from,$seen) = $self->_resolve_from($rel);
 
     my $join_count = $seen->{$rel};
@@ -2253,32 +2471,43 @@ sub current_source_alias {
   return ($self->{attrs} || {})->{alias} || 'me';
 }
 
+# This code is called by search_related, and makes sure there
+# is clear separation between the joins before, during, and
+# after the relationship. This information is needed later
+# in order to properly resolve prefetch aliases (any alias
+# with a relation_chain_depth less than the depth of the
+# current prefetch is not considered)
 sub _resolve_from {
   my ($self, $extra_join) = @_;
   my $source = $self->result_source;
   my $attrs = $self->{attrs};
-  
+
   my $from = $attrs->{from}
     || [ { $attrs->{alias} => $source->from } ];
-    
+
   my $seen = { %{$attrs->{seen_join}||{}} };
 
-  my $join = ($attrs->{join}
-               ? [ $attrs->{join}, $extra_join ]
-               : $extra_join);
+  # we need to take the prefetch the attrs into account before we
+  # ->_resolve_join as otherwise they get lost - captainL
+  my $merged = $self->_merge_attr( $attrs->{join}, $attrs->{prefetch} );
+
+  push @$from, $source->_resolve_join($merged, $attrs->{alias}, $seen) if ($merged);
 
-  # we need to take the prefetch the attrs into account before we 
-  # ->resolve_join as otherwise they get lost - captainL
-  my $merged = $self->_merge_attr( $join, $attrs->{prefetch} );
+  ++$seen->{-relation_chain_depth};
 
-  $from = [
-    @$from,
-    ($join ? $source->resolve_join($merged, $attrs->{alias}, $seen) : ()),
-  ];
+  push @$from, $source->_resolve_join($extra_join, $attrs->{alias}, $seen);
+
+  ++$seen->{-relation_chain_depth};
 
   return ($from,$seen);
 }
 
+# too many times we have to do $attrs = { %{$self->_resolved_attrs} }
+sub _resolved_attrs_copy {
+  my $self = shift;
+  return { %{$self->_resolved_attrs (@_)} };
+}
+
 sub _resolved_attrs {
   my $self = shift;
   return $self->{_attrs} if $self->{_attrs};
@@ -2293,12 +2522,20 @@ sub _resolved_attrs {
   # build columns (as long as select isn't set) into a set of as/select hashes
   unless ( $attrs->{select} ) {
       @colbits = map {
-          ( ref($_) eq 'HASH' ) ? $_
-            : {
-              (
-                  /^\Q${alias}.\E(.+)$/ ? $1
-                  : $_
-                ) => ( /\./ ? $_ : "${alias}.$_" )
+          ( ref($_) eq 'HASH' )
+              ? $_
+              : {
+                  (
+                    /^\Q${alias}.\E(.+)$/
+                      ? "$1"
+                      : "$_"
+                  )
+                =>
+                  (
+                    /\./
+                      ? "$_"
+                      : "${alias}.$_"
+                  )
             }
       } ( ref($attrs->{columns}) eq 'ARRAY' ) ? @{ delete $attrs->{columns}} : (delete $attrs->{columns} || $source->columns );
   }
@@ -2364,15 +2601,13 @@ sub _resolved_attrs {
     $attrs->{from} =    # have to copy here to avoid corrupting the original
       [
       @{ $attrs->{from} },
-      $source->resolve_join(
+      $source->_resolve_join(
         $join, $alias, { %{ $attrs->{seen_join} || {} } }
       )
       ];
 
   }
 
-  $attrs->{group_by} ||= $attrs->{select}
-    if delete $attrs->{distinct};
   if ( $attrs->{order_by} ) {
     $attrs->{order_by} = (
       ref( $attrs->{order_by} ) eq 'ARRAY'
@@ -2388,30 +2623,53 @@ sub _resolved_attrs {
   if ( my $prefetch = delete $attrs->{prefetch} ) {
     $prefetch = $self->_merge_attr( {}, $prefetch );
     my @pre_order;
-    my $seen = { %{ $attrs->{seen_join} || {} } };
     foreach my $p ( ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch) ) {
 
       # bring joins back to level of current class
+      my $join_map = $self->_joinpath_aliases ($attrs->{from}, $attrs->{seen_join});
       my @prefetch =
-        $source->resolve_prefetch( $p, $alias, $seen, \@pre_order, $collapse );
+        $source->_resolve_prefetch( $p, $alias, $join_map, \@pre_order, $collapse );
       push( @{ $attrs->{select} }, map { $_->[0] } @prefetch );
       push( @{ $attrs->{as} },     map { $_->[1] } @prefetch );
     }
     push( @{ $attrs->{order_by} }, @pre_order );
   }
+
+  if (delete $attrs->{distinct}) {
+    $attrs->{group_by} ||= [ grep { !ref($_) || (ref($_) ne 'HASH') } @{$attrs->{select}} ];
+  }
+
   $attrs->{collapse} = $collapse;
 
-  if ( $attrs->{page} ) {
-    $attrs->{offset} ||= 0;
-    $attrs->{offset} += ( $attrs->{rows} * ( $attrs->{page} - 1 ) );
+  if ( $attrs->{page} and not defined $attrs->{offset} ) {
+    $attrs->{offset} = ( $attrs->{rows} * ( $attrs->{page} - 1 ) );
   }
 
   return $self->{_attrs} = $attrs;
 }
 
+sub _joinpath_aliases {
+  my ($self, $fromspec, $seen) = @_;
+
+  my $paths = {};
+  return $paths unless ref $fromspec eq 'ARRAY';
+
+  for my $j (@$fromspec) {
+
+    next if ref $j ne 'ARRAY';
+    next if $j->[0]{-relation_chain_depth} < ( $seen->{-relation_chain_depth} || 0);
+
+    my $p = $paths;
+    $p = $p->{$_} ||= {} for @{$j->[0]{-join_path}};
+    push @{$p->{-join_aliases} }, $j->[0]{-join_alias};
+  }
+
+  return $paths;
+}
+
 sub _rollout_attr {
   my ($self, $attr) = @_;
-  
+
   if (ref $attr eq 'HASH') {
     return $self->_rollout_hash($attr);
   } elsif (ref $attr eq 'ARRAY') {
@@ -2462,7 +2720,7 @@ sub _calculate_score {
       }
     } else {
       return ($a eq $b_key) ? 1 : 0;
-    }       
+    }
   } else {
     if (ref $a eq 'HASH') {
       my ($a_key) = keys %{$a};
@@ -2478,7 +2736,7 @@ sub _merge_attr {
 
   return $import unless defined($orig);
   return $orig unless defined($import);
-  
+
   $orig = $self->_rollout_attr($orig);
   $import = $self->_rollout_attr($import);
 
@@ -2555,22 +2813,26 @@ These are in no particular order:
 
 =over 4
 
-=item Value: ($order_by | \@order_by)
+=item Value: ( $order_by | \@order_by | \%order_by )
 
 =back
 
-Which column(s) to order the results by. This is currently passed
-through directly to SQL, so you can give e.g. C<year DESC> for a
-descending order on the column `year'.
+Which column(s) to order the results by. If a single column name, or
+an arrayref of names is supplied, the argument is passed through
+directly to SQL. The hashref syntax allows for connection-agnostic
+specification of ordering direction:
+
+ For descending order:
+
+  order_by => { -desc => [qw/col1 col2 col3/] }
 
-Please note that if you have C<quote_char> enabled (see
-L<DBIx::Class::Storage::DBI/connect_info>) you will need to do C<\'year DESC' > to
-specify an order. (The scalar ref causes it to be passed as raw sql to the DB,
-so you will need to manually quote things as appropriate.)
+ For explicit ascending order:
 
-If your L<SQL::Abstract> version supports it (>=1.50), you can also use
-C<{-desc => 'year'}>, which takes care of the quoting for you. This is the
-recommended syntax.
+  order_by => { -asc => 'col' }
+
+The old scalarref syntax (i.e. order_by => \'year DESC') is still
+supported, although you are strongly encouraged to use the hashref
+syntax as outlined above.
 
 =head2 columns
 
@@ -2750,19 +3012,19 @@ For example:
     }
   );
 
-You need to use the relationship (not the table) name in  conditions, 
-because they are aliased as such. The current table is aliased as "me", so 
+You need to use the relationship (not the table) name in  conditions,
+because they are aliased as such. The current table is aliased as "me", so
 you need to use me.column_name in order to avoid ambiguity. For example:
 
-  # Get CDs from 1984 with a 'Foo' track 
+  # Get CDs from 1984 with a 'Foo' track
   my $rs = $schema->resultset('CD')->search(
-    { 
+    {
       'me.year' => 1984,
       'tracks.name' => 'Foo'
     },
     { join => 'tracks' }
   );
-  
+
 If the same join is supplied twice, it will be aliased to <rel>_2 (and
 similarly for a third time). For e.g.
 
@@ -2815,12 +3077,12 @@ C<cd> or C<artist> relationships, which saves us two SQL statements in this
 case.
 
 Simple prefetches will be joined automatically, so there is no need
-for a C<join> attribute in the above search. 
+for a C<join> attribute in the above search.
 
 C<prefetch> can be used with the following relationship types: C<belongs_to>,
 C<has_one> (or if you're using C<add_relationship>, any relationship declared
 with an accessor type of 'single' or 'filter'). A more complex example that
-prefetches an artists cds, the tracks on those cds, and the tags associted 
+prefetches an artists cds, the tracks on those cds, and the tags associted
 with that artist is given below (assuming many-to-many from artists to tags):
 
  my $rs = $schema->resultset('Artist')->search(
@@ -2832,7 +3094,7 @@ with that artist is given below (assuming many-to-many from artists to tags):
      ]
    }
  );
+
 
 B<NOTE:> If you specify a C<prefetch> attribute, the C<join> and C<select>
 attributes will be ignored.
@@ -2851,6 +3113,10 @@ on it.
 
 If L<rows> attribute is not specified it defualts to 10 rows per page.
 
+When you have a paged resultset, L</count> will only return the number
+of rows in the page. To get the total, use the L</pager> and call
+C<total_entries> on it.
+
 =head2 rows
 
 =over 4
@@ -3080,12 +3346,12 @@ searches - you have been warned.
     $table = $rs->result_source->name;
     $latest = $rs->search (
         undef,
-        { from => \ " 
-            (SELECT e1.* FROM $table e1 
-                JOIN $table e2 
-                    ON e1.location = e2.location 
-                    AND e1.sequence < e2.sequence 
-                WHERE e2.sequence is NULL 
+        { from => \ "
+            (SELECT e1.* FROM $table e1
+                JOIN $table e2
+                    ON e1.location = e2.location
+                    AND e1.sequence < e2.sequence
+                WHERE e2.sequence is NULL
             ) me",
         },
     );
index 3248ecb..3ed9342 100644 (file)
@@ -36,25 +36,36 @@ passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
 sub new {
   my ($class, $rs, $column) = @_;
   $class = ref $class if ref $class;
+
+  $rs->throw_exception("column must be supplied") unless $column;
+
   my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
-  my $attrs = $new_parent_rs->_resolved_attrs;
-  $new_parent_rs->{attrs}->{$_} = undef for qw(prefetch include_columns +select +as); # prefetch, include_columns, +select, +as cause additional columns to be fetched
+
+  # prefetch causes additional columns to be fetched, but we can not just make a new
+  # rs via the _resolved_attrs trick - we need to retain the separation between
+  # +select/+as and select/as
+  for my $attr (qw/prefetch collapse/) {
+    for (qw/attrs _attrs/) {
+      delete $new_parent_rs->{$_}{$attr} if ref $new_parent_rs->{$_};
+    }
+  }
 
   # If $column can be found in the 'as' list of the parent resultset, use the
   # corresponding element of its 'select' list (to keep any custom column
   # definition set up with 'select' or '+select' attrs), otherwise use $column
   # (to create a new column definition on-the-fly).
+  my $attrs = $new_parent_rs->_resolved_attrs;
+
   my $as_list = $attrs->{as} || [];
   my $select_list = $attrs->{select} || [];
   my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
   my $select = defined $as_index ? $select_list->[$as_index] : $column;
 
   my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
-  $new->throw_exception("column must be supplied") unless $column;
   return $new;
 }
 
-=head2 as_query
+=head2 as_query (EXPERIMENTAL)
 
 =over 4
 
@@ -68,9 +79,11 @@ Returns the SQL query and bind vars associated with the invocant.
 
 This is generally used as the RHS for a subquery.
 
+B<NOTE>: This feature is still experimental.
+
 =cut
 
-sub as_query { return shift->_resultset->as_query }
+sub as_query { return shift->_resultset->as_query(@_) }
 
 =head2 next
 
index 0d49c00..4a6eaa8 100644 (file)
@@ -125,8 +125,12 @@ L<DBIx::Class::Schema/deploy>.
 =item default_value
 
 Set this to the default value which will be inserted into a column
-by the database. Can contain either a value or a function. This is
-currently only used by L<DBIx::Class::Schema/deploy>.
+by the database. Can contain either a value or a function (use a
+reference to a scalar e.g. C<\'now()'> if you want a function). This
+is currently only used by L<DBIx::Class::Schema/deploy>.
+
+See the note on L<DBIx::Class::Row/new> for more information about possible
+issues related to db-side default values.
 
 =item sequence
 
@@ -837,7 +841,7 @@ relationship.
 =back
 
 Throws an exception if the condition is improperly supplied, or cannot
-be resolved using L</resolve_join>.
+be resolved.
 
 =cut
 
@@ -877,7 +881,7 @@ sub add_relationship {
   }
   return unless $f_source; # Can't test rel without f_source
 
-  eval { $self->resolve_join($rel, 'me') };
+  eval { $self->_resolve_join($rel, 'me') };
 
   if ($@) { # If the resolve failed, back out and re-throw the error
     delete $rels{$rel}; #
@@ -1011,29 +1015,22 @@ sub reverse_relationship_info {
       my @other_cond = keys(%$othercond);
       my @other_refkeys = map {/^\w+\.(\w+)$/} @other_cond;
       my @other_keys = map {$othercond->{$_} =~ /^\w+\.(\w+)$/} @other_cond;
-      next if (!$self->compare_relationship_keys(\@refkeys, \@other_keys) ||
-               !$self->compare_relationship_keys(\@other_refkeys, \@keys));
+      next if (!$self->_compare_relationship_keys(\@refkeys, \@other_keys) ||
+               !$self->_compare_relationship_keys(\@other_refkeys, \@keys));
       $ret->{$otherrel} =  $otherrel_info;
     }
   }
   return $ret;
 }
 
-=head2 compare_relationship_keys
-
-=over 4
-
-=item Arguments: \@keys1, \@keys2
-
-=item Return value: 1/0 (true/false)
-
-=back
-
-Returns true if both sets of keynames are the same, false otherwise.
-
-=cut
-
 sub compare_relationship_keys {
+  carp 'compare_relationship_keys is a private method, stop calling it';
+  my $self = shift;
+  $self->_compare_relationship_keys (@_);
+}
+
+# Returns true if both sets of keynames are the same, false otherwise.
+sub _compare_relationship_keys {
   my ($self, $keys1, $keys2) = @_;
 
   # Make sure every keys1 is in keys2
@@ -1066,44 +1063,54 @@ sub compare_relationship_keys {
   return $found;
 }
 
-=head2 resolve_join
-
-=over 4
-
-=item Arguments: $relation
+sub resolve_join {
+  carp 'resolve_join is a private method, stop calling it';
+  my $self = shift;
+  $self->_resolve_join (@_);
+}
 
-=item Return value: Join condition arrayref
+# Returns the {from} structure used to express JOIN conditions
+sub _resolve_join {
+  my ($self, $join, $alias, $seen, $force_left, $jpath) = @_;
 
-=back
+  # we need a supplied one, because we do in-place modifications, no returns
+  $self->throw_exception ('You must supply a seen hashref as the 3rd argument to _resolve_join')
+    unless $seen;
 
-Returns the join structure required for the related result source.
+  $force_left ||= { force => 0 };
 
-=cut
+  # This isn't quite right, we should actually dive into $seen and reconstruct
+  # the entire path (the reference entry point would be the join conditional
+  # with depth == current_depth - 1. At this point however nothing depends on
+  # having the entire path, transcending related_resultset, so just leave it
+  # as is, hairy enough already.
+  $jpath ||= [];  
 
-sub resolve_join {
-  my ($self, $join, $alias, $seen, $force_left) = @_;
-  $seen ||= {};
-  $force_left ||= { force => 0 };
   if (ref $join eq 'ARRAY') {
-    return map { $self->resolve_join($_, $alias, $seen) } @$join;
+    return
+      map {
+        local $force_left->{force} = $force_left->{force};
+        $self->_resolve_join($_, $alias, $seen, $force_left, [@$jpath]);
+      } @$join;
   } elsif (ref $join eq 'HASH') {
     return
       map {
-        my $as = ($seen->{$_} ? $_.'_'.($seen->{$_}+1) : $_);
-        local $force_left->{force};
+        my $as = ($seen->{$_} ? join ('_', $_, $seen->{$_} + 1) : $_);  # the actual seen value will be incremented below
+        local $force_left->{force} = $force_left->{force};
         (
-          $self->resolve_join($_, $alias, $seen, $force_left),
-          $self->related_source($_)->resolve_join(
-            $join->{$_}, $as, $seen, $force_left
+          $self->_resolve_join($_, $alias, $seen, $force_left, [@$jpath]),
+          $self->related_source($_)->_resolve_join(
+            $join->{$_}, $as, $seen, $force_left, [@$jpath, $_]
           )
         );
       } keys %$join;
   } elsif (ref $join) {
     $self->throw_exception("No idea how to resolve join reftype ".ref $join);
   } else {
+
     my $count = ++$seen->{$join};
-    #use Data::Dumper; warn Dumper($seen);
     my $as = ($count > 1 ? "${join}_${count}" : $join);
+
     my $rel_info = $self->relationship_info($join);
     $self->throw_exception("No such relationship ${join}") unless $rel_info;
     my $type;
@@ -1114,28 +1121,25 @@ sub resolve_join {
       $force_left->{force} = 1 if lc($type) eq 'left';
     }
     return [ { $as => $self->related_source($join)->from,
-               -join_type => $type },
-             $self->resolve_condition($rel_info->{cond}, $as, $alias) ];
+               -join_type => $type,
+               -join_path => [@$jpath, $join],
+               -join_alias => $as,
+               -relation_chain_depth => $seen->{-relation_chain_depth} || 0,
+             },
+             $self->_resolve_condition($rel_info->{cond}, $as, $alias) ];
   }
 }
 
-=head2 pk_depends_on
-
-=over 4
-
-=item Arguments: $relname, $rel_data
-
-=item Return value: 1/0 (true/false)
-
-=back
-
-Determines whether a relation is dependent on an object from this source
-having already been inserted. Takes the name of the relationship and a
-hashref of columns of the related object.
-
-=cut
-
 sub pk_depends_on {
+  carp 'pk_depends_on is a private method, stop calling it';
+  my $self = shift;
+  $self->_pk_depends_on (@_);
+}
+
+# Determines whether a relation is dependent on an object from this source
+# having already been inserted. Takes the name of the relationship and a
+# hashref of columns of the related object.
+sub _pk_depends_on {
   my ($self, $relname, $rel_data) = @_;
   my $cond = $self->relationship_info($relname)->{cond};
 
@@ -1164,23 +1168,18 @@ sub pk_depends_on {
   return 1;
 }
 
-=head2 resolve_condition
-
-=over 4
-
-=item Arguments: $cond, $as, $alias|$object
-
-=back
-
-Resolves the passed condition to a concrete query fragment. If given an alias,
-returns a join condition; if given an object, inverts that object to produce
-a related conditional from that object.
-
-=cut
+sub resolve_condition {
+  carp 'resolve_condition is a private method, stop calling it';
+  my $self = shift;
+  $self->_resolve_condition (@_);
+}
 
+# Resolves the passed condition to a concrete query fragment. If given an alias,
+# returns a join condition; if given an object, inverts that object to produce
+# a related conditional from that object.
 our $UNRESOLVABLE_CONDITION = \'1 = 0';
 
-sub resolve_condition {
+sub _resolve_condition {
   my ($self, $cond, $as, $for) = @_;
   #warn %$cond;
   if (ref $cond eq 'HASH') {
@@ -1196,7 +1195,11 @@ sub resolve_condition {
         #warn "$self $k $for $v";
         unless ($for->has_column_loaded($v)) {
           if ($for->in_storage) {
-            $self->throw_exception("Column ${v} not loaded on ${for} trying to resolve relationship");
+            $self->throw_exception(
+              "Column ${v} not loaded or not passed to new() prior to insert()"
+                ." on ${for} trying to resolve relationship (maybe you forgot "
+                  ."to call ->reload_from_storage to get defaults from the db)"
+            );
           }
           return $UNRESOLVABLE_CONDITION;
         }
@@ -1217,66 +1220,18 @@ sub resolve_condition {
     }
     return \%ret;
   } elsif (ref $cond eq 'ARRAY') {
-    return [ map { $self->resolve_condition($_, $as, $for) } @$cond ];
+    return [ map { $self->_resolve_condition($_, $as, $for) } @$cond ];
   } else {
    die("Can't handle this yet :(");
   }
 }
 
-=head2 resolve_prefetch
-
-=over 4
-
-=item Arguments: hashref/arrayref/scalar
-
-=back
-
-Accepts one or more relationships for the current source and returns an
-array of column names for each of those relationships. Column names are
-prefixed relative to the current source, in accordance with where they appear
-in the supplied relationships. Examples:
-
-  my $source = $schema->resultset('Tag')->source;
-  @columns = $source->resolve_prefetch( { cd => 'artist' } );
-
-  # @columns =
-  #(
-  #  'cd.cdid',
-  #  'cd.artist',
-  #  'cd.title',
-  #  'cd.year',
-  #  'cd.artist.artistid',
-  #  'cd.artist.name'
-  #)
-
-  @columns = $source->resolve_prefetch( qw[/ cd /] );
-
-  # @columns =
-  #(
-  #   'cd.cdid',
-  #   'cd.artist',
-  #   'cd.title',
-  #   'cd.year'
-  #)
-
-  $source = $schema->resultset('CD')->source;
-  @columns = $source->resolve_prefetch( qw[/ artist producer /] );
-
-  # @columns =
-  #(
-  #  'artist.artistid',
-  #  'artist.name',
-  #  'producer.producerid',
-  #  'producer.name'
-  #)
-
-=cut
-
+# Legacy code, needs to go entirely away (fully replaced by _resolve_prefetch)
 sub resolve_prefetch {
+  carp 'resolve_prefetch is a private method, stop calling it';
+
   my ($self, $pre, $alias, $seen, $order, $collapse) = @_;
   $seen ||= {};
-  #$alias ||= $self->name;
-  #warn $alias, Dumper $pre;
   if( ref $pre eq 'ARRAY' ) {
     return
       map { $self->resolve_prefetch( $_, $alias, $seen, $order, $collapse ) }
@@ -1289,7 +1244,6 @@ sub resolve_prefetch {
       $self->related_source($_)->resolve_prefetch(
                $pre->{$_}, "${alias}.$_", $seen, $order, $collapse)
     } keys %$pre;
-    #die Dumper \@ret;
     return @ret;
   }
   elsif( ref $pre ) {
@@ -1342,8 +1296,92 @@ sub resolve_prefetch {
 
     return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
       $rel_source->columns;
-    #warn $alias, Dumper (\@ret);
-    #return @ret;
+  }
+}
+
+# Accepts one or more relationships for the current source and returns an
+# array of column names for each of those relationships. Column names are
+# prefixed relative to the current source, in accordance with where they appear
+# in the supplied relationships. Needs an alias_map generated by
+# $rs->_joinpath_aliases
+
+sub _resolve_prefetch {
+  my ($self, $pre, $alias, $alias_map, $order, $collapse, $pref_path) = @_;
+  $pref_path ||= [];
+
+  if( ref $pre eq 'ARRAY' ) {
+    return
+      map { $self->_resolve_prefetch( $_, $alias, $alias_map, $order, $collapse, [ @$pref_path ] ) }
+        @$pre;
+  }
+  elsif( ref $pre eq 'HASH' ) {
+    my @ret =
+    map {
+      $self->_resolve_prefetch($_, $alias, $alias_map, $order, $collapse, [ @$pref_path ] ),
+      $self->related_source($_)->_resolve_prefetch(
+               $pre->{$_}, "${alias}.$_", $alias_map, $order, $collapse, [ @$pref_path, $_] )
+    } keys %$pre;
+    return @ret;
+  }
+  elsif( ref $pre ) {
+    $self->throw_exception(
+      "don't know how to resolve prefetch reftype ".ref($pre));
+  }
+  else {
+
+    my $p = $alias_map;
+    $p = $p->{$_} for (@$pref_path, $pre);
+
+    $self->throw_exception (
+      "Unable to resolve prefetch $pre - join alias map does not contain an entry for path "
+      . join (' -> ', @$pref_path, $pre)
+    ) if (ref $p->{-join_aliases} ne 'ARRAY' or not @{$p->{-join_aliases}} );
+    
+    my $as = shift @{$p->{-join_aliases}};
+
+    my $rel_info = $self->relationship_info( $pre );
+    $self->throw_exception( $self->name . " has no such relationship '$pre'" )
+      unless $rel_info;
+    my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : '');
+    my $rel_source = $self->related_source($pre);
+
+    if (exists $rel_info->{attrs}{accessor}
+         && $rel_info->{attrs}{accessor} eq 'multi') {
+      $self->throw_exception(
+        "Can't prefetch has_many ${pre} (join cond too complex)")
+        unless ref($rel_info->{cond}) eq 'HASH';
+      my $dots = @{[$as_prefix =~ m/\./g]} + 1; # +1 to match the ".${as_prefix}"
+      if (my ($fail) = grep { @{[$_ =~ m/\./g]} == $dots }
+                         keys %{$collapse}) {
+        my ($last) = ($fail =~ /([^\.]+)$/);
+        carp (
+          "Prefetching multiple has_many rels ${last} and ${pre} "
+          .(length($as_prefix)
+            ? "at the same level (${as_prefix}) "
+            : "at top level "
+          )
+          . 'will currently disrupt both the functionality of $rs->count(), '
+          . 'and the amount of objects retrievable via $rs->next(). '
+          . 'Use at your own risk.'
+        );
+      }
+      #my @col = map { (/^self\.(.+)$/ ? ("${as_prefix}.$1") : ()); }
+      #              values %{$rel_info->{cond}};
+      $collapse->{".${as_prefix}${pre}"} = [ $rel_source->primary_columns ];
+        # action at a distance. prepending the '.' allows simpler code
+        # in ResultSet->_collapse_result
+      my @key = map { (/^foreign\.(.+)$/ ? ($1) : ()); }
+                    keys %{$rel_info->{cond}};
+      my @ord = (ref($rel_info->{attrs}{order_by}) eq 'ARRAY'
+                   ? @{$rel_info->{attrs}{order_by}}
+                   : (defined $rel_info->{attrs}{order_by}
+                       ? ($rel_info->{attrs}{order_by})
+                       : ()));
+      push(@$order, map { "${as}.$_" } (@key, @ord));
+    }
+
+    return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
+      $rel_source->columns;
   }
 }
 
index f1ab5f1..0bcb0fc 100644 (file)
@@ -46,7 +46,7 @@ L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
 Deploying the view does B<not> translate it between different database
 syntaxes, so be careful what you write in your view SQL.
 
-Virtual views (L</is_virtual> unset or false), are assumed to not
+Virtual views (L</is_virtual> true), are assumed to not
 exist in your database as a real view. The L</view_definition> in this
 case replaces the view name in a FROM clause in a subselect.
 
@@ -54,13 +54,13 @@ case replaces the view name in a FROM clause in a subselect.
 
 =over
 
-=item is_virtual set to true
+=item is_virtual set to false
 
   $schema->resultset('Year2000CDs')->all();
 
   SELECT cdid, artist, title FROM year2000cds me
 
-=item is_virtual set to false
+=item is_virtual set to true
 
   $schema->resultset('Year2000CDs')->all();
 
@@ -115,6 +115,8 @@ Guillermo Roditi E<lt>groditi@cpan.orgE<gt>
 
 Jess Robinson <castaway@desert-island.me.uk>
 
+Wallace Reis <wreis@cpan.org>
+
 =head1 LICENSE
 
 You may distribute this code under the same terms as Perl itself.
index 9354318..7531954 100644 (file)
@@ -20,7 +20,7 @@ our $thaw_schema;
 
 =head1 NAME
 
-DBIx::Class::ResultSourceHandle
+DBIx::Class::ResultSourceHandle - Decouple Rows/ResultSets objects from their Source objects
 
 =head1 DESCRIPTION
 
@@ -87,7 +87,7 @@ sub STORABLE_freeze {
 
 Thaws frozen handle. Resets the internal schema reference to the package
 variable C<$thaw_schema>. The recomened way of setting this is to use 
-C<$schema->thaw($ice)> which handles this for you.
+C<< $schema->thaw($ice) >> which handles this for you.
 
 =cut
 
index b004d24..c08c00d 100644 (file)
@@ -8,6 +8,10 @@ use Carp::Clan qw/^DBIx::Class/;
 use Scalar::Util ();
 use Scope::Guard;
 
+###
+### Internal method
+### Do not use
+###
 BEGIN {
   *MULTICREATE_DEBUG =
     $ENV{DBIC_MULTICREATE_DEBUG}
@@ -73,6 +77,23 @@ passed objects.
 
 For a more involved explanation, see L<DBIx::Class::ResultSet/create>.
 
+Please note that if a value is not passed to new, no value will be sent
+in the SQL INSERT call, and the column will therefore assume whatever
+default value was specified in your database. While DBIC will retrieve the
+value of autoincrement columns, it will never make an explicit database
+trip to retrieve default values assigned by the RDBMS. You can explicitly
+request that all values be fetched back from the database by calling
+L</discard_changes>, or you can supply an explicit C<undef> to columns
+with NULL as the default, and save yourself a SELECT.
+
+ CAVEAT:
+
+ The behavior described above will backfire if you use a foreign key column
+ with a database-defined default. If you call the relationship accessor on
+ an object that doesn't have a set value for the FK column, DBIC will throw
+ an exception, as it has no way of knowing the PK of the related object (if
+ there is one).
+
 =cut
 
 ## It needs to store the new objects somewhere, and call insert on that list later when insert is called on this object. We may need an accessor for these so the user can retrieve them, if just doing ->new().
@@ -91,7 +112,7 @@ sub __new_related_find_or_new_helper {
                 ->resultset
                 ->new_result($data);
   }
-  if ($self->result_source->pk_depends_on($relname, $data)) {
+  if ($self->result_source->_pk_depends_on($relname, $data)) {
     MULTICREATE_DEBUG and warn "MC $self constructing $relname via find_or_new";
     return $self->result_source
                 ->related_source($relname)
@@ -111,7 +132,7 @@ sub __their_pk_needs_us { # this should maybe be in resultsource.
   foreach my $key (keys %$reverse) {
     # if their primary key depends on us, then we have to
     # just create a result and we'll fill it out afterwards
-    return 1 if $rel_source->pk_depends_on($key, $us);
+    return 1 if $rel_source->_pk_depends_on($key, $us);
   }
   return 0;
 }
@@ -283,7 +304,7 @@ sub insert {
       next REL unless (Scalar::Util::blessed($rel_obj)
                        && $rel_obj->isa('DBIx::Class::Row'));
 
-      next REL unless $source->pk_depends_on(
+      next REL unless $source->_pk_depends_on(
                         $relname, { $rel_obj->get_columns }
                       );
 
@@ -326,7 +347,6 @@ sub insert {
     $self->throw_exception( "Can't get last insert id" )
       unless (@ids == @auto_pri);
     $self->store_column($auto_pri[$_] => $ids[$_]) for 0 .. $#ids;
-#use Data::Dumper; warn Dumper($self);
   }
 
 
@@ -860,10 +880,10 @@ Inserts a new row into the database, as a copy of the original
 object. If a hashref of replacement data is supplied, these will take
 precedence over data in the original.
 
-If the row has related objects in a
-L<DBIx::Class::Relationship/has_many> then those objects may be copied
-too depending on the L<cascade_copy|DBIx::Class::Relationship>
-relationship attribute.
+Relationships will be followed by the copy procedure B<only> if the
+relationship specifes a true value for its
+L<cascade_copy|DBIx::Class::Relationship::Base> attribute. C<cascade_copy>
+is set by default on C<has_many> relationships and unset on all others.
 
 =cut
 
@@ -893,7 +913,7 @@ sub copy {
 
     next unless $rel_info->{attrs}{cascade_copy};
   
-    my $resolved = $self->result_source->resolve_condition(
+    my $resolved = $self->result_source->_resolve_condition(
       $rel_info->{cond}, $rel, $new
     );
 
@@ -960,6 +980,9 @@ for example to rebless the result into a different class.
 Reblessing can also be done more easily by setting C<result_class> in
 your Result class. See L<DBIx::Class::ResultSource/result_class>.
 
+Different types of results can also be created from a particular
+L<DBIx::Class::ResultSet>, see L<DBIx::Class::ResultSet/result_class>.
+
 =cut
 
 sub inflate_result {
@@ -1005,7 +1028,6 @@ sub inflate_result {
         $fetched = $pre_source->result_class->inflate_result(
                       $pre_source, @{$pre_val});
       }
-      $new->related_resultset($pre)->set_cache([ $fetched ]);
       my $accessor = $source->relationship_info($pre)->{attrs}{accessor};
       $class->throw_exception("No accessor for prefetched $pre")
        unless defined $accessor;
@@ -1016,6 +1038,7 @@ sub inflate_result {
       } else {
        $class->throw_exception("Prefetch not supported with accessor '$accessor'");
       }
+      $new->related_resultset($pre)->set_cache([ $fetched ]);
     }
   }
   return $new;
diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm
new file mode 100644 (file)
index 0000000..90d41b2
--- /dev/null
@@ -0,0 +1,468 @@
+package # Hide from PAUSE
+  DBIx::Class::SQLAHacks;
+
+use base qw/SQL::Abstract::Limit/;
+use strict;
+use warnings;
+use Carp::Clan qw/^DBIx::Class/;
+
+sub new {
+  my $self = shift->SUPER::new(@_);
+
+  # This prevents the caching of $dbh in S::A::L, I believe
+  # If limit_dialect is a ref (like a $dbh), go ahead and replace
+  #   it with what it resolves to:
+  $self->{limit_dialect} = $self->_find_syntax($self->{limit_dialect})
+    if ref $self->{limit_dialect};
+
+  $self;
+}
+
+
+# Some databases (sqlite) do not handle multiple parenthesis
+# around in/between arguments. A tentative x IN ( ( 1, 2 ,3) )
+# is interpreted as x IN 1 or something similar.
+#
+# Since we currently do not have access to the SQLA AST, resort
+# to barbaric mutilation of any SQL supplied in literal form
+
+sub _strip_outer_paren {
+  my ($self, $arg) = @_;
+
+  return $self->_SWITCH_refkind ($arg, {
+    ARRAYREFREF => sub {
+      $$arg->[0] = __strip_outer_paren ($$arg->[0]);
+      return $arg;
+    },
+    SCALARREF => sub {
+      return \__strip_outer_paren( $$arg );
+    },
+    FALLBACK => sub {
+      return $arg
+    },
+  });
+}
+
+sub __strip_outer_paren {
+  my $sql = shift;
+
+  if ($sql and not ref $sql) {
+    while ($sql =~ /^ \s* \( (.*) \) \s* $/x ) {
+      $sql = $1;
+    }
+  }
+
+  return $sql;
+}
+
+sub _where_field_IN {
+  my ($self, $lhs, $op, $rhs) = @_;
+  $rhs = $self->_strip_outer_paren ($rhs);
+  return $self->SUPER::_where_field_IN ($lhs, $op, $rhs);
+}
+
+sub _where_field_BETWEEN {
+  my ($self, $lhs, $op, $rhs) = @_;
+  $rhs = $self->_strip_outer_paren ($rhs);
+  return $self->SUPER::_where_field_BETWEEN ($lhs, $op, $rhs);
+}
+
+
+
+# DB2 is the only remaining DB using this. Even though we are not sure if
+# RowNumberOver is still needed here (should be part of SQLA) leave the 
+# code in place
+sub _RowNumberOver {
+  my ($self, $sql, $order, $rows, $offset ) = @_;
+
+  $offset += 1;
+  my $last = $rows + $offset - 1;
+  my ( $order_by ) = $self->_order_by( $order );
+
+  $sql = <<"SQL";
+SELECT * FROM
+(
+   SELECT Q1.*, ROW_NUMBER() OVER( ) AS ROW_NUM FROM (
+      $sql
+      $order_by
+   ) Q1
+) Q2
+WHERE ROW_NUM BETWEEN $offset AND $last
+
+SQL
+
+  return $sql;
+}
+
+
+# While we're at it, this should make LIMIT queries more efficient,
+#  without digging into things too deeply
+use Scalar::Util 'blessed';
+sub _find_syntax {
+  my ($self, $syntax) = @_;
+  
+  # DB2 is the only remaining DB using this. Even though we are not sure if
+  # RowNumberOver is still needed here (should be part of SQLA) leave the 
+  # code in place
+  my $dbhname = blessed($syntax) ? $syntax->{Driver}{Name} : $syntax;
+  if(ref($self) && $dbhname) {
+    if ($dbhname eq 'DB2') {
+      return 'RowNumberOver';
+    }
+  }
+  
+  $self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax);
+}
+
+sub select {
+  my ($self, $table, $fields, $where, $order, @rest) = @_;
+  local $self->{having_bind} = [];
+  local $self->{from_bind} = [];
+
+  if (ref $table eq 'SCALAR') {
+    $table = $$table;
+  }
+  elsif (not ref $table) {
+    $table = $self->_quote($table);
+  }
+  local $self->{rownum_hack_count} = 1
+    if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum');
+  @rest = (-1) unless defined $rest[0];
+  croak "LIMIT 0 Does Not Compute" if $rest[0] == 0;
+    # and anyway, SQL::Abstract::Limit will cause a barf if we don't first
+  my ($sql, @where_bind) = $self->SUPER::select(
+    $table, $self->_recurse_fields($fields), $where, $order, @rest
+  );
+  $sql .= 
+    $self->{for} ?
+    (
+      $self->{for} eq 'update' ? ' FOR UPDATE' :
+      $self->{for} eq 'shared' ? ' FOR SHARE'  :
+      ''
+    ) :
+    ''
+  ;
+  return wantarray ? ($sql, @{$self->{from_bind}}, @where_bind, @{$self->{having_bind}}) : $sql;
+}
+
+sub insert {
+  my $self = shift;
+  my $table = shift;
+  $table = $self->_quote($table) unless ref($table);
+  $self->SUPER::insert($table, @_);
+}
+
+sub update {
+  my $self = shift;
+  my $table = shift;
+  $table = $self->_quote($table) unless ref($table);
+  $self->SUPER::update($table, @_);
+}
+
+sub delete {
+  my $self = shift;
+  my $table = shift;
+  $table = $self->_quote($table) unless ref($table);
+  $self->SUPER::delete($table, @_);
+}
+
+sub _emulate_limit {
+  my $self = shift;
+  if ($_[3] == -1) {
+    return $_[1].$self->_order_by($_[2]);
+  } else {
+    return $self->SUPER::_emulate_limit(@_);
+  }
+}
+
+sub _recurse_fields {
+  my ($self, $fields, $params) = @_;
+  my $ref = ref $fields;
+  return $self->_quote($fields) unless $ref;
+  return $$fields if $ref eq 'SCALAR';
+
+  if ($ref eq 'ARRAY') {
+    return join(', ', map {
+      $self->_recurse_fields($_)
+        .(exists $self->{rownum_hack_count} && !($params && $params->{no_rownum_hack})
+          ? ' AS col'.$self->{rownum_hack_count}++
+          : '')
+      } @$fields);
+  } elsif ($ref eq 'HASH') {
+    foreach my $func (keys %$fields) {
+      if ($func eq 'distinct') {
+        my $_fields = $fields->{$func};
+        if (ref $_fields eq 'ARRAY' && @{$_fields} > 1) {
+          croak "Unsupported syntax, please use " . 
+              "{ group_by => [ qw/" . (join ' ', @$_fields) . "/ ] }" .
+              " or " .
+              "{ select => [ qw/" . (join ' ', @$_fields) . "/ ], distinct => 1 }";
+        }
+        else {
+          $_fields = @{$_fields}[0] if ref $_fields eq 'ARRAY';
+          carp "This syntax will be deprecated in 09, please use " . 
+               "{ group_by => '${_fields}' }" . 
+               " or " .
+               "{ select => '${_fields}', distinct => 1 }";
+        }
+      }
+      
+      return $self->_sqlcase($func)
+        .'( '.$self->_recurse_fields($fields->{$func}).' )';
+    }
+  }
+  # Is the second check absolutely necessary?
+  elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) {
+    return $self->_fold_sqlbind( $fields );
+  }
+  else {
+    croak($ref . qq{ unexpected in _recurse_fields()})
+  }
+}
+
+sub _order_by {
+  my $self = shift;
+  my $ret = '';
+  my @extra;
+  if (ref $_[0] eq 'HASH') {
+    if (defined $_[0]->{group_by}) {
+      $ret = $self->_sqlcase(' group by ')
+        .$self->_recurse_fields($_[0]->{group_by}, { no_rownum_hack => 1 });
+    }
+    if (defined $_[0]->{having}) {
+      my $frag;
+      ($frag, @extra) = $self->_recurse_where($_[0]->{having});
+      push(@{$self->{having_bind}}, @extra);
+      $ret .= $self->_sqlcase(' having ').$frag;
+    }
+    if (defined $_[0]->{order_by}) {
+      $ret .= $self->_order_by($_[0]->{order_by});
+    }
+    if (grep { $_ =~ /^-(desc|asc)/i } keys %{$_[0]}) {
+      return $self->SUPER::_order_by($_[0]);
+    }
+  } elsif (ref $_[0] eq 'SCALAR') {
+    $ret = $self->_sqlcase(' order by ').${ $_[0] };
+  } elsif (ref $_[0] eq 'ARRAY' && @{$_[0]}) {
+    my @order = @{+shift};
+    $ret = $self->_sqlcase(' order by ')
+          .join(', ', map {
+                        my $r = $self->_order_by($_, @_);
+                        $r =~ s/^ ?ORDER BY //i;
+                        $r;
+                      } @order);
+  } else {
+    $ret = $self->SUPER::_order_by(@_);
+  }
+  return $ret;
+}
+
+sub _order_directions {
+  my ($self, $order) = @_;
+  $order = $order->{order_by} if ref $order eq 'HASH';
+  if (ref $order eq 'HASH') {
+    $order = [$self->_order_directions_hash($order)];
+  } elsif (ref $order eq 'ARRAY') {
+    $order = [map {
+      if (ref $_ eq 'HASH') {
+        $self->_order_directions_hash($_);
+      } else {
+        $_;
+      }
+    } @{ $order }];
+  }
+  return $self->SUPER::_order_directions($order);
+}
+
+sub _order_directions_hash {
+  my ($self, $order) = @_;
+  my @new_order;
+  foreach my $key (keys %{ $order }) {
+    if ($key =~ /^-(desc|asc)/i ) {
+      my $direction = $1;
+      my $type = ref $order->{ $key };
+      if ($type eq 'ARRAY') {
+        push @new_order, map( "$_ $direction", @{ $order->{ $key } } );
+      } elsif (!$type) {
+        push @new_order, "$order->{$key} $direction";
+      } else {
+        croak "hash order_by can only contain Scalar or Array, not $type";
+      }
+    } else {
+      croak "$key is not a valid direction, use -asc or -desc";
+    }
+  }
+  return @new_order;
+}
+
+sub _table {
+  my ($self, $from) = @_;
+  if (ref $from eq 'ARRAY') {
+    return $self->_recurse_from(@$from);
+  } elsif (ref $from eq 'HASH') {
+    return $self->_make_as($from);
+  } else {
+    return $from; # would love to quote here but _table ends up getting called
+                  # twice during an ->select without a limit clause due to
+                  # the way S::A::Limit->select works. should maybe consider
+                  # bypassing this and doing S::A::select($self, ...) in
+                  # our select method above. meantime, quoting shims have
+                  # been added to select/insert/update/delete here
+  }
+}
+
+sub _recurse_from {
+  my ($self, $from, @join) = @_;
+  my @sqlf;
+  push(@sqlf, $self->_make_as($from));
+  foreach my $j (@join) {
+    my ($to, $on) = @$j;
+
+    # check whether a join type exists
+    my $join_clause = '';
+    my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to;
+    if (ref($to_jt) eq 'HASH' and exists($to_jt->{-join_type})) {
+      $join_clause = ' '.uc($to_jt->{-join_type}).' JOIN ';
+    } else {
+      $join_clause = ' JOIN ';
+    }
+    push(@sqlf, $join_clause);
+
+    if (ref $to eq 'ARRAY') {
+      push(@sqlf, '(', $self->_recurse_from(@$to), ')');
+    } else {
+      push(@sqlf, $self->_make_as($to));
+    }
+    push(@sqlf, ' ON ', $self->_join_condition($on));
+  }
+  return join('', @sqlf);
+}
+
+sub _fold_sqlbind {
+  my ($self, $sqlbind) = @_;
+  my $sql = shift @$$sqlbind;
+  push @{$self->{from_bind}}, @$$sqlbind;
+  return $sql;
+}
+
+sub _make_as {
+  my ($self, $from) = @_;
+  return join(' ', map { (ref $_ eq 'SCALAR' ? $$_
+                        : ref $_ eq 'REF'    ? $self->_fold_sqlbind($_)
+                        : $self->_quote($_))
+                       } reverse each %{$self->_skip_options($from)});
+}
+
+sub _skip_options {
+  my ($self, $hash) = @_;
+  my $clean_hash = {};
+  $clean_hash->{$_} = $hash->{$_}
+    for grep {!/^-/} keys %$hash;
+  return $clean_hash;
+}
+
+sub _join_condition {
+  my ($self, $cond) = @_;
+  if (ref $cond eq 'HASH') {
+    my %j;
+    for (keys %$cond) {
+      my $v = $cond->{$_};
+      if (ref $v) {
+        croak (ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'})
+            if ref($v) ne 'SCALAR';
+        $j{$_} = $v;
+      }
+      else {
+        my $x = '= '.$self->_quote($v); $j{$_} = \$x;
+      }
+    };
+    return scalar($self->_recurse_where(\%j));
+  } elsif (ref $cond eq 'ARRAY') {
+    return join(' OR ', map { $self->_join_condition($_) } @$cond);
+  } else {
+    die "Can't handle this yet!";
+  }
+}
+
+sub _quote {
+  my ($self, $label) = @_;
+  return '' unless defined $label;
+  return "*" if $label eq '*';
+  return $label unless $self->{quote_char};
+  if(ref $self->{quote_char} eq "ARRAY"){
+    return $self->{quote_char}->[0] . $label . $self->{quote_char}->[1]
+      if !defined $self->{name_sep};
+    my $sep = $self->{name_sep};
+    return join($self->{name_sep},
+        map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1]  }
+       split(/\Q$sep\E/,$label));
+  }
+  return $self->SUPER::_quote($label);
+}
+
+sub limit_dialect {
+    my $self = shift;
+    $self->{limit_dialect} = shift if @_;
+    return $self->{limit_dialect};
+}
+
+sub quote_char {
+    my $self = shift;
+    $self->{quote_char} = shift if @_;
+    return $self->{quote_char};
+}
+
+sub name_sep {
+    my $self = shift;
+    $self->{name_sep} = shift if @_;
+    return $self->{name_sep};
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+DBIx::Class::SQLAHacks - This module is a subclass of SQL::Abstract::Limit
+and includes a number of DBIC-specific workarounds, not yet suitable for
+inclusion into SQLA proper.
+
+=head1 METHODS
+
+=head2 new
+
+Tries to determine limit dialect.
+
+=head2 select
+
+Quotes table names, handles "limit" dialects (e.g. where rownum between x and
+y), supports SELECT ... FOR UPDATE and SELECT ... FOR SHARE.
+
+=head2 insert update delete
+
+Just quotes table names.
+
+=head2 limit_dialect
+
+Specifies the dialect of used for implementing an SQL "limit" clause for
+restricting the number of query results returned.  Valid values are: RowNum.
+
+See L<DBIx::Class::Storage::DBI/connect_info> for details.
+
+=head2 name_sep
+
+Character separating quoted table names.
+
+See L<DBIx::Class::Storage::DBI/connect_info> for details.
+
+=head2 quote_char
+
+Set to an array-ref to specify separate left and right quotes for table names.
+
+See L<DBIx::Class::Storage::DBI/connect_info> for details.
+
+=cut
+
diff --git a/lib/DBIx/Class/SQLAHacks/OracleJoins.pm b/lib/DBIx/Class/SQLAHacks/OracleJoins.pm
new file mode 100644 (file)
index 0000000..f02a913
--- /dev/null
@@ -0,0 +1,171 @@
+package # Hide from PAUSE
+  DBIx::Class::SQLAHacks::OracleJoins;
+
+use base qw( DBIx::Class::SQLAHacks );
+use Carp::Clan qw/^DBIx::Class/;
+
+sub select {
+  my ($self, $table, $fields, $where, $order, @rest) = @_;
+
+  if (ref($table) eq 'ARRAY') {
+    $where = $self->_oracle_joins($where, @{ $table });
+  }
+
+  return $self->SUPER::select($table, $fields, $where, $order, @rest);
+}
+
+sub _recurse_from {
+  my ($self, $from, @join) = @_;
+
+  my @sqlf = $self->_make_as($from);
+
+  foreach my $j (@join) {
+    my ($to, $on) = @{ $j };
+
+    if (ref $to eq 'ARRAY') {
+      push (@sqlf, $self->_recurse_from(@{ $to }));
+    }
+    else {
+      push (@sqlf, $self->_make_as($to));
+    }
+  }
+
+  return join q{, }, @sqlf;
+}
+
+sub _oracle_joins {
+  my ($self, $where, $from, @join) = @_;
+  my $join_where = {};
+  $self->_recurse_oracle_joins($join_where, $from, @join);
+  if (keys %$join_where) {
+    if (!defined($where)) {
+      $where = $join_where;
+    } else {
+      if (ref($where) eq 'ARRAY') {
+        $where = { -or => $where };
+      }
+      $where = { -and => [ $join_where, $where ] };
+    }
+  }
+  return $where;
+}
+
+sub _recurse_oracle_joins {
+  my ($self, $where, $from, @join) = @_;
+
+  foreach my $j (@join) {
+    my ($to, $on) = @{ $j };
+
+    if (ref $to eq 'ARRAY') {
+      $self->_recurse_oracle_joins($where, @{ $to });
+    }
+
+    my $to_jt      = ref $to eq 'ARRAY' ? $to->[0] : $to;
+    my $left_join  = q{};
+    my $right_join = q{};
+
+    if (ref $to_jt eq 'HASH' and exists $to_jt->{-join_type}) {
+      #TODO: Support full outer joins -- this would happen much earlier in
+      #the sequence since oracle 8's full outer join syntax is best
+      #described as INSANE.
+      croak "Can't handle full outer joins in Oracle 8 yet!\n"
+        if $to_jt->{-join_type} =~ /full/i;
+
+      $left_join  = q{(+)} if $to_jt->{-join_type} =~ /left/i
+        && $to_jt->{-join_type} !~ /inner/i;
+
+      $right_join = q{(+)} if $to_jt->{-join_type} =~ /right/i
+        && $to_jt->{-join_type} !~ /inner/i;
+    }
+
+    foreach my $lhs (keys %{ $on }) {
+      $where->{$lhs . $left_join} = \"= $on->{ $lhs }$right_join";
+    }
+  }
+}
+
+1;
+
+=pod
+
+=head1 NAME
+
+DBIx::Class::SQLAHacks::OracleJoins - Pre-ANSI Joins-via-Where-Clause Syntax
+
+=head1 PURPOSE
+
+This module was originally written to support Oracle < 9i where ANSI joins
+weren't supported at all, but became the module for Oracle >= 8 because
+Oracle's optimising of ANSI joins is horrible.  (See:
+http://scsys.co.uk:8001/7495)
+
+=head1 SYNOPSIS
+
+Not intended for use directly; used as the sql_maker_class for schemas and components.
+
+=head1 DESCRIPTION
+
+Implements pre-ANSI joins specified in the where clause.  Instead of:
+
+    SELECT x FROM y JOIN z ON y.id = z.id
+
+It will write:
+
+    SELECT x FROM y, z WHERE y.id = z.id
+
+It should properly support left joins, and right joins.  Full outer joins are
+not possible due to the fact that Oracle requires the entire query be written
+to union the results of a left and right join, and by the time this module is
+called to create the where query and table definition part of the sql query,
+it's already too late.
+
+=head1 METHODS
+
+=over
+
+=item select ($\@$;$$@)
+
+Replaces DBIx::Class::SQLAHacks's select() method, which calls _oracle_joins()
+to modify the column and table list before calling SUPER::select().
+
+=item _recurse_from ($$\@)
+
+Recursive subroutine that builds the table list.
+
+=item _oracle_joins ($$$@)
+
+Creates the left/right relationship in the where query.
+
+=back
+
+=head1 BUGS
+
+Does not support full outer joins.
+Probably lots more.
+
+=head1 SEE ALSO
+
+=over
+
+=item L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> - Storage class using this
+
+=item L<DBIx::Class::SQLAHacks> - Parent module
+
+=item L<DBIx::Class> - Duh
+
+=back
+
+=head1 AUTHOR
+
+Justin Wheeler C<< <jwheeler@datademons.com> >>
+
+=head1 CONTRIBUTORS
+
+David Jack Olrik C<< <djo@cpan.org> >>
+
+=head1 LICENSE
+
+This module is licensed under the same terms as Perl itself.
+
+=cut
+
index 1c75a50..63bff46 100644 (file)
@@ -157,6 +157,21 @@ sub _expand_relative_name {
   return $name;
 }
 
+# Finds all modules in the supplied namespace, or if omitted in the
+# namespace of $class. Untaints all findings as they can be assumed
+# to be safe
+sub _findallmod {
+  my $proto = shift;
+  my $ns = shift || ref $proto || $proto;
+
+  my @mods = Module::Find::findallmod($ns);
+
+  # try to untaint module names. mods where this fails
+  # are left alone so we don't have to change the old behavior
+  no locale; # localized \w doesn't untaint expression
+  return map { $_ =~ m/^( (?:\w+::)* \w+ )$/x ? $1 : $_ } @mods;
+}
+
 # returns a hash of $shortname => $fullname for every package
 #  found in the given namespaces ($shortname is with the $fullname's
 #  namespace stripped off)
@@ -168,7 +183,7 @@ sub _map_namespaces {
     push(
       @results_hash,
       map { (substr($_, length "${namespace}::"), $_) }
-      Module::Find::findallmod($namespace)
+      $class->_findallmod($namespace)
     );
   }
 
@@ -220,7 +235,7 @@ sub load_namespaces {
       
       if($rs_set && $rs_set ne 'DBIx::Class::ResultSet') {
         if($rs_class && $rs_class ne $rs_set) {
-          warn "We found ResultSet class '$rs_class' for '$result', but it seems "
+          carp "We found ResultSet class '$rs_class' for '$result', but it seems "
              . "that you had already set '$result' to use '$rs_set' instead";
         }
       }
@@ -236,7 +251,7 @@ sub load_namespaces {
   }
 
   foreach (sort keys %resultsets) {
-    warn "load_namespaces found ResultSet class $_ with no "
+    carp "load_namespaces found ResultSet class $_ with no "
       . 'corresponding Result class';
   }
 
@@ -314,7 +329,7 @@ sub load_classes {
     }
   } else {
     my @comp = map { substr $_, length "${class}::"  }
-                 Module::Find::findallmod($class);
+                 $class->_findallmod;
     $comps_for{$class} = \@comp;
   }
 
@@ -325,18 +340,11 @@ sub load_classes {
     foreach my $prefix (keys %comps_for) {
       foreach my $comp (@{$comps_for{$prefix}||[]}) {
         my $comp_class = "${prefix}::${comp}";
-        { # try to untaint module name. mods where this fails
-          # are left alone so we don't have to change the old behavior
-          no locale; # localized \w doesn't untaint expression
-          if ( $comp_class =~ m/^( (?:\w+::)* \w+ )$/x ) {
-            $comp_class = $1;
-          }
-        }
         $class->ensure_class_loaded($comp_class);
 
         my $snsub = $comp_class->can('source_name');
         if(! $snsub ) {
-          warn "Failed to load $comp_class. Can't find source_name method. Is $comp_class really a full DBIC result class? Fix it, move it elsewhere, or make your load_classes call more specific.";
+          carp "Failed to load $comp_class. Can't find source_name method. Is $comp_class really a full DBIC result class? Fix it, move it elsewhere, or make your load_classes call more specific.";
           next;
         }
         $comp = $snsub->($comp_class) || $comp;
@@ -475,6 +483,12 @@ Note that C<connect_info> expects an arrayref of arguments, but
 C<connect> does not. C<connect> wraps it's arguments in an arrayref
 before passing them to C<connect_info>.
 
+=head3 Overloading
+
+C<connect> is a convenience method. It is equivalent to calling
+$schema->clone->connection(@connectinfo). To write your own overloaded
+version, overload L</connection> instead.
+
 =cut
 
 sub connect { shift->clone->connection(@_) }
@@ -603,7 +617,7 @@ sub txn_do {
   $self->storage->txn_do(@_);
 }
 
-=head2 txn_scope_guard (EXPERIMENTAL)
+=head2 txn_scope_guard
 
 Runs C<txn_scope_guard> on the schema's storage. See 
 L<DBIx::Class::Storage/txn_scope_guard>.
@@ -752,6 +766,9 @@ Similar to L</connect> except sets the storage object and connection
 data in-place on the Schema class. You should probably be calling
 L</connect> to get a proper Schema object instead.
 
+=head3 Overloading
+
+Overload C<connection> to change the behaviour of C<connect>.
 
 =cut
 
@@ -990,7 +1007,9 @@ Attempts to deploy the schema to the current storage using L<SQL::Translator>.
 
 See L<SQL::Translator/METHODS> for a list of values for C<$sqlt_args>. The most
 common value for this would be C<< { add_drop_table => 1, } >> to have the SQL
-produced include a DROP TABLE statement for each table created.
+produced include a DROP TABLE statement for each table created. For quoting
+purposes use C<producer_options> value with C<quote_table_names> and
+C<quote_field_names>.
 
 Additionally, the DBIx::Class parser accepts a C<sources> parameter as a hash 
 ref or an array ref, containing a list of source to deploy. If present, then 
@@ -1248,24 +1267,33 @@ sub register_extra_source {
 sub _register_source {
   my ($self, $moniker, $source, $params) = @_;
 
+  my $orig_source = $source;
+
   $source = $source->new({ %$source, source_name => $moniker });
+  $source->schema($self);
+  weaken($source->{schema}) if ref($self);
+
+  my $rs_class = $source->result_class;
 
   my %reg = %{$self->source_registrations};
   $reg{$moniker} = $source;
   $self->source_registrations(\%reg);
 
-  $source->schema($self);
-  weaken($source->{schema}) if ref($self);
   return if ($params->{extra});
-
-  if ($source->result_class) {
-    my %map = %{$self->class_mappings};
-    if (exists $map{$source->result_class}) {
-      warn $source->result_class . ' already has a source, use register_extra_source for additional sources';
-    }
-    $map{$source->result_class} = $moniker;
-    $self->class_mappings(\%map);
+  return unless defined($rs_class) && $rs_class->can('result_source_instance');
+
+  my %map = %{$self->class_mappings};
+  if (
+    exists $map{$rs_class}
+      and
+    $map{$rs_class} ne $moniker
+      and
+    $rs_class->result_source_instance ne $orig_source
+  ) {
+    carp "$rs_class already has a source, use register_extra_source for additional sources";
   }
+  $map{$rs_class} = $moniker;
+  $self->class_mappings(\%map);
 }
 
 sub _unregister_source {
@@ -1322,7 +1350,7 @@ more information.
   sub compose_connection {
     my ($self, $target, @info) = @_;
 
-    warn "compose_connection deprecated as of 0.08000"
+    carp "compose_connection deprecated as of 0.08000"
       unless ($INC{"DBIx/Class/CDBICompat.pm"} || $warn++);
 
     my $base = 'DBIx::Class::ResultSetProxy';
index eb4c352..49baa57 100644 (file)
@@ -70,12 +70,12 @@ DBIx::Class::Schema::Versioned - DBIx::Class::Schema plugin for Schema upgrades
 
 =head1 SYNOPSIS
 
-  package Library::Schema;
+  package MyApp::Schema;
   use base qw/DBIx::Class::Schema/;
 
   our $VERSION = 0.001;
 
-  # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD
+  # load MyApp::Schema::CD, MyApp::Schema::Book, MyApp::Schema::DVD
   __PACKAGE__->load_classes(qw/CD Book DVD/);
 
   __PACKAGE__->load_components(qw/Schema::Versioned/);
@@ -181,8 +181,9 @@ package DBIx::Class::Schema::Versioned;
 use strict;
 use warnings;
 use base 'DBIx::Class';
+
+use Carp::Clan qw/^DBIx::Class/;
 use POSIX 'strftime';
-use Data::Dumper;
 
 __PACKAGE__->mk_classdata('_filedata');
 __PACKAGE__->mk_classdata('upgrade_directory');
@@ -226,7 +227,7 @@ sub install
 
   # must be called on a fresh database
   if ($self->get_db_version()) {
-    warn 'Install not possible as versions table already exists in database';
+    carp 'Install not possible as versions table already exists in database';
   }
 
   # default to current version if none passed
@@ -292,13 +293,13 @@ sub upgrade
 
   # db unversioned
   unless ($db_version) {
-    warn 'Upgrade not possible as database is unversioned. Please call install first.';
+    carp 'Upgrade not possible as database is unversioned. Please call install first.';
     return;
   }
 
   # db and schema at same version. do nothing
   if ($db_version eq $self->schema_version) {
-    print "Upgrade not necessary\n";
+    carp "Upgrade not necessary\n";
     return;
   }
 
@@ -318,11 +319,11 @@ sub upgrade
   $self->create_upgrade_path({ upgrade_file => $upgrade_file });
 
   unless (-f $upgrade_file) {
-    warn "Upgrade not possible, no upgrade file found ($upgrade_file), please create one\n";
+    carp "Upgrade not possible, no upgrade file found ($upgrade_file), please create one\n";
     return;
   }
 
-  warn "\nDB version ($db_version) is lower than the schema version (".$self->schema_version."). Attempting upgrade.\n";
+  carp "\nDB version ($db_version) is lower than the schema version (".$self->schema_version."). Attempting upgrade.\n";
 
   # backup if necessary then apply upgrade
   $self->_filedata($self->_read_sql_file($upgrade_file));
@@ -392,7 +393,7 @@ differently.
 sub apply_statement {
     my ($self, $statement) = @_;
 
-    $self->storage->dbh->do($_) or warn "SQL was:\n $_";
+    $self->storage->dbh->do($_) or carp "SQL was:\n $_";
 }
 
 =head2 get_db_version
@@ -491,17 +492,17 @@ sub _on_connect
 
   if($pversion eq $self->schema_version)
     {
-#         warn "This version is already installed\n";
+#         carp "This version is already installed\n";
         return 1;
     }
 
   if(!$pversion)
     {
-        warn "Your DB is currently unversioned. Please call upgrade on your schema to sync the DB.\n";
+        carp "Your DB is currently unversioned. Please call upgrade on your schema to sync the DB.\n";
         return 1;
     }
 
-  warn "Versions out of sync. This is " . $self->schema_version . 
+  carp "Versions out of sync. This is " . $self->schema_version . 
     ", your database contains version $pversion, please call upgrade on your Schema.\n";
 }
 
@@ -533,7 +534,6 @@ sub _create_db_to_schema_diff {
   $db_tr->producer($db);
   my $dbic_tr = SQL::Translator->new;
   $dbic_tr->parser('SQL::Translator::Parser::DBIx::Class');
-  $dbic_tr = $self->storage->configure_sqlt($dbic_tr, $db);
   $dbic_tr->data($self);
   $dbic_tr->producer($db);
 
@@ -565,7 +565,7 @@ sub _create_db_to_schema_diff {
   print $file $diff;
   close($file);
 
-  print "WARNING: There may be differences between your DB and your DBIC schema. Please review and if necessary run the SQL in $filename to sync your DB.\n";
+  carp "WARNING: There may be differences between your DB and your DBIC schema. Please review and if necessary run the SQL in $filename to sync your DB.\n";
 }
 
 
@@ -587,7 +587,7 @@ sub _read_sql_file {
   my $file = shift || return;
 
   my $fh;
-  open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)");
+  open $fh, "<$file" or carp("Can't open upgrade file, $file ($!)");
   my @data = split(/\n/, join('', <$fh>));
   @data = grep(!/^--/, @data);
   @data = split(/;/, join('', @data));
@@ -614,7 +614,7 @@ sub _source_exists
 
 =head1 AUTHORS
 
-Jess Robinson <castaway@desert-island.demon.co.uk>
+Jess Robinson <castaway@desert-island.me.uk>
 Luke Saunders <luke@shadowcatsystems.co.uk>
 
 =head1 LICENSE
index 604ad5b..a38cf47 100644 (file)
@@ -299,21 +299,25 @@ sub svp_rollback { die "Virtual method!" }
 
 =for comment
 
-=head2 txn_scope_guard (EXPERIMENTAL)
+=head2 txn_scope_guard
 
-An alternative way of using transactions to C<txn_do>:
+An alternative way of transaction handling based on
+L<DBIx::Class::Storage::TxnScopeGuard>:
 
- my $txn = $storage->txn_scope_guard;
+ my $txn_guard = $storage->txn_scope_guard;
 
  $row->col1("val1");
  $row->update;
 
- $txn->commit;
+ $txn_guard->commit;
 
-If a exception occurs, the transaction will be rolled back. This is still very
-experiemental, and we are not 100% sure it is working right when nested. The
-onus is on you as the user to make sure you dont forget to call
-$C<$txn->commit>.
+If an exception occurs, or the guard object otherwise leaves the scope
+before C<< $txn_guard->commit >> is called, the transaction will be rolled
+back by an explicit L</txn_rollback> call. In essence this is akin to
+using a L</txn_begin>/L</txn_commit> pair, without having to worry
+about calling L</txn_rollback> at the right places. Note that since there
+is no defined code closure, there will be no retries and other magic upon
+database disconnection. If you need such functionality see L</txn_do>.
 
 =cut
 
@@ -324,7 +328,7 @@ sub txn_scope_guard {
 =head2 sql_maker
 
 Returns a C<sql_maker> object - normally an object of class
-C<DBIC::SQL::Abstract>.
+C<DBIx::Class::SQLAHacks>.
 
 =cut
 
index 1b05615..83de252 100644 (file)
@@ -7,7 +7,7 @@ use strict;
 use warnings;
 use Carp::Clan qw/^DBIx::Class/;
 use DBI;
-use SQL::Abstract::Limit;
+use DBIx::Class::SQLAHacks;
 use DBIx::Class::Storage::DBI::Cursor;
 use DBIx::Class::Storage::Statistics;
 use Scalar::Util qw/blessed weaken/;
@@ -29,326 +29,8 @@ __PACKAGE__->mk_group_accessors('simple' => @storage_options);
 __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::Cursor');
 
 __PACKAGE__->mk_group_accessors('inherited' => qw/sql_maker_class/);
-__PACKAGE__->sql_maker_class('DBIC::SQL::Abstract');
+__PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks');
 
-BEGIN {
-
-package # Hide from PAUSE
-  DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :(
-
-use base qw/SQL::Abstract::Limit/;
-
-# This prevents the caching of $dbh in S::A::L, I believe
-sub new {
-  my $self = shift->SUPER::new(@_);
-
-  # If limit_dialect is a ref (like a $dbh), go ahead and replace
-  #   it with what it resolves to:
-  $self->{limit_dialect} = $self->_find_syntax($self->{limit_dialect})
-    if ref $self->{limit_dialect};
-
-  $self;
-}
-
-# DB2 is the only remaining DB using this. Even though we are not sure if
-# RowNumberOver is still needed here (should be part of SQLA) leave the 
-# code in place
-sub _RowNumberOver {
-  my ($self, $sql, $order, $rows, $offset ) = @_;
-
-  $offset += 1;
-  my $last = $rows + $offset;
-  my ( $order_by ) = $self->_order_by( $order );
-
-  $sql = <<"SQL";
-SELECT * FROM
-(
-   SELECT Q1.*, ROW_NUMBER() OVER( ) AS ROW_NUM FROM (
-      $sql
-      $order_by
-   ) Q1
-) Q2
-WHERE ROW_NUM BETWEEN $offset AND $last
-
-SQL
-
-  return $sql;
-}
-
-
-# While we're at it, this should make LIMIT queries more efficient,
-#  without digging into things too deeply
-use Scalar::Util 'blessed';
-sub _find_syntax {
-  my ($self, $syntax) = @_;
-  
-  # DB2 is the only remaining DB using this. Even though we are not sure if
-  # RowNumberOver is still needed here (should be part of SQLA) leave the 
-  # code in place
-  my $dbhname = blessed($syntax) ? $syntax->{Driver}{Name} : $syntax;
-  if(ref($self) && $dbhname && $dbhname eq 'DB2') {
-    return 'RowNumberOver';
-  }
-  
-  $self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax);
-}
-
-sub select {
-  my ($self, $table, $fields, $where, $order, @rest) = @_;
-  if (ref $table eq 'SCALAR') {
-    $table = $$table;
-  }
-  elsif (not ref $table) {
-    $table = $self->_quote($table);
-  }
-  local $self->{rownum_hack_count} = 1
-    if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum');
-  @rest = (-1) unless defined $rest[0];
-  die "LIMIT 0 Does Not Compute" if $rest[0] == 0;
-    # and anyway, SQL::Abstract::Limit will cause a barf if we don't first
-  local $self->{having_bind} = [];
-  my ($sql, @ret) = $self->SUPER::select(
-    $table, $self->_recurse_fields($fields), $where, $order, @rest
-  );
-  $sql .= 
-    $self->{for} ?
-    (
-      $self->{for} eq 'update' ? ' FOR UPDATE' :
-      $self->{for} eq 'shared' ? ' FOR SHARE'  :
-      ''
-    ) :
-    ''
-  ;
-  return wantarray ? ($sql, @ret, @{$self->{having_bind}}) : $sql;
-}
-
-sub insert {
-  my $self = shift;
-  my $table = shift;
-  $table = $self->_quote($table) unless ref($table);
-  $self->SUPER::insert($table, @_);
-}
-
-sub update {
-  my $self = shift;
-  my $table = shift;
-  $table = $self->_quote($table) unless ref($table);
-  $self->SUPER::update($table, @_);
-}
-
-sub delete {
-  my $self = shift;
-  my $table = shift;
-  $table = $self->_quote($table) unless ref($table);
-  $self->SUPER::delete($table, @_);
-}
-
-sub _emulate_limit {
-  my $self = shift;
-  if ($_[3] == -1) {
-    return $_[1].$self->_order_by($_[2]);
-  } else {
-    return $self->SUPER::_emulate_limit(@_);
-  }
-}
-
-sub _recurse_fields {
-  my ($self, $fields, $params) = @_;
-  my $ref = ref $fields;
-  return $self->_quote($fields) unless $ref;
-  return $$fields if $ref eq 'SCALAR';
-
-  if ($ref eq 'ARRAY') {
-    return join(', ', map {
-      $self->_recurse_fields($_)
-        .(exists $self->{rownum_hack_count} && !($params && $params->{no_rownum_hack})
-          ? ' AS col'.$self->{rownum_hack_count}++
-          : '')
-      } @$fields);
-  } elsif ($ref eq 'HASH') {
-    foreach my $func (keys %$fields) {
-      return $self->_sqlcase($func)
-        .'( '.$self->_recurse_fields($fields->{$func}).' )';
-    }
-  }
-  # Is the second check absolutely necessary?
-  elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) {
-    return $self->_bind_to_sql( $fields );
-  }
-  else {
-    Carp::croak($ref . qq{ unexpected in _recurse_fields()})
-  }
-}
-
-sub _order_by {
-  my $self = shift;
-  my $ret = '';
-  my @extra;
-  if (ref $_[0] eq 'HASH') {
-    if (defined $_[0]->{group_by}) {
-      $ret = $self->_sqlcase(' group by ')
-        .$self->_recurse_fields($_[0]->{group_by}, { no_rownum_hack => 1 });
-    }
-    if (defined $_[0]->{having}) {
-      my $frag;
-      ($frag, @extra) = $self->_recurse_where($_[0]->{having});
-      push(@{$self->{having_bind}}, @extra);
-      $ret .= $self->_sqlcase(' having ').$frag;
-    }
-    if (defined $_[0]->{order_by}) {
-      $ret .= $self->_order_by($_[0]->{order_by});
-    }
-    if (grep { $_ =~ /^-(desc|asc)/i } keys %{$_[0]}) {
-      return $self->SUPER::_order_by($_[0]);
-    }
-  } elsif (ref $_[0] eq 'SCALAR') {
-    $ret = $self->_sqlcase(' order by ').${ $_[0] };
-  } elsif (ref $_[0] eq 'ARRAY' && @{$_[0]}) {
-    my @order = @{+shift};
-    $ret = $self->_sqlcase(' order by ')
-          .join(', ', map {
-                        my $r = $self->_order_by($_, @_);
-                        $r =~ s/^ ?ORDER BY //i;
-                        $r;
-                      } @order);
-  } else {
-    $ret = $self->SUPER::_order_by(@_);
-  }
-  return $ret;
-}
-
-sub _order_directions {
-  my ($self, $order) = @_;
-  $order = $order->{order_by} if ref $order eq 'HASH';
-  return $self->SUPER::_order_directions($order);
-}
-
-sub _table {
-  my ($self, $from) = @_;
-  if (ref $from eq 'ARRAY') {
-    return $self->_recurse_from(@$from);
-  } elsif (ref $from eq 'HASH') {
-    return $self->_make_as($from);
-  } else {
-    return $from; # would love to quote here but _table ends up getting called
-                  # twice during an ->select without a limit clause due to
-                  # the way S::A::Limit->select works. should maybe consider
-                  # bypassing this and doing S::A::select($self, ...) in
-                  # our select method above. meantime, quoting shims have
-                  # been added to select/insert/update/delete here
-  }
-}
-
-sub _recurse_from {
-  my ($self, $from, @join) = @_;
-  my @sqlf;
-  push(@sqlf, $self->_make_as($from));
-  foreach my $j (@join) {
-    my ($to, $on) = @$j;
-
-    # check whether a join type exists
-    my $join_clause = '';
-    my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to;
-    if (ref($to_jt) eq 'HASH' and exists($to_jt->{-join_type})) {
-      $join_clause = ' '.uc($to_jt->{-join_type}).' JOIN ';
-    } else {
-      $join_clause = ' JOIN ';
-    }
-    push(@sqlf, $join_clause);
-
-    if (ref $to eq 'ARRAY') {
-      push(@sqlf, '(', $self->_recurse_from(@$to), ')');
-    } else {
-      push(@sqlf, $self->_make_as($to));
-    }
-    push(@sqlf, ' ON ', $self->_join_condition($on));
-  }
-  return join('', @sqlf);
-}
-
-sub _bind_to_sql {
-  my $self = shift;
-  my $arr  = shift;
-  my $sql = shift @$$arr;
-  $sql =~ s/\?/$self->_quote((shift @$$arr)->[1])/eg;
-  return $sql
-}
-
-sub _make_as {
-  my ($self, $from) = @_;
-  return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ 
-                        : ref $_ eq 'REF'    ? $self->_bind_to_sql($_) 
-                        : $self->_quote($_)) 
-                       } reverse each %{$self->_skip_options($from)});
-}
-
-sub _skip_options {
-  my ($self, $hash) = @_;
-  my $clean_hash = {};
-  $clean_hash->{$_} = $hash->{$_}
-    for grep {!/^-/} keys %$hash;
-  return $clean_hash;
-}
-
-sub _join_condition {
-  my ($self, $cond) = @_;
-  if (ref $cond eq 'HASH') {
-    my %j;
-    for (keys %$cond) {
-      my $v = $cond->{$_};
-      if (ref $v) {
-        # XXX no throw_exception() in this package and croak() fails with strange results
-        Carp::croak(ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'})
-            if ref($v) ne 'SCALAR';
-        $j{$_} = $v;
-      }
-      else {
-        my $x = '= '.$self->_quote($v); $j{$_} = \$x;
-      }
-    };
-    return scalar($self->_recurse_where(\%j));
-  } elsif (ref $cond eq 'ARRAY') {
-    return join(' OR ', map { $self->_join_condition($_) } @$cond);
-  } else {
-    die "Can't handle this yet!";
-  }
-}
-
-sub _quote {
-  my ($self, $label) = @_;
-  return '' unless defined $label;
-  return "*" if $label eq '*';
-  return $label unless $self->{quote_char};
-  if(ref $self->{quote_char} eq "ARRAY"){
-    return $self->{quote_char}->[0] . $label . $self->{quote_char}->[1]
-      if !defined $self->{name_sep};
-    my $sep = $self->{name_sep};
-    return join($self->{name_sep},
-        map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1]  }
-       split(/\Q$sep\E/,$label));
-  }
-  return $self->SUPER::_quote($label);
-}
-
-sub limit_dialect {
-    my $self = shift;
-    $self->{limit_dialect} = shift if @_;
-    return $self->{limit_dialect};
-}
-
-sub quote_char {
-    my $self = shift;
-    $self->{quote_char} = shift if @_;
-    return $self->{quote_char};
-}
-
-sub name_sep {
-    my $self = shift;
-    $self->{name_sep} = shift if @_;
-    return $self->{name_sep};
-}
-
-} # End of BEGIN block
 
 =head1 NAME
 
@@ -471,6 +153,10 @@ the database.  Its value may contain:
 
 =over
 
+=item a scalar
+
+This contains one SQL statement to execute.
+
 =item an array reference
 
 This contains SQL statements to execute in order.  Each element contains
@@ -928,35 +614,56 @@ sub _populate_dbh {
   my @info = @{$self->_dbi_connect_info || []};
   $self->_dbh($self->_connect(@info));
 
+  $self->_determine_driver;
+
   # Always set the transaction depth on connect, since
   #  there is no transaction in progress by definition
   $self->{transaction_depth} = $self->_dbh_autocommit ? 0 : 1;
 
-  if(ref $self eq 'DBIx::Class::Storage::DBI') {
-    my $driver = $self->_dbh->{Driver}->{Name};
+  $self->_conn_pid($$);
+  $self->_conn_tid(threads->tid) if $INC{'threads.pm'};
+
+  my $connection_do = $self->on_connect_do;
+  $self->_do_connection_actions($connection_do) if $connection_do;
+}
+
+sub _determine_driver {
+  my ($self) = @_;
+
+  if (ref $self eq 'DBIx::Class::Storage::DBI') {
+    my $driver;
+
+    if ($self->_dbh) { # we are connected
+      $driver = $self->_dbh->{Driver}{Name};
+    } else {
+      # try to use dsn to not require being connected, the driver may still
+      # force a connection in _rebless to determine version
+      ($driver) = $self->_dbi_connect_info->[0] =~ /dbi:([^:]+):/i;
+    }
+
     if ($self->load_optional_class("DBIx::Class::Storage::DBI::${driver}")) {
       bless $self, "DBIx::Class::Storage::DBI::${driver}";
       $self->_rebless();
     }
   }
-
-  $self->_conn_pid($$);
-  $self->_conn_tid(threads->tid) if $INC{'threads.pm'};
-
-  my $connection_do = $self->on_connect_do;
-  $self->_do_connection_actions($connection_do) if ref($connection_do);
 }
 
 sub _do_connection_actions {
   my $self = shift;
   my $connection_do = shift;
 
-  if (ref $connection_do eq 'ARRAY') {
+  if (!ref $connection_do) {
+    $self->_do_query($connection_do);
+  }
+  elsif (ref $connection_do eq 'ARRAY') {
     $self->_do_query($_) foreach @$connection_do;
   }
   elsif (ref $connection_do eq 'CODE') {
     $connection_do->($self);
   }
+  else {
+    $self->throw_exception (sprintf ("Don't know how to process conection actions of type '%s'", ref $connection_do) );
+  }
 
   return $self;
 }
@@ -1286,20 +993,22 @@ sub insert {
   my $ident = $source->from; 
   my $bind_attributes = $self->source_bind_attributes($source);
 
+  my $updated_cols = {};
+
   $self->ensure_connected;
   foreach my $col ( $source->columns ) {
     if ( !defined $to_insert->{$col} ) {
       my $col_info = $source->column_info($col);
 
       if ( $col_info->{auto_nextval} ) {
-        $to_insert->{$col} = $self->_sequence_fetch( 'nextval', $col_info->{sequence} || $self->_dbh_get_autoinc_seq($self->dbh, $source) );
+        $updated_cols->{$col} = $to_insert->{$col} = $self->_sequence_fetch( 'nextval', $col_info->{sequence} || $self->_dbh_get_autoinc_seq($self->dbh, $source) );
       }
     }
   }
 
   $self->_execute('insert' => [], $source, $bind_attributes, $to_insert);
 
-  return $to_insert;
+  return $updated_cols;
 }
 
 ## Still not quite perfect, and EXPERIMENTAL
@@ -1319,13 +1028,7 @@ sub insert_bulk {
 #  @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
 
   ## This must be an arrayref, else nothing works!
-  
   my $tuple_status = [];
-  
-  ##use Data::Dumper;
-  ##print STDERR Dumper( $data, $sql, [@bind] );
-
-  my $time = time();
 
   ## Get the bind_attributes, if any exist
   my $bind_attributes = $self->source_bind_attributes($source);
@@ -1373,6 +1076,92 @@ sub delete {
   return $self->_execute('delete' => [], $source, $bind_attrs, @_);
 }
 
+# We were sent here because the $rs contains a complex search
+# which will require a subquery to select the correct rows
+# (i.e. joined or limited resultsets)
+#
+# Genarating a single PK column subquery is trivial and supported
+# by all RDBMS. However if we have a multicolumn PK, things get ugly.
+# Look at _multipk_update_delete()
+sub subq_update_delete {
+  my $self = shift;
+  my ($rs, $op, $values) = @_;
+
+  my $rsrc = $rs->result_source;
+
+  # we already check this, but double check naively just in case. Should be removed soon
+  my $sel = $rs->_resolved_attrs->{select};
+  $sel = [ $sel ] unless ref $sel eq 'ARRAY';
+  my @pcols = $rsrc->primary_columns;
+  if (@$sel != @pcols) {
+    $self->throw_exception (
+      'Subquery update/delete can not be called on resultsets selecting a'
+     .' number of columns different than the number of primary keys'
+    );
+  }
+
+  if (@pcols == 1) {
+    return $self->$op (
+      $rsrc,
+      $op eq 'update' ? $values : (),
+      { $pcols[0] => { -in => $rs->as_query } },
+    );
+  }
+
+  else {
+    return $self->_multipk_update_delete (@_);
+  }
+}
+
+# ANSI SQL does not provide a reliable way to perform a multicol-PK
+# resultset update/delete involving subqueries. So by default resort
+# to simple (and inefficient) delete_all style per-row opearations,
+# while allowing specific storages to override this with a faster
+# implementation.
+#
+sub _multipk_update_delete {
+  return shift->_per_row_update_delete (@_);
+}
+
+# This is the default loop used to delete/update rows for multi PK
+# resultsets, and used by mysql exclusively (because it can't do anything
+# else).
+#
+# We do not use $row->$op style queries, because resultset update/delete
+# is not expected to cascade (this is what delete_all/update_all is for).
+#
+# There should be no race conditions as the entire operation is rolled
+# in a transaction.
+#
+sub _per_row_update_delete {
+  my $self = shift;
+  my ($rs, $op, $values) = @_;
+
+  my $rsrc = $rs->result_source;
+  my @pcols = $rsrc->primary_columns;
+
+  my $guard = $self->txn_scope_guard;
+
+  my $subrs_cur = $rs->cursor;
+  while (my @pks = $subrs_cur->next) {
+
+    my $cond;
+    for my $i (0.. $#pcols) {
+      $cond->{$pcols[$i]} = $pks[$i];
+    }
+
+    $self->$op (
+      $rsrc,
+      $op eq 'update' ? $values : (),
+      $cond,
+    );
+  }
+
+  $guard->commit;
+
+  return 1;
+}
+
 sub _select {
   my $self = shift;
   my $sql_maker = $self->sql_maker;
@@ -1384,14 +1173,6 @@ sub _select_args {
   my ($self, $ident, $select, $condition, $attrs) = @_;
   my $order = $attrs->{order_by};
 
-  if (ref $condition eq 'SCALAR') {
-    my $unwrap = ${$condition};
-    if ($unwrap =~ s/ORDER BY (.*)$//i) {
-      $order = $1;
-      $condition = \$unwrap;
-    }
-  }
-
   my $for = delete $attrs->{for};
   my $sql_maker = $self->sql_maker;
   $sql_maker->{for} = $for;
@@ -1565,9 +1346,18 @@ Return the row id of the last insert.
 =cut
 
 sub _dbh_last_insert_id {
-    my ($self, $dbh, $source, $col) = @_;
-    # XXX This is a SQLite-ism as a default... is there a DBI-generic way?
-    $dbh->func('last_insert_rowid');
+    # All Storage's need to register their own _dbh_last_insert_id
+    # the old SQLite-based method was highly inappropriate
+
+    my $self = shift;
+    my $class = ref $self;
+    $self->throw_exception (<<EOE);
+
+No _dbh_last_insert_id() method found in $class.
+Since the method of obtaining the autoincrement id of the last insert
+operation varies greatly between different databases, this method must be
+individually implemented for every storage class.
+EOE
 }
 
 sub last_insert_id {
@@ -1623,7 +1413,7 @@ sub create_ddl_dir {
   my ($self, $schema, $databases, $version, $dir, $preversion, $sqltargs) = @_;
 
   if(!$dir || !-d $dir) {
-    warn "No directory given, using ./\n";
+    carp "No directory given, using ./\n";
     $dir = "./";
   }
   $databases ||= ['MySQL', 'SQLite', 'PostgreSQL'];
@@ -1646,11 +1436,11 @@ sub create_ddl_dir {
   my $sqlt = SQL::Translator->new( $sqltargs );
 
   $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
-  my $sqlt_schema = $sqlt->translate({ data => $schema }) or die $sqlt->error;
+  my $sqlt_schema = $sqlt->translate({ data => $schema })
+    or $self->throw_exception ($sqlt->error);
 
   foreach my $db (@$databases) {
     $sqlt->reset();
-    $sqlt = $self->configure_sqlt($sqlt, $db);
     $sqlt->{schema} = $sqlt_schema;
     $sqlt->producer($db);
 
@@ -1658,13 +1448,13 @@ sub create_ddl_dir {
     my $filename = $schema->ddl_filename($db, $version, $dir);
     if (-e $filename && ($version eq $schema_version )) {
       # if we are dumping the current version, overwrite the DDL
-      warn "Overwriting existing DDL file - $filename";
+      carp "Overwriting existing DDL file - $filename";
       unlink($filename);
     }
 
     my $output = $sqlt->translate;
     if(!$output) {
-      warn("Failed to translate to $db, skipping. (" . $sqlt->error . ")");
+      carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")");
       next;
     }
     if(!open($file, ">$filename")) {
@@ -1680,13 +1470,13 @@ sub create_ddl_dir {
 
     my $prefilename = $schema->ddl_filename($db, $preversion, $dir);
     if(!-e $prefilename) {
-      warn("No previous schema file found ($prefilename)");
+      carp("No previous schema file found ($prefilename)");
       next;
     }
 
     my $difffile = $schema->ddl_filename($db, $version, $dir, $preversion);
     if(-e $difffile) {
-      warn("Overwriting existing diff file - $difffile");
+      carp("Overwriting existing diff file - $difffile");
       unlink($difffile);
     }
     
@@ -1695,28 +1485,37 @@ sub create_ddl_dir {
       my $t = SQL::Translator->new($sqltargs);
       $t->debug( 0 );
       $t->trace( 0 );
-      $t->parser( $db )                       or die $t->error;
-      $t = $self->configure_sqlt($t, $db);
-      my $out = $t->translate( $prefilename ) or die $t->error;
+
+      $t->parser( $db )
+        or $self->throw_exception ($t->error);
+
+      my $out = $t->translate( $prefilename )
+        or $self->throw_exception ($t->error);
+
       $source_schema = $t->schema;
-      unless ( $source_schema->name ) {
-        $source_schema->name( $prefilename );
-      }
+
+      $source_schema->name( $prefilename )
+        unless ( $source_schema->name );
     }
 
     # The "new" style of producers have sane normalization and can support 
     # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't
     # And we have to diff parsed SQL against parsed SQL.
     my $dest_schema = $sqlt_schema;
-    
+
     unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) {
       my $t = SQL::Translator->new($sqltargs);
       $t->debug( 0 );
       $t->trace( 0 );
-      $t->parser( $db )                    or die $t->error;
-      $t = $self->configure_sqlt($t, $db);
-      my $out = $t->translate( $filename ) or die $t->error;
+
+      $t->parser( $db )
+        or $self->throw_exception ($t->error);
+
+      my $out = $t->translate( $filename )
+        or $self->throw_exception ($t->error);
+
       $dest_schema = $t->schema;
+
       $dest_schema->name( $filename )
         unless $dest_schema->name;
     }
@@ -1734,17 +1533,6 @@ sub create_ddl_dir {
   }
 }
 
-sub configure_sqlt() {
-  my $self = shift;
-  my $tr = shift;
-  my $db = shift || $self->sqlt_type;
-  if ($db eq 'PostgreSQL') {
-    $tr->quote_table_names(0);
-    $tr->quote_field_names(0);
-  }
-  return $tr;
-}
-
 =head2 deployment_statements
 
 =over 4
@@ -1819,7 +1607,7 @@ sub deploy {
       $self->dbh->do($line); # shouldn't be using ->dbh ?
     };
     if ($@) {
-      warn qq{$@ (running "${line}")};
+      carp qq{$@ (running "${line}")};
     }
     $self->_query_end($line);
   };
@@ -1941,37 +1729,6 @@ cases if you choose the C<< AutoCommit => 0 >> path, just as you would
 be with raw DBI.
 
 
-=head1 SQL METHODS
-
-The module defines a set of methods within the DBIC::SQL::Abstract
-namespace.  These build on L<SQL::Abstract::Limit> to provide the
-SQL query functions.
-
-The following methods are extended:-
-
-=over 4
-
-=item delete
-
-=item insert
-
-=item select
-
-=item update
-
-=item limit_dialect
-
-See L</connect_info> for details.
-
-=item quote_char
-
-See L</connect_info> for details.
-
-=item name_sep
-
-See L</connect_info> for details.
-
-=back
 
 =head1 AUTHORS
 
index 60df379..ddad661 100644 (file)
@@ -36,8 +36,8 @@ Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
 
 sub new {
   my ($class, $storage, $args, $attrs) = @_;
-  #use Data::Dumper; warn Dumper(@_);
   $class = ref $class if ref $class;
+
   my $new = {
     storage => $storage,
     args => $args,
@@ -72,7 +72,7 @@ sub as_query {
 
   my @args = $storage->_select_args(@{$self->{args}});
   my ($sql, $bind)  = $storage->_prep_for_execute(@args[0 .. 2], [@args[4 .. $#args]]);
-  return \[ "($sql)", @$bind ];
+  return \[ "($sql)", @{ $bind || [] }];
 }
 
 =head2 next
diff --git a/lib/DBIx/Class/Storage/DBI/MultiColumnIn.pm b/lib/DBIx/Class/Storage/DBI/MultiColumnIn.pm
new file mode 100644 (file)
index 0000000..050c018
--- /dev/null
@@ -0,0 +1,60 @@
+package DBIx::Class::Storage::DBI::MultiColumnIn;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Storage::DBI';
+
+=head1 NAME 
+
+DBIx::Class::Storage::DBI::MultiColumnIn - Storage component for RDBMS supporting multicolumn in clauses
+
+=head1 DESCRIPTION
+
+While ANSI SQL does not define a multicolumn in operator, many databases can
+in fact understand WHERE (cola, colb) IN ( SELECT subcol_a, subcol_b ... )
+The storage class for any such RDBMS should inherit from this class, in order
+to dramatically speed up update/delete operations on joined multipk resultsets.
+
+At this point the only overriden method is C<_multipk_update_delete()>
+
+=cut
+
+sub _multipk_update_delete {
+  my $self = shift;
+  my ($rs, $op, $values) = @_;
+
+  my $rsrc = $rs->result_source;
+  my @pcols = $rsrc->primary_columns;
+  my $attrs = $rs->_resolved_attrs;
+
+  # naive check - this is an internal method after all, we should know what we are doing 
+  $self->throw_exception ('Number of columns selected by supplied resultset does not match number of primary keys')
+    if ( ref $attrs->{select} ne 'ARRAY' or @{$attrs->{select}} != @pcols );
+
+  # This is hideously ugly, but SQLA does not understand multicol IN expressions
+  my ($sql, @bind) = @${$rs->as_query};
+  $sql = sprintf ('(%s) IN %s',
+    join (', ', @pcols),
+    $sql,
+  );
+
+  return $self->$op (
+    $rsrc,
+    $op eq 'update' ? $values : (),
+    \[$sql, @bind],
+  );
+
+}
+
+=head1 AUTHORS
+
+See L<DBIx::Class/CONTRIBUTORS>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+
+1;
diff --git a/lib/DBIx/Class/Storage/DBI/MultiDistinctEmulation.pm b/lib/DBIx/Class/Storage/DBI/MultiDistinctEmulation.pm
deleted file mode 100644 (file)
index 7ab7846..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-package DBIx::Class::Storage::DBI::MultiDistinctEmulation;
-
-use strict;
-use warnings;
-
-use base qw/DBIx::Class::Storage::DBI/;
-
-sub _select {
-  my ($self, $ident, $select, $condition, $attrs) = @_;
-
-  # hack to make count distincts with multiple columns work in SQLite and Oracle
-  if (ref $select eq 'ARRAY') { 
-      @{$select} = map {$self->replace_distincts($_)} @{$select};
-  } else { 
-      $select = $self->replace_distincts($select);
-  }
-
-  return $self->next::method($ident, $select, $condition, $attrs);
-}
-
-sub replace_distincts {
-    my ($self, $select) = @_;
-
-    $select->{count}->{distinct} = join("||", @{$select->{count}->{distinct}}) 
-       if (ref $select eq 'HASH' && $select->{count} && ref $select->{count} eq 'HASH' && 
-           $select->{count}->{distinct} && ref $select->{count}->{distinct} eq 'ARRAY');
-
-    return $select;
-}
-
-1;
-
-=head1 NAME 
-
-DBIx::Class::Storage::DBI::MultiDistinctEmulation - Some databases can't handle count distincts with multiple cols. They should use base on this.
-
-=head1 SYNOPSIS
-
-=head1 DESCRIPTION
-
-This class allows count distincts with multiple columns for retarded databases (Oracle and SQLite)
-
-=head1 AUTHORS
-
-Luke Saunders <luke.saunders@gmail.com>
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
index 51ef7d5..7027ad6 100644 (file)
@@ -50,22 +50,20 @@ sub _prep_for_execute {
 
   foreach my $bound (@$bind) {
     my $col = shift @$bound;
-    my $do_quote = $self->should_quote_data_type($col);
+    my $datatype = 'FIXME!!!';
     foreach my $data (@$bound) {
         if(ref $data) {
             $data = ''.$data;
         }
-        $data = $self->_dbh->quote($data) if $do_quote;
+        $data = $self->_dbh->quote($data);
         $new_sql .= shift(@sql_part) . $data;
     }
   }
   $new_sql .= join '', @sql_part;
 
-  return ($new_sql);
+  return ($new_sql, []);
 }
 
-sub should_quote_data_type { 1 }
-
 =head1 AUTHORS
 
 Brandon Black <blblack@gmail.com>
index e8b9c12..09a7c6b 100644 (file)
@@ -7,7 +7,7 @@ use base qw/DBIx::Class::Storage::DBI/;
 sub _rebless {
     my ($self) = @_;
 
-    my $dbtype = eval { $self->_dbh->get_info(17) };
+    my $dbtype = eval { $self->dbh->get_info(17) };
     unless ( $@ ) {
         # Translate the backend name into a perl identifier
         $dbtype =~ s/\W/_/gi;
index 7d5bf10..96ff543 100644 (file)
@@ -2,8 +2,6 @@ package DBIx::Class::Storage::DBI::ODBC::ACCESS;
 use strict;\r
 use warnings;\r
 \r
-use Data::Dump qw( dump );\r
-\r
 use DBI;\r
 use base qw/DBIx::Class::Storage::DBI/;\r
 \r
index 29bfce1..f8f981b 100644 (file)
@@ -8,7 +8,7 @@ use base qw/DBIx::Class::Storage::DBI/;
 sub _rebless {
     my ($self) = @_;
 
-    my $version = eval { $self->_dbh->get_info(18); };
+    my $version = eval { $self->dbh->get_info(18); };
 
     if ( !$@ ) {
         my ($major, $minor, $patchlevel) = split(/\./, $version);
index 2e9a8c1..bd3de18 100644 (file)
@@ -1,5 +1,4 @@
 package DBIx::Class::Storage::DBI::Oracle::Generic;
-# -*- mode: cperl; cperl-indent-level: 2 -*-
 
 use strict;
 use warnings;
@@ -24,11 +23,11 @@ This class implements autoincrements for Oracle.
 
 =cut
 
+use base qw/DBIx::Class::Storage::DBI/;
 use Carp::Clan qw/^DBIx::Class/;
 
-use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
-
-# __PACKAGE__->load_components(qw/PK::Auto/);
+# For ORA_BLOB => 113, ORA_CLOB => 112
+use DBD::Oracle qw( :ora_types );
 
 sub _dbh_last_insert_id {
   my ($self, $dbh, $source, @columns) = @_;
@@ -190,6 +189,48 @@ sub _svp_begin {
     $self->dbh->do("SAVEPOINT $name");
 }
 
+=head2 source_bind_attributes
+
+Handle LOB types in Oracle.  Under a certain size (4k?), you can get away
+with the driver assuming your input is the deprecated LONG type if you
+encode it as a hex string.  That ain't gonna fly at larger values, where
+you'll discover you have to do what this does.
+
+This method had to be overridden because we need to set ora_field to the
+actual column, and that isn't passed to the call (provided by Storage) to
+bind_attribute_by_data_type.
+
+According to L<DBD::Oracle>, the ora_field isn't always necessary, but
+adding it doesn't hurt, and will save your bacon if you're modifying a
+table with more than one LOB column.
+
+=cut
+
+sub source_bind_attributes 
+{
+       my $self = shift;
+       my($source) = @_;
+
+       my %bind_attributes;
+
+       foreach my $column ($source->columns) {
+               my $data_type = $source->column_info($column)->{data_type} || '';
+               next unless $data_type;
+
+               my %column_bind_attrs = $self->bind_attribute_by_data_type($data_type);
+
+               if ($data_type =~ /^[BC]LOB$/i) {
+                       $column_bind_attrs{'ora_type'}
+                               = uc($data_type) eq 'CLOB' ? ORA_CLOB : ORA_BLOB;
+                       $column_bind_attrs{'ora_field'} = $column;
+               }
+
+               $bind_attributes{$column} = \%column_bind_attrs;
+       }
+
+       return \%bind_attributes;
+}
+
 # Oracle automatically releases a savepoint when you start another one with the
 # same name.
 sub _svp_release { 1 }
index 730c73b..d4e7385 100644 (file)
@@ -5,94 +5,7 @@ use base qw( DBIx::Class::Storage::DBI::Oracle::Generic );
 use strict;
 use warnings;
 
-__PACKAGE__->sql_maker_class('DBIC::SQL::Abstract::Oracle');
-
-BEGIN {
-  package # Hide from PAUSE
-    DBIC::SQL::Abstract::Oracle;
-
-  use base qw( DBIC::SQL::Abstract );
-
-  sub select {
-    my ($self, $table, $fields, $where, $order, @rest) = @_;
-
-    if (ref($table) eq 'ARRAY') {
-      $where = $self->_oracle_joins($where, @{ $table });
-    }
-
-    return $self->SUPER::select($table, $fields, $where, $order, @rest);
-  }
-
-  sub _recurse_from {
-    my ($self, $from, @join) = @_;
-
-    my @sqlf = $self->_make_as($from);
-
-    foreach my $j (@join) {
-      my ($to, $on) = @{ $j };
-
-      if (ref $to eq 'ARRAY') {
-        push (@sqlf, $self->_recurse_from(@{ $to }));
-      }
-      else {
-        push (@sqlf, $self->_make_as($to));
-      }
-    }
-
-    return join q{, }, @sqlf;
-  }
-
-  sub _oracle_joins {
-    my ($self, $where, $from, @join) = @_;
-    my $join_where = {};
-    $self->_recurse_oracle_joins($join_where, $from, @join);
-    if (keys %$join_where) {
-      if (!defined($where)) {
-        $where = $join_where;
-      } else {
-        if (ref($where) eq 'ARRAY') {
-          $where = { -or => $where };
-        }
-        $where = { -and => [ $join_where, $where ] };
-      }
-    }
-    return $where;
-  }
-
-  sub _recurse_oracle_joins {
-    my ($self, $where, $from, @join) = @_;
-
-    foreach my $j (@join) {
-      my ($to, $on) = @{ $j };
-
-      if (ref $to eq 'ARRAY') {
-        $self->_recurse_oracle_joins($where, @{ $to });
-      }
-
-      my $to_jt      = ref $to eq 'ARRAY' ? $to->[0] : $to;
-      my $left_join  = q{};
-      my $right_join = q{};
-
-      if (ref $to_jt eq 'HASH' and exists $to_jt->{-join_type}) {
-        #TODO: Support full outer joins -- this would happen much earlier in
-        #the sequence since oracle 8's full outer join syntax is best
-        #described as INSANE.
-        die "Can't handle full outer joins in Oracle 8 yet!\n"
-          if $to_jt->{-join_type} =~ /full/i;
-
-        $left_join  = q{(+)} if $to_jt->{-join_type} =~ /left/i
-                             && $to_jt->{-join_type} !~ /inner/i;
-
-        $right_join = q{(+)} if $to_jt->{-join_type} =~ /right/i
-                             && $to_jt->{-join_type} !~ /inner/i;
-      }
-
-      foreach my $lhs (keys %{ $on }) {
-        $where->{$lhs . $left_join} = \"= $on->{ $lhs }$right_join";
-      }
-    }
-  }
-}
+__PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::OracleJoins');
 
 1;
 
@@ -135,33 +48,7 @@ it's already too late.
 
 =head1 METHODS
 
-This module replaces a subroutine contained in DBIC::SQL::Abstract:
-
-=over
-
-=item sql_maker
-
-=back
-
-It also creates a new module in its BEGIN { } block called
-DBIC::SQL::Abstract::Oracle which has the following methods:
-
-=over
-
-=item select ($\@$;$$@)
-
-Replaces DBIC::SQL::Abstract's select() method, which calls _oracle_joins()
-to modify the column and table list before calling SUPER::select().
-
-=item _recurse_from ($$\@)
-
-Recursive subroutine that builds the table list.
-
-=item _oracle_joins ($$$@)
-
-Creates the left/right relationship in the where query.
-
-=back
+See L<DBIx::Class::SQLAHacks::OracleJoins> for implementation details.
 
 =head1 BUGS
 
@@ -172,7 +59,9 @@ Probably lots more.
 
 =over
 
-=item L<DBIC::SQL::Abstract>
+=item L<DBIx::Class::SQLAHacks>
+
+=item L<DBIx::Class::SQLAHacks::OracleJoins>
 
 =item L<DBIx::Class::Storage::DBI::Oracle::Generic>
 
index 5fcaa17..41b2357 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 
 use DBD::Pg qw(:pg_types);
 
-use base qw/DBIx::Class::Storage::DBI/;
+use base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
 
 # __PACKAGE__->load_components(qw/PK::Auto/);
 
index 3b99b3f..259cdc5 100644 (file)
@@ -7,10 +7,11 @@ BEGIN {
   ## use, so we explicitly test for these.
        
   my %replication_required = (
-    Moose => '0.54',
+    Moose => '0.77',
     MooseX::AttributeHelpers => '0.12',
-    Moose::Util::TypeConstraints => '0.54',
-    Class::MOP => '0.63',
+    MooseX::Types => '0.10',
+    namespace::clean => '0.11',
+    Hash::Merge => '0.11'
   );
        
   my @didnt_load;
@@ -25,9 +26,17 @@ BEGIN {
     if @didnt_load;    
 }
 
+use Moose;
 use DBIx::Class::Storage::DBI;
 use DBIx::Class::Storage::DBI::Replicated::Pool;
 use DBIx::Class::Storage::DBI::Replicated::Balancer;
+use DBIx::Class::Storage::DBI::Replicated::Types 'BalancerClassNamePart';
+use MooseX::Types::Moose qw/ClassName HashRef Object/;
+use Scalar::Util 'reftype';
+use Carp::Clan qw/^DBIx::Class/;
+use Hash::Merge 'merge';
+
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -99,10 +108,11 @@ to force a query to run against Master when needed.
 
 Replicated Storage has additional requirements not currently part of L<DBIx::Class>
 
-  Moose => 0.54
+  Moose => 0.77
   MooseX::AttributeHelpers => 0.12 
-  Moose::Util::TypeConstraints => 0.54
-  Class::MOP => 0.63
+  MooseX::Types => 0.10
+  namespace::clean => 0.11
+  Hash::Merge => 0.11
   
 You will need to install these modules manually via CPAN or make them part of the
 Makefile for your distribution.
@@ -132,9 +142,8 @@ to: L<DBIx::Class::Storage::DBI::Replicated::Pool>.
 =cut
 
 has 'pool_type' => (
-  is=>'ro',
-  isa=>'ClassName',
-  required=>1,
+  is=>'rw',
+  isa=>ClassName,
   default=>'DBIx::Class::Storage::DBI::Replicated::Pool',
   handles=>{
     'create_pool' => 'new',
@@ -149,10 +158,9 @@ See L<DBIx::Class::Storage::Replicated::Pool> for available arguments.
 =cut
 
 has 'pool_args' => (
-  is=>'ro',
-  isa=>'HashRef',
+  is=>'rw',
+  isa=>HashRef,
   lazy=>1,
-  required=>1,
   default=>sub { {} },
 );
 
@@ -164,23 +172,9 @@ choose how to spread the query load across each replicant in the pool.
 
 =cut
 
-subtype 'DBIx::Class::Storage::DBI::Replicated::BalancerClassNamePart',
-  as 'ClassName';
-    
-coerce 'DBIx::Class::Storage::DBI::Replicated::BalancerClassNamePart',
-  from 'Str',
-  via {
-       my $type = $_;
-    if($type=~m/^::/) {
-      $type = 'DBIx::Class::Storage::DBI::Replicated::Balancer'.$type;
-    }  
-    Class::MOP::load_class($type);  
-    $type;     
-  };
-
 has 'balancer_type' => (
-  is=>'ro',
-  isa=>'DBIx::Class::Storage::DBI::Replicated::BalancerClassNamePart',
+  is=>'rw',
+  isa=>BalancerClassNamePart,
   coerce=>1,
   required=>1,
   default=> 'DBIx::Class::Storage::DBI::Replicated::Balancer::First',
@@ -197,8 +191,8 @@ See L<DBIx::Class::Storage::Replicated::Balancer> for available arguments.
 =cut
 
 has 'balancer_args' => (
-  is=>'ro',
-  isa=>'HashRef',
+  is=>'rw',
+  isa=>HashRef,
   lazy=>1,
   required=>1,
   default=>sub { {} },
@@ -230,7 +224,7 @@ is a class that takes a pool (<DBIx::Class::Storage::DBI::Replicated::Pool>)
 =cut
 
 has 'balancer' => (
-  is=>'ro',
+  is=>'rw',
   isa=>'DBIx::Class::Storage::DBI::Replicated::Balancer',
   lazy_build=>1,
   handles=>[qw/auto_validate_every/],
@@ -265,7 +259,7 @@ Defines an object that implements the read side of L<BIx::Class::Storage::DBI>.
 
 has 'read_handler' => (
   is=>'rw',
-  isa=>'Object',
+  isa=>Object,
   lazy_build=>1,
   handles=>[qw/
     select
@@ -282,8 +276,7 @@ Defines an object that implements the write side of L<BIx::Class::Storage::DBI>.
 
 has 'write_handler' => (
   is=>'ro',
-  isa=>'Object',
-  lazy_build=>1,
+  isa=>Object,
   lazy_build=>1,
   handles=>[qw/   
     on_connect_do
@@ -313,11 +306,74 @@ has 'write_handler' => (
 
     reload_row
     _prep_for_execute
-    configure_sqlt
     
   /],
 );
 
+has _master_connect_info_opts =>
+  (is => 'rw', isa => HashRef, default => sub { {} });
+
+=head2 around: connect_info
+
+Preserve master's C<connect_info> options (for merging with replicants.)
+Also set any Replicated related options from connect_info, such as
+C<pool_type>, C<pool_args>, C<balancer_type> and C<balancer_args>.
+
+=cut
+
+around connect_info => sub {
+  my ($next, $self, $info, @extra) = @_;
+
+  my $wantarray = wantarray;
+
+  my %opts;
+  for my $arg (@$info) {
+    next unless (reftype($arg)||'') eq 'HASH';
+    %opts = %{ merge($arg, \%opts) };
+  }
+  delete $opts{dsn};
+
+  if (@opts{qw/pool_type pool_args/}) {
+    $self->pool_type(delete $opts{pool_type})
+      if $opts{pool_type};
+
+    $self->pool_args(
+      merge((delete $opts{pool_args} || {}), $self->pool_args)
+    );
+
+    $self->pool($self->_build_pool)
+       if $self->pool;
+  }
+
+  if (@opts{qw/balancer_type balancer_args/}) {
+    $self->balancer_type(delete $opts{balancer_type})
+      if $opts{balancer_type};
+
+    $self->balancer_args(
+      merge((delete $opts{balancer_args} || {}), $self->balancer_args)
+    );
+
+    $self->balancer($self->_build_balancer)
+       if $self->balancer;
+  }
+
+  $self->_master_connect_info_opts(\%opts);
+
+  my (@res, $res);
+  if ($wantarray) {
+    @res = $self->$next($info, @extra);
+  } else {
+    $res = $self->$next($info, @extra);
+  }
+
+  # May have to reapply role if master will be reblessed to a more specific
+  # driver.
+  $self->master->_determine_driver;
+  DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($self->master);
+
+  $wantarray ? @res : $res;
+};
+
 =head1 METHODS
 
 This class defines the following methods.
@@ -348,7 +404,9 @@ Lazy builder for the L</master> attribute.
 
 sub _build_master {
   my $self = shift @_;
-  DBIx::Class::Storage::DBI->new($self->schema);
+  my $master = DBIx::Class::Storage::DBI->new($self->schema);
+  DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($master);
+  $master
 }
 
 =head2 _build_pool
@@ -403,13 +461,49 @@ sub _build_read_handler {
 =head2 around: connect_replicants
 
 All calls to connect_replicants needs to have an existing $schema tacked onto
-top of the args, since L<DBIx::Storage::DBI> needs it.
+top of the args, since L<DBIx::Storage::DBI> needs it, and any C<connect_info>
+options merged with the master, with replicant opts having higher priority.
 
 =cut
 
-around 'connect_replicants' => sub {
-  my ($method, $self, @args) = @_;
-  $self->$method($self->schema, @args);
+around connect_replicants => sub {
+  my ($next, $self, @args) = @_;
+
+  for my $r (@args) {
+    $r = [ $r ] unless reftype $r eq 'ARRAY';
+
+    croak "coderef replicant connect_info not supported"
+      if ref $r->[0] && reftype $r->[0] eq 'CODE';
+
+# any connect_info options?
+    my $i = 0;
+    $i++ while $i < @$r && (reftype($r->[$i])||'') ne 'HASH';
+
+# make one if none    
+    $r->[$i] = {} unless $r->[$i];
+
+# merge if two hashes
+    my @hashes = @$r[$i .. $#{$r}];
+
+    croak "invalid connect_info options"
+      if (grep { reftype($_) eq 'HASH' } @hashes) != @hashes;
+
+    croak "too many hashrefs in connect_info"
+      if @hashes > 2;
+
+    my %opts = %{ merge(reverse @hashes) };
+
+# delete them
+    splice @$r, $i+1, ($#{$r} - $i), ();
+
+# merge with master
+    %opts = %{ merge(\%opts, $self->_master_connect_info_opts) };
+
+# update
+    $r->[$i] = \%opts;
+  }
+
+  $self->$next($self->schema, @args);
 };
 
 =head2 all_storages
@@ -424,7 +518,7 @@ sub all_storages {
   my $self = shift @_;
   return grep {defined $_ && blessed $_} (
      $self->master,
-     $self->replicants,
+     values %{ $self->replicants },
   );
 }
 
@@ -694,6 +788,21 @@ sub disconnect {
   }
 }
 
+=head2 cursor_class
+
+set cursor class on all storages, or return master's
+
+=cut
+
+sub cursor_class {
+  my ($self, $cursor_class) = @_;
+
+  if ($cursor_class) {
+    $_->cursor_class($cursor_class) for $self->all_storages;
+  }
+  $self->master->cursor_class;
+}
+  
 =head1 GOTCHAS
 
 Due to the fact that replicants can lag behind a master, you must take care to
index 8af899c..798c0ef 100644 (file)
@@ -2,6 +2,9 @@ package DBIx::Class::Storage::DBI::Replicated::Balancer;
 
 use Moose::Role;
 requires 'next_storage';
+use MooseX::Types::Moose qw/Int/;
+
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -31,7 +34,7 @@ validating every query.
 
 has 'auto_validate_every' => (
   is=>'rw',
-  isa=>'Int',
+  isa=>Int,
   predicate=>'has_auto_validate_every',
 );
 
index e8fa630..b230346 100644 (file)
@@ -2,6 +2,7 @@ package DBIx::Class::Storage::DBI::Replicated::Balancer::First;
 
 use Moose;
 with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -50,4 +51,4 @@ You may distribute this code under the same terms as Perl itself.
 
 __PACKAGE__->meta->make_immutable;
 
-1;
\ No newline at end of file
+1;
index 6f786d3..f23db75 100644 (file)
@@ -2,6 +2,8 @@ package DBIx::Class::Storage::DBI::Replicated::Balancer::Random;
 
 use Moose;
 with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
+use DBIx::Class::Storage::DBI::Replicated::Types 'Weight';
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -26,6 +28,23 @@ you, patches welcome.
 
 This class defines the following attributes.
 
+=head2 master_read_weight
+
+A number greater than 0 that specifies what weight to give the master when
+choosing which backend to execute a read query on. A value of 0, which is the
+default, does no reads from master, while a value of 1 gives it the same
+priority as any single replicant.
+
+For example: if you have 2 replicants, and a L</master_read_weight> of C<0.5>,
+the chance of reading from master will be C<20%>.
+
+You can set it to a value higher than 1, making master have higher weight than
+any single replicant, if for example you have a very powerful master.
+
+=cut
+
+has master_read_weight => (is => 'rw', isa => Weight, default => sub { 0 });
+
 =head1 METHODS
 
 This class defines the following methods.
@@ -40,11 +59,23 @@ be requested several times in a row.
 
 sub next_storage {
   my $self = shift @_;
-  my @active_replicants = $self->pool->active_replicants;
-  my $count_active_replicants = $#active_replicants +1;
-  my $random_replicant = int(rand($count_active_replicants));
-  
-  return $active_replicants[$random_replicant];
+
+  my @replicants = $self->pool->active_replicants;
+
+  if (not @replicants) {
+    # will fall back to master anyway
+    return;
+  }
+
+  my $master     = $self->master;
+
+  my $rnd = $self->_random_number(@replicants + $self->master_read_weight);
+
+  return $rnd >= @replicants ? $master : $replicants[int $rnd];
+}
+
+sub _random_number {
+  rand($_[1])
 }
 
 =head1 AUTHOR
@@ -59,4 +90,4 @@ You may distribute this code under the same terms as Perl itself.
 
 __PACKAGE__->meta->make_immutable;
 
-1;
\ No newline at end of file
+1;
index 76ca7f2..2f3b444 100644 (file)
@@ -3,7 +3,12 @@ package DBIx::Class::Storage::DBI::Replicated::Pool;
 use Moose;
 use MooseX::AttributeHelpers;
 use DBIx::Class::Storage::DBI::Replicated::Replicant;
-use List::Util qw(sum);
+use List::Util 'sum';
+use Scalar::Util 'reftype';
+use Carp::Clan qw/^DBIx::Class/;
+use MooseX::Types::Moose qw/Num Int ClassName HashRef/;
+
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -37,7 +42,7 @@ return a number of seconds that the replicating database is lagging.
 
 has 'maximum_lag' => (
   is=>'rw',
-  isa=>'Num',
+  isa=>Num,
   required=>1,
   lazy=>1,
   default=>0,
@@ -53,7 +58,7 @@ builtin.
 
 has 'last_validated' => (
   is=>'rw',
-  isa=>'Int',
+  isa=>Int,
   reader=>'last_validated',
   writer=>'_last_validated',
   lazy=>1,
@@ -70,7 +75,7 @@ just leave this alone.
 
 has 'replicant_type' => (
   is=>'ro',
-  isa=>'ClassName',
+  isa=>ClassName,
   required=>1,
   default=>'DBIx::Class::Storage::DBI',
   handles=>{
@@ -120,7 +125,7 @@ removes the replicant under $key from the pool
 has 'replicants' => (
   is=>'rw',
   metaclass => 'Collection::Hash',
-  isa=>'HashRef[DBIx::Class::Storage::DBI]',
+  isa=>HashRef['DBIx::Class::Storage::DBI'],
   default=>sub {{}},
   provides  => {
     'set' => 'set_replicant',
@@ -137,9 +142,9 @@ This class defines the following methods.
 
 =head2 connect_replicants ($schema, Array[$connect_info])
 
-Given an array of $dsn suitable for connected to a database, create an
-L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
-L</replicants> attribute.
+Given an array of $dsn or connect_info structures suitable for connected to a
+database, create an L<DBIx::Class::Storage::DBI::Replicated::Replicant> object
+and store it in the L</replicants> attribute.
 
 =cut
 
@@ -149,8 +154,18 @@ sub connect_replicants {
   
   my @newly_created = ();
   foreach my $connect_info (@_) {
+    $connect_info = [ $connect_info ]
+      if reftype $connect_info ne 'ARRAY';
+
+    croak "coderef replicant connect_info not supported"
+      if ref $connect_info->[0] && reftype $connect_info->[0] eq 'CODE';
+
     my $replicant = $self->connect_replicant($schema, $connect_info);
-    my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
+
+    my $key = $connect_info->[0];
+    $key = $key->{dsn} if ref $key && reftype $key eq 'HASH';
+    ($key) = ($key =~ m/^dbi\:.+\:(.+)$/);
+
     $self->set_replicant( $key => $replicant);  
     push @newly_created, $replicant;
   }
@@ -169,7 +184,20 @@ sub connect_replicant {
   my ($self, $schema, $connect_info) = @_;
   my $replicant = $self->create_replicant($schema);
   $replicant->connect_info($connect_info);
-  $self->_safely_ensure_connected($replicant);
+
+## It is undesirable for catalyst to connect at ->conect_replicants time, as
+## connections should only happen on the first request that uses the database.
+## So we try to set the driver without connecting, however this doesn't always
+## work, as a driver may need to connect to determine the DB version, and this
+## may fail.
+##
+## Why this is necessary at all, is that we need to have the final storage
+## class to apply the Replicant role.
+
+  $self->_safely($replicant, '->_determine_driver', sub {
+    $replicant->_determine_driver
+  });
+
   DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);  
   return $replicant;
 }
@@ -180,21 +208,39 @@ The standard ensure_connected method with throw an exception should it fail to
 connect.  For the master database this is desirable, but since replicants are
 allowed to fail, this behavior is not desirable.  This method wraps the call
 to ensure_connected in an eval in order to catch any generated errors.  That
-way a slave to go completely offline (ie, the box itself can die) without
+way a slave can go completely offline (ie, the box itself can die) without
 bringing down your entire pool of databases.
 
 =cut
 
 sub _safely_ensure_connected {
   my ($self, $replicant, @args) = @_;
+
+  return $self->_safely($replicant, '->ensure_connected', sub {
+    $replicant->ensure_connected(@args)
+  });
+}
+
+=head2 _safely ($replicant, $name, $code)
+
+Execute C<$code> for operation C<$name> catching any exceptions and printing an
+error message to the C<<$replicant->debugobj>>.
+
+Returns 1 on success and undef on failure.
+
+=cut
+
+sub _safely {
+  my ($self, $replicant, $name, $code) = @_;
+
   eval {
-    $replicant->ensure_connected(@args);
+    $code->()
   }; 
   if ($@) {
     $replicant
       ->debugobj
       ->print(
-        sprintf( "Exception trying to ->ensure_connected for replicant %s, error is %s",
+        sprintf( "Exception trying to $name for replicant %s, error is %s",
           $replicant->_dbi_connect_info->[0], $@)
         );
        return;
@@ -280,13 +326,13 @@ sub validate_replicants {
     if($self->_safely_ensure_connected($replicant)) {
       my $is_replicating = $replicant->is_replicating;
       unless(defined $is_replicating) {
-        $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'is_replicating' method.  Assuming you are manually managing.");
+        $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'is_replicating' method.  Assuming you are manually managing.\n");
         next;
       } else {
         if($is_replicating) {
           my $lag_behind_master = $replicant->lag_behind_master;
           unless(defined $lag_behind_master) {
-            $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'lag_behind_master' method.  Assuming you are manually managing.");
+            $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'lag_behind_master' method.  Assuming you are manually managing.\n");
             next;
           } else {
             if($lag_behind_master <= $self->maximum_lag) {
index e9612f3..9c9f1c2 100644 (file)
@@ -2,6 +2,10 @@ package DBIx::Class::Storage::DBI::Replicated::Replicant;
 
 use Moose::Role;
 requires qw/_query_start/;
+with 'DBIx::Class::Storage::DBI::Replicated::WithDSN';
+use MooseX::Types::Moose 'Bool';
+
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -42,7 +46,7 @@ storage driver for more information.
 
 has 'active' => (
   is=>'rw',
-  isa=>'Bool',
+  isa=>Bool,
   lazy=>1,
   required=>1,
   default=>1,
@@ -52,18 +56,6 @@ has 'active' => (
 
 This class defines the following methods.
 
-=head2 around: _query_start
-
-advice iof the _query_start method to add more debuggin
-
-=cut
-
-around '_query_start' => sub {
-  my ($method, $self, $sql, @bind) = @_;
-  my $dsn = $self->_dbi_connect_info->[0];
-  $self->$method("DSN: $dsn SQL: $sql", @bind);
-};
-
 =head2 debugobj
 
 Override the debugobj method to redirect this method call back to the master.
@@ -76,7 +68,8 @@ sub debugobj {
 
 =head1 ALSO SEE
 
-L<<a href="http://en.wikipedia.org/wiki/Replicant">http://en.wikipedia.org/wiki/Replicant</a>>
+L<http://en.wikipedia.org/wiki/Replicant>,
+L<DBIx::Class::Storage::DBI::Replicated>
 
 =head1 AUTHOR
 
@@ -88,4 +81,4 @@ You may distribute this code under the same terms as Perl itself.
 
 =cut
 
-1;
\ No newline at end of file
+1;
diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm
new file mode 100644 (file)
index 0000000..c366ea5
--- /dev/null
@@ -0,0 +1,47 @@
+package # hide from PAUSE
+  DBIx::Class::Storage::DBI::Replicated::Types;
+
+=head1 NAME
+
+DBIx::Class::Storage::DBI::Replicated::Types - Types used internally by
+L<DBIx::Class::Storage::DBI::Replicated>
+
+=cut
+
+use MooseX::Types
+  -declare => [qw/BalancerClassNamePart Weight/];
+use MooseX::Types::Moose qw/ClassName Str Num/;
+
+class_type 'DBIx::Class::Storage::DBI';
+class_type 'DBIx::Class::Schema';
+
+subtype BalancerClassNamePart,
+  as ClassName;
+    
+coerce BalancerClassNamePart,
+  from Str,
+  via {
+    my $type = $_;
+    if($type=~m/^::/) {
+      $type = 'DBIx::Class::Storage::DBI::Replicated::Balancer'.$type;
+    }  
+    Class::MOP::load_class($type);  
+    $type;     
+  };
+
+subtype Weight,
+  as Num,
+  where { $_ >= 0 },
+  message { 'weight must be a decimal greater than 0' };
+
+=head1 AUTHOR
+
+  John Napiorkowski <john.napiorkowski@takkle.com>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+
+1;
diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/WithDSN.pm b/lib/DBIx/Class/Storage/DBI/Replicated/WithDSN.pm
new file mode 100644 (file)
index 0000000..69a3add
--- /dev/null
@@ -0,0 +1,51 @@
+package DBIx::Class::Storage::DBI::Replicated::WithDSN;
+
+use Moose::Role;
+requires qw/_query_start/;
+
+use namespace::clean -except => 'meta';
+
+=head1 NAME
+
+DBIx::Class::Storage::DBI::Replicated::WithDSN - A DBI Storage Role with DSN
+information in trace output
+
+=head1 SYNOPSIS
+
+This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>.
+    
+=head1 DESCRIPTION
+
+This role adds C<DSN: > info to storage debugging output.
+
+=head1 METHODS
+
+This class defines the following methods.
+
+=head2 around: _query_start
+
+Add C<DSN: > to debugging output.
+
+=cut
+
+around '_query_start' => sub {
+  my ($method, $self, $sql, @bind) = @_;
+  my $dsn = $self->_dbi_connect_info->[0];
+  $self->$method("DSN: $dsn SQL: $sql", @bind);
+};
+
+=head1 ALSO SEE
+
+L<DBIx::Class::Storage::DBI>
+
+=head1 AUTHOR
+
+John Napiorkowski <john.napiorkowski@takkle.com>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+
+1;
index e29c2ee..5e125c9 100644 (file)
@@ -6,7 +6,7 @@ use POSIX 'strftime';
 use File::Copy;
 use File::Spec;
 
-use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
+use base qw/DBIx::Class::Storage::DBI/;
 
 sub _dbh_last_insert_id {
   my ($self, $dbh, $source, $col) = @_;
@@ -45,21 +45,6 @@ sub backup
   return $backupfile;
 }
 
-sub disconnect {
-
-  # As described in this node http://www.perlmonks.org/?node_id=666210
-  # there seems to be no sane way to ->disconnect a SQLite database with
-  # cached statement handles. As per mst we just zap the cache and 
-  # proceed as normal.
-
-  my $self = shift;
-  if ($self->connected) {
-    $self->_dbh->{CachedKids} = {};
-    $self->next::method (@_);
-  }
-}
-
-
 1;
 
 =head1 NAME
index f18de95..28b4059 100644 (file)
@@ -3,17 +3,51 @@ package DBIx::Class::Storage::DBI::Sybase;
 use strict;
 use warnings;
 
-use base qw/DBIx::Class::Storage::DBI::NoBindVars/;
+use base qw/DBIx::Class::Storage::DBI/;
 
-my %noquote = map ($_ => 1), qw(int integer);
-
-sub should_quote_data_type {
+sub _rebless {
   my $self = shift;
-  my ($type) = @_;
-  return 0 if $noquote{$type};
-  return $self->next::method(@_);
+
+  if (ref($self) eq 'DBIx::Class::Storage::DBI::Sybase') {
+    my $dbtype = eval {
+      @{$self->dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2]
+    } || '';
+
+    my $exception = $@;
+    $dbtype =~ s/\W/_/gi;
+    my $subclass = "DBIx::Class::Storage::DBI::Sybase::${dbtype}";
+
+    if (!$exception && $dbtype && $self->load_optional_class($subclass)) {
+      bless $self, $subclass;
+      $self->_rebless;
+    } else { # probably real Sybase
+      if (not $self->dbh->{syb_dynamic_supported}) {
+        bless $self, 'DBIx::Class::Storage:DBI::Sybase::NoBindVars';
+        $self->_rebless;
+      }
+
+      $self->dbh->syb_date_fmt('ISO_strict');
+      $self->dbh->do('set dateformat mdy');
+    }
+  }
 }
 
+sub _dbh_last_insert_id {
+  my ($self, $dbh, $source, $col) = @_;
+
+  if (not $self->dbh->{syb_dynamic_supported}) {
+    # @@identity works only if not using placeholders
+    # Should this query be cached?
+    return ($dbh->selectrow_array('select @@identity'))[0];
+  }
+
+  # sorry, there's no other way!
+  my $sth = $dbh->prepare_cached("select max($col) from ".$source->from);
+  return ($dbh->selectrow_array($sth))[0];
+}
+
+sub datetime_parser_type { "DBIx::Class::Storage::DBI::Sybase::DateTime" }
+
 1;
 
 =head1 NAME
@@ -26,12 +60,43 @@ This subclass supports L<DBD::Sybase> for real Sybase databases.  If
 you are using an MSSQL database via L<DBD::Sybase>, see
 L<DBIx::Class::Storage::DBI::Sybase::MSSQL>.
 
+=head1 CAVEATS
+
+If your version of Sybase does not support placeholders, then this storage
+driver uses L<DBIx::Class::Storage::DBI::NoBindVars> as a base,
+
+In which case, bind variables will be interpolated (properly quoted of course)
+into the SQL query itself, without using bind placeholders.
+
+More importantly this means that caching of prepared statements is explicitly
+disabled, as the interpolation renders it useless.
+
+If your version of Sybase B<DOES> support placeholders (check
+C<<$dbh->{syb_dynamic_supported}>> then unfortunately there's no way to get the
+C<last_insert_id> without doing a C<select max(col)>.
+
+But your queries will be cached.
+
+=head1 DATES
+
+On connection C<syb_date_fmt> is set to C<ISO_strict>, e.g.:
+C<2004-08-21T14:36:48.080Z> and C<dateformat> is set to C<mdy>, e.g.:
+C<08/13/1979>.
+
+You will need the L<DateTime::Format::Strptime> module if you are going to use
+L<DBIx::Class::InflateColumn::DateTime>.
+
 =head1 AUTHORS
 
 Brandon L Black <blblack@gmail.com>
 
+Justin Hunter <justin.d.hunter@gmail.com>
+
+Rafael Kitover <rkitover@cpan.org>
+
 =head1 LICENSE
 
 You may distribute this code under the same terms as Perl itself.
 
 =cut
+# vim:sts=2 sw=2:
index 81222e9..cc3ad12 100644 (file)
@@ -3,8 +3,11 @@ package DBIx::Class::Storage::DBI::Sybase::MSSQL;
 use strict;
 use warnings;
 
-use Class::C3;
-use base qw/DBIx::Class::Storage::DBI::MSSQL DBIx::Class::Storage::DBI::Sybase/;
+use base qw/
+  DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server
+  DBIx::Class::Storage::DBI::NoBindVars
+  DBIx::Class::Storage::DBI::Sybase
+/;
 
 1;
 
@@ -29,6 +32,8 @@ after connecting.
 
 Brandon L Black <blblack@gmail.com>
 
+Justin Hunter <justin.d.hunter@gmail.com>
+
 =head1 LICENSE
 
 You may distribute this code under the same terms as Perl itself.
diff --git a/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm
new file mode 100644 (file)
index 0000000..63f45ee
--- /dev/null
@@ -0,0 +1,45 @@
+package DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server;
+
+use strict;
+use warnings;
+
+use base qw/
+  DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server
+  DBIx::Class::Storage::DBI::NoBindVars
+  DBIx::Class::Storage::DBI::Sybase
+/;
+
+1;
+
+=head1 NAME
+
+DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server - Storage::DBI subclass for MSSQL via
+DBD::Sybase
+
+=head1 SYNOPSIS
+
+This subclass supports MSSQL connected via L<DBD::Sybase>.
+
+  $schema->storage_type('::DBI::Sybase::Microsoft_SQL_Server');
+  $schema->connect_info('dbi:Sybase:....', ...);
+
+=head1 CAVEATS
+
+This storage driver uses L<DBIx::Class::Storage::DBI::NoBindVars> as a base.
+This means that bind variables will be interpolated (properly quoted of course)
+into the SQL query itself, without using bind placeholders.
+
+More importantly this means that caching of prepared statements is explicitly
+disabled, as the interpolation renders it useless.
+
+=head1 AUTHORS
+
+Brandon L Black <blblack@gmail.com>
+
+Justin Hunter <justin.d.hunter@gmail.com>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
index fa0419f..7391593 100644 (file)
@@ -3,7 +3,7 @@ package DBIx::Class::Storage::DBI::mysql;
 use strict;
 use warnings;
 
-use base qw/DBIx::Class::Storage::DBI/;
+use base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
 
 # __PACKAGE__->load_components(qw/PK::Auto/);
 
@@ -51,6 +51,12 @@ sub lag_behind_master {
     return shift->dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
 }
 
+# MySql can not do subquery update/deletes, only way is slow per-row operations.
+# This assumes you have proper privilege separation and use innodb.
+sub subq_update_delete {
+  return shift->_per_row_update_delete (@_);
+}
+
 1;
 
 =head1 NAME
index b60c44e..22dcadc 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 use base qw/Class::Accessor::Grouped/;
 use IO::File;
 
-__PACKAGE__->mk_group_accessors(simple => qw/callback debugfh/);
+__PACKAGE__->mk_group_accessors(simple => qw/callback debugfh silence/);
 
 =head1 NAME
 
@@ -56,6 +56,8 @@ to display the message.
 sub print {
   my ($self, $msg) = @_;
 
+  return if $self->silence;
+
   if(!defined($self->debugfh())) {
     my $fh;
     my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}
@@ -75,6 +77,10 @@ sub print {
   $self->debugfh->print($msg);
 }
 
+=head2 silence
+
+Turn off all output if set to true.
+
 =head2 txn_begin
 
 Called when a transaction begins.
index ca7cad7..1c2a070 100644 (file)
@@ -1,8 +1,8 @@
-package # Hide from pause for now - till we get it working
-  DBIx::Class::Storage::TxnScopeGuard;
+package DBIx::Class::Storage::TxnScopeGuard;
 
 use strict;
 use warnings;
+use Carp ();
 
 sub new {
   my ($class, $storage) = @_;
@@ -47,7 +47,7 @@ __END__
 
 =head1 NAME
 
-DBIx::Class::Storage::TxnScopeGuard - Experimental
+DBIx::Class::Storage::TxnScopeGuard - Scope-based transaction handling
 
 =head1 SYNOPSIS
 
@@ -70,14 +70,15 @@ right thing with transactions in DBIx::Class.
 
 =head2 new
 
-Creating an instance of this class will start a new transaction. Expects a
+Creating an instance of this class will start a new transaction (by
+implicitly calling L<DBIx::Class::Storage/txn_begin>. Expects a
 L<DBIx::Class::Storage> object as its only argument.
 
 =head2 commit
 
 Commit the transaction, and stop guarding the scope. If this method is not
-called (i.e. an exception is thrown) and this object goes out of scope then
-the transaction is rolled back.
+called and this object goes out of scope (i.e. an exception is thrown) then
+the transaction is rolled back, via L<DBIx::Class::Storage/txn_rollback>
 
 =cut
 
index bff7ddc..97e333c 100644 (file)
@@ -13,7 +13,6 @@ $VERSION = '1.10';
 $DEBUG = 0 unless defined $DEBUG;
 
 use Exporter;
-use Data::Dumper;
 use SQL::Translator::Utils qw(debug normalize_name);
 
 use base qw(Exporter);
@@ -114,7 +113,7 @@ sub parse {
         my @primary = $source->primary_columns;
         my %unique_constraints = $source->unique_constraints;
         foreach my $uniq (sort keys %unique_constraints) {
-            if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
+            if (!$source->_compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
                 $table->add_constraint(
                             type             => 'unique',
                             name             => $uniq,
@@ -168,7 +167,7 @@ sub parse {
             # this is supposed to indicate a has_one/might_have...
             # where's the introspection!!?? :)
             else {
-                $fk_constraint = not $source->compare_relationship_keys(\@keys, \@primary);
+                $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary);
             }
 
             my $cascade;
@@ -246,6 +245,8 @@ sub parse {
         if ($source->result_class->can('sqlt_deploy_hook')) {
           $source->result_class->sqlt_deploy_hook($view);
         }
+
+        $source->_invoke_sqlt_deploy_hook($view);
     }
 
     if ($dbicschema->can('sqlt_deploy_hook')) {
@@ -291,14 +292,14 @@ C<SQL::Translator::Parser::DBIx::Class> reads a DBIx::Class schema,
 interrogates the columns, and stuffs it all in an $sqlt_schema object.
 
 It's primary use is in deploying database layouts described as a set
-of L<DBIx::Class> classes, to a database. To do this, see the
-L<DBIx::Class::Schema/deploy> method.
+of L<DBIx::Class> classes, to a database. To do this, see
+L<DBIx::Class::Schema/deploy>.
 
 This can also be achieved by having DBIx::Class export the schema as a
 set of SQL files ready for import into your database, or passed to
 other machines that need to have your application installed but don't
-have SQL::Translator installed. To do this see the
-L<DBIx::Class::Schema/create_ddl_dir> method.
+have SQL::Translator installed. To do this see
+L<DBIx::Class::Schema/create_ddl_dir>.
 
 =head1 SEE ALSO
 
index 300ce50..4132c73 100644 (file)
@@ -25,6 +25,7 @@ $DEBUG   = 0 unless defined $DEBUG;
 
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Utils qw(header_comment);
+use Data::Dumper ();
 
 ## Skip all column type translation, as we want to use whatever the parser got.
 
index 18c5292..1f4c5f6 100644 (file)
@@ -34,6 +34,15 @@ my $exceptions = {
            qw( MULTICREATE_DEBUG )
         ],
     },
+    'DBIx::Class::ResultSource' => {
+        ignore => [qw/
+          compare_relationship_keys
+          pk_depends_on
+          resolve_condition
+          resolve_join
+          resolve_prefetch
+        /],
+    },
     'DBIx::Class::Storage' => {
         ignore => [
             qw(cursor)
@@ -44,6 +53,11 @@ my $exceptions = {
             qw(setup_connection_class)
         ]
     },
+    'DBIx::Class::Storage::DBI::Sybase' => {
+        ignore => [
+            qw/should_quote_data_type/,
+        ]
+    },
     'DBIx::Class::CDBICompat::AccessorMapping'          => { skip => 1 },
     'DBIx::Class::CDBICompat::AbstractSearch' => {
         ignore => [qw(search_where)]
@@ -94,7 +108,6 @@ my $exceptions = {
     'DBIx::Class::Storage::DBI'                         => { skip => 1 },
     'DBIx::Class::Storage::DBI::DB2'                    => { skip => 1 },
     'DBIx::Class::Storage::DBI::MSSQL'                  => { skip => 1 },
-    'DBIx::Class::Storage::DBI::MultiDistinctEmulation' => { skip => 1 },
     'DBIx::Class::Storage::DBI::ODBC400'                => { skip => 1 },
     'DBIx::Class::Storage::DBI::ODBC::DB2_400_SQL'      => { skip => 1 },
     'DBIx::Class::Storage::DBI::Oracle'                 => { skip => 1 },
index 5869869..a7de9fc 100644 (file)
@@ -1,9 +1,10 @@
-#!/usr/bin/perl -w
-#Simon Ilyushchenko, 12/05/05
-#Testing the case when we try to inject into @ISA a class that's already a parent of the target class.
 
 use strict;
 use Test::More tests => 2;
+use MRO::Compat;
+
+use lib qw(t/lib);
+use DBICTest; # do not remove even though it is not used
 
 {
 package AAA;
@@ -26,8 +27,8 @@ use base 'AAA';
 __PACKAGE__->inject_base( __PACKAGE__, 'DBIx::Class::Core' );
 }
 
-eval { Class::C3::calculateMRO('BBB'); };
+eval { mro::get_linear_isa('BBB'); };
 ok (! $@, "Correctly skipped injecting a direct parent of class BBB");
 
-eval { Class::C3::calculateMRO('CCC'); };
+eval { mro::get_linear_isa('CCC'); };
 ok (! $@, "Correctly skipped injecting an indirect parent of class BBB");
index 5c531fd..05d99b9 100644 (file)
@@ -12,7 +12,7 @@ use DBICTest;
     use base qw/DBIx::Class::ResultSource::Table/;
 }
 
-plan tests => 3;
+plan tests => 4;
 
 my $schema = DBICTest->init_schema();
 my $artist_source = $schema->source('Artist');
@@ -36,6 +36,12 @@ local $SIG{__WARN__} = sub { $warn = shift };
 }
 
 {
+  my $source = $schema->source('DBICTest::Artist');
+  $schema->register_source($source->source_name, $source);
+  is($warn, '', "re-registering an existing source under the same name causes no errors");
+}
+
+{
   my $new_source_name = 'Artist->preview(artist_preview)';
   $schema->register_source( $new_source_name => $new_source );
 
index a8f790a..37a0472 100644 (file)
@@ -5,42 +5,38 @@ use Test::More;
 use lib qw(t/lib);
 use Data::Dumper;
 
-plan ( ($] >= 5.009000 and $] < 5.010001)
-  ? (skip_all => 'warnings::register broken under 5.10: http://rt.perl.org/rt3/Public/Bug/Display.html?id=62522')
-  : (tests => 4)
-);
+plan tests => 4;
+my $exp_warn = qr/The many-to-many relationship 'bars' is trying to create/;
 
 {
   my @w; 
-  local $SIG{__WARN__} = sub { push @w, @_ };
+  local $SIG{__WARN__} = sub { $_[0] =~ $exp_warn ? push @w, $_[0] : warn $_[0] };
   my $code = gen_code ( suffix => 1 );
   eval "$code";
   ok (! $@, 'Eval code without warnings suppression')
     || diag $@;
 
-  ok ( (grep { $_ =~ /The many-to-many relationship bars is trying to create/ } @w), "Warning triggered without relevant 'no warnings'");
+  ok (@w, "Warning triggered without DBIC_OVERWRITE_HELPER_METHODS_OK");
 }
 
 {
   my @w; 
-  local $SIG{__WARN__} = sub { push @w, @_ };
+  local $SIG{__WARN__} = sub { $_[0] =~ $exp_warn ? push @w, $_[0] : warn $_[0] };
 
-  my $code = gen_code ( suffix => 2, no_warn => 1 );
+  my $code = gen_code ( suffix => 2 );
+
+  local $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK} = 1;
   eval "$code";
   ok (! $@, 'Eval code with warnings suppression')
     || diag $@;
 
-  ok ( (not grep { $_ =~ /The many-to-many relationship bars is trying to create/ } @w), "No warning triggered with relevant 'no warnings'");
+  ok (! @w, "No warning triggered with DBIC_OVERWRITE_HELPER_METHODS_OK");
 }
 
 sub gen_code {
 
   my $args = { @_ };
   my $suffix = $args->{suffix};
-  my $no_warn = ( $args->{no_warn}
-    ? "no warnings 'DBIx::Class::Relationship::ManyToMany';"
-    : '',
-  );
 
   return <<EOF;
 use strict;
@@ -95,7 +91,6 @@ use warnings;
     },
   );
 
-  ${no_warn}
   __PACKAGE__->set_primary_key('barid');
   __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'foo');
 
index d8ba469..c4768c8 100644 (file)
@@ -19,12 +19,12 @@ use_ok('DBICTest');
 use_ok('DBIC::DebugObj');
 my $schema = DBICTest->init_schema();
 
-diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
+#diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
 
 $schema->storage->sql_maker->quote_char('`');
 $schema->storage->sql_maker->name_sep('.');
 
-my ($sql, @bind) = ('');
+my ($sql, @bind);
 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
 $schema->storage->debug(1);
 
index 748b112..f0c34a9 100644 (file)
@@ -19,7 +19,7 @@ use_ok('DBIC::DebugObj');
 
 my $schema = DBICTest->init_schema();
 
-diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
+#diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
 
 my $dsn = $schema->storage->_dbi_connect_info->[0];
 $schema->connection(
@@ -30,7 +30,7 @@ $schema->connection(
   { quote_char => '`', name_sep => '.' },
 );
 
-my ($sql, @bind) = ('');
+my ($sql, @bind);
 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
 $schema->storage->debug(1);
 
index 993cfad..8f1eba1 100644 (file)
@@ -21,12 +21,11 @@ cmp_ok(@art, '==', 3, "Three artists returned");
 
 # Disconnect the dbh, and be sneaky about it
 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
-TODO: {
-    local $TODO = 'SQLite is evil/braindead. Once this test starts passing, remove the related atrocity from DBIx::Class::Storage::DBI::SQLite';
-    my $w;
-    local $SIG{__WARN__} = sub { $w = shift };
-    $schema->storage->_dbh->disconnect;
-    ok ($w !~ /active statement handles/, 'SQLite can disconnect properly \o/');
+{
+  my $w;
+  local $SIG{__WARN__} = sub { $w = shift };
+  $schema->storage->_dbh->disconnect;
+  ok ($w !~ /active statement handles/, 'SQLite can disconnect properly');
 }
 
 # Try the operation again - What should happen here is:
index 84ddd83..f20ca8c 100644 (file)
@@ -4,7 +4,8 @@ use strict;
 use warnings;
 use Test::More;
 
-unshift(@INC, './t/lib');
+use lib qw(t/lib);
+use DBICTest; # do not remove even though it is not used
 
 plan tests => 8;
 
index 6daf05f..5b31c09 100644 (file)
@@ -4,7 +4,8 @@ use strict;
 use warnings;
 use Test::More;
 
-unshift(@INC, './t/lib');
+use lib qw(t/lib);
+use DBICTest; # do not remove even though it is not used
 
 plan tests => 6;
 
index f48c838..f4fa386 100644 (file)
@@ -4,7 +4,8 @@ use strict;
 use warnings;
 use Test::More;
 
-unshift(@INC, './t/lib');
+use lib qw(t/lib);
+use DBICTest; # do not remove even though it is not used
 
 plan tests => 7;
 
index b674f30..4c7c818 100644 (file)
@@ -4,7 +4,8 @@ use strict;
 use warnings;
 use Test::More;
 
-unshift(@INC, './t/lib');
+use lib qw(t/lib);
+use DBICTest; # do not remove even though it is not used
 
 plan tests => 6;
 
index ba99fe5..293506b 100644 (file)
@@ -2,41 +2,66 @@
 
 use strict;
 use warnings;
-use Test::More;
 
 use lib 't/lib';
-
-plan tests => 4;
+use DBICTest; # do not remove even though it is not used 
+use Test::More tests => 8;
 
 sub _chk_warning {
-       defined $_[0]? 
-               $_[0] !~ qr/We found ResultSet class '([^']+)' for '([^']+)', but it seems that you had already set '([^']+)' to use '([^']+)' instead/ :
-               1
+  defined $_[0]?
+    $_[0] !~ qr/We found ResultSet class '([^']+)' for '([^']+)', but it seems that you had already set '([^']+)' to use '([^']+)' instead/ :
+    1
+}
+
+sub _chk_extra_sources_warning {
+  my $p = qr/already has a source, use register_extra_source for additional sources/;
+  defined $_[0]? $_[0] !~ /$p/ : 1;
+}
+
+sub _verify_sources {
+  my @monikers = @_;
+  is_deeply (
+    [ sort DBICNSTest::RtBug41083->sources ],
+    \@monikers,
+    'List of resultsource registrations',
+  );
 }
 
-my $warnings;
-eval {
+{
+  my $warnings;
+  eval {
     local $SIG{__WARN__} = sub { $warnings .= shift };
     package DBICNSTest::RtBug41083;
     use base 'DBIx::Class::Schema';
     __PACKAGE__->load_namespaces(
-       result_namespace => 'Schema_A',
-       resultset_namespace => 'ResultSet_A',
-       default_resultset_class => 'ResultSet'
+      result_namespace => 'Schema_A',
+      resultset_namespace => 'ResultSet_A',
+      default_resultset_class => 'ResultSet'
     );
-};
-ok(!$@) or diag $@;
-ok(_chk_warning($warnings), 'expected no complaint');
+  };
 
-eval {
+  ok(!$@) or diag $@;
+  ok(_chk_warning($warnings), 'expected no resultset complaint');
+  ok(_chk_extra_sources_warning($warnings), 'expected no extra sources complaint') or diag($warnings);
+
+  _verify_sources (qw/A A::Sub/);
+}
+
+{
+  my $warnings;
+  eval {
     local $SIG{__WARN__} = sub { $warnings .= shift };
     package DBICNSTest::RtBug41083;
     use base 'DBIx::Class::Schema';
     __PACKAGE__->load_namespaces(
-       result_namespace => 'Schema',
-       resultset_namespace => 'ResultSet',
-       default_resultset_class => 'ResultSet'
+      result_namespace => 'Schema',
+      resultset_namespace => 'ResultSet',
+      default_resultset_class => 'ResultSet'
     );
-};
-ok(!$@) or diag $@;
-ok(_chk_warning($warnings), 'expected no complaint') or diag $warnings;
+  };
+  ok(!$@) or diag $@;
+  ok(_chk_warning($warnings), 'expected no resultset complaint') or diag $warnings;
+  ok(_chk_extra_sources_warning($warnings), 'expected no extra sources complaint') or diag($warnings);
+
+  _verify_sources (qw/A A::Sub Foo Foo::Sub/);
+}
index 660ee40..b0117a7 100644 (file)
@@ -2,14 +2,15 @@ use strict;
 use warnings;
 
 use Test::More;
-use DBIx::Class::Storage::DBI::Oracle::WhereJoins;
+use DBIx::Class::SQLAHacks::OracleJoins;
 
 use lib qw(t/lib);
+use DBICTest; # do not remove even though it is not used
 use DBIC::SqlMakerTest;
 
 plan tests => 4;
 
-my $sa = new DBIC::SQL::Abstract::Oracle;
+my $sa = new DBIx::Class::SQLAHacks::OracleJoins;
 
 $sa->limit_dialect('RowNum');
 
index 4fad29c..aefbc0c 100644 (file)
@@ -3,24 +3,43 @@ use warnings;
 \r
 use Test::More;\r
 use DBIx::Class::Storage::DBI;\r
+use lib qw(t/lib);\r
+use DBICTest; # do not remove even though it is not used\r
+use DBIC::SqlMakerTest;\r
 \r
-plan tests => 1;\r
-\r
-my $sa = new DBIC::SQL::Abstract;\r
+plan tests => 8;\r
 \r
+my $sa = new DBIx::Class::SQLAHacks;\r
 $sa->limit_dialect( 'Top' );\r
 \r
-is(\r
-    $sa->select( 'rubbish', [ 'foo.id', 'bar.id' ], undef, { order_by => 'artistid' }, 1, 3 ),\r
-    'SELECT * FROM\r
-(\r
-    SELECT TOP 1 * FROM\r
-    (\r
-        SELECT TOP 4  foo.id, bar.id FROM rubbish ORDER BY artistid ASC\r
-    ) AS foo\r
-    ORDER BY artistid DESC\r
-) AS bar\r
-ORDER BY artistid ASC\r
-',\r
-    "make sure limit_dialect( 'Top' ) is working okay"\r
-);\r
+sub test_order {\r
+  my $args = shift;\r
+  my $order_by = $args->{order_by};\r
+  my $expected_sql_order = $args->{expected_sql_order};\r
+\r
+  my $query = $sa->select( 'foo', [qw{bar baz}], undef, {\r
+      order_by => $order_by,\r
+     }, 1, 3\r
+  );\r
+  is_same_sql(\r
+    $query,\r
+    "SELECT * FROM ( SELECT TOP 1 * FROM ( SELECT TOP 4 bar,baz FROM foo ORDER BY $expected_sql_order->[0] ) AS foo ORDER BY $expected_sql_order->[1] ) AS bar ORDER BY $expected_sql_order->[0]",\r
+  );\r
+}\r
+\r
+  test_order({ order_by => \'foo DESC'       , expected_sql_order => [ 'foo DESC', 'foo ASC' ] });\r
+  test_order({ order_by => 'foo'             , expected_sql_order => [ 'foo ASC', 'foo DESC'] });\r
+  test_order({ order_by => [ qw{ foo bar}   ], expected_sql_order => [ 'foo ASC,bar ASC', 'foo DESC, bar DESC']});\r
+  test_order({ order_by => { -asc => 'foo'  }, expected_sql_order => [ 'foo ASC', 'foo DESC' ] });\r
+  test_order({ order_by => { -desc => 'foo' }, expected_sql_order => [ 'foo DESC', 'foo ASC' ] });\r
+\r
+  test_order({ order_by => ['foo', { -desc => 'bar' } ], expected_sql_order => [ 'foo ASC, bar DESC', 'foo DESC, bar ASC'] });\r
+  test_order({ order_by => {-asc => [qw{ foo bar }] }, expected_sql_order => ['foo ASC, bar ASC', 'foo DESC, bar DESC' ] });\r
+  test_order({ order_by =>\r
+      [\r
+        { -asc => 'foo' },\r
+        { -desc => [qw{bar}] },\r
+        { -asc  => [qw{baz frew}]},\r
+      ],\r
+      expected_sql_order => ['foo ASC, bar DESC, baz ASC, frew ASC', 'foo DESC, bar ASC, baz DESC, frew DESC']\r
+  });\r
index 3bc1935..b9946a4 100644 (file)
@@ -3,7 +3,9 @@ use warnings;
 
 use Test::More;
 use lib qw(t/lib);
-use DBICTest;
+use DBIC::SqlMakerTest;
+
+use_ok('DBICTest');
 
 my $schema = DBICTest->init_schema;
 
@@ -11,11 +13,9 @@ BEGIN {
     eval "use DBD::SQLite";
     plan $@
         ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 7 );
+        : ( tests => 9 );
 }
 
-### $schema->storage->debug(1);
-
 my $where_bind = {
     where => \'name like ?',
     bind  => [ 'Cat%' ],
@@ -55,10 +55,10 @@ my $new_source = $source->new($source);
 $new_source->source_name('Complex');
 
 $new_source->name(\<<'');
-( select a.*, cd.cdid as cdid, cd.title as title, cd.year as year 
-  from artist a
-  join cd on cd.artist=a.artistid
-  where cd.year=?)
+( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year 
+  FROM artist a
+  JOIN cd ON cd.artist = a.artistid
+  WHERE cd.year = ?)
 
 $schema->register_extra_source('Complex' => $new_source);
 
@@ -72,11 +72,21 @@ $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
     ->search({ 'artistid' => 1 });
 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
 
-TODO: {
-    # not sure what causes an uninit warning here, please remove when the TODO starts to pass,
-    # so the real reason for the warning can be found and fixed
-    local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /uninitialized/ };
+{
+  $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year FROM artist a JOIN cd ON cd.artist = a.artistid WHERE cd.year = ?) WHERE title LIKE ?)",
+    [
+      [ '!!dummy' => '1999' ], 
+      [ '!!dummy' => 'Spoon%' ]
+    ],
+    'got correct SQL'
+);
 
+}
+
+TODO: {
     local $TODO = 'bind args order needs fixing (semifor)';
     $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
         ->search({ 'artistid' => 1 }, {
diff --git a/t/53delete_chained.t b/t/53delete_chained.t
deleted file mode 100644 (file)
index 4619548..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-use Test::More;
-use strict;
-use warnings;
-use lib qw(t/lib);
-use DBICTest;
-
-plan tests => 9;
-
-# This set of tests attempts to do a delete on a chained resultset, which
-# would lead to SQL DELETE with a JOIN, which is not supported by the 
-# SQL generator right now.
-# So it currently checks that these operations fail with a warning.
-# When the SQL generator is fixed this test will need fixing up appropriately.
-
-my $schema = DBICTest->init_schema();
-my $total_tracks = $schema->resultset('Track')->count;
-cmp_ok($total_tracks, '>', 0, 'need track records');
-
-# test that delete_related w/o conditions deletes all related records only
-{
-  my $w;
-  local $SIG{__WARN__} = sub { $w = shift };
-
-  my $artist = $schema->resultset("Artist")->find(3);
-  my $artist_tracks = $artist->cds->search_related('tracks')->count;
-  cmp_ok($artist_tracks, '<', $total_tracks, 'need more tracks than just related tracks');
-
-  ok(!eval{$artist->cds->search_related('tracks')->delete});
-  cmp_ok($schema->resultset('Track')->count, '==', $total_tracks, 'No tracks should be deleted');
-  like ($w, qr/Currently \$rs->delete\(\) does not generate proper SQL/, 'Delete join warning');
-}
-
-# test that delete_related w/conditions deletes just the matched related records only
-{
-  my $w;
-  local $SIG{__WARN__} = sub { $w = shift };
-
-  my $artist2 = $schema->resultset("Artist")->find(2);
-  my $artist2_tracks = $artist2->search_related('cds')->search_related('tracks')->count;
-  cmp_ok($artist2_tracks, '<', $total_tracks, 'need more tracks than related tracks');
-  
-  ok(!eval{$artist2->search_related('cds')->search_related('tracks')->delete});
-  cmp_ok($schema->resultset('Track')->count, '==', $total_tracks, 'No tracks should be deleted');
-  like ($w, qr/Currently \$rs->delete\(\) does not generate proper SQL/, 'Delete join warning');
-}
diff --git a/t/53delete_related.t b/t/53delete_related.t
deleted file mode 100644 (file)
index 4df8698..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-use Test::More;
-use strict;
-use warnings;
-use lib qw(t/lib);
-use DBICTest;
-
-plan tests => 7;
-
-my $schema = DBICTest->init_schema();
-my $total_cds = $schema->resultset('CD')->count;
-cmp_ok($total_cds, '>', 0, 'need cd records');
-
-# test that delete_related w/o conditions deletes all related records only
-my $artist = $schema->resultset("Artist")->find(3);
-my $artist_cds = $artist->cds->count;
-cmp_ok($artist_cds, '<', $total_cds, 'need more cds than just related cds');
-
-ok($artist->delete_related('cds'));
-cmp_ok($schema->resultset('CD')->count, '==', ($total_cds - $artist_cds), 'wrong number of cds were deleted');
-
-$total_cds -= $artist_cds;
-
-# test that delete_related w/conditions deletes just the matched related records only
-my $artist2 = $schema->resultset("Artist")->find(2);
-my $artist2_cds = $artist2->search_related('cds')->count;
-cmp_ok($artist2_cds, '<', $total_cds, 'need more cds than related cds');
-
-ok($artist2->delete_related('cds', {title => {like => '%'}}));
-cmp_ok($schema->resultset('CD')->count, '==', ($total_cds - $artist2_cds), 'wrong number of cds were deleted');
-
index 69f69ef..c3df11f 100644 (file)
@@ -5,29 +5,41 @@
 use strict;
 use warnings;
 
-our @plan;
+use Test::More;
+BEGIN { plan tests => 7 }
 
-BEGIN {
-  eval "require Module::Find;";
-  @plan = $@ ? ( skip_all => 'Could not load Module::Find' )
-    : ( tests => 2 );
-}
+package DBICTest::Taint::Classes;
 
-package DBICTest::Plain;
-
-# Use the Plain test class namespace to avoid the need for a
-# new test infrastructure. If invalid classes will be introduced to
-# 't/lib/DBICTest/Plain/' someday, this has to be reworked.
+use Test::More;
+use Test::Exception;
 
 use lib qw(t/lib);
+use base qw/DBIx::Class::Schema/;
+
+lives_ok (sub {
+  __PACKAGE__->load_classes(qw/Manual/);
+  ok( __PACKAGE__->source('Manual'), 'The Classes::Manual source has been registered' );
+  __PACKAGE__->_unregister_source (qw/Manual/);
+}, 'Loading classes with explicit load_classes worked in taint mode' );
+
+lives_ok (sub {
+  __PACKAGE__->load_classes();
+  ok( __PACKAGE__->source('Auto'), 'The Classes::Auto source has been registered' );
+  ok( __PACKAGE__->source('Auto'), 'The Classes::Manual source has been re-registered' );
+}, 'Loading classes with Module::Find/load_classes worked in taint mode' );
 
-use Test::More @plan;
 
+package DBICTest::Taint::Namespaces;
+
+use Test::More;
+use Test::Exception;
+
+use lib qw(t/lib);
 use base qw/DBIx::Class::Schema/;
 
-eval{ __PACKAGE__->load_classes() };
-cmp_ok( $@, 'eq', '',
-        'Loading classes with Module::Find worked in taint mode' );
-ok( __PACKAGE__->source('Test'), 'The Plain::Test source has been registered' );
+lives_ok (sub {
+  __PACKAGE__->load_namespaces();
+  ok( __PACKAGE__->source('Test'), 'The Namespaces::Test source has been registered' );
+}, 'Loading classes with Module::Find/load_namespaces worked in taint mode' );
 
 1;
index 011675b..6a710dc 100644 (file)
@@ -8,27 +8,14 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 95;
+plan tests => 103;
 
 eval { require DateTime::Format::MySQL };
 my $NO_DTFM = $@ ? 1 : 0;
 
-# figure out if we've got a version of sqlite that is older than 3.2.6, in
-# which case COUNT(DISTINCT()) doesn't work
-my $is_broken_sqlite = 0;
-my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
-    split /\./, $schema->storage->dbh->get_info(18);
-if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
-    ( ($sqlite_major_ver < 3) ||
-      ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
-      ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
-    $is_broken_sqlite = 1;
-}
-
-
 my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
 
-cmp_ok(@art, '==', 3, "Three artists returned");
+is(@art, 3, "Three artists returned");
 
 my $art = $art[0];
 
@@ -39,7 +26,7 @@ $art->name('We Are In Rehab');
 is($art->name, 'We Are In Rehab', "Accessor update ok");
 
 my %dirty = $art->get_dirty_columns();
-cmp_ok(scalar(keys(%dirty)), '==', 1, '1 dirty column');
+is(scalar(keys(%dirty)), 1, '1 dirty column');
 ok(grep($_ eq 'name', keys(%dirty)), 'name is dirty');
 
 is($art->get_column("name"), 'We Are In Rehab', 'And via get_column');
@@ -47,7 +34,7 @@ is($art->get_column("name"), 'We Are In Rehab', 'And via get_column');
 ok($art->update, 'Update run');
 
 my %not_dirty = $art->get_dirty_columns();
-cmp_ok(scalar(keys(%not_dirty)), '==', 0, 'Nothing is dirty');
+is(scalar(keys(%not_dirty)), 0, 'Nothing is dirty');
 
 eval {
   my $ret = $art->make_column_dirty('name2');
@@ -55,7 +42,7 @@ eval {
 ok(defined($@), 'Failed to make non-existent column dirty');
 $art->make_column_dirty('name');
 my %fake_dirty = $art->get_dirty_columns();
-cmp_ok(scalar(keys(%fake_dirty)), '==', 1, '1 fake dirty column');
+is(scalar(keys(%fake_dirty)), 1, '1 fake dirty column');
 ok(grep($_ eq 'name', keys(%fake_dirty)), 'name is fake dirty');
 
 my $record_jp = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => 'cds' })->next;
@@ -68,15 +55,15 @@ ok($record_fn, "funny join is okay");
 
 @art = $schema->resultset("Artist")->search({ name => 'We Are In Rehab' });
 
-cmp_ok(@art, '==', 1, "Changed artist returned by search");
+is(@art, 1, "Changed artist returned by search");
 
-cmp_ok($art[0]->artistid, '==', 3,'Correct artist too');
+is($art[0]->artistid, 3,'Correct artist too');
 
 lives_ok (sub { $art->delete }, 'Cascading delete on Ordered has_many works' );  # real test in ordered.t
 
 @art = $schema->resultset("Artist")->search({ });
 
-cmp_ok(@art, '==', 2, 'And then there were two');
+is(@art, 2, 'And then there were two');
 
 ok(!$art->in_storage, "It knows it's dead");
 
@@ -90,15 +77,15 @@ ok($art->in_storage, "Re-created");
 
 @art = $schema->resultset("Artist")->search({ });
 
-cmp_ok(@art, '==', 3, 'And now there are three again');
+is(@art, 3, 'And now there are three again');
 
 my $new = $schema->resultset("Artist")->create({ artistid => 4 });
 
-cmp_ok($new->artistid, '==', 4, 'Create produced record ok');
+is($new->artistid, 4, 'Create produced record ok');
 
 @art = $schema->resultset("Artist")->search({ });
 
-cmp_ok(@art, '==', 4, "Oh my god! There's four of them!");
+is(@art, 4, "Oh my god! There's four of them!");
 
 $new->set_column('name' => 'Man With A Fork');
 
@@ -152,7 +139,7 @@ is($schema->resultset("Artist")->count, 4, 'count ok');
 my $cd = $schema->resultset("CD")->find(1);
 my %cols = $cd->get_columns;
 
-cmp_ok(keys %cols, '==', 6, 'get_columns number of columns ok');
+is(keys %cols, 6, 'get_columns number of columns ok');
 
 is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
 
@@ -234,32 +221,39 @@ my $search = [ { 'tags.tag' => 'Cheesy' }, { 'tags.tag' => 'Blue' } ];
 
 my( $or_rs ) = $schema->resultset("CD")->search_rs($search, { join => 'tags',
                                                   order_by => 'cdid' });
+is($or_rs->all, 5, 'Joined search with OR returned correct number of rows');
+is($or_rs->count, 5, 'Search count with OR ok');
 
-cmp_ok($or_rs->count, '==', 5, 'Search with OR ok');
-
-my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 });
-cmp_ok($distinct_rs->all, '==', 4, 'DISTINCT search with OR ok');
+my $collapsed_or_rs = $or_rs->search ({}, { distinct => 1 }); # induce collapse
+is ($collapsed_or_rs->all, 4, 'Collapsed joined search with OR returned correct number of rows');
+is ($collapsed_or_rs->count, 4, 'Collapsed search count with OR ok');
 
-SKIP: {
-  skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 2
-    if $is_broken_sqlite;
+{
+  my $tcount = $schema->resultset('Track')->search(
+    {},
+    {
+      select => [ qw/position title/ ],
+      distinct => 1,
+    }
+  );
+  is($tcount->count, 13, 'multiple column COUNT DISTINCT ok');
 
-  my $tcount = $schema->resultset("Track")->search(
+  $tcount = $schema->resultset('Track')->search(
     {},
-    {       
-       select => {count => {distinct => ['position', 'title']}},
-          as => ['count']
+    {
+      columns => [ qw/position title/ ],
+      distinct => 1,
     }
   );
-  cmp_ok($tcount->next->get_column('count'), '==', 13, 'multiple column COUNT DISTINCT ok');
+  is($tcount->count, 13, 'multiple column COUNT DISTINCT ok');
 
-  $tcount = $schema->resultset("Track")->search(
+  $tcount = $schema->resultset('Track')->search(
     {},
-    {       
-       columns => {count => {count => {distinct => ['position', 'title']}}},
+    {
+       group_by => [ qw/position title/ ]
     }
   );
-  cmp_ok($tcount->next->get_column('count'), '==', 13, 'multiple column COUNT DISTINCT using column syntax ok');
+  is($tcount->count, 13, 'multiple column COUNT DISTINCT using column syntax ok');  
 }
 
 my $tag_rs = $schema->resultset('Tag')->search(
@@ -267,17 +261,17 @@ my $tag_rs = $schema->resultset('Tag')->search(
 
 my $rel_rs = $tag_rs->search_related('cd');
 
-cmp_ok($rel_rs->count, '==', 5, 'Related search ok');
+is($rel_rs->count, 5, 'Related search ok');
 
-cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok');
+is($or_rs->next->cdid, $rel_rs->next->cdid, 'Related object ok');
 $or_rs->reset;
 $rel_rs->reset;
 
 my $tag = $schema->resultset('Tag')->search(
                [ { 'me.tag' => 'Blue' } ], { cols=>[qw/tagid/] } )->next;
 
-cmp_ok($tag->has_column_loaded('tagid'), '==', 1, 'Has tagid loaded');
-cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag  loaded');
+ok($tag->has_column_loaded('tagid'), 'Has tagid loaded');
+ok(!$tag->has_column_loaded('tag'), 'Has not tag loaded');
 
 ok($schema->storage(), 'Storage available');
 
@@ -309,7 +303,7 @@ ok($schema->storage(), 'Storage available');
   ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists');
 
   my @artsn = $schema->resultset('SourceNameArtists')->search({}, { order_by => 'name DESC' });
-  cmp_ok(@artsn, '==', 4, "Four artists returned");
+  is(@artsn, 4, "Four artists returned");
   
   # make sure subclasses that don't set source_name are ok
   ok($schema->source('ArtistSubclass'), 'ArtistSubclass exists');
@@ -323,8 +317,8 @@ lives_ok (sub { my $newlink = $newbook->link}, "stringify to false value doesn't
 {
   my $art_del = $schema->resultset("Artist")->find({ artistid => 1 });
   lives_ok (sub { $art_del->delete }, 'Cascading delete on Ordered has_many works' );  # real test in ordered.t
-  cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.');
-  cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.');
+  is( $schema->resultset("CD")->search({artist => 1}), 0, 'Cascading through has_many top level.');
+  is( $schema->resultset("CD_to_Producer")->search({cd => 1}), 0, 'Cascading through has_many children.');
 }
 
 # test column_info
@@ -404,3 +398,12 @@ SKIP: {
   $en_row->insert;
   is($en_row->encoded, 'amliw', 'insert does not encode again');
 }
+
+# make sure we got rid of the compat shims
+SKIP: {
+    skip "Remove in 0.09", 5 if $DBIx::Class::VERSION < 0.09;
+
+    for (qw/compare_relationship_keys pk_depends_on resolve_condition resolve_join resolve_prefetch/) {
+      ok (! DBIx::Class::ResultSource->can ($_), "$_ no longer provided by DBIx::Class::ResultSource");
+    }
+}
index 513c6d3..e9053a3 100644 (file)
@@ -1,19 +1,14 @@
 use strict;
 use warnings;  
 
-use Test::More tests => 3;
+use Test::More tests => 2;
 use lib qw(t/lib);
 use DBICTest;
 use DBICTest::Schema;
 use DBICTest::Schema::Artist;
 
 DBICTest::Schema::Artist->source_name('MyArtist');
-{
-    my $w;
-    local $SIG{__WARN__} = sub { $w = shift };
-    DBICTest::Schema->register_class('FooA', 'DBICTest::Schema::Artist');
-    like ($w, qr/use register_extra_source/, 'Complain about using register_class on an already-registered class');
-}
+DBICTest::Schema->register_class('FooA', 'DBICTest::Schema::Artist');
 
 my $schema = DBICTest->init_schema();
 
diff --git a/t/63register_source.t b/t/63register_source.t
new file mode 100644 (file)
index 0000000..6951962
--- /dev/null
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+
+use Test::Exception tests => 1;
+use lib qw(t/lib);
+use DBICTest;
+use DBICTest::Schema;
+use DBIx::Class::ResultSource::Table;
+
+my $schema = DBICTest->init_schema();
+
+my $foo = DBIx::Class::ResultSource::Table->new({ name => "foo" });
+my $bar = DBIx::Class::ResultSource::Table->new({ name => "bar" });
+
+lives_ok {
+    $schema->register_source(foo => $foo);
+    $schema->register_source(bar => $bar);
+} 'multiple classless sources can be registered';
index 7631439..14ad6e0 100644 (file)
--- a/t/64db.t
+++ b/t/64db.t
@@ -64,6 +64,11 @@ TODO: {
       'rank' => {
           'data_type' => 'integer',
           'is_nullable' => 0,
+          'default_value' => '13',
+      },
+      'charfield' => {
+          'data_type' => 'char',
+          'is_nullable' => 1,
       },
     },
     'Correctly retrieve column info (mixed null and non-null columns)'
index 798e660..65093c4 100644 (file)
@@ -8,7 +8,7 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 70;
+plan tests => 78;
 
 # has_a test
 my $cd = $schema->resultset("CD")->find(4);
@@ -35,10 +35,15 @@ is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' )
 if ($INC{'DBICTest/HelperRels.pm'}) {
   $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
 } else {
-  $artist->create_related( 'cds', {
+  my $big_flop = $artist->create_related( 'cds', {
       title => 'Big Flop',
       year => 2005,
   } );
+
+ SKIP:{
+    skip "Can't fix right now", 1 if $DBIx::Class::VERSION < 0.09;
+    lives_ok { $big_flop->genre} "Don't throw exception when col is not loaded after insert";
+  };
 }
 
 my $big_flop_cd = ($artist->search_related('cds'))[3];
@@ -57,10 +62,10 @@ is( $big_flop_cd->title, 'Big Flop', 'create_related ok' );
 }
 
 my( $rs_from_list ) = $artist->search_related_rs('cds');
-is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
+isa_ok( $rs_from_list, 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
 
 ( $rs_from_list ) = $artist->cds_rs();
-is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
+isa_ok( $rs_from_list, 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
 
 # count_related
 is( $artist->count_related('cds'), 4, 'count_related ok' );
@@ -184,6 +189,14 @@ is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' );
 is( $prod_rs->first->name, 'Matt S Trout',
     'many_to_many add_to_$rel($obj) ok' );
 $cd->remove_from_producers($prod);
+$cd->add_to_producers($prod, {attribute => 1});
+is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj, $link_vals) count ok' );
+is( $cd->cd_to_producer->first->attribute, 1, 'many_to_many $link_vals ok');
+$cd->remove_from_producers($prod);
+$cd->set_producers([$prod], {attribute => 2});
+is( $prod_rs->count(), 1, 'many_to_many set_$rel($obj, $link_vals) count ok' );
+is( $cd->cd_to_producer->first->attribute, 2, 'many_to_many $link_vals ok');
+$cd->remove_from_producers($prod);
 is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout',
     "producer object exists after remove of link" );
 is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' );
@@ -229,6 +242,7 @@ is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
 is( $twokey->fourkeys_to_twokeys->count, 0,
     'twokey has no links to fourkey' );
 
+
 my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 });
 is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded');
 is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db');
@@ -284,3 +298,14 @@ cmp_ok($relinfo->{attrs}{is_foreign_key_constraint}, '==', 1, "is_foreign_key_co
 my $rs_overridden = $schema->source('ForceForeign');
 my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3');
 cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr.");
+
+# check that relationships below left join relationships are forced to left joins 
+# when traversing multiple belongs_to
+my $cds = $schema->resultset("CD")->search({ 'me.cdid' => 5 }, { join => { single_track => 'cd' } });
+is($cds->count, 1, "subjoins under left joins force_left (string)");
+
+$cds = $schema->resultset("CD")->search({ 'me.cdid' => 5 }, { join => { single_track => ['cd'] } });
+is($cds->count, 1, "subjoins under left joins force_left (arrayref)");
+
+$cds = $schema->resultset("CD")->search({ 'me.cdid' => 5 }, { join => { single_track => { cd => {} } } });
+is($cds->count, 1, "subjoins under left joins force_left (hashref)");
index b7bb73f..17b05c3 100644 (file)
@@ -21,6 +21,8 @@ is( $it->pager->next_page, 2, "next_page ok" );
 
 is( $it->count, 3, "count on paged rs ok" );
 
+is( $it->pager->total_entries, 5, "total_entries ok" );
+
 is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" );
 
 $it->next;
index dbba1cd..a2e868f 100644 (file)
@@ -2,6 +2,7 @@ use strict;
 use warnings;  
 
 use Test::More;
+use Test::Exception;
 use lib qw(t/lib);
 use DBICTest;
 use DBI::Const::GetInfoType;
@@ -13,7 +14,7 @@ my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
   unless ($dsn && $user);
 
-plan tests => 10;
+plan tests => 11;
 
 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
 
@@ -23,6 +24,18 @@ $dbh->do("DROP TABLE IF EXISTS artist;");
 
 $dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10));");
 
+$dbh->do("DROP TABLE IF EXISTS cd;");
+
+$dbh->do("CREATE TABLE cd (cdid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, artist INTEGER, title TEXT, year INTEGER, genreid INTEGER, single_track INTEGER);");
+
+$dbh->do("DROP TABLE IF EXISTS producer;");
+
+$dbh->do("CREATE TABLE producer (producerid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT);");
+
+$dbh->do("DROP TABLE IF EXISTS cd_to_producer;");
+
+$dbh->do("CREATE TABLE cd_to_producer (cd INTEGER,producer INTEGER);");
+
 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
 
 # This is in Core now, but it's here just to test that it doesn't break
@@ -41,7 +54,7 @@ my $it = $schema->resultset('Artist')->search( {},
       offset => 2,
       order_by => 'artistid' }
 );
-is( $it->count, 3, "LIMIT count ok" );
+is( $it->count, 3, "LIMIT count ok" );  # ask for 3 rows out of 7 artists
 is( $it->next->name, "Artist 2", "iterator->next ok" );
 $it->next;
 $it->next;
@@ -108,7 +121,7 @@ NULLINSEARCH: {
     => 'Created an artist resultset of undef';
     
     TODO: {
-       $TODO = "need to fix the row count =1 when select * from table where pk IS NULL problem";
+       local $TODO = "need to fix the row count =1 when select * from table where pk IS NULL problem";
            is $artist2_rs->count, 0
            => 'got no rows';           
     }
@@ -119,8 +132,13 @@ NULLINSEARCH: {
     => 'Nothing Found!';
 }
     
+my $cd = $schema->resultset ('CD')->create ({});
+
+my $producer = $schema->resultset ('Producer')->create ({});
+
+lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
 
 # clean up our mess
 END {
     #$dbh->do("DROP TABLE artist") if $dbh;
-}
\ No newline at end of file
+}
index 45e614f..05470a6 100644 (file)
--- a/t/72pg.t
+++ b/t/72pg.t
@@ -15,10 +15,15 @@ use DBICTest;
 
   __PACKAGE__->load_components(qw/Core/);
   __PACKAGE__->table('testschema.casecheck');
-  __PACKAGE__->add_columns(qw/id name NAME uc_name/);
+  __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/);
   __PACKAGE__->column_info_from_storage(1);
   __PACKAGE__->set_primary_key('id');
 
+  sub store_column {
+    my ($self, $name, $value) = @_;
+    $value = '#'.$value if($name eq "storecolumn");
+    $self->maybe::next::method($name, $value);
+  }
 }
 
 {
@@ -45,7 +50,7 @@ plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test '.
     unless ($dsn && $user);
 
 
-plan tests => 37;
+plan tests => 39;
 
 DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' );
 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
@@ -67,16 +72,25 @@ $schema->source("Artist")->name("testschema.artist");
 $schema->source("SequenceTest")->name("testschema.sequence_test");
 {
     local $SIG{__WARN__} = sub {};
+    _cleanup ($dbh);
+
     $dbh->do("CREATE SCHEMA testschema;");
     $dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10), arrayfield INTEGER[]);");
     $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
     $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
     $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
     $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
-    ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3));'), 'Creation of casecheck table');
+    ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3), "storecolumn" VARCHAR(10));'), 'Creation of casecheck table');
     ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table');
 }
 
+# store_column is called once for create() for non sequence columns
+
+ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'}));
+
+is($storecolumn->storecolumn, '#a'); # was '##a'
+
+
 # This is in Core now, but it's here just to test that it doesn't break
 $schema->class('Artist')->load_components('PK::Auto');
 
@@ -130,10 +144,7 @@ like($artistid_defval,
 is_deeply($type_info, $test_type_info,
           'columns_info_for - column data types');
 
-SKIP: {
-  skip "SQL::Abstract < 1.49 does not pass through arrayrefs", 4
-    if $SQL::Abstract::VERSION < 1.49;
-
+{
   lives_ok {
     $schema->resultset('ArrayTest')->create({
       arrayfield => [1, 2],
@@ -153,7 +164,7 @@ SKIP: {
   my $count;
   lives_ok {
     $count = $schema->resultset('ArrayTest')->search({
-      arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ],   #TODO anything less ugly than this?
+      arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ],   #Todo anything less ugly than this?
     })->count;
   } 'comparing arrayref to pg array data does not blow up';
   is($count, 1, 'comparing arrayref to pg array data gives correct result');
@@ -246,30 +257,30 @@ SKIP: {
     });
 }
 
-SKIP: {
-  skip "Oracle Auto-PK tests are broken", 16;
-
-  # test auto increment using sequences WITHOUT triggers
-  for (1..5) {
+for (1..5) {
     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
-  }
-  my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
-  is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
 }
-
-END {
-    if($dbh) {
-        $dbh->do("DROP TABLE testschema.artist;");
-        $dbh->do("DROP TABLE testschema.casecheck;");
-        $dbh->do("DROP TABLE testschema.sequence_test;");
-        $dbh->do("DROP TABLE testschema.array_test;");
-        $dbh->do("DROP SEQUENCE pkid1_seq");
-        $dbh->do("DROP SEQUENCE pkid2_seq");
-        $dbh->do("DROP SEQUENCE nonpkid_seq");
-        $dbh->do("DROP SCHEMA testschema;");
-    }
+my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
+is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
+
+sub _cleanup {
+  my $dbh = shift or return;
+
+  for my $stat (
+    'DROP TABLE testschema.artist',
+    'DROP TABLE testschema.casecheck',
+    'DROP TABLE testschema.sequence_test',
+    'DROP TABLE testschema.array_test',
+    'DROP SEQUENCE pkid1_seq',
+    'DROP SEQUENCE pkid2_seq',
+    'DROP SEQUENCE nonpkid_seq',
+    'DROP SCHEMA testschema',
+  ) {
+    eval { $dbh->do ($stat) };
+  }
 }
 
+END { _cleanup($dbh) }
index 51cc932..2e73050 100644 (file)
@@ -28,6 +28,7 @@
 use strict;
 use warnings;  
 
+use Test::Exception;
 use Test::More;
 use lib qw(t/lib);
 use DBICTest;
@@ -39,7 +40,7 @@ plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.
   ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
   unless ($dsn && $user && $pass);
 
-plan tests => 24;
+plan tests => 34;
 
 DBICTest::Schema->load_classes('ArtistFQN');
 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
@@ -63,7 +64,7 @@ $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0")
 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
-$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
+$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at DATE)");
 
 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
@@ -80,6 +81,23 @@ $dbh->do(qq{
   END;
 });
 
+{
+    # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
+
+    local $SIG{__WARN__} = sub {};
+    eval { $dbh->do('DROP TABLE bindtype_test') };
+
+    $dbh->do(qq[
+        CREATE TABLE bindtype_test 
+        (
+            id              integer      NOT NULL   PRIMARY KEY,
+            bytea           integer      NULL,
+            blob            blob         NULL,
+            clob            clob         NULL
+        )
+    ],{ RaiseError => 1, PrintError => 1 });
+}
+
 # This is in Core now, but it's here just to test that it doesn't break
 $schema->class('Artist')->load_components('PK::Auto');
 # These are compat shims for PK::Auto...
@@ -106,15 +124,32 @@ is($tjoin->next->title, 'Track1', "ambiguous column ok");
 
 # check count distinct with multiple columns
 my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
+
 my $tcount = $schema->resultset('Track')->search(
-    {},
-    {
-        select => [{count => {distinct => ['position', 'title']}}],
-        as => ['count']
-    }
-  );
+  {},
+  {
+    select => [ qw/position title/ ],
+    distinct => 1,
+  }
+);
+is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
+
+$tcount = $schema->resultset('Track')->search(
+  {},
+  {
+    columns => [ qw/position title/ ],
+    distinct => 1,
+  }
+);
+is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
 
-is($tcount->next->get_column('count'), 2, "multiple column select distinct ok");
+$tcount = $schema->resultset('Track')->search(
+  {},
+  { 
+     group_by => [ qw/position title/ ]
+  }
+);
+is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
 
 # test LIMIT support
 for (1..6) {
@@ -147,6 +182,28 @@ for (1..5) {
 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
 
+{
+       my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
+       $binstr{'large'} = $binstr{'small'} x 1024;
+
+       my $maxloblen = length $binstr{'large'};
+       note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
+       local $dbh->{'LongReadLen'} = $maxloblen;
+
+       my $rs = $schema->resultset('BindType');
+       my $id = 0;
+
+       foreach my $type (qw( blob clob )) {
+               foreach my $size (qw( small large )) {
+                       $id++;
+
+                       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
+                               "inserted $size $type without dying";
+                       ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
+               }
+       }
+}
+
 # clean up our mess
 END {
     if($schema && ($dbh = $schema->storage->dbh)) {
@@ -158,6 +215,7 @@ END {
         $dbh->do("DROP TABLE sequence_test");
         $dbh->do("DROP TABLE cd");
         $dbh->do("DROP TABLE track");
+        $dbh->do("DROP TABLE bindtype_test");
     }
 }
 
index 20e7089..0f2fc23 100644 (file)
@@ -17,12 +17,14 @@ else {
         plan skip_all => 'needs DateTime and DateTime::Format::Oracle for testing';
     }
     else {
-        plan tests => 4;
+        plan tests => 7;
     }
 }
 
 # DateTime::Format::Oracle needs this set
 $ENV{NLS_DATE_FORMAT} = 'DD-MON-YY';
+$ENV{NLS_TIMESTAMP_FORMAT} = 'YYYY-MM-DD HH24:MI:SSXFF';
+$ENV{NLS_LANG} = 'AMERICAN_AMERICA.WE8ISO8859P1';
 
 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
 
@@ -30,16 +32,20 @@ my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
 my $col_metadata = $schema->class('Track')->column_info('last_updated_on');
 $schema->class('Track')->add_column( 'last_updated_on' => {
     data_type => 'date' });
+$schema->class('Track')->add_column( 'last_updated_at' => {
+    data_type => 'timestamp' });
 
 my $dbh = $schema->storage->dbh;
 
+#$dbh->do("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SSXFF'");
+
 eval {
   $dbh->do("DROP TABLE track");
 };
-$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
+$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at TIMESTAMP)");
 
 # insert a row to play with
-my $new = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1', last_updated_on => '06-MAY-07' });
+my $new = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1', last_updated_on => '06-MAY-07', last_updated_at => '2009-05-03 21:17:18.5' });
 is($new->trackid, 1, "insert sucessful");
 
 my $track = $schema->resultset('Track')->find( 1 );
@@ -48,11 +54,18 @@ is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok");
 
 is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column");
 
+#note '$track->last_updated_at => ', $track->last_updated_at;
+is( ref($track->last_updated_at), 'DateTime', "last_updated_at inflated ok");
+
+is( $track->last_updated_at->nanosecond, 500_000_000, "DateTime methods work with nanosecond precision");
+
 my $dt = DateTime->now();
 $track->last_updated_on($dt);
+$track->last_updated_at($dt);
 $track->update;
 
 is( $track->last_updated_on->month, $dt->month, "deflate ok");
+is( int $track->last_updated_at->nanosecond, int $dt->nanosecond, "deflate ok with nanosecond precision");
 
 # clean up our mess
 END {
index 5d628e8..3f635f8 100644 (file)
@@ -12,7 +12,7 @@ my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_DB2_${_}" } qw/DSN USER PASS/};
 plan skip_all => 'Set $ENV{DBICTEST_DB2_DSN}, _USER and _PASS to run this test'
   unless ($dsn && $user);
 
-plan tests => 6;
+plan tests => 9;
 
 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
 
@@ -25,24 +25,36 @@ $dbh->do("CREATE TABLE artist (artistid INTEGER GENERATED BY DEFAULT AS IDENTITY
 # This is in core, just testing that it still loads ok
 $schema->class('Artist')->load_components('PK::Auto');
 
+my $ars = $schema->resultset('Artist');
+
 # test primary key handling
-my $new = $schema->resultset('Artist')->create({ name => 'foo' });
+my $new = $ars->create({ name => 'foo' });
 ok($new->artistid, "Auto-PK worked");
 
-# test LIMIT support
+my $init_count = $ars->count;
 for (1..6) {
-    $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
+    $ars->create({ name => 'Artist ' . $_ });
 }
-my $it = $schema->resultset('Artist')->search( {},
-    { rows => 3,
-      order_by => 'artistid'
-      }
+is ($ars->count, $init_count + 6, 'Simple count works');
+
+# test LIMIT support
+my $it = $ars->search( {},
+  {
+    rows => 3,
+    order_by => 'artistid'
+  }
 );
 is( $it->count, 3, "LIMIT count ok" );
+
+my @all = $it->all;
+is (@all, 3, 'Number of ->all objects matches count');
+
+$it->reset;
 is( $it->next->name, "foo", "iterator->next ok" );
-$it->next;
+is( $it->next->name, "Artist 1", "iterator->next ok" );
 is( $it->next->name, "Artist 2", "iterator->next ok" );
-is( $it->next, undef, "next past end of resultset ok" );
+is( $it->next, undef, "next past end of resultset ok" );  # this can not succeed if @all > 3
+
 
 my $test_type_info = {
     'artistid' => {
diff --git a/t/746sybase.t b/t/746sybase.t
new file mode 100644 (file)
index 0000000..035091f
--- /dev/null
@@ -0,0 +1,105 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+use DBIx::Class::Storage::DBI::Sybase::DateTime;
+
+my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
+
+plan skip_all => 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test'
+  unless ($dsn && $user);
+
+plan tests => 15;
+
+my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
+
+$schema->storage->ensure_connected;
+isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::Sybase' );
+
+$schema->storage->dbh_do (sub {
+    my ($storage, $dbh) = @_;
+    eval { $dbh->do("DROP TABLE artist") };
+    eval { $dbh->do("DROP TABLE track") };
+    $dbh->do(<<'SQL');
+CREATE TABLE artist (
+   artistid INT IDENTITY PRIMARY KEY,
+   name VARCHAR(100),
+   rank INT DEFAULT 13 NOT NULL,
+   charfield CHAR(10) NULL
+)
+SQL
+
+# we only need the DT
+    $dbh->do(<<'SQL');
+CREATE TABLE track (
+   trackid INT IDENTITY PRIMARY KEY,
+   cd INT,
+   position INT,
+   last_updated_on DATETIME,
+)
+SQL
+
+});
+
+my %seen_id;
+
+# fresh $schema so we start unconnected
+$schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
+
+# test primary key handling
+my $new = $schema->resultset('Artist')->create({ name => 'foo' });
+ok($new->artistid > 0, "Auto-PK worked");
+
+$seen_id{$new->artistid}++;
+
+# test LIMIT support
+for (1..6) {
+    $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
+    is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
+    $seen_id{$new->artistid}++;
+}
+
+my $it = $schema->resultset('Artist')->search( {}, {
+    rows => 3,
+    order_by => 'artistid',
+});
+
+TODO: {
+    local $TODO = 'Sybase is very very fucked in the limit department';
+
+    is( $it->count, 3, "LIMIT count ok" );
+}
+
+# The iterator still works correctly with rows => 3, even though the sql is
+# fucked, very interesting.
+
+is( $it->next->name, "foo", "iterator->next ok" );
+$it->next;
+is( $it->next->name, "Artist 2", "iterator->next ok" );
+is( $it->next, undef, "next past end of resultset ok" );
+
+# Test DateTime inflation
+
+my $dt = DBIx::Class::Storage::DBI::Sybase::DateTime
+    ->parse_datetime('2004-08-21T14:36:48.080Z');
+
+my $row;
+ok( $row = $schema->resultset('Track')->create({
+    last_updated_on => $dt,
+    cd => 1,
+}));
+ok( $row = $schema->resultset('Track')
+    ->search({ trackid => $row->trackid }, { select => ['last_updated_on'] })
+    ->first
+);
+is( $row->updated_date, $dt, 'DateTime inflation works' );
+
+# clean up our mess
+END {
+    if (my $dbh = eval { $schema->storage->_dbh }) {
+        $dbh->do('DROP TABLE artist');
+        $dbh->do('DROP TABLE track');
+    }
+}
index 238f27a..49f7967 100644 (file)
@@ -7,23 +7,18 @@ use DBICTest;
 
 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
 
-#warn "$dsn $user $pass";
-
 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
   unless ($dsn);
 
-plan tests => 5;
-
-my $storage_type = '::DBI::MSSQL';
-$storage_type = '::DBI::Sybase::MSSQL' if $dsn =~ /^dbi:Sybase:/;
-# Add more for others in the future when they exist (ODBC? ADO? JDBC?)
+plan tests => 6;
 
 my $schema = DBICTest::Schema->clone;
-$schema->storage_type($storage_type);
 $schema->connection($dsn, $user, $pass);
 
 my $dbh = $schema->storage->dbh;
 
+isa_ok($schema->storage, 'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server');
+
 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
     DROP TABLE artist");
 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
@@ -40,7 +35,7 @@ ok($new->artistid, "Auto-PK worked");
 
 # Test LIMIT
 for (1..6) {
-    $schema->resultset('Artist')->create( { name => 'Artist ' . $_ } );
+    $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
 }
 
 my $it = $schema->resultset('Artist')->search( { },
index 84d8ba5..5706dfa 100644 (file)
@@ -17,23 +17,11 @@ BEGIN {
     eval "use DBD::SQLite";
     plan $@
         ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 18 );
-}
-
-# figure out if we've got a version of sqlite that is older than 3.2.6, in
-# which case COUNT(DISTINCT()) doesn't work
-my $is_broken_sqlite = 0;
-my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
-    split /\./, $schema->storage->dbh->get_info(18);
-if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
-    ( ($sqlite_major_ver < 3) ||
-      ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
-      ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
-    $is_broken_sqlite = 1;
+        : ( tests => 33 );
 }
 
 # test the abstract join => SQL generator
-my $sa = new DBIC::SQL::Abstract;
+my $sa = new DBIx::Class::SQLAHacks;
 
 my @j = (
     { child => 'person' },
@@ -44,9 +32,9 @@ my $match = 'person child JOIN person father ON ( father.person_id = '
           . 'child.father_id ) JOIN person mother ON ( mother.person_id '
           . '= child.mother_id )'
           ;
-is_same_sql_bind(
-  $sa->_recurse_from(@j), [],
-  $match, [],
+is_same_sql(
+  $sa->_recurse_from(@j),
+  $match,
   'join 1 ok'
 );
 
@@ -64,9 +52,9 @@ $match = 'person mother JOIN (person child JOIN person father ON ('
        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
        . 'child.mother_id )'
        ;
-is_same_sql_bind(
-  $sa->_recurse_from(@j2), [],
-  $match, [],
+is_same_sql(
+  $sa->_recurse_from(@j2),
+  $match,
   'join 2 ok'
 );
 
@@ -81,9 +69,9 @@ $match = 'person child INNER JOIN person father ON ( father.person_id = '
           . '= child.mother_id )'
           ;
 
-is_same_sql_bind(
-  $sa->_recurse_from(@j3), [],
-  $match, [],
+is_same_sql(
+  $sa->_recurse_from(@j3),
+  $match,
   'join 3 (inner join) ok'
 );
 
@@ -101,9 +89,9 @@ $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
        . 'child.mother_id )'
        ;
-is_same_sql_bind(
-  $sa->_recurse_from(@j4), [],
-  $match, [],
+is_same_sql(
+  $sa->_recurse_from(@j4),
+  $match,
   'join 4 (nested joins + join types) ok'
 );
 
@@ -116,9 +104,9 @@ $match = 'person child JOIN person father ON ( father.person_id != '
           . 'child.father_id ) JOIN person mother ON ( mother.person_id '
           . '= child.mother_id )'
           ;
-is_same_sql_bind(
-  $sa->_recurse_from(@j5), [],
-  $match, [],
+is_same_sql(
+  $sa->_recurse_from(@j5),
+  $match,
   'join 5 (SCALAR reference for ON statement) ok'
 );
 
@@ -127,7 +115,7 @@ my @j6 = (
     [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ],
     [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
 );
-$match = qr/^HASH reference arguments are not supported in JOINS - try using "\.\.\." instead/;
+$match = qr/HASH reference arguments are not supported in JOINS/;
 eval { $sa->_recurse_from(@j6) };
 like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' );
 
@@ -140,7 +128,7 @@ my $rs = $schema->resultset("CD")->search(
                          ] ] }
          );
 
-cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
+is( $rs + 0, 1, "Single record in resultset");
 
 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
 
@@ -148,7 +136,7 @@ $rs = $schema->resultset("CD")->search(
            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
            { join => 'artist' });
 
-cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
+is( $rs + 0, 1, "Single record in resultset");
 
 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
 
@@ -157,7 +145,7 @@ $rs = $schema->resultset("CD")->search(
              'liner_notes.notes' => 'Kill Yourself!' },
            { join => [ qw/artist liner_notes/ ] });
 
-cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
+is( $rs + 0, 1, "Single record in resultset");
 
 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
 
@@ -166,7 +154,7 @@ $rs = $schema->resultset("CD")->search(
     { 'artist' => 1 },
     { join => [qw/artist/], order_by => 'artist.name' }
 );
-cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
+is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
 
 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
   'Slicing beyond end of rs returns a zero count');
@@ -175,32 +163,83 @@ $rs = $schema->resultset("Artist")->search(
         { 'liner_notes.notes' => 'Kill Yourself!' },
         { join => { 'cds' => 'liner_notes' } });
 
-cmp_ok( $rs->count, '==', 1, "Single record in resultset");
+is( $rs->count, 1, "Single record in resultset");
 
 is($rs->first->name, 'We Are Goth', 'Correct record returned');
 
-# test for warnings on delete of joined resultset
-$rs = $schema->resultset("CD")->search(
-    { 'artist.name' => 'Caterwauler McCrae' },
-    { join => [qw/artist/]}
-);
-my $tst_delete_warning;
-eval {
-    local $SIG{__WARN__} = sub { $tst_delete_warning = shift };
-    $rs->delete();
-};
-
-ok( ($@ || $tst_delete_warning), 'fail/warning on attempt to delete a join-ed resultset');
 
-# test for warnings on update of joined resultset
-$rs = $schema->resultset("CD")->search(
-    { 'artist.name' => 'Random Boy Band' },
-    { join => [qw/artist/]}
-);
-my $tst_update_warning;
-eval {
-    local $SIG{__WARN__} = sub { $tst_update_warning = shift };
-    $rs->update({ 'artist' => 1 });
-};
-
-ok( ($@ || $tst_update_warning), 'fail/warning on attempt to update a join-ed resultset');
+{
+    $schema->populate('Artist', [
+        [ qw/artistid name/ ],
+        [ 4, 'Another Boy Band' ],
+    ]);
+    $schema->populate('CD', [
+        [ qw/cdid artist title year/ ],
+        [ 6, 2, "Greatest Hits", 2001 ],
+        [ 7, 4, "Greatest Hits", 2005 ],
+        [ 8, 4, "BoyBandBlues", 2008 ],
+    ]);
+    $schema->populate('TwoKeys', [
+        [ qw/artist cd/ ],
+        [ 2, 4 ],
+        [ 2, 6 ],
+        [ 4, 7 ],
+        [ 4, 8 ],
+    ]);
+    
+    sub cd_count {
+        return $schema->resultset("CD")->count;
+    }
+    sub tk_count {
+        return $schema->resultset("TwoKeys")->count;
+    }
+
+    is(cd_count(), 8, '8 rows in table cd');
+    is(tk_count(), 7, '7 rows in table twokeys');
+    sub artist1 {
+        return $schema->resultset("CD")->search(
+            { 'artist.name' => 'Caterwauler McCrae' },
+            { join => [qw/artist/]}
+        );
+    }
+    sub artist2 {
+        return $schema->resultset("CD")->search(
+            { 'artist.name' => 'Random Boy Band' },
+            { join => [qw/artist/]}
+        );
+    }
+
+    is( artist1()->count, 3, '3 Caterwauler McCrae CDs' );
+    ok( artist1()->delete, 'Successfully deleted 3 CDs' );
+    is( artist1()->count, 0, '0 Caterwauler McCrae CDs' );
+    is( artist2()->count, 2, '3 Random Boy Band CDs' );
+    ok( artist2()->update( { 'artist' => 1 } ) );
+    is( artist2()->count, 0, '0 Random Boy Band CDs' );
+    is( artist1()->count, 2, '2 Caterwauler McCrae CDs' );
+
+    # test update on multi-column-pk
+    sub tk1 {
+        return $schema->resultset("TwoKeys")->search(
+            {
+                'artist.name' => { like => '%Boy Band' },
+                'cd.title'    => 'Greatest Hits',
+            },
+            { join => [qw/artist cd/] }
+        );
+    }
+    sub tk2 {
+        return $schema->resultset("TwoKeys")->search(
+            { 'artist.name' => 'Caterwauler McCrae' },
+            { join => [qw/artist/]}
+        );
+    }
+    is( tk2()->count, 2, 'TwoKeys count == 2' );
+    is( tk1()->count, 2, 'TwoKeys count == 2' );
+    ok( tk1()->update( { artist => 1 } ) );
+    is( tk1()->count, 0, 'TwoKeys count == 0' );
+    is( tk2()->count, 4, '2 Caterwauler McCrae CDs' );
+    ok( tk2()->delete, 'Successfully deleted 4 CDs' );
+    is(cd_count(), 5, '5 rows in table cd');
+    is(tk_count(), 3, '3 rows in table twokeys');
+}
index 4325a70..bee69e6 100644 (file)
@@ -5,10 +5,11 @@ use Test::More;
 use Test::Exception;
 use lib qw(t/lib);
 use DBICTest;
+use DBIC::SqlMakerTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 12;
+plan tests => 24;
 
 my $rs = $schema->resultset('CD')->search({},
     {
@@ -28,6 +29,16 @@ $rs = $schema->resultset('CD')->search({},
 lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
 lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present');
 
+# Tests a regression in ResultSetColumn wrt +select
+$rs = $schema->resultset('CD')->search(undef,
+    {
+        '+select'   => [ \'COUNT(*) AS year_count' ],
+               order_by => 'year_count'
+       }
+);
+my @counts = $rs->get_column('cdid')->all;
+ok(scalar(@counts), 'got rows from ->all using +select');
+
 $rs = $schema->resultset('CD')->search({},
     {
         '+select'   => [ \ 'COUNT(*)', 'title' ],
@@ -63,3 +74,124 @@ is ($subsel->next->title, $cds->next->title, 'First CD title match');
 is ($subsel->next->title, $cds->next->title, 'Second CD title match');
 
 is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
+
+
+
+$rs = $schema->resultset('CD')->search({},
+    {
+        'join' => 'artist',
+        'columns' => ['cdid', 'title', 'artist.name'],
+    }
+);
+
+is_same_sql_bind (
+  $rs->as_query,
+  '(SELECT me.cdid, me.title, artist.name FROM cd me  JOIN artist artist ON artist.artistid = me.artist)',
+  [],
+  'Use of columns attribute results in proper sql'
+);
+
+lives_ok(sub {
+  $rs->first->get_column('cdid')
+}, 'columns 1st rscolumn present');
+
+lives_ok(sub {
+  $rs->first->get_column('title')
+}, 'columns 2nd rscolumn present');
+
+lives_ok(sub {
+  $rs->first->artist->get_column('name') 
+}, 'columns 3rd rscolumn present'); 
+
+
+
+$rs = $schema->resultset('CD')->search({},
+    {  
+        'join' => 'artist',
+        '+columns' => ['cdid', 'title', 'artist.name'],
+    }
+);
+
+is_same_sql_bind (
+  $rs->as_query,
+  '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.cdid, me.title, artist.name FROM cd me  JOIN artist artist ON artist.artistid = me.artist)',
+  [],
+  'Use of columns attribute results in proper sql'
+);
+
+lives_ok(sub {
+  $rs->first->get_column('cdid') 
+}, 'columns 1st rscolumn present');
+
+lives_ok(sub {
+  $rs->first->get_column('title')
+}, 'columns 2nd rscolumn present');
+
+lives_ok(sub {
+  $rs->first->artist->get_column('name')
+}, 'columns 3rd rscolumn present');
+
+
+$rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
+  {
+    join => 'tracks',
+    columns => [qw/me.cdid me.title/],
+    '+select' => ['tracks.position'],
+    '+as' => ['track_position'],
+
+    # get a hashref of CD1 only (the first with a second track)
+    result_class => 'DBIx::Class::ResultClass::HashRefInflator',
+    order_by => 'cdid',
+    rows => 1,
+  }
+);
+
+is_deeply (
+  $rs->single,
+  {
+    cdid => 1,
+    track_position => 2,
+    title => 'Spoonful of bees',
+  },
+  'limited prefetch via column works on a multi-relationship',
+);
+
+my $sub_rs = $rs->search ({},
+  {
+    columns => [qw/artist tracks.trackid/],    # columns should not be merged but override $rs columns
+    '+select' => ['tracks.title'],
+    '+as' => ['tracks.title'],
+  }
+);
+
+is_deeply (
+  $sub_rs->single,
+  {
+    artist => 1,
+    track_position => 2,
+    tracks =>
+      {
+        trackid => 17,
+        title => 'Apiary',
+      },
+  },
+  'columns/select/as fold properly on sub-searches',
+);
+
+TODO: {
+  local $TODO = "Multi-collapsing still doesn't work right - HRI should be getting an arrayref, not an individual hash";
+  is_deeply (
+    $sub_rs->single,
+    {
+      artist => 1,
+      track_position => 2,
+      tracks => [
+        {
+          trackid => 17,
+          title => 'Apiary',
+        },
+      ],
+    },
+    'columns/select/as fold properly on sub-searches',
+  );
+}
diff --git a/t/77prefetch.t b/t/77prefetch.t
deleted file mode 100644 (file)
index 3e50d9b..0000000
+++ /dev/null
@@ -1,485 +0,0 @@
-use strict;
-use warnings;  
-
-use Test::More;
-use Test::Exception;
-use lib qw(t/lib);
-use DBICTest;
-use Data::Dumper;
-
-my $schema = DBICTest->init_schema();
-
-my $orig_debug = $schema->storage->debug;
-
-use IO::File;
-
-BEGIN {
-    eval "use DBD::SQLite";
-    plan $@
-        ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 74 );
-}
-
-# figure out if we've got a version of sqlite that is older than 3.2.6, in
-# which case COUNT(DISTINCT()) doesn't work
-my $is_broken_sqlite = 0;
-my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
-    split /\./, $schema->storage->dbh->get_info(18);
-if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
-    ( ($sqlite_major_ver < 3) ||
-      ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
-      ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
-    $is_broken_sqlite = 1;
-}
-
-# bug in 0.07000 caused attr (join/prefetch) to be modifed by search
-# so we check the search & attr arrays are not modified
-my $search = { 'artist.name' => 'Caterwauler McCrae' };
-my $attr = { prefetch => [ qw/artist liner_notes/ ],
-             order_by => 'me.cdid' };
-my $search_str = Dumper($search);
-my $attr_str = Dumper($attr);
-
-my $rs = $schema->resultset("CD")->search($search, $attr);
-
-is(Dumper($search), $search_str, 'Search hash untouched after search()');
-is(Dumper($attr), $attr_str, 'Attribute hash untouched after search()');
-cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
-
-# A search() with prefetch seems to pollute an already joined resultset
-# in a way that offsets future joins (adapted from a test case by Debolaz)
-{
-  my ($cd_rs, $attrs);
-
-  # test a real-life case - rs is obtained by an implicit m2m join
-  $cd_rs = $schema->resultset ('Producer')->first->cds;
-  $attrs = Dumper $cd_rs->{attrs};
-
-  $cd_rs->search ({})->all;
-  is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
-
-  lives_ok (sub {
-    $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
-    is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
-  }, 'first prefetching search ok');
-
-  lives_ok (sub {
-    $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
-    is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
-  }, 'second prefetching search ok');
-
-
-  # test a regular rs with an empty seen_join injected - it should still work!
-  $cd_rs = $schema->resultset ('CD');
-  $cd_rs->{attrs}{seen_join}  = {};
-  $attrs = Dumper $cd_rs->{attrs};
-
-  $cd_rs->search ({})->all;
-  is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
-
-  lives_ok (sub {
-    $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
-    is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
-  }, 'first prefetching search ok');
-
-  lives_ok (sub {
-    $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
-    is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
-  }, 'second prefetching search ok');
-}
-
-
-my $queries = 0;
-$schema->storage->debugcb(sub { $queries++; });
-$schema->storage->debug(1);
-
-my @cd = $rs->all;
-
-is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
-
-ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
-
-is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
-
-is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
-
-is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
-
-is($queries, 1, 'prefetch ran only 1 select statement');
-
-$schema->storage->debug($orig_debug);
-$schema->storage->debugobj->callback(undef);
-
-# test for partial prefetch via columns attr
-my $cd = $schema->resultset('CD')->find(1,
-    {
-      columns => [qw/title artist artist.name/], 
-      join => { 'artist' => {} }
-    }
-);
-ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
-
-# start test for nested prefetch SELECT count
-$queries = 0;
-$schema->storage->debugcb(sub { $queries++ });
-$schema->storage->debug(1);
-
-$rs = $schema->resultset('Tag')->search(
-  {},
-  {
-    prefetch => { cd => 'artist' }
-  }
-);
-
-my $tag = $rs->first;
-
-is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
-
-is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
-
-# count the SELECTs
-#$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
-is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
-
-$queries = 0;
-
-is($tag->search_related('cd')->search_related('artist')->first->name,
-   'Caterwauler McCrae',
-   'chained belongs_to->belongs_to search_related ok');
-
-is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
-
-$queries = 0;
-
-$cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
-
-is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
-
-is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
-
-$queries = 0;
-
-$schema->storage->debugcb(sub { $queries++; });
-
-$cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
-
-is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
-
-is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
-
-$queries = 0;
-
-my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
-
-is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
-
-is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
-
-$schema->storage->debug($orig_debug);
-$schema->storage->debugobj->callback(undef);
-
-$rs = $schema->resultset('Tag')->search(
-  {},
-  {
-    join => { cd => 'artist' },
-    prefetch => { cd => 'artist' }
-  }
-);
-
-cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
-
-my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
-                 { order_by => 'artistid DESC', join => 'cds' });
-
-is($artist->name, 'Random Boy Band', "Join search by object ok");
-
-my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
-                               { join => 'liner_notes' });
-
-cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
-
-is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
-
-my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
-                                       { join => { 'cds' => 'tags' } });
-
-cmp_ok( @artists, '==', 2, "two-join search ok" );
-
-$rs = $schema->resultset("CD")->search(
-  {},
-  { group_by => [qw/ title me.cdid /] }
-);
-
-SKIP: {
-    skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
-        if $is_broken_sqlite;
-    cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
-}
-
-cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
-
-$rs = $schema->resultset("CD")->search(
-  {},
-  { join => [qw/ artist /], group_by => [qw/ artist.name /] }
-);
-
-SKIP: {
-    skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
-        if $is_broken_sqlite;
-    cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
-}
-
-$rs = $schema->resultset("Artist")->search(
-  {},
-      { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
-);
-
-cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
-
-$rs = $rs->search( undef, {  having =>{ 'count(*)'=> \'> 2' }});
-
-cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
-
-$rs = $schema->resultset("Artist")->search(
-        { 'cds.title' => 'Spoonful of bees',
-          'cds_2.title' => 'Forkful of bees' },
-        { join => [ 'cds', 'cds' ] });
-
-SKIP: {
-    skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
-        if $is_broken_sqlite;
-    cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
-}
-
-is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
-
-$cd = $schema->resultset('Artist')->first->create_related('cds',
-    {
-    title   => 'Unproduced Single',
-    year    => 2007
-});
-
-my $left_join = $schema->resultset('CD')->search(
-    { 'me.cdid' => $cd->cdid },
-    { prefetch => { cd_to_producer => 'producer' } }
-);
-
-cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
-
-$queries = 0;
-$schema->storage->debugcb(sub { $queries++ });
-$schema->storage->debug(1);
-
-my $tree_like =
-     $schema->resultset('TreeLike')->find(5,
-       { join     => { parent => { parent => 'parent' } },
-         prefetch => { parent => { parent => 'parent' } } });
-
-is($tree_like->name, 'quux', 'Bottom of tree ok');
-$tree_like = $tree_like->parent;
-is($tree_like->name, 'baz', 'First level up ok');
-$tree_like = $tree_like->parent;
-is($tree_like->name, 'bar', 'Second level up ok');
-$tree_like = $tree_like->parent;
-is($tree_like->name, 'foo', 'Third level up ok');
-
-$schema->storage->debug($orig_debug);
-$schema->storage->debugobj->callback(undef);
-
-cmp_ok($queries, '==', 1, 'Only one query run');
-
-$tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
-$tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
-is($tree_like->name, 'quux', 'Tree search_related ok');
-
-$tree_like = $schema->resultset('TreeLike')->search_related('children',
-    { 'children.id' => 3, 'children_2.id' => 4 },
-    { prefetch => { children => 'children' } }
-  )->first;
-is(eval { $tree_like->children->first->children->first->name }, 'quux',
-   'Tree search_related with prefetch ok');
-
-$tree_like = eval { $schema->resultset('TreeLike')->search(
-    { 'children.id' => 3, 'children_2.id' => 6 }, 
-    { join => [qw/children children/] }
-  )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
-  )->first->children->first; };
-is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
-
-# test that collapsed joins don't get a _2 appended to the alias
-
-my $sql = '';
-$schema->storage->debugcb(sub { $sql = $_[1] });
-$schema->storage->debug(1);
-
-eval {
-  my $row = $schema->resultset('Artist')->search_related('cds', undef, {
-    join => 'tracks',
-    prefetch => 'tracks',
-  })->search_related('tracks')->first;
-};
-
-like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
-
-$schema->storage->debug($orig_debug);
-$schema->storage->debugobj->callback(undef);
-
-$rs = $schema->resultset('Artist');
-$rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
-$rs->create({ artistid => 5, name => 'Emo 4ever' });
-@artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
-is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
-
-# -------------
-#
-# Tests for multilevel has_many prefetch
-
-# artist resultsets - with and without prefetch
-my $art_rs = $schema->resultset('Artist');
-my $art_rs_pr = $art_rs->search(
-    {},
-    {
-        join     => [ { cds => ['tracks'] } ],
-        prefetch => [ { cds => ['tracks'] } ],
-        cache    => 1 # last test needs this
-    }
-);
-
-# This test does the same operation twice - once on a
-# set of items fetched from the db with no prefetch of has_many rels
-# The second prefetches 2 levels of has_many
-# We check things are the same by comparing the name or title
-# we build everything into a hash structure and compare the one
-# from each rs to see what differs
-
-sub make_hash_struc {
-    my $rs = shift;
-
-    my $struc = {};
-    foreach my $art ( $rs->all ) {
-        foreach my $cd ( $art->cds ) {
-            foreach my $track ( $cd->tracks ) {
-                $struc->{ $art->name }{ $cd->title }{ $track->title }++;
-            }
-        }
-    }
-    return $struc;
-}
-
-$queries = 0;
-$schema->storage->debugcb(sub { $queries++ });
-$schema->storage->debug(1);
-
-my $prefetch_result = make_hash_struc($art_rs_pr);
-
-is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
-
-my $nonpre_result   = make_hash_struc($art_rs);
-
-is_deeply( $prefetch_result, $nonpre_result,
-    'Compare 2 level prefetch result to non-prefetch result' );
-
-$queries = 0;
-
-is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
-   'Fowlin',
-   'chained has_many->has_many search_related ok'
-  );
-
-is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');
-
-# once the following TODO is complete, remove the 2 warning tests immediately after the TODO block
-# (the TODO block itself contains tests ensuring that the warns are removed)
-TODO: {
-    local $TODO = 'Prefetch of multiple has_many rels at the same level (currently warn to protect the clueless git)';
-
-    #( 1 -> M + M )
-    my $cd_rs = $schema->resultset('CD')->search ({ 'me.title' => 'Forkful of bees' });
-    my $pr_cd_rs = $cd_rs->search ({}, {
-        prefetch => [qw/tracks tags/],
-    });
-
-    my $tracks_rs = $cd_rs->first->tracks;
-    my $tracks_count = $tracks_rs->count;
-
-    my ($pr_tracks_rs, $pr_tracks_count);
-
-    $queries = 0;
-    $schema->storage->debugcb(sub { $queries++ });
-    $schema->storage->debug(1);
-
-    my $o_mm_warn;
-    {
-        local $SIG{__WARN__} = sub { $o_mm_warn = shift };
-        $pr_tracks_rs = $pr_cd_rs->first->tracks;
-    };
-    $pr_tracks_count = $pr_tracks_rs->count;
-
-    ok(! $o_mm_warn, 'no warning on attempt to prefetch several same level has_many\'s (1 -> M + M)');
-
-    is($queries, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query');
-    is($pr_tracks_count, $tracks_count, 'equal count of prefetched relations over several same level has_many\'s (1 -> M + M)');
-
-    for ($pr_tracks_rs, $tracks_rs) {
-        $_->result_class ('DBIx::Class::ResultClass::HashRefInflator');
-    }
-
-    is_deeply ([$pr_tracks_rs->all], [$tracks_rs->all], 'same structure returned with and without prefetch over several same level has_many\'s (1 -> M + M)');
-
-    #( M -> 1 -> M + M )
-    my $note_rs = $schema->resultset('LinerNotes')->search ({ notes => 'Buy Whiskey!' });
-    my $pr_note_rs = $note_rs->search ({}, {
-        prefetch => {
-            cd => [qw/tags tracks/]
-        },
-    });
-
-    my $tags_rs = $note_rs->first->cd->tags;
-    my $tags_count = $tags_rs->count;
-
-    my ($pr_tags_rs, $pr_tags_count);
-
-    $queries = 0;
-    $schema->storage->debugcb(sub { $queries++ });
-    $schema->storage->debug(1);
-
-    my $m_o_mm_warn;
-    {
-        local $SIG{__WARN__} = sub { $m_o_mm_warn = shift };
-        $pr_tags_rs = $pr_note_rs->first->cd->tags;
-    };
-    $pr_tags_count = $pr_tags_rs->count;
-
-    ok(! $m_o_mm_warn, 'no warning on attempt to prefetch several same level has_many\'s (M -> 1 -> M + M)');
-
-    is($queries, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query');
-
-    is($pr_tags_count, $tags_count, 'equal count of prefetched relations over several same level has_many\'s (M -> 1 -> M + M)');
-
-    for ($pr_tags_rs, $tags_rs) {
-        $_->result_class ('DBIx::Class::ResultClass::HashRefInflator');
-    }
-
-    is_deeply ([$pr_tags_rs->all], [$tags_rs->all], 'same structure returned with and without prefetch over several same level has_many\'s (M -> 1 -> M + M)');
-};
-
-# remove this closure once the TODO above is working
-my $w;
-{
-    local $SIG{__WARN__} = sub { $w = shift };
-
-    my $rs = $schema->resultset('CD')->search ({ 'me.title' => 'Forkful of bees' }, { prefetch => [qw/tracks tags/] });
-    for (qw/all count next first/) {
-        undef $w;
-        my @stuff = $rs->search()->$_;
-        like ($w, qr/will currently disrupt both the functionality of .rs->count\(\), and the amount of objects retrievable via .rs->next\(\)/,
-            "warning on ->$_ attempt prefetching several same level has_manys (1 -> M + M)");
-    }
-    my $rs2 = $schema->resultset('LinerNotes')->search ({ notes => 'Buy Whiskey!' }, { prefetch => { cd => [qw/tags tracks/] } });
-    for (qw/all count next first/) {
-        undef $w;
-        my @stuff = $rs2->search()->$_;
-        like ($w, qr/will currently disrupt both the functionality of .rs->count\(\), and the amount of objects retrievable via .rs->next\(\)/,
-            "warning on ->$_ attempt prefetching several same level has_manys (M -> 1 -> M + M)");
-    }
-}
index 6108f28..8bd7e2c 100644 (file)
@@ -7,7 +7,7 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 45;
+plan tests => 49;
 
 # Check the defined unique constraints
 is_deeply(
@@ -183,3 +183,30 @@ is($row->baz, 3, 'baz is correct');
   ok($cd->in_storage, 'find correctly grepped the key across a relationship');
   is($cd->cdid, 1, 'cdid is correct');
 }
+
+# Test update_or_new
+{
+    my $cd1 = $schema->resultset('CD')->update_or_new(
+      {
+        artist => $artistid,
+        title  => "SuperHits $$",
+        year   => 2007,
+      },
+      { key => 'cd_artist_title' }
+    );
+
+    ok(!$cd1->in_storage, 'CD is not in storage yet after update_or_new');
+    $cd1->insert;
+    ok($cd1->in_storage, 'CD got added to strage after update_or_new && insert');
+
+    my $cd2 = $schema->resultset('CD')->update_or_new(
+      {
+        artist => $artistid,
+        title  => "SuperHits $$",
+        year   => 2008,
+      },
+      { key => 'cd_artist_title' }
+    );
+    ok($cd2->in_storage, 'Updating year using update_or_new was successful');
+    is($cd2->id, $cd1->id, 'Got the same CD using update_or_new');
+}
\ No newline at end of file
index 027ba76..32d5f17 100644 (file)
@@ -272,7 +272,7 @@ $schema->storage->disconnect;
 
   ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
 
-  eval {
+  lives_ok (sub {
     my $w;
     local $SIG{__WARN__} = sub { $w = shift };
 
@@ -281,32 +281,27 @@ $schema->storage->disconnect;
     outer($schema, 0);
 
     like ($w, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or an error/, 'Out of scope warning detected');
-  };
-
-  local $TODO = "Work out how this should work";
-  is($@, "Not sure what we want here, but something", "Rollback okay");
-
-  ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
+    ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
+  }, 'rollback successful withot exception');
 
   sub outer {
     my ($schema) = @_;
-   
+
     my $guard = $schema->txn_scope_guard;
     $schema->resultset('Artist')->create({
       name => 'Death Cab for Cutie',
     });
     inner(@_);
-    $guard->commit;
   }
 
   sub inner {
     my ($schema, $fatal) = @_;
-    my $guard = $schema->txn_scope_guard;
+
+    my $inner_guard = $schema->txn_scope_guard;
+    is($schema->storage->transaction_depth, 2, "Correct transaction depth");
 
     my $artist = $artist_rs->find({ name => 'Death Cab for Cutie' });
 
-    is($schema->storage->transaction_depth, 2, "Correct transaction depth");
-    undef $@;
     eval {
       $artist->cds->create({ 
         title => 'Plans',
@@ -320,6 +315,7 @@ $schema->storage->disconnect;
       die $@;
     }
 
-    # See what happens if we dont $guard->commit;
+    # inner guard should commit without consequences
+    $inner_guard->commit;
   }
 }
index 26b8425..5f8a542 100644 (file)
@@ -11,14 +11,7 @@ plan tests => 4;
 my $artist = $schema->resultset('Artist')->find(1);
 my $artist_cds = $artist->search_related('cds');
 
-my $cover_band;
-
-{
-  no warnings qw(redefine once);
-  local *DBICTest::Artist::result_source_instance = \&DBICTest::Schema::Artist::result_source_instance;
-
-  $cover_band = $artist->copy;
-}
+my $cover_band = $artist->copy;
 
 my $cover_cds = $cover_band->search_related('cds');
 cmp_ok($cover_band->id, '!=', $artist->id, 'ok got new column id...');
index 63de0d3..45f50c1 100644 (file)
@@ -83,7 +83,7 @@ $artist = $rs->first;
 $rs->reset();
 
 # make sure artist contains a related resultset for cds
-is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
+isa_ok( $artist->{related_resultsets}{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
 
 # check if $artist->cds->get_cache is populated
 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
index 154ddab..53930c2 100644 (file)
@@ -21,7 +21,6 @@ my $tests_per_run = 5;
 
 plan tests => $tests_per_run * @json_backends;
 
-use JSON::Any;
 for my $js (@json_backends) {
 
     eval {JSON::Any->import ($js) };
index baf8ef8..f3c0e01 100644 (file)
@@ -5,12 +5,17 @@ use Test::More;
 use lib qw(t/lib);
 use DBICTest;
 
+{
+  local $SIG{__WARN__} = sub { warn @_ if $_[0] !~ /extra \=\> .+? has been deprecated/ };
+  DBICTest::Schema->load_classes('EventTZDeprecated');
+}
+
 my $schema = DBICTest->init_schema();
 
 eval { require DateTime::Format::MySQL };
 plan skip_all => "Need DateTime::Format::MySQL for inflation tests" if $@;
 
-plan tests => 32;
+plan tests => 50;
 
 # inflation test
 my $event = $schema->resultset("Event")->find(1);
@@ -52,75 +57,78 @@ is("$created_cron", '2006-06-23T00:00:00', 'Correct date/time');
 
 
 # Test "timezone" parameter
-my $event_tz = $schema->resultset('EventTZ')->create({
-    starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ),
-    created_on => DateTime->new(year=>2006, month=>1, day=>31,
-        hour => 13, minute => 34, second => 56, time_zone => "America/New_York" ),
-});
 
-is ($event_tz->starts_at->day_name, "Montag", 'Locale de_DE loaded: day_name');
-is ($event_tz->starts_at->month_name, "Dezember", 'Locale de_DE loaded: month_name');
-is ($event_tz->created_on->day_name, "Tuesday", 'Default locale loaded: day_name');
-is ($event_tz->created_on->month_name, "January", 'Default locale loaded: month_name');
-
-my $starts_at = $event_tz->starts_at;
-is("$starts_at", '2007-12-31T00:00:00', 'Correct date/time using timezone');
-
-my $created_on = $event_tz->created_on;
-is("$created_on", '2006-01-31T12:34:56', 'Correct timestamp using timezone');
-is($event_tz->created_on->time_zone->name, "America/Chicago", "Correct timezone");
-
-my $loaded_event = $schema->resultset('EventTZ')->find( $event_tz->id );
-
-isa_ok($loaded_event->starts_at, 'DateTime', 'DateTime returned');
-$starts_at = $loaded_event->starts_at;
-is("$starts_at", '2007-12-31T00:00:00', 'Loaded correct date/time using timezone');
-is($starts_at->time_zone->name, 'America/Chicago', 'Correct timezone');
-
-isa_ok($loaded_event->created_on, 'DateTime', 'DateTime returned');
-$created_on = $loaded_event->created_on;
-is("$created_on", '2006-01-31T12:34:56', 'Loaded correct timestamp using timezone');
-is($created_on->time_zone->name, 'America/Chicago', 'Correct timezone');
-
-# Test floating timezone warning
-# We expect one warning
-SKIP: {
-    skip "ENV{DBIC_FLOATING_TZ_OK} was set, skipping", 1 if $ENV{DBIC_FLOATING_TZ_OK};
-    local $SIG{__WARN__} = sub {
-        like(
-            shift,
-            qr/You're using a floating timezone, please see the documentation of DBIx::Class::InflateColumn::DateTime for an explanation/,
-            'Floating timezone warning'
-        );
-    };
-    my $event_tz_floating = $schema->resultset('EventTZ')->create({
-        starts_at => DateTime->new(year=>2007, month=>12, day=>31, ),
-        created_on => DateTime->new(year=>2006, month=>1, day=>31,
-            hour => 13, minute => 34, second => 56, ),
-    });
-    delete $SIG{__WARN__};
-};
-
-# This should fail to set
-my $prev_str = "$created_on";
-$loaded_event->update({ created_on => '0000-00-00' });
-is("$created_on", $prev_str, "Don't update invalid dates");
-
-my $invalid = $schema->resultset('Event')->create({
-    starts_at  => '0000-00-00',
-    created_on => $created_on
-});
-
-is( $invalid->get_column('starts_at'), '0000-00-00', "Invalid date stored" );
-is( $invalid->starts_at, undef, "Inflate to undef" );
-
-$invalid->created_on('0000-00-00');
-$invalid->update;
-
-{
-    local $@;
-    eval { $invalid->created_on };
-    like( $@, qr/invalid date format/i, "Invalid date format exception");
+foreach my $tbl (qw/EventTZ EventTZDeprecated/) {
+  my $event_tz = $schema->resultset($tbl)->create({
+      starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ),
+      created_on => DateTime->new(year=>2006, month=>1, day=>31,
+          hour => 13, minute => 34, second => 56, time_zone => "America/New_York" ),
+  });
+
+  is ($event_tz->starts_at->day_name, "Montag", 'Locale de_DE loaded: day_name');
+  is ($event_tz->starts_at->month_name, "Dezember", 'Locale de_DE loaded: month_name');
+  is ($event_tz->created_on->day_name, "Tuesday", 'Default locale loaded: day_name');
+  is ($event_tz->created_on->month_name, "January", 'Default locale loaded: month_name');
+
+  my $starts_at = $event_tz->starts_at;
+  is("$starts_at", '2007-12-31T00:00:00', 'Correct date/time using timezone');
+
+  my $created_on = $event_tz->created_on;
+  is("$created_on", '2006-01-31T12:34:56', 'Correct timestamp using timezone');
+  is($event_tz->created_on->time_zone->name, "America/Chicago", "Correct timezone");
+
+  my $loaded_event = $schema->resultset($tbl)->find( $event_tz->id );
+
+  isa_ok($loaded_event->starts_at, 'DateTime', 'DateTime returned');
+  $starts_at = $loaded_event->starts_at;
+  is("$starts_at", '2007-12-31T00:00:00', 'Loaded correct date/time using timezone');
+  is($starts_at->time_zone->name, 'America/Chicago', 'Correct timezone');
+
+  isa_ok($loaded_event->created_on, 'DateTime', 'DateTime returned');
+  $created_on = $loaded_event->created_on;
+  is("$created_on", '2006-01-31T12:34:56', 'Loaded correct timestamp using timezone');
+  is($created_on->time_zone->name, 'America/Chicago', 'Correct timezone');
+
+  # Test floating timezone warning
+  # We expect one warning
+  SKIP: {
+      skip "ENV{DBIC_FLOATING_TZ_OK} was set, skipping", 1 if $ENV{DBIC_FLOATING_TZ_OK};
+      local $SIG{__WARN__} = sub {
+          like(
+              shift,
+              qr/You're using a floating timezone, please see the documentation of DBIx::Class::InflateColumn::DateTime for an explanation/,
+              'Floating timezone warning'
+          );
+      };
+      my $event_tz_floating = $schema->resultset($tbl)->create({
+          starts_at => DateTime->new(year=>2007, month=>12, day=>31, ),
+          created_on => DateTime->new(year=>2006, month=>1, day=>31,
+              hour => 13, minute => 34, second => 56, ),
+      });
+      delete $SIG{__WARN__};
+  };
+
+  # This should fail to set
+  my $prev_str = "$created_on";
+  $loaded_event->update({ created_on => '0000-00-00' });
+  is("$created_on", $prev_str, "Don't update invalid dates");
+
+  my $invalid = $schema->resultset('Event')->create({
+      starts_at  => '0000-00-00',
+      created_on => $created_on
+  });
+
+  is( $invalid->get_column('starts_at'), '0000-00-00', "Invalid date stored" );
+  is( $invalid->starts_at, undef, "Inflate to undef" );
+
+  $invalid->created_on('0000-00-00');
+  $invalid->update;
+
+  {
+      local $@;
+      eval { $invalid->created_on };
+      like( $@, qr/invalid date format/i, "Invalid date format exception");
+  }
 }
 
 ## varchar field using inflate_date => 1
index a277475..0339bfc 100644 (file)
@@ -45,8 +45,8 @@ my @cds = $artists2[0]->cds;
 cmp_ok(scalar @cds, '==', 1, "condition based on inherited join okay");
 
 my $rs3 = $rs2->search_related('cds');
-cmp_ok(scalar($rs3->all), '==', 45, "All cds for artist returned");
 
+cmp_ok(scalar($rs3->all), '==', 45, "All cds for artist returned");
 
 cmp_ok($rs3->count, '==', 45, "All cds for artist returned via count");
 
index d940eaa..bb55aba 100644 (file)
@@ -51,22 +51,21 @@ open(STDERR, '>&STDERRCOPY');
 
 # test trace output correctness for bind params
 {
-    my ($sql, @bind) = ('');
-    $schema->storage->debugcb( sub { $sql = $_[1] } );
+    my ($sql, @bind);
+    $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
 
     my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
     is_same_sql_bind(
-        $sql, [],
-        "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'", [],
+        $sql, \@bind,
+        "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND (cdid BETWEEN ? AND ?) ): '1', '1', '3'",
+        [qw/'1' '1' '3'/],
         'got correct SQL with all bind parameters (debugcb)'
     );
 
-    $schema->storage->debugcb(undef);
-    $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
     @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
     is_same_sql_bind(
         $sql, \@bind,
-        "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? )", ["'1'", "'1'", "'3'"],
+        "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND (cdid BETWEEN ? AND ?) )", ["'1'", "'1'", "'3'"],
         'got correct SQL with all bind parameters (debugobj)'
     );
 }
index 6c467cd..d132e35 100644 (file)
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 10;
+use Test::More tests => 12;
 
 use lib qw(t/lib);
 use base 'DBICTest';
@@ -11,6 +11,22 @@ my $schema = DBICTest->init_schema(
     no_connect  => 1,
     no_deploy   => 1,
 );
+
+ok $schema->connection(
+  DBICTest->_database,
+  {
+    on_connect_do => 'CREATE TABLE TEST_empty (id INTEGER)',
+  },
+), 'connection()';
+
+is_deeply (
+  $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
+  [],
+  'string version on_connect_do() worked'
+);
+
+$schema->storage->disconnect;
+
 ok $schema->connection(
     DBICTest->_database,
     {
@@ -24,10 +40,11 @@ ok $schema->connection(
     },
 ), 'connection()';
 
-is_deeply
+is_deeply (
   $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
   [ [ 2 ], [ 3 ], [ 7 ] ],
-  'on_connect_do() worked';
+  'on_connect_do() worked'
+);
 eval { $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); };
 ok $@, 'Searching for nonexistent table dies';
 
index 8a72864..ea77526 100644 (file)
@@ -57,7 +57,7 @@ my $it = $schema->resultset('Artist')->search( {},
       offset => 2,
       order_by => 'artistid' }
 );
-is( $it->count, 3, "LIMIT count ok" );
+is( $it->count, 3, "LIMIT count ok" );  # ask for 3 rows out of 7 artists
 is( $it->next->name, "Artist 2", "iterator->next ok" );
 $it->next;
 $it->next;
index b72405b..b85da4a 100644 (file)
@@ -4,12 +4,15 @@ use lib qw(t/lib);
 use Test::More;
 use Test::Exception;
 use DBICTest;
+use List::Util 'first';
+use Scalar::Util 'reftype';
+use IO::Handle;
 
 BEGIN {
     eval "use DBIx::Class::Storage::DBI::Replicated; use Test::Moose";
     plan $@
         ? ( skip_all => "Deps not installed: $@" )
-        : ( tests => 79 );
+        : ( tests => 90 );
 }
 
 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
@@ -49,10 +52,10 @@ TESTSCHEMACLASSES: {
     ## Initialize the object
     
        sub new {
-           my $class = shift @_;
+           my ($class, $schema_method) = (shift, shift);
            my $self = $class->SUPER::new(@_);
        
-           $self->schema( $self->init_schema );
+           $self->schema( $self->init_schema($schema_method) );
            return $self;
        }
     
@@ -62,30 +65,71 @@ TESTSCHEMACLASSES: {
         # current SQLT SQLite producer does not handle DROP TABLE IF EXISTS, trap warnings here
         local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/ };
 
-        my $class = shift @_;
-
-        my $schema = DBICTest->init_schema(
-            sqlite_use_file => 1,
-            storage_type=>{
-               '::DBI::Replicated' => {
-                       balancer_type=>'::Random',
-                    balancer_args=>{
-                       auto_validate_every=>100,
-                    },
-               }
-            },
-            deploy_args=>{
-                   add_drop_table => 1,
-            },
-        );
+        my ($class, $schema_method) = @_;
+
+        my $method = "get_schema_$schema_method";
+        my $schema = $class->$method;
 
         return $schema;
     }
-    
+
+    sub get_schema_by_storage_type {
+      DBICTest->init_schema(
+        sqlite_use_file => 1,
+        storage_type=>{
+          '::DBI::Replicated' => {
+            balancer_type=>'::Random',
+            balancer_args=>{
+              auto_validate_every=>100,
+             master_read_weight => 1
+            },
+          }
+        },
+        deploy_args=>{
+          add_drop_table => 1,
+        },
+      );
+    }
+
+    sub get_schema_by_connect_info {
+      DBICTest->init_schema(
+        sqlite_use_file => 1,
+        storage_type=> '::DBI::Replicated',
+        balancer_type=>'::Random',
+        balancer_args=> {
+          auto_validate_every=>100,
+         master_read_weight => 1
+        },
+        deploy_args=>{
+          add_drop_table => 1,
+        },
+      );
+    }
+
     sub generate_replicant_connect_info {}
     sub replicate {}
     sub cleanup {}
 
+    ## --------------------------------------------------------------------- ##
+    ## Add a connect_info option to test option merging.
+    ## --------------------------------------------------------------------- ##
+    {
+    package DBIx::Class::Storage::DBI::Replicated;
+
+    use Moose;
+
+    __PACKAGE__->meta->make_mutable;
+
+    around connect_info => sub {
+      my ($next, $self, $info) = @_;
+      $info->[3]{master_option} = 1;
+      $self->$next($info);
+    };
+
+    __PACKAGE__->meta->make_immutable;
+
+    no Moose;
+    }
   
     ## --------------------------------------------------------------------- ##
     ## Subclass for when you are using SQLite for testing, this provides a fake
@@ -124,9 +168,20 @@ TESTSCHEMACLASSES: {
             "dbi:SQLite:${_}";
         } @{$self->slave_paths};
         
-        return map { [$_,'','',{AutoCommit=>1}] } @dsn;
+        my @connect_infos = map { [$_,'','',{AutoCommit=>1}] } @dsn;
+
+    # try a hashref too
+        my $c = $connect_infos[0];
+        $connect_infos[0] = {
+          dsn => $c->[0],
+          user => $c->[1],
+          password => $c->[2],
+          %{ $c->[3] }
+        };
+
+        @connect_infos
     }
-    
+
     ## Do a 'good enough' replication by copying the master dbfile over each of
     ## the slave dbfiles.  If the master is SQLite we do this, otherwise we
     ## just do a one second pause to let the slaves catch up.
@@ -185,14 +240,22 @@ my $replicated_class = DBICTest->has_custom_dsn ?
     'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
     'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
 
-ok my $replicated = $replicated_class->new
-    => 'Created a replication object';
-    
-isa_ok $replicated->schema
-    => 'DBIx::Class::Schema';
-    
-isa_ok $replicated->schema->storage
-    => 'DBIx::Class::Storage::DBI::Replicated';
+my $replicated;
+
+for my $method (qw/by_connect_info by_storage_type/) {
+  ok $replicated = $replicated_class->new($method)
+      => "Created a replication object $method";
+      
+  isa_ok $replicated->schema
+      => 'DBIx::Class::Schema';
+      
+  isa_ok $replicated->schema->storage
+      => 'DBIx::Class::Storage::DBI::Replicated';
+
+  isa_ok $replicated->schema->storage->balancer
+      => 'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
+      => 'configured balancer_type';
+}
 
 ok $replicated->schema->storage->meta
     => 'has a meta object';
@@ -211,10 +274,38 @@ ok my @replicant_connects = $replicated->generate_replicant_connect_info
 
 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
     => 'Created some storages suitable for replicants';
-    
+
+ok my @all_storages = $replicated->schema->storage->all_storages
+    => '->all_storages';
+
+is scalar @all_storages,
+    3
+    => 'correct number of ->all_storages';
+
+is ((grep $_->isa('DBIx::Class::Storage::DBI'), @all_storages),
+    3
+    => '->all_storages are correct type');
+
+my @all_storage_opts =
+  grep { (reftype($_)||'') eq 'HASH' }
+    map @{ $_->_connect_info }, @all_storages;
+
+is ((grep $_->{master_option}, @all_storage_opts),
+    3
+    => 'connect_info was merged from master to replicants');
+my @replicant_names = keys %{ $replicated->schema->storage->replicants };
+
+## Silence warning about not supporting the is_replicating method if using the
+## sqlite dbs.
+$replicated->schema->storage->debugobj->silence(1)
+  if first { m{^t/} } @replicant_names;
+   
 isa_ok $replicated->schema->storage->balancer->current_replicant
-    => 'DBIx::Class::Storage::DBI';
-    
+    => 'DBIx::Class::Storage::DBI'; 
+
+$replicated->schema->storage->debugobj->silence(0);
+
 ok $replicated->schema->storage->pool->has_replicants
     => 'does have replicants';     
 
@@ -227,8 +318,6 @@ does_ok $replicated_storages[0]
 does_ok $replicated_storages[1]
     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
     
-my @replicant_names = keys %{$replicated->schema->storage->replicants};
-
 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
 
@@ -249,8 +338,16 @@ $replicated
 $replicated->replicate;
 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
+
+## Silence warning about not supporting the is_replicating method if using the
+## sqlite dbs.
+$replicated->schema->storage->debugobj->silence(1)
+  if first { m{^t/} } @replicant_names;
 $replicated->schema->storage->pool->validate_replicants;
 
+$replicated->schema->storage->debugobj->silence(0);
+
 ## Make sure we can read the data.
 
 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
@@ -262,6 +359,28 @@ isa_ok $artist1
 is $artist1->name, 'Ozric Tentacles'
     => 'Found expected name for first result';
 
+## Check that master_read_weight is honored
+{
+    no warnings 'once';
+
+    # turn off redefined warning
+    local $SIG{__WARN__} = sub {};
+
+    local
+    *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::_random_number =
+       sub { 999 };
+
+    $replicated->schema->storage->balancer->increment_storage;
+
+    is $replicated->schema->storage->balancer->current_replicant,
+       $replicated->schema->storage->master
+       => 'master_read_weight is honored';
+
+    ## turn it off for the duration of the test
+    $replicated->schema->storage->balancer->master_read_weight(0);
+    $replicated->schema->storage->balancer->increment_storage;
+}
+
 ## Add some new rows that only the master will have  This is because
 ## we overload any type of write operation so that is must hit the master
 ## database.
@@ -350,14 +469,34 @@ ok $replicated->schema->resultset('Artist')->find(2)
 
 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
-    
-ok $replicated->schema->resultset('Artist')->find(2)
-    => 'Fallback to master';
+
+{
+    ## catch the fallback to master warning
+    open my $debugfh, '>', \my $fallback_warning;
+    my $oldfh = $replicated->schema->storage->debugfh;
+    $replicated->schema->storage->debugfh($debugfh);
+
+    ok $replicated->schema->resultset('Artist')->find(2)
+       => 'Fallback to master';
+
+    like $fallback_warning, qr/falling back to master/
+       => 'emits falling back to master warning';
+
+    $replicated->schema->storage->debugfh($oldfh);
+}
 
 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
+
+## Silence warning about not supporting the is_replicating method if using the
+## sqlite dbs.
+$replicated->schema->storage->debugobj->silence(1)
+  if first { m{^t/} } @replicant_names;
 $replicated->schema->storage->pool->validate_replicants;
 
+$replicated->schema->storage->debugobj->silence(0);
+
 ok $replicated->schema->resultset('Artist')->find(2)
     => 'Returned to replicates';
     
@@ -578,11 +717,4 @@ ok $replicated->schema->resultset('Artist')->find(1)
 ## Delete the old database files
 $replicated->cleanup;
 
-use Data::Dump qw/dump/;
-#warn dump $replicated->schema->storage->read_handler;
-
-
-
-
-
-
+# vim: sw=4 sts=4 :
index 245d492..d62f117 100644 (file)
@@ -33,6 +33,8 @@ my $fn = {
 };
 
 use lib qw(t/lib);
+use DBICTest; # do not remove even though it is not used
+
 use_ok('DBICVersionOrig');
 
 my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
@@ -83,7 +85,7 @@ my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_v
   # should overwrite files and warn about it
   my @w;
   local $SIG{__WARN__} = sub { 
-    if ($_[0] =~ /^Overwriting/) {
+    if ($_[0] =~ /Overwriting existing/) {
       push @w, $_[0];
     }
     else {
@@ -93,8 +95,8 @@ my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_v
   $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
 
   is (2, @w, 'A warning generated for both the DDL and the diff');
-  like ($w[0], qr/^Overwriting existing DDL file - $fn->{v2}/, 'New version DDL overwrite warning');
-  like ($w[1], qr/^Overwriting existing diff file - $fn->{trans}/, 'Upgrade diff overwrite warning');
+  like ($w[0], qr/Overwriting existing DDL file - $fn->{v2}/, 'New version DDL overwrite warning');
+  like ($w[1], qr/Overwriting existing diff file - $fn->{trans}/, 'Upgrade diff overwrite warning');
 }
 
 {
index 0f7dda8..48f66ac 100644 (file)
@@ -20,10 +20,7 @@ my $schema = DBICTest->init_schema();
 my $sql_maker = $schema->storage->sql_maker;
 
 
-SKIP: {
-  skip "SQL::Abstract < 1.49 does not pass through arrayrefs", 2
-    if $SQL::Abstract::VERSION < 1.49;
-
+{
   my ($sql, @bind) = $sql_maker->insert(
             'lottery',
             {
index 4fa987b..a7687f8 100644 (file)
@@ -110,10 +110,7 @@ is_same_sql_bind(
   'scalar ORDER BY okay (multiple values)'
 );
 
-SKIP: {
-  skip "SQL::Abstract < 1.49 does not support hashrefs in order_by", 2
-    if $SQL::Abstract::VERSION < 1.49;
-
+{
   ($sql, @bind) = $sql_maker->select(
             [
               {
@@ -236,10 +233,7 @@ is_same_sql_bind(
   'quoted table names for UPDATE'
 );
 
-SKIP: {
-  skip "select attr with star does not work in SQL::Abstract < 1.49", 1
-    if $SQL::Abstract::VERSION < 1.49;
-
+{
   ($sql, @bind) = $sql_maker->select(
         [
           {
index 20b8c88..9cd754f 100644 (file)
@@ -362,16 +362,16 @@ lives_ok ( sub {
 
        });
        
-       ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
+       isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
        ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
        
        my $tracks = $cd_result->tracks;
        
-       ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
+       isa_ok( $tracks, 'DBIx::Class::ResultSet', 'Got Expected Tracks ResultSet');
        
        foreach my $track ($tracks->all)
        {
-               ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
+               isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
        }
 }, 'First create_related pass');
 
@@ -391,17 +391,17 @@ lives_ok ( sub {
 
        });
        
-       ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
+       isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
        ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
   ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
        
        my $tracks = $cd_result->tracks;
        
-       ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
+       isa_ok( $tracks, 'DBIx::Class::ResultSet', "Got Expected Tracks ResultSet");
        
        foreach my $track ($tracks->all)
        {
-               ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
+               isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
        }
 }, 'second create_related with same arguments');
 
diff --git a/t/98rows_prefetch.t b/t/98rows_prefetch.t
deleted file mode 100644 (file)
index 8619c13..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-# Test to ensure we get a consistent result set wether or not we use the
-# prefetch option in combination rows (LIMIT).
-use strict;
-use warnings;
-
-use Test::More;
-use lib qw(t/lib);
-use DBICTest;
-
-plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 2);
-
-my $schema = DBICTest->init_schema();
-my $no_prefetch = $schema->resultset('Artist')->search(
-       undef,
-       { rows => 3 }
-);
-
-my $use_prefetch = $schema->resultset('Artist')->search(
-       undef,
-       {
-               prefetch => 'cds',
-               rows     => 3
-       }
-);
-
-my $no_prefetch_count  = 0;
-my $use_prefetch_count = 0;
-
-is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
-
-TODO: {
-       local $TODO = "This is a difficult bug to fix, workaround is not to use prefetch with rows";
-       $no_prefetch_count++  while $no_prefetch->next;
-       $use_prefetch_count++ while $use_prefetch->next;
-       is(
-               $no_prefetch_count,
-               $use_prefetch_count,
-               "manual row count confirms consistency"
-               . " (\$no_prefetch_count == $no_prefetch_count, "
-               . " \$use_prefetch_count == $use_prefetch_count)"
-       );
-}
index 0a42306..9968a82 100644 (file)
@@ -7,9 +7,9 @@ use DBICTest;
 
 
 BEGIN {
-    eval "use DBD::mysql; use SQL::Translator 0.09003;";
+    eval "use SQL::Translator 0.09003;";
     if ($@) {
-        plan skip_all => 'needs DBD::mysql and SQL::Translator 0.09003 for testing';
+        plan skip_all => 'needs SQL::Translator 0.09003 for testing';
     }
 }
 
index 4166226..61c7b90 100644 (file)
@@ -1,6 +1,7 @@
 use strict;
 
 use Test::More;
+use lib 't/cdbi/testlib';
 
 BEGIN {
   eval "use DBIx::Class::CDBICompat;";
@@ -13,7 +14,7 @@ BEGIN {
 #-----------------------------------------------------------------------
 package State;
 
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 State->table('State');
 State->columns(Essential => qw/Abbreviation Name/);
@@ -39,7 +40,7 @@ sub Snowfall { 1 }
 
 package City;
 
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 City->table('City');
 City->columns(All => qw/Name State Population/);
@@ -56,7 +57,7 @@ City->columns(All => qw/Name State Population/);
 
 #-------------------------------------------------------------------------
 package CD;
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 CD->table('CD');
 CD->columns('All' => qw/artist title length/);
index 988951d..0e1b22e 100644 (file)
@@ -151,7 +151,7 @@ is($blrunner_dc->NumExplodingSheep, undef, 'Sheep correct');
 
 # Multi-column search
 {
-       my @films = $blrunner->search_like(title => "Bladerunner%", rating => '15');
+       my @films = $blrunner->search (title => { -like => "Bladerunner%"}, rating => '15');
        is @films, 1, "Only one Bladerunner is a 15";
 }
 
@@ -208,7 +208,7 @@ is(scalar @films, 1, ' search returns one film');
 is($films[0]->id, $gone->id, ' ... the correct one');
 
 # Find all films which were directed by Bob
-@films = Film->search_like('Director', 'Bob %');
+@films = Film->search ( { 'Director' => { -like => 'Bob %' } });
 is(scalar @films, 3, ' search_like returns 3 films');
 ok(
        eq_array(
index bb1f288..4c6e178 100644 (file)
@@ -82,9 +82,9 @@ eval {    # Multiple false columns
 ok($@, $@);
 
 
-warning_is {
+warning_like {
     Lazy->columns( TEMP => qw(that) );
-} "Declaring column that as TEMP but it already exists";
+} qr/Declaring column that as TEMP but it already exists/;
 
 # Test that create() and update() throws out columns that changed
 {
index 72f2c54..3419cf0 100644 (file)
@@ -25,6 +25,11 @@ INIT {
     sub Class::DBI::sheep { ok 0; }
 }
 
+# Install the deprecation warning intercept here for the rest of the 08 dev cycle
+local $SIG{__WARN__} = sub {
+  warn @_ unless (DBIx::Class->VERSION < 0.09 and $_[0] =~ /Query returned more than one row/);
+};
+
 sub Film::mutator_name {
     my ($class, $col) = @_;
     return "set_sheep" if lc $col eq "numexplodingsheep";
@@ -160,9 +165,6 @@ is $@, '', "No errors";
     like $@, qr/film/, "no hasa film";
 
     eval {
-        local $SIG{__WARN__} = sub {
-            warn @_ unless $_[0] =~ /Query returned more than one row/;
-        };
         ok my $f = $ac->movie, "hasa movie";
         isa_ok $f, "Film";
         is $f->id, $bt->id, " - Bad Taste";
@@ -264,5 +266,5 @@ is $@, '', "No errors";
     my $abigail = eval { Film->create({ title => "Abigail's Party" }) };
     like $@, qr/read only/, "Or create new films";
 
-    $sandl->discard_changes;
+    $_->discard_changes for ($naked, $sandl);
 }
index 9d943e5..5267ae0 100644 (file)
@@ -12,6 +12,7 @@ if ($@) {
 eval { require Time::Piece::MySQL };
 plan skip_all => "Need Time::Piece::MySQL for this test" if $@;
 
+use lib 't/cdbi/testlib';
 eval { require 't/cdbi/testlib/Log.pm' };
 plan skip_all => "Need MySQL for this test" if $@;
 
index f9bd027..6ec7fe1 100644 (file)
@@ -1,5 +1,6 @@
 use strict;
 use Test::More;
+use lib 't/cdbi/testlib';
 
 BEGIN {
   eval "use DBIx::Class::CDBICompat;";
@@ -10,7 +11,7 @@ BEGIN {
 {
     package Thing;
 
-    use base 'DBIx::Class::Test::SQLite';
+    use base 'DBIC::Test::SQLite';
 
     Thing->columns(TEMP => qw[foo bar]);
     Thing->columns(All  => qw[thing_id yarrow flower]);
index 25eb255..a6e60ba 100644 (file)
@@ -15,7 +15,7 @@ INIT {
     package # hide from PAUSE 
         MyFilm;
 
-    use base 'DBIx::Class::Test::SQLite';
+    use base 'DBIC::Test::SQLite';
     use strict;
 
     __PACKAGE__->set_table('Movies');
index 4311456..d6a9484 100644 (file)
@@ -1,5 +1,6 @@
 use strict;
 use Test::More;
+use lib 't/cdbi/testlib';
 
 BEGIN {
   eval "use DBIx::Class::CDBICompat;";
@@ -10,7 +11,7 @@ BEGIN {
 {
     package Thing;
 
-    use base 'DBIx::Class::Test::SQLite';
+    use base 'DBIC::Test::SQLite';
 
     Thing->columns(TEMP => qw[foo bar baz]);
     Thing->columns(All  => qw[some real stuff]);
index 83cf1a2..47b0a35 100644 (file)
@@ -1,5 +1,6 @@
 use strict;
 use Test::More;
+use lib 't/cdbi/testlib';
 
 BEGIN {
   eval "use DBIx::Class::CDBICompat;";
@@ -19,7 +20,7 @@ local $SIG{__WARN__} = sub {
 {
     package Thing;
 
-    use base 'DBIx::Class::Test::SQLite';
+    use base 'DBIC::Test::SQLite';
 
     Thing->columns(All  => qw[thing_id this that date]);
 }
index 7dd17ce..45ce621 100644 (file)
@@ -1,6 +1,7 @@
 use strict;
 use Test::More;
 use Test::Exception;
+use lib 't/cdbi/testlib';
 
 BEGIN {
   eval "use DBIx::Class::CDBICompat;";
@@ -13,7 +14,7 @@ BEGIN {
 {
     package Thing;
 
-    use base 'DBIx::Class::Test::SQLite';
+    use base 'DBIC::Test::SQLite';
 
     Thing->columns(All  => qw[thing_id this that date]);
 }
index 4e27abe..2944390 100644 (file)
@@ -4,7 +4,7 @@ package # hide from PAUSE
 use strict;
 use warnings;
 
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 __PACKAGE__->set_table('Actor');
 
index ba38551..9e4ebe4 100644 (file)
@@ -4,7 +4,7 @@ package # hide from PAUSE
 use strict;
 use warnings;
 
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 __PACKAGE__->set_table( 'ActorAlias' );
 
index a112f47..7c6dfdb 100644 (file)
@@ -2,7 +2,7 @@ package # hide from PAUSE
     Blurb;
 
 use strict;
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 __PACKAGE__->set_table('Blurbs');
 __PACKAGE__->columns('Primary', 'Title');
index 22c6262..4367ef0 100644 (file)
@@ -2,6 +2,6 @@ package # hide from PAUSE
     CDBase;
 
 use strict;
-use base qw(DBIx::Class::Test::SQLite);
+use base qw(DBIC::Test::SQLite);
 
 1;
similarity index 97%
rename from lib/DBIx/Class/Test/SQLite.pm
rename to t/cdbi/testlib/DBIC/Test/SQLite.pm
index 3302289..3b17953 100644 (file)
@@ -1,4 +1,5 @@
-package DBIx::Class::Test::SQLite;
+package # hide from PAUSE
+    DBIC::Test::SQLite;
 
 =head1 NAME
 
index af0a453..a9dd199 100644 (file)
@@ -2,7 +2,7 @@ package # hide from PAUSE
     Director;
 
 use strict;
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 __PACKAGE__->set_table('Directors');
 __PACKAGE__->columns('All' => qw/ Name Birthday IsInsane /);
index 6521a59..b1f50ac 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     Film;
 
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 use strict;
 
 __PACKAGE__->set_table('Movies');
index 2b2137e..5835de2 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     Lazy;
 
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 use strict;
 
 __PACKAGE__->set_table("Lazy");
index eeb7cf0..5dfbfed 100644 (file)
@@ -8,9 +8,7 @@ use DBI;
 
 use vars qw/$dbh/;
 
-# temporary, might get switched to the new test framework someday
-my @connect = ("dbi:mysql:test", "", "", { PrintError => 0});
-
+my @connect = (@ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}, { PrintError => 0});
 $dbh = DBI->connect(@connect) or die DBI->errstr;
 my @table;
 
index 703005d..fa1f296 100644 (file)
@@ -2,7 +2,7 @@ package # hide from PAUSE
     Order;
 
 use strict;
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 __PACKAGE__->set_table('orders');
 __PACKAGE__->table_alias('orders');
index 08c31ba..59fb818 100644 (file)
@@ -1,5 +1,5 @@
 package OtherThing;
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 OtherThing->set_table("other_thing");
 OtherThing->columns(All => qw(id));
index d71e22a..4080b66 100644 (file)
@@ -1,5 +1,5 @@
 package Thing;
-use base 'DBIx::Class::Test::SQLite';
+use base 'DBIC::Test::SQLite';
 
 Thing->set_table("thing");
 Thing->columns(All => qw(id that_thing));
diff --git a/t/count/distinct.t b/t/count/distinct.t
new file mode 100644 (file)
index 0000000..04900d2
--- /dev/null
@@ -0,0 +1,90 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use Test::Exception;
+
+use lib qw(t/lib);
+
+use DBICTest;
+use DBIC::SqlMakerTest;
+
+my $schema = DBICTest->init_schema();
+
+eval "use DBD::SQLite";
+plan skip_all => 'needs DBD::SQLite for testing' if $@;
+plan tests => 22;
+
+# The tag Blue is assigned to cds 1 2 3 and 5
+# The tag Cheesy is assigned to cds 2 4 and 5
+#
+# This combination should make some interesting group_by's
+#
+my $rs;
+my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
+
+$rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
+is($rs->count, 4, 'Count without DISTINCT');
+
+$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
+is($rs->count, 2, 'Count with single column group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
+is($rs->count, 5, 'Count with another single column group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
+is($rs->count, 4, 'Count with multiple column group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
+is($rs->count, 4, 'Count with single column distinct');
+
+$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
+is($rs->count, 7, 'Count with IN subquery');
+
+$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
+is($rs->count, 2, 'Count with IN subquery with outside group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
+is($rs->count, 7, 'Count with IN subquery with outside distinct');
+
+$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }), 
+is($rs->count, 2, 'Count with IN subquery with outside distinct on a single column');
+
+$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
+is($rs->count, 7, 'Count with IN subquery with single group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'cd' })->get_column('tag')->as_query } });
+is($rs->count, 7, 'Count with IN subquery with another single group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
+is($rs->count, 7, 'Count with IN subquery with multiple group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
+is($rs->count, 4, 'Count without DISTINCT, using literal SQL');
+
+$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
+is($rs->count, 2, 'Count with literal SQL and single group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
+is($rs->count, 5, 'Count with literal SQL and another single group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] });
+is($rs->count, 7, 'Count with literal SQL and multiple group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 });
+is($rs->count, 4, 'Count with +select aggreggate');
+
+$rs = $schema->resultset('Tag')->search({}, { select => 'length(me.tag)', distinct => 1 });
+is($rs->count, 3, 'Count by distinct function result as select literal');
+
+my @warnings;
+{
+  local $SIG{__WARN__} = sub { push @warnings, shift };
+  my $row = $schema->resultset('Tag')->search({}, { select => { distinct => 'tag' } })->first;
+}
+
+is(@warnings, 1, 'expecteing warn');
+
+dies_ok(sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first }, 'expecting to die');
+dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');
+dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { select => { length => 'tag' }, distinct => 1 })->count }, 'expecting to die');
diff --git a/t/count/grouped_pager.t b/t/count/grouped_pager.t
new file mode 100644 (file)
index 0000000..cfe29af
--- /dev/null
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+use lib qw(t/lib);
+
+use DBICTest;
+
+plan tests => 7;
+
+my $schema = DBICTest->init_schema();
+
+use Data::Dumper;
+
+# add 2 extra artists
+$schema->populate ('Artist', [
+    [qw/name/],
+    [qw/ar_1/],
+    [qw/ar_2/],
+]);
+
+# add 3 extra cds to every artist
+for my $ar ($schema->resultset ('Artist')->all) {
+  for my $cdnum (1 .. 3) {
+    $ar->create_related ('cds', {
+      title => "bogon $cdnum",
+      year => 2000 + $cdnum,
+    });
+  }
+}
+
+my $cds = $schema->resultset ('CD')->search ({}, { group_by => 'artist' } );
+is ($cds->count, 5, 'Resultset collapses to 5 groups');
+
+my ($pg1, $pg2, $pg3) = map { $cds->search_rs ({}, {rows => 2, page => $_}) } (1..3);
+
+for ($pg1, $pg2, $pg3) {
+  is ($_->pager->total_entries, 5, 'Total count via pager correct');
+}
+
+is ($pg1->count, 2, 'First page has 2 groups');
+is ($pg2->count, 2, 'Second page has 2 groups');
+is ($pg3->count, 1, 'Third page has one group remaining');
diff --git a/t/count/in_subquery.t b/t/count/in_subquery.t
new file mode 100644 (file)
index 0000000..1275c1e
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Data::Dumper;
+
+use Test::More;
+
+plan ( tests => 1 );
+
+use lib qw(t/lib);
+use DBICTest;
+use DBIC::SqlMakerTest;
+
+my $schema = DBICTest->init_schema();
+
+{
+    my $rs = $schema->resultset("CD")->search(
+        { 'artist.name' => 'Caterwauler McCrae' },
+        { join => [qw/artist/]}
+    );
+    my $squery = $rs->get_column('cdid')->as_query;
+    my $subsel_rs = $schema->resultset("CD")->search( { cdid => { IN => $squery } } );
+    is($subsel_rs->count, $rs->count, 'Subselect on PK got the same row count');
+}
diff --git a/t/count/joined.t b/t/count/joined.t
new file mode 100644 (file)
index 0000000..5ba7deb
--- /dev/null
@@ -0,0 +1,31 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+use lib qw(t/lib);
+
+use DBICTest;
+
+plan tests => 3;
+
+my $schema = DBICTest->init_schema();
+
+my $cds = $schema->resultset("CD")->search({ cdid => 1 }, { join => { cd_to_producer => 'producer' } });
+cmp_ok($cds->count, '>', 1, "extra joins explode entity count");
+
+is (
+  $cds->search({}, { prefetch => 'cd_to_producer' })->count,
+  1,
+  "Count correct with extra joins collapsed by prefetch"
+);
+
+is (
+  $cds->search({}, { distinct => 1 })->count,
+  1,
+  "Count correct with requested distinct collapse of main table"
+);
+
+
+
+
diff --git a/t/count/prefetch.t b/t/count/prefetch.t
new file mode 100644 (file)
index 0000000..54f6c05
--- /dev/null
@@ -0,0 +1,63 @@
+use strict;
+use warnings;
+
+use lib qw(t/lib);
+
+use Test::More;
+use DBICTest;
+use DBIC::SqlMakerTest;
+use DBIC::DebugObj;
+
+plan tests => 6;
+
+my $schema = DBICTest->init_schema();
+
+# collapsing prefetch
+{
+  my $rs = $schema->resultset("Artist")
+            ->search_related('cds',
+                { 'tracks.position' => [1,2] },
+                { prefetch => [qw/tracks artist/] },
+            );
+  is ($rs->all, 5, 'Correct number of objects');
+
+
+  my ($sql, @bind);
+  $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
+  $schema->storage->debug(1);
+
+
+  is ($rs->count, 5, 'Correct count');
+
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid) count_subq',
+    [ qw/'1' '2'/ ],
+  );
+}
+
+# non-collapsing prefetch (no multi prefetches)
+{
+  my $rs = $schema->resultset("CD")
+            ->search_related('tracks',
+                { position => [1,2] },
+                { prefetch => [qw/disc lyrics/] },
+            );
+  is ($rs->all, 10, 'Correct number of objects');
+
+
+  my ($sql, @bind);
+  $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
+  $schema->storage->debug(1);
+
+
+  is ($rs->count, 10, 'Correct count');
+
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    'SELECT COUNT( * ) FROM cd me LEFT JOIN track tracks ON tracks.cd = me.cdid JOIN cd disc ON disc.cdid = tracks.cd LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid WHERE ( ( position = ? OR position = ? ) )',
+    [ qw/'1' '2'/ ],
+  );
+}
similarity index 100%
rename from t/deleting_many_to_many.t
rename to t/delete/m2m.t
diff --git a/t/delete/related.t b/t/delete/related.t
new file mode 100644 (file)
index 0000000..f3fb78b
--- /dev/null
@@ -0,0 +1,45 @@
+use Test::More;
+use strict;
+use warnings;
+use lib qw(t/lib);
+use DBICTest;
+
+plan tests => 3;
+
+my $schema = DBICTest->init_schema();
+
+my $ars = $schema->resultset('Artist');
+my $cdrs = $schema->resultset('CD');
+
+# create some custom entries
+$ars->populate ([
+  [qw/artistid  name/],
+  [qw/71        a1/],
+  [qw/72        a2/],
+  [qw/73        a3/],
+]);
+$cdrs->populate ([
+  [qw/cdid artist title   year/],
+  [qw/70   71     delete0 2005/],
+  [qw/71   72     delete1 2005/],
+  [qw/72   72     delete2 2005/],
+  [qw/73   72     delete3 2006/],
+  [qw/74   72     delete4 2007/],
+  [qw/75   73     delete5 2008/],
+]);
+
+my $total_cds = $cdrs->count;
+
+# test that delete_related w/o conditions deletes all related records only
+$ars->search ({name => 'a3' })->search_related ('cds')->delete;
+is ($cdrs->count, $total_cds -= 1, 'related delete ok');
+
+my $a2_cds = $ars->search ({ name => 'a2' })->search_related ('cds');
+
+# test that related deletion w/conditions deletes just the matched related records only
+$a2_cds->search ({ year => 2005 })->delete;
+is ($cdrs->count, $total_cds -= 2, 'related + condition delete ok');
+
+# test that related deletion with limit condition works
+$a2_cds->search ({}, { rows => 1})->delete;
+is ($cdrs->count, $total_cds -= 1, 'related + limit delete ok');
diff --git a/t/from_subquery.t b/t/from_subquery.t
new file mode 100644 (file)
index 0000000..8c777ea
--- /dev/null
@@ -0,0 +1,174 @@
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More;
+
+BEGIN {
+    eval "use SQL::Abstract 1.49";
+    plan $@
+        ? ( skip_all => "Needs SQLA 1.49+" )
+        : ( tests => 8 );
+}
+
+use lib qw(t/lib);
+use DBICTest;
+use DBIC::SqlMakerTest;
+
+my $schema = DBICTest->init_schema();
+my $art_rs = $schema->resultset('Artist');
+my $cdrs = $schema->resultset('CD');
+
+{
+  my $cdrs2 = $cdrs->search({
+    artist_id => { 'in' => $art_rs->search({}, { rows => 1 })->get_column( 'id' )->as_query },
+  });
+
+  is_same_sql_bind(
+    $cdrs2->as_query,
+    "(SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE artist_id IN ( SELECT id FROM artist me LIMIT 1 ))",
+    [],
+  );
+}
+
+{
+  my $rs = $art_rs->search(
+    {},
+    {
+      'select' => [
+        $cdrs->search({}, { rows => 1 })->get_column('id')->as_query,
+      ],
+    },
+  );
+
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me)",
+    [],
+  );
+}
+
+{
+  my $rs = $art_rs->search(
+    {},
+    {
+      '+select' => [
+        $cdrs->search({}, { rows => 1 })->get_column('id')->as_query,
+      ],
+    },
+  );
+
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me)",
+    [],
+  );
+}
+
+# simple from
+{
+  my $rs = $cdrs->search(
+    {},
+    {
+      alias => 'cd2',
+      from => [
+        { cd2 => $cdrs->search({ id => { '>' => 20 } })->as_query },
+      ],
+    },
+  );
+
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE ( id > ? ) ) cd2)",
+    [
+      [ 'id', 20 ]
+    ],
+  );
+}
+
+# nested from
+{
+  my $art_rs2 = $schema->resultset('Artist')->search({}, 
+  {
+    from => [ { 'me' => 'artist' }, 
+      [ { 'cds' => $cdrs->search({},{ 'select' => [\'me.artist as cds_artist' ]})->as_query },
+      { 'me.artistid' => 'cds_artist' } ] ]
+  });
+
+  is_same_sql_bind(
+    $art_rs2->as_query,
+    "(SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me JOIN (SELECT me.artist as cds_artist FROM cd me) cds ON me.artistid = cds_artist)",
+    []
+  );
+
+
+}
+
+# nested subquery in from
+{
+  my $rs = $cdrs->search(
+    {},
+    {
+      alias => 'cd2',
+      from => [
+        { cd2 => $cdrs->search(
+            { id => { '>' => 20 } }, 
+            { 
+                alias => 'cd3',
+                from => [ 
+                { cd3 => $cdrs->search( { id => { '<' => 40 } } )->as_query }
+                ],
+            }, )->as_query },
+      ],
+    },
+  );
+
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track 
+      FROM 
+        (SELECT cd3.cdid,cd3.artist,cd3.title,cd3.year,cd3.genreid,cd3.single_track 
+          FROM 
+            (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track 
+              FROM cd me WHERE ( id < ? ) ) cd3
+          WHERE ( id > ? ) ) cd2)",
+    [
+      [ 'id', 40 ], 
+      [ 'id', 20 ]
+    ],
+  );
+
+}
+
+{
+  my $rs = $cdrs->search({
+    year => {
+      '=' => $cdrs->search(
+        { artistid => { '=' => \'me.artistid' } },
+        { alias => 'inner' }
+      )->get_column('year')->max_rs->as_query,
+    },
+  });
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE year = (SELECT MAX(inner.year) FROM cd inner WHERE artistid = me.artistid))",
+    [],
+  );
+}
+
+{
+  my $rs = $cdrs->search(
+    {},
+    {
+      alias => 'cd2',
+      from => [
+        { cd2 => $cdrs->search({ title => 'Thriller' })->as_query },
+      ],
+    },
+  );
+
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE ( title = ? ) ) cd2)",
+    [ [ 'title', 'Thriller' ] ],
+  );
+}
index cf33fd9..1098e25 100644 (file)
@@ -3,144 +3,52 @@ package DBIC::SqlMakerTest;
 use strict;
 use warnings;
 
-use base qw/Test::Builder::Module Exporter/;
+use base qw/Exporter/;
+
+use Carp;
+use SQL::Abstract::Test;
 
 our @EXPORT = qw/
-  &is_same_sql_bind
-  &is_same_sql
-  &is_same_bind
-  &eq_sql
-  &eq_bind
-  &eq_sql_bind
+  is_same_sql_bind
+  is_same_sql
+  is_same_bind
+/;
+our @EXPORT_OK = qw/
+  eq_sql
+  eq_bind
+  eq_sql_bind
 /;
 
+sub is_same_sql_bind {
+  # unroll possible as_query arrayrefrefs
+  my @args;
 
-{
-  package DBIC::SqlMakerTest::SQLATest;
-
-  # replacement for SQL::Abstract::Test if not available
-
-  use strict;
-  use warnings;
-
-  use base qw/Test::Builder::Module Exporter/;
-
-  use Scalar::Util qw(looks_like_number blessed reftype);
-  use Data::Dumper;
-  use Test::Builder;
-  use Test::Deep qw(eq_deeply);
-
-  our $tb = __PACKAGE__->builder;
-
-  sub is_same_sql_bind
-  {
-    my ($sql1, $bind_ref1, $sql2, $bind_ref2, $msg) = @_;
-
-    my $same_sql = eq_sql($sql1, $sql2);
-    my $same_bind = eq_bind($bind_ref1, $bind_ref2);
-
-    $tb->ok($same_sql && $same_bind, $msg);
-
-    if (!$same_sql) {
-      _sql_differ_diag($sql1, $sql2);
-    }
-    if (!$same_bind) {
-      _bind_differ_diag($bind_ref1, $bind_ref2);
-    }
-  }
-
-  sub is_same_sql
-  {
-    my ($sql1, $sql2, $msg) = @_;
-
-    my $same_sql = eq_sql($sql1, $sql2);
-
-    $tb->ok($same_sql, $msg);
+  for (1,2) {
+    my $chunk = shift @_;
 
-    if (!$same_sql) {
-      _sql_differ_diag($sql1, $sql2);
+    if ( ref $chunk eq 'REF' and ref $$chunk eq 'ARRAY' ) {
+      my ($sql, @bind) = @$$chunk;
+      push @args, ($sql, \@bind);
     }
-  }
-
-  sub is_same_bind
-  {
-    my ($bind_ref1, $bind_ref2, $msg) = @_;
-
-    my $same_bind = eq_bind($bind_ref1, $bind_ref2);
-
-    $tb->ok($same_bind, $msg);
-
-    if (!$same_bind) {
-      _bind_differ_diag($bind_ref1, $bind_ref2);
+    else {
+      push @args, $chunk, shift @_;
     }
-  }
-
-  sub _sql_differ_diag
-  {
-    my ($sql1, $sql2) = @_;
 
-    $tb->diag("SQL expressions differ\n"
-      . "     got: $sql1\n"
-      . "expected: $sql2\n"
-    );
   }
 
-  sub _bind_differ_diag
-  {
-    my ($bind_ref1, $bind_ref2) = @_;
+  push @args, shift @_;
 
-    $tb->diag("BIND values differ\n"
-      . "     got: " . Dumper($bind_ref1)
-      . "expected: " . Dumper($bind_ref2)
-    );
-  }
-
-  sub eq_sql
-  {
-    my ($left, $right) = @_;
-
-    $left =~ s/\s+//g;
-    $right =~ s/\s+//g;
-
-    return $left eq $right;
-  }
-
-  sub eq_bind
-  {
-    my ($bind_ref1, $bind_ref2) = @_;
-
-    return eq_deeply($bind_ref1, $bind_ref2);
-  }
-
-  sub eq_sql_bind
-  {
-    my ($sql1, $bind_ref1, $sql2, $bind_ref2) = @_;
-
-    return eq_sql($sql1, $sql2) && eq_bind($bind_ref1, $bind_ref2);
-  }
-}
+  croak "Unexpected argument(s) supplied to is_same_sql_bind: " . join ('; ', @_)
+    if @_;
 
-eval "use SQL::Abstract::Test;";
-if ($@ eq '') {
-  # SQL::Abstract::Test available
-
-  *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind;
-  *is_same_sql = \&SQL::Abstract::Test::is_same_sql;
-  *is_same_bind = \&SQL::Abstract::Test::is_same_bind;
-  *eq_sql = \&SQL::Abstract::Test::eq_sql;
-  *eq_bind = \&SQL::Abstract::Test::eq_bind;
-  *eq_sql_bind = \&SQL::Abstract::Test::eq_sql_bind;
-} else {
-  # old SQL::Abstract
-
-  *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind;
-  *is_same_sql = \&DBIC::SqlMakerTest::SQLATest::is_same_sql;
-  *is_same_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_bind;
-  *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql;
-  *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind;
-  *eq_sql_bind = \&DBIC::SqlMakerTest::SQLATest::eq_sql_bind;
+  SQL::Abstract::Test::is_same_sql_bind (@args);
 }
 
+*is_same_sql = \&SQL::Abstract::Test::is_same_sql;
+*is_same_bind = \&SQL::Abstract::Test::is_same_bind;
+*eq_sql = \&SQL::Abstract::Test::eq_sql;
+*eq_bind = \&SQL::Abstract::Test::eq_bind;
+*eq_sql_bind = \&SQL::Abstract::Test::eq_sql_bind;
 
 1;
 
@@ -167,19 +75,27 @@ DBIC::SqlMakerTest - Helper package for testing sql_maker component of DBIC
 
 Exports functions that can be used to compare generated SQL and bind values.
 
-If L<SQL::Abstract::Test> (packaged in L<SQL::Abstract> versions 1.50 and
-above) is available, then it is used to perform the comparisons (all functions
-are delegated to id). Otherwise uses simple string comparison for the SQL
-statements and simple L<Data::Dumper>-like recursive stringification for
-comparison of bind values.
-
+This is a thin wrapper around L<SQL::Abstract::Test>, which makes it easier
+to compare as_query sql/bind arrayrefrefs directly.
 
 =head1 FUNCTIONS
 
 =head2 is_same_sql_bind
 
   is_same_sql_bind(
-    $given_sql, \@given_bind, 
+    $given_sql, \@given_bind,
+    $expected_sql, \@expected_bind,
+    $test_msg
+  );
+
+  is_same_sql_bind(
+    $rs->as_query
+    $expected_sql, \@expected_bind,
+    $test_msg
+  );
+
+  is_same_sql_bind(
+    \[$given_sql, @given_bind],
     $expected_sql, \@expected_bind,
     $test_msg
   );
@@ -245,4 +161,4 @@ Norbert Buchmuller, <norbi@nix.hu>
 Copyright 2008 by Norbert Buchmuller.
 
 This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself. 
+it under the same terms as Perl itself.
diff --git a/t/lib/DBICNGTest/Schema.pm b/t/lib/DBICNGTest/Schema.pm
deleted file mode 100644 (file)
index 57d2d50..0000000
+++ /dev/null
@@ -1,162 +0,0 @@
-package # hide from PAUSE
- DBICNGTest::Schema;
-   
-       use Moose;
-       use Path::Class::File;
-    extends 'DBIx::Class::Schema',  'Moose::Object'; 
-
-
-=head1 NAME
-
-DBICNGTest::Schema; Schema Base For Testing Moose Roles, Traits, etc.
-
-=head1 SYNOPSIS
-
-    my $schema = DBICNGTest::Schema->connect($dsn);
-    
-    ## Do anything you would as with a normal $schema object.
-
-=head1 DESCRIPTION
-
-Defines the base case for loading DBIC Schemas.  We add in some additional
-helpful functions for administering you schemas.  This namespace is dedicated
-to integration of Moose based development practices.
-
-=head1 PACKAGE METHODS
-
-The following is a list of package methods declared with this class.
-
-=head2 load_namespaces
-
-Automatically load the classes and resultsets from their default namespaces.
-
-=cut
-
-__PACKAGE__->load_namespaces(
-    default_resultset_class => 'ResultSet',
-);
-
-
-=head1 ATTRIBUTES
-
-This class defines the following attributes.
-
-=head1 METHODS
-
-This module declares the following methods
-
-=head2 new
-
-overload new to make sure we get a good meta object and that the attributes all
-get properly setup.  This is done so that our instances properly get a L<Moose>
-meta class.
-
-=cut
-
-sub new
-{
-    my $class = shift @_;
-    my $obj = $class->SUPER::new(@_);
-  
-    return $class->meta->new_object(
-        __INSTANCE__ => $obj, @_
-    );
-}
-
-
-=head2 connect_and_setup
-
-Creates a schema, deploys a database and sets the testing data.
-
-=cut
-
-sub connect_and_setup {
-    my $class = shift @_;
-    my $db_file = shift @_;
-    
-    my ($dsn, $user, $pass) = (
-      $ENV{DBICNG_DSN} || "dbi:SQLite:${db_file}",
-      $ENV{DBICNG_USER} || '',
-      $ENV{DBICNG_PASS} || '',
-    );
-    
-    return $class
-        ->connect($dsn, $user, $pass, { AutoCommit => 1 })
-        ->setup;
-}
-
-
-=head2 setup
-
-deploy a database and populate it with the initial data
-
-=cut
-
-sub setup {
-    my $self = shift @_;
-    $self->deploy();
-    $self->initial_populate(@_);
-    
-    return $self;
-}
-
-
-=head2 initial_populate
-
-initializing the startup database information
-
-=cut
-
-sub initial_populate {
-    my $self = shift @_;
-    
-    my @genders = $self->populate('Gender' => [
-        [qw(gender_id label)],
-        [qw(1 female)],
-        [qw(2 male)],
-        [qw(3 transgender)],
-    ]);
-  
-    my @persons = $self->populate('Person' => [
-        [ qw(person_id fk_gender_id name age) ],
-        [ qw(1 1 john 25) ],
-        [ qw(2 1 dan 35) ],
-        [ qw(3 2 mary 15) ],
-        [ qw(4 2 jane 95) ],
-        [ qw(5 3 steve 40) ], 
-    ]);
-    
-    my @friends = $self->populate('FriendList' => [
-        [ qw(fk_person_id fk_friend_id) ],
-        [ qw(1 2) ],
-        [ qw(1 3) ],   
-        [ qw(2 3) ], 
-        [ qw(3 2) ],             
-    ]);
-}
-
-
-=head2 job_handler_echo
-
-This is a method to test the job handler role.
-
-=cut
-
-sub job_handler_echo {
-       my ($schema, $job, $alert) = @_;
-       return $alert;
-}
-
-
-=head1 AUTHORS
-
-See L<DBIx::Class> for more information regarding authors.
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
-
-
-1;
diff --git a/t/lib/DBICNGTest/Schema/Result.pm b/t/lib/DBICNGTest/Schema/Result.pm
deleted file mode 100644 (file)
index 9d13c4a..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-package # hide from PAUSE
- DBICNGTest::Schema::Result;
-    use Moose;
-    extends 'DBIx::Class', 'Moose::Object';
-       
-=head1 NAME
-
-DBICNGTest::Schema::Result; Base Class for result and class objects
-
-=head1 SYNOPSIS
-
-    package DBICNGTest::Schema::Result::Member;
-    
-    use Moose;
-    extends 'DBICNGTest::Schema::Result';
-    
-    ## Rest of the class definition.
-
-=head1 DESCRIPTION
-
-Defines the base case for loading DBIC Schemas.  We add in some additional
-helpful functions for administering you schemas.  This namespace is dedicated
-to integration of Moose based development practices
-
-=head1 PACKAGE METHODS
-
-The following is a list of package methods declared with this class.
-
-=head2 load_components
-
-Components to preload.
-
-=cut
-
-__PACKAGE__->load_components(qw/ 
-    PK::Auto 
-    InflateColumn::DateTime
-    Core 
-/);
-
-
-=head1 ATTRIBUTES
-
-This class defines the following attributes.
-
-=head1 METHODS
-
-This module declares the following methods
-
-=head2 new
-
-overload new to make sure we get a good meta object and that the attributes all
-get properly setup.  This is done so that our instances properly get a L<Moose>
-meta class.
-
-=cut
-
-sub new
-{
-    my $class = shift @_;
-    my $attrs = shift @_;
-  
-    my $obj = $class->SUPER::new($attrs);
-
-    return $class->meta->new_object(
-        __INSTANCE__ => $obj, %$attrs
-    );
-}
-
-
-=head1 AUTHORS
-
-See L<DBIx::Class> for more information regarding authors.
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
-
-
-1;
\ No newline at end of file
diff --git a/t/lib/DBICNGTest/Schema/Result/FriendList.pm b/t/lib/DBICNGTest/Schema/Result/FriendList.pm
deleted file mode 100644 (file)
index 8c87003..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-package #hide from pause
- DBICNGTest::Schema::Result::FriendList;
-
-    use Moose;
-    extends 'DBICNGTest::Schema::Result';
-
-
-=head1 NAME
-
-Zoomwit::tlib::DBIC::Schema::Result::FriendList; An example Friends Class;
-
-=head1 VERSION
-
-0.01
-
-=cut
-
-our $VERSION = '0.01';
-
-
-=head1 DESCRIPTION
-
-A Person can have zero or more friends
-A Person can't be their own friend
-A Person over 18 can't be friends with Persons under 18 and vis versa.
-A Person can have friendships that are not mutual.
-
-=head1 ATTRIBUTES
-
-This class defines the following attributes.
-
-=head1 PACKAGE METHODS
-
-This module defines the following package methods
-
-=head2 table
-
-Name of the Physical table in the database
-
-=cut
-
-__PACKAGE__
-    ->table('friend_list');
-
-
-=head2 add_columns
-
-Add columns and meta information
-
-=head3 fk_person_id
-
-ID of the person with friends
-
-=head3 fk_friend_id
-
-Who is the friend?
-
-=cut
-
-__PACKAGE__
-    ->add_columns(
-        fk_person_id => {
-            data_type=>'integer',
-        },
-        fk_friend_id => {
-            data_type=>'integer',
-        },
-);
-        
-
-=head2 primary_key
-
-Sets the Primary keys for this table
-
-=cut
-
-__PACKAGE__
-    ->set_primary_key(qw/fk_person_id fk_friend_id/);
-    
-
-=head2 befriender
-
-The person that 'owns' the friendship (list)
-
-=cut
-
-__PACKAGE__
-    ->belongs_to( befriender => 'DBICNGTest::Schema::Result::Person', {
-        'foreign.person_id' => 'self.fk_person_id' });
-
-
-=head2 friendee
-
-The actual friend that befriender is listing
-
-=cut
-
-__PACKAGE__
-    ->belongs_to( friendee => 'DBICNGTest::Schema::Result::Person', { 
-        'foreign.person_id' => 'self.fk_friend_id' });
-
-
-=head1 METHODS
-
-This module defines the following methods.
-
-=head1 AUTHORS
-
-See L<DBIx::Class> for more information regarding authors.
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
-
-
-1;
diff --git a/t/lib/DBICNGTest/Schema/Result/Gender.pm b/t/lib/DBICNGTest/Schema/Result/Gender.pm
deleted file mode 100644 (file)
index a47e5dd..0000000
+++ /dev/null
@@ -1,117 +0,0 @@
-package #hide from pause
- DBICNGTest::Schema::Result::Gender;
-
-    use Moose;
-    extends 'DBICNGTest::Schema::Result';
-
-
-=head1 NAME
-
-DBICNGTest::Schema::Result::Gender; An example Gender Class;
-
-=head1 DESCRIPTION
-
-Tests for this type of FK relationship
-
-=head1 ATTRIBUTES
-
-This class defines the following attributes.
-
-=head2 label
-
-example of using an attribute to add constraints on a table insert
-
-=cut
-
-has 'label' =>(is=>'rw', required=>1, isa=>'Str');
-
-
-=head1 PACKAGE METHODS
-
-This module defines the following package methods
-
-=head2 table
-
-Name of the Physical table in the database
-
-=cut
-
-__PACKAGE__
-    ->table('gender');
-
-
-=head2 add_columns
-
-Add columns and meta information
-
-=head3 gender_id
-
-Primary Key which is an auto generated UUID
-
-=head3 label
-
-Text label of the gender (ie, 'male', 'female', 'transgender', etc.).
-
-=cut
-
-__PACKAGE__
-    ->add_columns(
-        gender_id => {
-            data_type=>'integer',
-        },
-        label => {
-            data_type=>'varchar',
-            size=>12,
-        },
-    );
-
-
-=head2 primary_key
-
-Sets the Primary keys for this table
-
-=cut
-
-__PACKAGE__
-    ->set_primary_key(qw/gender_id/);
-    
-    
-=head2 
-
-Marks the unique columns
-
-=cut
-
-__PACKAGE__
-    ->add_unique_constraint('gender_label_unique' => [ qw/label/ ]);
-
-
-=head2 people
-
-A resultset of people with this gender
-
-=cut
-
-__PACKAGE__
-    ->has_many(
-        people => 'DBICNGTest::Schema::Result::Person', 
-        {'foreign.fk_gender_id' => 'self.gender_id'}
-    );
-
-
-=head1 METHODS
-
-This module defines the following methods.
-
-=head1 AUTHORS
-
-See L<DBIx::Class> for more information regarding authors.
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
-
-
-1;
diff --git a/t/lib/DBICNGTest/Schema/Result/Person.pm b/t/lib/DBICNGTest/Schema/Result/Person.pm
deleted file mode 100644 (file)
index 9547cc4..0000000
+++ /dev/null
@@ -1,178 +0,0 @@
-package #hide from pause
- DBICNGTest::Schema::Result::Person;
-
-    use Moose;
-    use DateTime;
-    extends 'DBICNGTest::Schema::Result';
-
-
-=head1 NAME
-
-DBICNGTest::Schema::Result::Person; An example Person Class;
-
-=head1 DESCRIPTION
-
-Tests for this type of FK relationship
-
-=head1 ATTRIBUTES
-
-This class defines the following attributes.
-
-=head2 created
-
-attribute for the created column
-
-=cut
-
-has 'created' => (
-    is=>'ro',
-    isa=>'DateTime',
-    required=>1,
-    default=>sub {
-       DateTime->now;
-    },
-);
-
-
-=head1 PACKAGE METHODS
-
-This module defines the following package methods
-
-=head2 table
-
-Name of the Physical table in the database
-
-=cut
-
-__PACKAGE__
-    ->table('person');
-
-
-=head2 add_columns
-
-Add columns and meta information
-
-=head3 person_id
-
-Primary Key which is an auto generated autoinc
-
-=head3 fk_gender_id
-
-foreign key to the Gender table
-
-=head3 name
-
-Just an ordinary name
-
-=head3 age
-
-The person's age
-
-head3 created
-
-When the person was added to the database
-
-=cut
-
-__PACKAGE__
-    ->add_columns(
-        person_id => {
-            data_type=>'integer',
-        },
-        fk_gender_id => {
-            data_type=>'integer',
-        },      
-        name => {
-            data_type=>'varchar',
-            size=>32,
-        },
-        age => {
-            data_type=>'integer',
-            default_value=>25,
-        },
-        created => {
-            data_type=>'datetime',
-            default_value=>'date("now")',
-        });
-
-
-=head2 primary_key
-
-Sets the Primary keys for this table
-
-=cut
-
-__PACKAGE__
-    ->set_primary_key(qw/person_id/);
-
-
-=head2 friendlist
-
-Each Person might have a resultset of friendlist 
-
-=cut
-
-__PACKAGE__
-    ->has_many( 
-        friendlist => 'DBICNGTest::Schema::Result::FriendList',
-        {'foreign.fk_person_id' => 'self.person_id'});
-    
-
-=head2 gender
-
-This person's gender
-
-=cut
-
-__PACKAGE__
-    ->belongs_to( gender => 'DBICNGTest::Schema::Result::Gender', { 
-        'foreign.gender_id' => 'self.fk_gender_id' });
-        
-
-=head2 fanlist
-
-A resultset of the people listing me as a friend (if any)
-
-=cut
-
-__PACKAGE__
-    ->belongs_to( fanlist => 'DBICNGTest::Schema::Result::FriendList', { 
-        'foreign.fk_friend_id' => 'self.person_id' });
-
-
-=head2 friends
-
-A resultset of Persons who are in my FriendList
-
-=cut
-
-__PACKAGE__
-    ->many_to_many( friends => 'friendlist', 'friend' );
-    
-
-=head2 fans
-
-A resultset of people that have me in their friendlist
-
-=cut
-
-__PACKAGE__
-    ->many_to_many( fans => 'fanlist', 'befriender' );
-
-
-=head1 METHODS
-
-This module defines the following methods.
-
-=head1 AUTHORS
-
-See L<DBIx::Class> for more information regarding authors.
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
-
-
-1;
diff --git a/t/lib/DBICNGTest/Schema/ResultSet.pm b/t/lib/DBICNGTest/Schema/ResultSet.pm
deleted file mode 100644 (file)
index 7bb83c6..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-package # hide from PAUSE
- DBICNGTest::Schema::ResultSet;
-    use Moose;
-    extends 'DBIx::Class::ResultSet', 'Moose::Object';
-       
-=head1 NAME
-
-DBICNGTest::Schema::ResultSet; A base ResultSet Class
-
-=head1 SYNOPSIS
-
-    package DBICNGTest::Schema::ResultSet::Member;
-    
-    use Moose;
-    extends 'DBICNGTest::Schema::ResultSet';
-    
-    ## Rest of the class definition.
-
-=head1 DESCRIPTION
-
-All ResultSet classes will inherit from this.  This provides some base function
-for all your resultsets and it is also the default resultset if you don't
-bother to declare a custom resultset in the resultset namespace
-
-=head1 PACKAGE METHODS
-
-The following is a list of package methods declared with this class.
-
-=head1 ATTRIBUTES
-
-This class defines the following attributes.
-
-=head1 METHODS
-
-This module declares the following methods
-
-=head2 new
-
-overload new to make sure we get a good meta object and that the attributes all
-get properly setup.  This is done so that our instances properly get a L<Moose>
-meta class.
-
-=cut
-
-sub new
-{
-    my $class = shift @_;
-    my $obj = $class->SUPER::new(@_);
-  
-    return $class->meta->new_object(
-        __INSTANCE__ => $obj, @_
-    );
-}
-
-
-=head1 AUTHORS
-
-See L<DBIx::Class> for more information regarding authors.
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
-
-
-1;
\ No newline at end of file
diff --git a/t/lib/DBICNGTest/Schema/ResultSet/Person.pm b/t/lib/DBICNGTest/Schema/ResultSet/Person.pm
deleted file mode 100644 (file)
index 86b4dbb..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-package # hide from pause
- DBICNGTest::Schema::ResultSet::Person;
-
-       use Moose;
-       extends 'DBICNGTest::Schema::ResultSet';
-
-
-=head1 NAME
-
-DBICNGTest::Schema::ResultSet:Person; Example Resultset
-
-=head1 VERSION
-
-0.01
-
-=cut
-
-our $VERSION = '0.01';
-
-
-=head1 SYNOPSIS
-
-    ##Example Usage
-
-See Tests for more example usage.
-
-=head1 DESCRIPTION
-
-Resultset Methods for the Person Source
-
-=head1 ATTRIBUTES
-
-This class defines the following attributes.
-
-=head2 literal
-
-a literal attribute for testing
-
-=cut
-
-has 'literal' => (is=>'ro', isa=>'Str', required=>1, lazy=>1, default=>'hi');
-
-
-=head2 available_genders
-
-A resultset of the genders people can have.  Keep in mind this get's run once
-only at the first compile, so it's only good for stuff that doesn't change
-between reboots.
-
-=cut
-
-has 'available_genders' => (
-    is=>'ro',
-    isa=>'DBICNGTest::Schema::ResultSet',
-    required=>1,
-    lazy=>1,
-    default=> sub {
-        shift
-            ->result_source
-            ->schema
-            ->resultset('Gender');
-    }
-);
-
-
-=head1 METHODS
-
-This module defines the following methods.
-
-=head2 older_than($int)
-
-Only people over a given age
-
-=cut
-
-sub older_than
-{
-    my ($self, $age) = @_;
-    
-    return $self->search({age=>{'>'=>$age}});
-}
-
-
-=head1 AUTHORS
-
-See L<DBIx::Class> for more information regarding authors.
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
-
-
-1;
old mode 100755 (executable)
new mode 100644 (file)
index e4b9b76..70501bf
@@ -3,6 +3,7 @@ package # hide from PAUSE
 
 use strict;
 use warnings;
+use DBICTest::AuthorCheck;
 use DBICTest::Schema;
 
 =head1 NAME
@@ -74,7 +75,7 @@ sub _database {
     my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
     my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
 
-    my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
+    my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1, %args });
 
     return @connect_info;
 }
diff --git a/t/lib/DBICTest/AuthorCheck.pm b/t/lib/DBICTest/AuthorCheck.pm
new file mode 100644 (file)
index 0000000..4d2a6f6
--- /dev/null
@@ -0,0 +1,106 @@
+package # hide from PAUSE 
+    DBICTest::AuthorCheck;
+
+use strict;
+use warnings;
+
+use Path::Class qw/file dir/;
+
+_check_author_makefile() unless $ENV{DBICTEST_NO_MAKEFILE_VERIFICATION};
+
+# Die if the author did not update his makefile
+#
+# This is pretty heavy handed, so the check is pretty solid:
+#
+# 1) Assume that this particular module is loaded from -I <$root>/t/lib
+# 2) Make sure <$root>/Makefile.PL exists
+# 3) Make sure we can stat() <$root>/Makefile.PL
+#
+# If all of the above is satisfied
+#
+# *) die if <$root>/inc does not exist
+# *) die if no stat() results for <$root>/Makefile (covers no Makefile)
+# *) die if Makefile.PL mtime > Makefile mtime
+#
+sub _check_author_makefile {
+
+  my $root = _find_co_root()
+    or return;
+
+  # not using file->stat as it invokes File::stat which in turn breaks stat(_)
+  my ($mf_pl_mtime, $mf_mtime) = ( map
+    { (stat ($root->file ($_)) )[9] }
+    qw/Makefile.PL Makefile/
+  );
+
+  return unless $mf_pl_mtime;   # something went wrong during co_root detection ?
+
+  if (
+    not -d $root->subdir ('inc') 
+      or
+    not $mf_mtime
+      or
+    $mf_mtime < $mf_pl_mtime
+  ) {
+    print STDERR <<'EOE';
+
+
+
+
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+======================== FATAL ERROR ===========================
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+We have a number of reasons to believe that this is a development
+checkout and that you, the user, did not run `perl Makefile.PL`
+before using this code. You absolutely _must_ perform this step,
+as not doing so often results in a lot of wasted time for other
+contributors trying to assit you with "it broke!" problems.
+
+If you are seeing this message unexpectedly (i.e. you are in fact
+attempting a regular installation be it through CPAN or manually,
+set the variable DBICTEST_NO_MAKEFILE_VERIFICATION to a true value
+so you can continue. Also _make_absolutely_sure_ to report this to
+either the mailing list or to the irc channel as described in
+
+http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class.pm#GETTING_HELP/SUPPORT
+
+Failure to do this will make us believe that all these checks are
+indeed foolproof and we will remove the ability to override this
+entirely.
+
+The DBIC team
+
+
+
+EOE
+
+    exit 1;
+  }
+}
+
+# Try to determine the root of a checkout/untar if possible
+# or return undef
+sub _find_co_root {
+
+    my @mod_parts = split /::/, (__PACKAGE__ . '.pm');
+    my $rel_path = file (@mod_parts);
+
+    return undef unless ($INC{$rel_path});
+
+    # a bit convoluted, but what we do here essentially is:
+    #  - get the file name of this particular module
+    #  - do 'cd ..' as many times as necessary to get to t/lib/../..
+
+    my $root = dir ($INC{$rel_path});
+    for (0 .. @mod_parts + 1) {
+        $root = $root->parent;
+    }
+
+    return (-f $root->file ('Makefile.PL') )
+      ? $root
+      : undef
+    ;
+}
+
+1;
diff --git a/t/lib/DBICTest/BaseResult.pm b/t/lib/DBICTest/BaseResult.pm
new file mode 100644 (file)
index 0000000..78de2a1
--- /dev/null
@@ -0,0 +1,14 @@
+package #hide from pause
+  DBICTest::BaseResult;
+
+use strict;
+use warnings;
+
+use base qw/DBIx::Class/;
+use DBICTest::BaseResultSet;
+
+__PACKAGE__->load_components (qw/Core/);
+__PACKAGE__->table ('bogus');
+__PACKAGE__->resultset_class ('DBICTest::BaseResultSet');
+
+1;
diff --git a/t/lib/DBICTest/BaseResultSet.pm b/t/lib/DBICTest/BaseResultSet.pm
new file mode 100644 (file)
index 0000000..6d9df85
--- /dev/null
@@ -0,0 +1,13 @@
+package #hide from pause
+  DBICTest::BaseResultSet;
+
+use strict;
+use warnings;
+
+use base qw/DBIx::Class::ResultSet/;
+
+sub hri_dump {
+  return shift->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
+}
+
+1;
index 959b4fc..b9794fe 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::Artist;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('artist');
 __PACKAGE__->source_info({
index c709572..1626787 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::ArtistUndirectedMap;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('artist_undirected_map');
 __PACKAGE__->add_columns(
index 10e07ce..849096b 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::Artwork;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('cd_artwork');
 __PACKAGE__->add_columns(
index 0d832ca..0859080 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::Artwork_to_Artist;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('artwork_to_artist');
 __PACKAGE__->add_columns(
index e92a777..5670f2f 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::BindType;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('bindtype_test');
 
index cac4841..bb32a14 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::Bookmark;
 
-    use base 'DBIx::Class::Core';
+    use base qw/DBICTest::BaseResult/;
 
 
 use strict;
index 6c2d6aa..1f5d7ea 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE \r
     DBICTest::Schema::BooksInLibrary;\r
 \r
-use base qw/DBIx::Class::Core/;\r
+use base qw/DBICTest::BaseResult/;\r
 \r
 __PACKAGE__->table('books');\r
 __PACKAGE__->add_columns(\r
index 5638e24..ec6ab24 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::CD;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('cd');
 __PACKAGE__->add_columns(
@@ -38,7 +38,9 @@ __PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist', undef, {
 });
 
 # in case this is a single-cd it promotes a track from another cd
-__PACKAGE__->belongs_to( single_track => 'DBICTest::Schema::Track' );
+__PACKAGE__->belongs_to( single_track => 'DBICTest::Schema::Track', 'single_track', 
+    { join_type => 'left'} 
+);
 
 __PACKAGE__->has_many( tracks => 'DBICTest::Schema::Track' );
 __PACKAGE__->has_many(
index cf18c4e..f0f14f0 100644 (file)
@@ -1,12 +1,13 @@
 package # hide from PAUSE 
     DBICTest::Schema::CD_to_Producer;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('cd_to_producer');
 __PACKAGE__->add_columns(
   cd => { data_type => 'integer' },
   producer => { data_type => 'integer' },
+  attribute => { data_type => 'integer', is_nullable => 1 },
 );
 __PACKAGE__->set_primary_key(qw/cd producer/);
 
index 1c11dc6..e3df51f 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE \r
     DBICTest::Schema::Collection;\r
 \r
-use base qw/DBIx::Class::Core/;\r
+use base qw/DBICTest::BaseResult/;\r
 \r
 __PACKAGE__->table('collection');\r
 __PACKAGE__->add_columns(\r
index d05ae5d..df43c9c 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE \r
     DBICTest::Schema::CollectionObject;\r
 \r
-use base qw/DBIx::Class::Core/;\r
+use base qw/DBICTest::BaseResult/;\r
 \r
 __PACKAGE__->table('collection_object');\r
 __PACKAGE__->add_columns(\r
index 6bc51d6..2a8396d 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::Dummy;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 use strict;
 use warnings;
index 258cab9..9bf015a 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::Employee;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->load_components(qw( Ordered ));
 
index 9d09f31..7fd77dc 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::Encoded;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 use strict;
 use warnings;
index 0c02568..caecdc1 100644 (file)
@@ -2,7 +2,7 @@ package DBICTest::Schema::Event;
 
 use strict;
 use warnings;
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
 
index d19980c..321b279 100644 (file)
@@ -2,7 +2,7 @@ package DBICTest::Schema::EventTZ;
 
 use strict;
 use warnings;
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
 
@@ -10,8 +10,8 @@ __PACKAGE__->table('event');
 
 __PACKAGE__->add_columns(
   id => { data_type => 'integer', is_auto_increment => 1 },
-  starts_at => { data_type => 'datetime', extra => { timezone => "America/Chicago", locale => 'de_DE' } },
-  created_on => { data_type => 'timestamp', extra => { timezone => "America/Chicago", floating_tz_ok => 1 } },
+  starts_at => { data_type => 'datetime', timezone => "America/Chicago", locale => 'de_DE' },
+  created_on => { data_type => 'timestamp', timezone => "America/Chicago", floating_tz_ok => 1 },
 );
 
 __PACKAGE__->set_primary_key('id');
diff --git a/t/lib/DBICTest/Schema/EventTZDeprecated.pm b/t/lib/DBICTest/Schema/EventTZDeprecated.pm
new file mode 100644 (file)
index 0000000..8a9a11d
--- /dev/null
@@ -0,0 +1,19 @@
+package DBICTest::Schema::EventTZDeprecated;
+
+use strict;
+use warnings;
+use base qw/DBICTest::BaseResult/;
+
+__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
+
+__PACKAGE__->table('event');
+
+__PACKAGE__->add_columns(
+  id => { data_type => 'integer', is_auto_increment => 1 },
+  starts_at => { data_type => 'datetime', extra => { timezone => "America/Chicago", locale => 'de_DE' } },
+  created_on => { data_type => 'timestamp', extra => { timezone => "America/Chicago", floating_tz_ok => 1 } },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;
index cc425ee..82fcebd 100644 (file)
@@ -3,7 +3,7 @@ DBICTest::Schema::FileColumn;
 
 use strict;
 use warnings;
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 use File::Temp qw/tempdir/;
 
 __PACKAGE__->load_components(qw/InflateColumn::File/);
index 82829b8..8e2daeb 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::ForceForeign;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('forceforeign');
 __PACKAGE__->add_columns(
index a1e23db..9966cfb 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::FourKeys;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('fourkeys');
 __PACKAGE__->add_columns(
@@ -9,7 +9,8 @@ __PACKAGE__->add_columns(
   'bar' => { data_type => 'integer' },
   'hello' => { data_type => 'integer' },
   'goodbye' => { data_type => 'integer' },
-  'sensors' => { data_type => 'character' },
+  'sensors' => { data_type => 'character', size => 10 },
+  'read_count' => { data_type => 'integer', is_nullable => 1 },
 );
 __PACKAGE__->set_primary_key(qw/foo bar hello goodbye/);
 
index 6e86313..d95ed6c 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::FourKeys_to_TwoKeys;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('fourkeys_to_twokeys');
 __PACKAGE__->add_columns(
@@ -12,6 +12,7 @@ __PACKAGE__->add_columns(
   't_artist' => { data_type => 'integer' },
   't_cd' => { data_type => 'integer' },
   'autopilot' => { data_type => 'character' },
+  'pilot_sequence' => { data_type => 'integer', is_nullable => 1 },
 );
 __PACKAGE__->set_primary_key(
   qw/f_foo f_bar f_hello f_goodbye t_artist t_cd/
index db2ca9c..3b3675a 100644 (file)
@@ -2,7 +2,7 @@ package DBICTest::Schema::Genre;
 
 use strict;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('genre');
 __PACKAGE__->add_columns(
index 8df5add..16f94a9 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::Image;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('images');
 __PACKAGE__->add_columns(
index 0c82588..5675f52 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::LinerNotes;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('liner_notes');
 __PACKAGE__->add_columns(
index 5343122..bf6d623 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::Link;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 use strict;
 use warnings;
index d2f9769..2a409ab 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::LyricVersion;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('lyric_versions');
 __PACKAGE__->add_columns(
index 3e4024e..268a553 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::Lyrics;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('lyrics');
 __PACKAGE__->add_columns(
index 1edda61..cb79178 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::NoPrimaryKey;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('noprimarykey');
 __PACKAGE__->add_columns(
index 63356ac..bd0e148 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::OneKey;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('onekey');
 __PACKAGE__->add_columns(
index acaf5ed..38aa025 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE \r
     DBICTest::Schema::Owners;\r
 \r
-use base qw/DBIx::Class::Core/;\r
+use base qw/DBICTest::BaseResult/;\r
 \r
 __PACKAGE__->table('owners');\r
 __PACKAGE__->add_columns(\r
index 26ecddb..c2fa611 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::Producer;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('producer');
 __PACKAGE__->add_columns(
index ec715c7..edcfe6c 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE \r
     DBICTest::Schema::SelfRef;\r
 \r
-use base 'DBIx::Class::Core';\r
+use base qw/DBICTest::BaseResult/;\r
 \r
 __PACKAGE__->table('self_ref');\r
 __PACKAGE__->add_columns(\r
index e7ed491..2f7d105 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE \r
     DBICTest::Schema::SelfRefAlias;\r
 \r
-use base 'DBIx::Class::Core';\r
+use base qw/DBICTest::BaseResult/;\r
 \r
 __PACKAGE__->table('self_ref_alias');\r
 __PACKAGE__->add_columns(\r
index bea3f4b..b0fa515 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::SequenceTest;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('sequence_test');
 __PACKAGE__->source_info({
index 687dcd1..92c210f 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::Serialized;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('serialized');
 __PACKAGE__->add_columns(
index b75c2ef..796616e 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::Tag;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('tags');
 __PACKAGE__->add_columns(
index e3f731e..4966800 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::Track;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 __PACKAGE__->load_components(qw/InflateColumn::DateTime Ordered/);
 
 __PACKAGE__->table('track');
@@ -26,6 +26,10 @@ __PACKAGE__->add_columns(
     accessor => 'updated_date',
     is_nullable => 1
   },
+  last_updated_at => {
+    data_type => 'datetime',
+    is_nullable => 1
+  },
 );
 __PACKAGE__->set_primary_key('trackid');
 
index 365571d..a5413d1 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::TreeLike;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('treelike');
 __PACKAGE__->add_columns(
index 89d8e0a..1ee8409 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::TwoKeyTreeLike;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('twokeytreelike');
 __PACKAGE__->add_columns(
index 69af2e6..bfb6c42 100755 (executable)
@@ -1,7 +1,7 @@
 package # hide from PAUSE
     DBICTest::Schema::TwoKeys;
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('twokeys');
 __PACKAGE__->add_columns(
index 6498add..50c5e44 100644 (file)
@@ -1,7 +1,7 @@
 package # hide from PAUSE 
     DBICTest::Schema::TypedObject;
 
-use base qw/DBIx::Class::Core/;
+use base qw/DBICTest::BaseResult/;
 
 __PACKAGE__->table('typed_object');
 __PACKAGE__->add_columns(
index 580ed33..4aea122 100644 (file)
@@ -2,7 +2,7 @@ package # hide from PAUSE
     DBICTest::Schema::Year1999CDs;
 ## Used in 104view.t
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 use DBIx::Class::ResultSource::View;
 
 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
index 5293c69..ebc4395 100644 (file)
@@ -2,7 +2,7 @@ package # hide from PAUSE
     DBICTest::Schema::Year2000CDs;
 ## Used in 104view.t
 
-use base 'DBIx::Class::Core';
+use base qw/DBICTest::BaseResult/;
 use DBIx::Class::ResultSource::View;
 
 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
diff --git a/t/lib/DBICTest/Taint/Classes/Auto.pm b/t/lib/DBICTest/Taint/Classes/Auto.pm
new file mode 100644 (file)
index 0000000..9a30c1a
--- /dev/null
@@ -0,0 +1,7 @@
+package # hide from PAUSE 
+    DBICTest::Taint::Classes::Auto;
+
+use base 'DBIx::Class::Core';
+__PACKAGE__->table('test');
+
+1;
diff --git a/t/lib/DBICTest/Taint/Classes/Manual.pm b/t/lib/DBICTest/Taint/Classes/Manual.pm
new file mode 100644 (file)
index 0000000..5d2109b
--- /dev/null
@@ -0,0 +1,7 @@
+package # hide from PAUSE 
+    DBICTest::Taint::Classes::Manual;
+
+use base 'DBIx::Class::Core';
+__PACKAGE__->table('test');
+
+1;
diff --git a/t/lib/DBICTest/Taint/Namespaces/Result/Test.pm b/t/lib/DBICTest/Taint/Namespaces/Result/Test.pm
new file mode 100644 (file)
index 0000000..7d57bb5
--- /dev/null
@@ -0,0 +1,7 @@
+package # hide from PAUSE 
+    DBICTest::Taint::Namespaces::Result::Test;
+
+use base 'DBIx::Class::Core';
+__PACKAGE__->table('test');
+
+1;
index 105a490..ec68a99 100644 (file)
@@ -1,6 +1,6 @@
 -- 
 -- Created by SQL::Translator::Producer::SQLite
--- Created on Sun Feb 22 00:15:06 2009
+-- Created on Sat May 23 21:30:53 2009
 -- 
 
 
@@ -108,6 +108,7 @@ CREATE UNIQUE INDEX cd_artist_title_cd ON cd (artist, title);
 CREATE TABLE cd_to_producer (
   cd integer NOT NULL,
   producer integer NOT NULL,
+  attribute integer,
   PRIMARY KEY (cd, producer)
 );
 
@@ -194,7 +195,8 @@ CREATE TABLE fourkeys (
   bar integer NOT NULL,
   hello integer NOT NULL,
   goodbye integer NOT NULL,
-  sensors character NOT NULL,
+  sensors character(10) NOT NULL,
+  read_count integer,
   PRIMARY KEY (foo, bar, hello, goodbye)
 );
 
@@ -209,6 +211,7 @@ CREATE TABLE fourkeys_to_twokeys (
   t_artist integer NOT NULL,
   t_cd integer NOT NULL,
   autopilot character NOT NULL,
+  pilot_sequence integer,
   PRIMARY KEY (f_foo, f_bar, f_hello, f_goodbye, t_artist, t_cd)
 );
 
@@ -375,7 +378,8 @@ CREATE TABLE track (
   cd integer NOT NULL,
   position integer NOT NULL,
   title varchar(100) NOT NULL,
-  last_updated_on datetime
+  last_updated_on datetime,
+  last_updated_at datetime
 );
 
 CREATE INDEX track_idx_cd_track ON track (cd);
diff --git a/t/prefetch/attrs_untouched.t b/t/prefetch/attrs_untouched.t
new file mode 100644 (file)
index 0000000..53894b8
--- /dev/null
@@ -0,0 +1,34 @@
+use warnings;  
+
+use Test::More;
+use Test::Exception;
+use lib qw(t/lib);
+use DBICTest;
+use Data::Dumper;
+
+my $schema = DBICTest->init_schema();
+
+my $orig_debug = $schema->storage->debug;
+
+use IO::File;
+
+BEGIN {
+    eval "use DBD::SQLite";
+    plan $@
+        ? ( skip_all => 'needs DBD::SQLite for testing' )
+        : ( tests => 3 );
+}
+
+# bug in 0.07000 caused attr (join/prefetch) to be modifed by search
+# so we check the search & attr arrays are not modified
+my $search = { 'artist.name' => 'Caterwauler McCrae' };
+my $attr = { prefetch => [ qw/artist liner_notes/ ],
+             order_by => 'me.cdid' };
+my $search_str = Dumper($search);
+my $attr_str = Dumper($attr);
+
+my $rs = $schema->resultset("CD")->search($search, $attr);
+
+is(Dumper($search), $search_str, 'Search hash untouched after search()');
+is(Dumper($attr), $attr_str, 'Attribute hash untouched after search()');
+cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
diff --git a/t/prefetch/diamond.t b/t/prefetch/diamond.t
new file mode 100644 (file)
index 0000000..fe30ac8
--- /dev/null
@@ -0,0 +1,107 @@
+# Test if prefetch and join in diamond relationship fetching the correct rows
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my $schema = DBICTest->init_schema();
+
+$schema->populate('Artwork', [
+    [ qw/cd_id/ ],
+    [ 1 ],
+]);
+
+$schema->populate('Artwork_to_Artist', [
+    [ qw/artwork_cd_id artist_id/ ],
+    [ 1, 2 ],
+]);
+
+my $ars = $schema->resultset ('Artwork');
+
+# The relationship diagram here is:
+#
+#  $ars --> artwork_to_artist
+#   |              |
+#   |              |
+#   V              V
+#   cd  ------>  artist
+#
+# The current artwork belongs to a cd by artist1
+# but the artwork itself is painted by artist2
+#
+# What we try is all possible permutations of join/prefetch 
+# combinations in both directions, while always expecting to
+# arrive at the specific artist at the end of each path.
+
+
+my $cd_paths = {
+  'no cd' => [],
+  'cd' => ['cd'],
+  'cd->artist1' => [{'cd' => 'artist'}]
+};
+my $a2a_paths = {
+  'no a2a' => [],
+  'a2a' => ['artwork_to_artist'],
+  'a2a->artist2' => [{'artwork_to_artist' => 'artist'}]
+};
+
+my %tests;
+
+foreach my $cd_path (keys %$cd_paths) {
+
+  foreach my $a2a_path (keys %$a2a_paths) {
+
+
+    $tests{sprintf "join %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
+      'join' => [
+        @{ $cd_paths->{$cd_path} },
+        @{ $a2a_paths->{$a2a_path} },
+      ],
+      'prefetch' => [
+      ],
+    });
+
+
+    $tests{sprintf "prefetch %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
+      'join' => [
+      ],
+      'prefetch' => [
+        @{ $cd_paths->{$cd_path} },
+        @{ $a2a_paths->{$a2a_path} },
+      ],
+    });
+
+
+    $tests{sprintf "join %s, prefetch %s", $cd_path, $a2a_path} = $ars->search({}, {
+      'join' => [
+        @{ $cd_paths->{$cd_path} },
+      ],
+      'prefetch' => [
+        @{ $a2a_paths->{$a2a_path} },
+      ],
+    });
+
+
+    $tests{sprintf "join %s, prefetch %s", $a2a_path, $cd_path} = $ars->search({}, {
+      'join' => [
+        @{ $a2a_paths->{$a2a_path} },
+      ],
+      'prefetch' => [
+        @{ $cd_paths->{$cd_path} },
+      ],
+    });
+
+  }
+}
+
+plan tests => (scalar (keys %tests) * 3);
+
+foreach my $name (keys %tests) {
+  foreach my $artwork ($tests{$name}->all()) {
+    is($artwork->id, 1, $name . ', correct artwork');
+    is($artwork->cd->artist->artistid, 1, $name . ', correct artist_id over cd');
+    is($artwork->artwork_to_artist->first->artist->artistid, 2, $name . ', correct artist_id over A2A');
+  }
+}
\ No newline at end of file
diff --git a/t/prefetch/multiple_hasmany.t b/t/prefetch/multiple_hasmany.t
new file mode 100644 (file)
index 0000000..7fc9381
--- /dev/null
@@ -0,0 +1,160 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use Test::Exception;
+use lib qw(t/lib);
+use DBICTest;
+use Data::Dumper;
+
+my $schema = DBICTest->init_schema();
+
+my $orig_debug = $schema->storage->debug;
+
+use IO::File;
+
+BEGIN {
+    eval "use DBD::SQLite";
+    plan $@
+        ? ( skip_all => 'needs DBD::SQLite for testing' )
+        : ( tests => 16 );
+}
+
+# once the following TODO is complete, remove the 2 warning tests immediately
+# after the TODO block
+# (the TODO block itself contains tests ensuring that the warns are removed)
+TODO: {
+    local $TODO = 'Prefetch of multiple has_many rels at the same level (currently warn to protect the clueless git)';
+
+    #( 1 -> M + M )
+    my $cd_rs = $schema->resultset('CD')->search ({ 'me.title' => 'Forkful of bees' });
+    my $pr_cd_rs = $cd_rs->search ({}, {
+        prefetch => [qw/tracks tags/],
+    });
+
+    my $tracks_rs = $cd_rs->first->tracks;
+    my $tracks_count = $tracks_rs->count;
+
+    my ($pr_tracks_rs, $pr_tracks_count);
+
+    my $queries = 0;
+    $schema->storage->debugcb(sub { $queries++ });
+    $schema->storage->debug(1);
+
+    my $o_mm_warn;
+    {
+        local $SIG{__WARN__} = sub { $o_mm_warn = shift };
+        $pr_tracks_rs = $pr_cd_rs->first->tracks;
+    };
+    $pr_tracks_count = $pr_tracks_rs->count;
+
+    ok(! $o_mm_warn, 'no warning on attempt to prefetch several same level has_many\'s (1 -> M + M)');
+
+    is($queries, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query');
+    is($pr_tracks_count, $tracks_count, 'equal count of prefetched relations over several same level has_many\'s (1 -> M + M)');
+
+    for ($pr_tracks_rs, $tracks_rs) {
+        $_->result_class ('DBIx::Class::ResultClass::HashRefInflator');
+    }
+
+    is_deeply ([$pr_tracks_rs->all], [$tracks_rs->all], 'same structure returned with and without prefetch over several same level has_many\'s (1 -> M + M)');
+
+    #( M -> 1 -> M + M )
+    my $note_rs = $schema->resultset('LinerNotes')->search ({ notes => 'Buy Whiskey!' });
+    my $pr_note_rs = $note_rs->search ({}, {
+        prefetch => {
+            cd => [qw/tags tracks/]
+        },
+    });
+
+    my $tags_rs = $note_rs->first->cd->tags;
+    my $tags_count = $tags_rs->count;
+
+    my ($pr_tags_rs, $pr_tags_count);
+
+    $queries = 0;
+    $schema->storage->debugcb(sub { $queries++ });
+    $schema->storage->debug(1);
+
+    my $m_o_mm_warn;
+    {
+        local $SIG{__WARN__} = sub { $m_o_mm_warn = shift };
+        $pr_tags_rs = $pr_note_rs->first->cd->tags;
+    };
+    $pr_tags_count = $pr_tags_rs->count;
+
+    ok(! $m_o_mm_warn, 'no warning on attempt to prefetch several same level has_many\'s (M -> 1 -> M + M)');
+
+    is($queries, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query');
+
+    is($pr_tags_count, $tags_count, 'equal count of prefetched relations over several same level has_many\'s (M -> 1 -> M + M)');
+
+    for ($pr_tags_rs, $tags_rs) {
+        $_->result_class ('DBIx::Class::ResultClass::HashRefInflator');
+    }
+
+    is_deeply ([$pr_tags_rs->all], [$tags_rs->all], 'same structure returned with and without prefetch over several same level has_many\'s (M -> 1 -> M + M)');
+}
+
+# remove this closure once the TODO above is working
+my $w;
+{
+    local $SIG{__WARN__} = sub { $w = shift };
+
+    my $rs = $schema->resultset('CD')->search ({ 'me.title' => 'Forkful of bees' }, { prefetch => [qw/tracks tags/] });
+    for (qw/all count next first/) {
+        undef $w;
+        my @stuff = $rs->search()->$_;
+        like ($w, qr/will currently disrupt both the functionality of .rs->count\(\), and the amount of objects retrievable via .rs->next\(\)/,
+            "warning on ->$_ attempt prefetching several same level has_manys (1 -> M + M)");
+    }
+    my $rs2 = $schema->resultset('LinerNotes')->search ({ notes => 'Buy Whiskey!' }, { prefetch => { cd => [qw/tags tracks/] } });
+    for (qw/all count next first/) {
+        undef $w;
+        my @stuff = $rs2->search()->$_;
+        like ($w, qr/will currently disrupt both the functionality of .rs->count\(\), and the amount of objects retrievable via .rs->next\(\)/,
+            "warning on ->$_ attempt prefetching several same level has_manys (M -> 1 -> M + M)");
+    }
+}
+
+__END__
+The solution is to rewrite ResultSet->_collapse_result() and
+ResultSource->resolve_prefetch() to focus on the final results from the collapse
+of the data. Right now, the code doesn't treat the columns from the various
+tables as grouped entities. While there is a concept of hierarchy (so that
+prefetching down relationships does work as expected), there is no idea of what
+the final product should look like and how the various columns in the row would
+play together. So, the actual prefetch datastructure from the search would be
+very useful in working through this problem. We already have access to the PKs
+and sundry for those. So, when collapsing the search result, we know we are
+looking for 1 cd object. We also know we're looking for tracks and tags records
+-independently- of each other. So, we can grab the data for tracks and data for
+tags separately, uniqueing on the PK as appropriate. Then, when we're done with
+the given cd object's datastream, we know we're good. This should work for all
+the various scenarios.
+
+My reccommendation is the row's data is preprocessed first, breaking it up into
+the data for each of the component tables. (This could be done in the single
+table case, too, but probably isn't necessary.) So, starting with something
+like:
+  my $row = {
+    t1.col1 => 1,
+    t1.col2 => 2,
+    t2.col1 => 3,
+    t2.col2 => 4,
+    t3.col1 => 5,
+    t3.col2 => 6,
+  };
+it is massaged to look something like:
+  my $row_massaged = {
+    t1 => { col1 => 1, col2 => 2 },
+    t2 => { col1 => 3, col2 => 4 },
+    t3 => { col1 => 5, col2 => 6 },
+  };
+At this point, find the stuff that's different is easy enough to do and slotting
+things into the right spot is, likewise, pretty straightforward. Instead of
+storing things in a AoH, store them in a HoH keyed on the PKs of the the table,
+then convert to an AoH after all collapsing is done.
+
+This implies that the collapse attribute can probably disappear or, at the
+least, be turned into a boolean (which is how it's used in every other place).
diff --git a/t/prefetch/pollute_already_joined.t b/t/prefetch/pollute_already_joined.t
new file mode 100644 (file)
index 0000000..152ca56
--- /dev/null
@@ -0,0 +1,63 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use Test::Exception;
+use lib qw(t/lib);
+use DBICTest;
+use Data::Dumper;
+
+my $schema = DBICTest->init_schema();
+
+my $orig_debug = $schema->storage->debug;
+
+use IO::File;
+
+BEGIN {
+    eval "use DBD::SQLite";
+    plan $@
+        ? ( skip_all => 'needs DBD::SQLite for testing' )
+        : ( tests => 10 );
+}
+
+# A search() with prefetch seems to pollute an already joined resultset
+# in a way that offsets future joins (adapted from a test case by Debolaz)
+{
+  my ($cd_rs, $attrs);
+
+  # test a real-life case - rs is obtained by an implicit m2m join
+  $cd_rs = $schema->resultset ('Producer')->first->cds;
+  $attrs = Dumper $cd_rs->{attrs};
+
+  $cd_rs->search ({})->all;
+  is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
+
+  lives_ok (sub {
+    $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
+    is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
+  }, 'first prefetching search ok');
+
+  lives_ok (sub {
+    $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
+    is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
+  }, 'second prefetching search ok');
+
+
+  # test a regular rs with an empty seen_join injected - it should still work!
+  $cd_rs = $schema->resultset ('CD');
+  $cd_rs->{attrs}{seen_join}  = {};
+  $attrs = Dumper $cd_rs->{attrs};
+
+  $cd_rs->search ({})->all;
+  is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
+
+  lives_ok (sub {
+    $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
+    is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
+  }, 'first prefetching search ok');
+
+  lives_ok (sub {
+    $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
+    is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
+  }, 'second prefetching search ok');
+}
diff --git a/t/prefetch/rows_bug.t b/t/prefetch/rows_bug.t
new file mode 100644 (file)
index 0000000..1457013
--- /dev/null
@@ -0,0 +1,83 @@
+# Test to ensure we get a consistent result set wether or not we use the
+# prefetch option in combination rows (LIMIT).
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 2);
+
+my $schema = DBICTest->init_schema();
+my $no_prefetch = $schema->resultset('Artist')->search(
+  undef,
+  { rows => 3 }
+);
+
+my $use_prefetch = $schema->resultset('Artist')->search(
+  undef,
+  {
+    prefetch => 'cds',
+    rows     => 3
+  }
+);
+
+my $no_prefetch_count  = 0;
+my $use_prefetch_count = 0;
+
+is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
+
+TODO: {
+  local $TODO = "This is a difficult bug to fix, workaround is not to use prefetch with rows";
+  $no_prefetch_count++  while $no_prefetch->next;
+  $use_prefetch_count++ while $use_prefetch->next;
+  is(
+    $no_prefetch_count,
+    $use_prefetch_count,
+    "manual row count confirms consistency"
+    . " (\$no_prefetch_count == $no_prefetch_count, "
+    . " \$use_prefetch_count == $use_prefetch_count)"
+  );
+}
+
+__END__
+The fix is to, when using prefetch, take the query and put it into a subquery
+joined to the tables we're prefetching from. This might result in the same
+table being joined once in the main subquery and once in the main query. This
+may actually resolve other, unknown edgecase bugs. It is also the right way
+to do prefetching. Optimizations can come later.
+
+This means that:
+  $foo_rs->search(
+    { ... },
+    {
+      prefetch => 'bar',
+      ...
+    },
+  );
+
+becomes:
+  my $temp = $foo_rs->search(
+    { ... },
+    {
+      join => 'bar',
+      ...
+    },
+  );
+  $foo_rs->storage->schema->resultset('foo')->search(
+    undef,
+    {
+      from => [
+        { me => $temp->as_query },
+      ],
+      prefetch => 'bar',
+    },
+  );
+
+Problem:
+  * The prefetch->join change needs to happen ONLY IF there are conditions
+    that depend on bar being joined.
+  * How will this work when the $rs is further searched on? Those clauses
+    need to be added to the subquery, not the outer one. This is particularly
+    true if rows is added in the attribute later per the Pager.
diff --git a/t/prefetch/standard.t b/t/prefetch/standard.t
new file mode 100644 (file)
index 0000000..56cf77d
--- /dev/null
@@ -0,0 +1,316 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use Test::Exception;
+use lib qw(t/lib);
+use DBICTest;
+use Data::Dumper;
+
+my $schema = DBICTest->init_schema();
+
+my $orig_debug = $schema->storage->debug;
+
+use IO::File;
+
+BEGIN {
+    eval "use DBD::SQLite";
+    plan $@
+        ? ( skip_all => 'needs DBD::SQLite for testing' )
+        : ( tests => 45 );
+}
+
+my $queries = 0;
+$schema->storage->debugcb(sub { $queries++; });
+$schema->storage->debug(1);
+
+my $search = { 'artist.name' => 'Caterwauler McCrae' };
+my $attr = { prefetch => [ qw/artist liner_notes/ ],
+             order_by => 'me.cdid' };
+my $search_str = Dumper($search);
+my $attr_str = Dumper($attr);
+
+my $rs = $schema->resultset("CD")->search($search, $attr);
+my @cd = $rs->all;
+
+is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
+
+ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
+
+is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
+
+is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
+
+is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
+
+is($queries, 1, 'prefetch ran only 1 select statement');
+
+$schema->storage->debug($orig_debug);
+$schema->storage->debugobj->callback(undef);
+
+# test for partial prefetch via columns attr
+my $cd = $schema->resultset('CD')->find(1,
+    {
+      columns => [qw/title artist artist.name/], 
+      join => { 'artist' => {} }
+    }
+);
+ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
+
+# start test for nested prefetch SELECT count
+$queries = 0;
+$schema->storage->debugcb(sub { $queries++ });
+$schema->storage->debug(1);
+
+$rs = $schema->resultset('Tag')->search(
+  {},
+  {
+    prefetch => { cd => 'artist' }
+  }
+);
+
+my $tag = $rs->first;
+
+is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
+
+is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
+
+# count the SELECTs
+#$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
+is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
+
+$queries = 0;
+
+is($tag->search_related('cd')->search_related('artist')->first->name,
+   'Caterwauler McCrae',
+   'chained belongs_to->belongs_to search_related ok');
+
+is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
+
+$queries = 0;
+
+$cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
+
+is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
+
+is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
+
+$queries = 0;
+
+$schema->storage->debugcb(sub { $queries++; });
+
+$cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
+
+is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
+
+is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
+
+$queries = 0;
+
+my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
+
+is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
+
+is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
+
+$schema->storage->debug($orig_debug);
+$schema->storage->debugobj->callback(undef);
+
+$rs = $schema->resultset('Tag')->search(
+  {},
+  {
+    join => { cd => 'artist' },
+    prefetch => { cd => 'artist' }
+  }
+);
+
+cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
+
+my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
+                 { order_by => 'artistid DESC', join => 'cds' });
+
+is($artist->name, 'Random Boy Band', "Join search by object ok");
+
+my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
+                               { join => 'liner_notes' });
+
+cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
+
+is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
+
+my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
+                                       { join => { 'cds' => 'tags' } });
+
+cmp_ok( @artists, '==', 2, "two-join search ok" );
+
+$rs = $schema->resultset("CD")->search(
+  {},
+  { group_by => [qw/ title me.cdid /] }
+);
+
+cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
+
+cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
+
+$rs = $schema->resultset("CD")->search(
+  {},
+  { join => [qw/ artist /], group_by => [qw/ artist.name /] }
+);
+
+cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
+
+$rs = $schema->resultset("Artist")->search(
+  {},
+      { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
+);
+
+cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
+
+$rs = $rs->search( undef, {  having =>{ 'count(*)'=> \'> 2' }});
+
+cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
+
+$rs = $schema->resultset("Artist")->search(
+        { 'cds.title' => 'Spoonful of bees',
+          'cds_2.title' => 'Forkful of bees' },
+        { join => [ 'cds', 'cds' ] });
+
+cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
+
+is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
+
+$cd = $schema->resultset('Artist')->first->create_related('cds',
+    {
+    title   => 'Unproduced Single',
+    year    => 2007
+});
+
+my $left_join = $schema->resultset('CD')->search(
+    { 'me.cdid' => $cd->cdid },
+    { prefetch => { cd_to_producer => 'producer' } }
+);
+
+cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
+
+$queries = 0;
+$schema->storage->debugcb(sub { $queries++ });
+$schema->storage->debug(1);
+
+my $tree_like =
+     $schema->resultset('TreeLike')->find(5,
+       { join     => { parent => { parent => 'parent' } },
+         prefetch => { parent => { parent => 'parent' } } });
+
+is($tree_like->name, 'quux', 'Bottom of tree ok');
+$tree_like = $tree_like->parent;
+is($tree_like->name, 'baz', 'First level up ok');
+$tree_like = $tree_like->parent;
+is($tree_like->name, 'bar', 'Second level up ok');
+$tree_like = $tree_like->parent;
+is($tree_like->name, 'foo', 'Third level up ok');
+
+$schema->storage->debug($orig_debug);
+$schema->storage->debugobj->callback(undef);
+
+cmp_ok($queries, '==', 1, 'Only one query run');
+
+$tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
+$tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
+is($tree_like->name, 'quux', 'Tree search_related ok');
+
+$tree_like = $schema->resultset('TreeLike')->search_related('children',
+    { 'children.id' => 3, 'children_2.id' => 4 },
+    { prefetch => { children => 'children' } }
+  )->first;
+is(eval { $tree_like->children->first->children->first->name }, 'quux',
+   'Tree search_related with prefetch ok');
+
+$tree_like = eval { $schema->resultset('TreeLike')->search(
+    { 'children.id' => 3, 'children_2.id' => 6 }, 
+    { join => [qw/children children/] }
+  )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
+  )->first->children->first; };
+is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
+
+# test that collapsed joins don't get a _2 appended to the alias
+
+my $sql = '';
+$schema->storage->debugcb(sub { $sql = $_[1] });
+$schema->storage->debug(1);
+
+eval {
+  my $row = $schema->resultset('Artist')->search_related('cds', undef, {
+    join => 'tracks',
+    prefetch => 'tracks',
+  })->search_related('tracks')->first;
+};
+
+like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
+
+$schema->storage->debug($orig_debug);
+$schema->storage->debugobj->callback(undef);
+
+$rs = $schema->resultset('Artist');
+$rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
+$rs->create({ artistid => 5, name => 'Emo 4ever' });
+@artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
+is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
+
+# -------------
+#
+# Tests for multilevel has_many prefetch
+
+# artist resultsets - with and without prefetch
+my $art_rs = $schema->resultset('Artist');
+my $art_rs_pr = $art_rs->search(
+    {},
+    {
+        join     => [ { cds => ['tracks'] } ],
+        prefetch => [ { cds => ['tracks'] } ],
+        cache    => 1 # last test needs this
+    }
+);
+
+# This test does the same operation twice - once on a
+# set of items fetched from the db with no prefetch of has_many rels
+# The second prefetches 2 levels of has_many
+# We check things are the same by comparing the name or title
+# we build everything into a hash structure and compare the one
+# from each rs to see what differs
+
+sub make_hash_struc {
+    my $rs = shift;
+
+    my $struc = {};
+    foreach my $art ( $rs->all ) {
+        foreach my $cd ( $art->cds ) {
+            foreach my $track ( $cd->tracks ) {
+                $struc->{ $art->name }{ $cd->title }{ $track->title }++;
+            }
+        }
+    }
+    return $struc;
+}
+
+$queries = 0;
+$schema->storage->debugcb(sub { $queries++ });
+$schema->storage->debug(1);
+
+my $prefetch_result = make_hash_struc($art_rs_pr);
+
+is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
+
+my $nonpre_result   = make_hash_struc($art_rs);
+
+is_deeply( $prefetch_result, $nonpre_result,
+    'Compare 2 level prefetch result to non-prefetch result' );
+
+$queries = 0;
+
+is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
+   'Fowlin',
+   'chained has_many->has_many search_related ok'
+  );
+
+is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');
+
index 5071f0c..9cf2e36 100644 (file)
@@ -7,12 +7,7 @@ use Data::Dumper;
 
 use Test::More;
 
-BEGIN {
-    eval "use SQL::Abstract 1.49";
-    plan $@
-        ? ( skip_all => "Needs SQLA 1.49+" )
-        : ( tests => 4 );
-}
+plan ( tests => 5 );
 
 use lib qw(t/lib);
 use DBICTest;
@@ -23,11 +18,8 @@ my $art_rs = $schema->resultset('Artist');
 my $cdrs = $schema->resultset('CD');
 
 {
-  my $arr = $art_rs->as_query;
-  my ($query, @bind) = @{$$arr};
-
   is_same_sql_bind(
-    $query, \@bind,
+    $art_rs->as_query,
     "(SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me)", [],
   );
 }
@@ -35,11 +27,8 @@ my $cdrs = $schema->resultset('CD');
 $art_rs = $art_rs->search({ name => 'Billy Joel' });
 
 {
-  my $arr = $art_rs->as_query;
-  my ($query, @bind) = @{$$arr};
-
   is_same_sql_bind(
-    $query, \@bind,
+    $art_rs->as_query,
     "(SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE ( name = ? ))",
     [ [ name => 'Billy Joel' ] ],
   );
@@ -48,11 +37,8 @@ $art_rs = $art_rs->search({ name => 'Billy Joel' });
 $art_rs = $art_rs->search({ rank => 2 });
 
 {
-  my $arr = $art_rs->as_query;
-  my ($query, @bind) = @{$$arr};
-
   is_same_sql_bind(
-    $query, \@bind,
+    $art_rs->as_query,
     "(SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE ( ( ( rank = ? ) AND ( name = ? ) ) ) )",
     [ [ rank => 2 ], [ name => 'Billy Joel' ] ],
   );
@@ -61,14 +47,18 @@ $art_rs = $art_rs->search({ rank => 2 });
 my $rscol = $art_rs->get_column( 'charfield' );
 
 {
-  my $arr = $rscol->as_query;
-  my ($query, @bind) = @{$$arr};
-
   is_same_sql_bind(
-    $query, \@bind,
+    $rscol->as_query,
     "(SELECT me.charfield FROM artist me WHERE ( ( ( rank = ? ) AND ( name = ? ) ) ) )",
     [ [ rank => 2 ], [ name => 'Billy Joel' ] ],
   );
 }
 
-__END__
+{
+  my $rs = $schema->resultset("CD")->search(
+    { 'artist.name' => 'Caterwauler McCrae' },
+    { join => [qw/artist/]}
+  );
+  my $subsel_rs = $schema->resultset("CD")->search( { cdid => { IN => $rs->get_column('cdid')->as_query } } );
+  is($subsel_rs->count, $rs->count, 'Subselect on PK got the same row count');
+}
diff --git a/t/resultset/update_delete.t b/t/resultset/update_delete.t
new file mode 100644 (file)
index 0000000..8810418
--- /dev/null
@@ -0,0 +1,97 @@
+use strict;
+use warnings;
+
+use lib qw(t/lib);
+use Test::More;
+use Test::Exception;
+use DBICTest;
+
+#plan tests => 5;
+plan 'no_plan';
+
+my $schema = DBICTest->init_schema();
+
+my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
+
+warn "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+
+my ($fa, $fb) = $tkfks->related_resultset ('fourkeys')->populate ([
+  [qw/foo bar hello goodbye sensors read_count/],
+  [qw/1   1   1     1       a       10         /],
+  [qw/2   2   2     2       b       20         /],
+]);
+
+# This is already provided by DBICTest
+#my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
+#  [qw/artist  cd /],
+#  [qw/1       1  /],
+#  [qw/2       2  /],
+#]);
+my ($ta, $tb) = $schema->resultset ('TwoKeys')
+                  ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
+                    ->all;
+
+my $tkfk_cnt = $tkfks->count;
+
+my $non_void_ctx = $tkfks->populate ([
+  { autopilot => 'a', fourkeys =>  $fa, twokeys => $ta, pilot_sequence => 10 },
+  { autopilot => 'b', fourkeys =>  $fb, twokeys => $tb, pilot_sequence => 20 },
+  { autopilot => 'x', fourkeys =>  $fa, twokeys => $tb, pilot_sequence => 30 },
+  { autopilot => 'y', fourkeys =>  $fb, twokeys => $ta, pilot_sequence => 40 },
+]);
+is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
+
+#
+# Make sure the forced group by works (i.e. the joining does not cause double-updates)
+#
+
+# create a resultset matching $fa and $fb only
+my $fks = $schema->resultset ('FourKeys')
+                  ->search ({ map { $_ => [1, 2] } qw/foo bar hello goodbye/}, { join => 'fourkeys_to_twokeys' });
+
+is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
+$fks->update ({ read_count => \ 'read_count + 1' });
+$_->discard_changes for ($fa, $fb);
+
+is ($fa->read_count, 11, 'Update ran only once on joined resultset');
+is ($fb->read_count, 21, 'Update ran only once on joined resultset');
+
+
+#
+# Make sure multicolumn in or the equivalen functions correctly
+#
+
+my $sub_rs = $tkfks->search (
+  [
+    { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
+    { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
+  ],
+  {
+    join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
+  },
+);
+
+is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
+
+# attempts to delete a grouped rs should fail miserably
+throws_ok (
+  sub { $sub_rs->search ({}, { distinct => 1 })->delete },
+  qr/attempted a delete operation on a resultset which does group_by/,
+  'Grouped rs update/delete not allowed',
+);
+
+# grouping on PKs only should pass
+$sub_rs->search ({}, { group_by => [ reverse $sub_rs->result_source->primary_columns ] })     # reverse to make sure the comaprison works
+          ->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
+
+is_deeply (
+  [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
+            ->get_column ('pilot_sequence')->all 
+  ],
+  [qw/11 21 30 40/],
+  'Only two rows incremented',
+);
+
+$sub_rs->delete;
+
+is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
index 078c57b..f43a71e 100644 (file)
@@ -11,7 +11,7 @@ plan tests => 5;
 
 use DBICTest;
 
-is(DBICTest::Schema->source('Artist')->resultset_class, 'DBIx::Class::ResultSet', 'default resultset class');
+is(DBICTest::Schema->source('Artist')->resultset_class, 'DBICTest::BaseResultSet', 'default resultset class');
 ok(!Class::Inspector->loaded('DBICNSTest::ResultSet::A'), 'custom resultset class not loaded');
 DBICTest::Schema->source('Artist')->resultset_class('DBICNSTest::ResultSet::A');
 ok(Class::Inspector->loaded('DBICNSTest::ResultSet::A'), 'custom resultset class loaded automatically');
index 2abf1a3..aa01183 100644 (file)
@@ -7,12 +7,7 @@ use Data::Dumper;
 
 use Test::More;
 
-BEGIN {
-    eval "use SQL::Abstract 1.49";
-    plan $@
-        ? ( skip_all => "Needs SQLA 1.49+" )
-        : ( tests => 7 );
-}
+plan ( tests => 8 );
 
 use lib qw(t/lib);
 use DBICTest;
@@ -27,11 +22,9 @@ my $cdrs = $schema->resultset('CD');
     artist_id => { 'in' => $art_rs->search({}, { rows => 1 })->get_column( 'id' )->as_query },
   });
 
-  my $arr = $cdrs2->as_query;
-  my ($query, @bind) = @{$$arr};
   is_same_sql_bind(
-    $query, \@bind,
-    "SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE artist_id IN ( SELECT id FROM artist me LIMIT 1 )",
+    $cdrs2->as_query,
+    "( SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE artist_id IN ( SELECT id FROM artist me LIMIT 1 ) )",
     [],
   );
 }
@@ -46,11 +39,9 @@ my $cdrs = $schema->resultset('CD');
     },
   );
 
-  my $arr = $rs->as_query;
-  my ($query, @bind) = @{$$arr};
   is_same_sql_bind(
-    $query, \@bind,
-    "SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me",
+    $rs->as_query,
+    "( SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me )",
     [],
   );
 }
@@ -65,11 +56,9 @@ my $cdrs = $schema->resultset('CD');
     },
   );
 
-  my $arr = $rs->as_query;
-  my ($query, @bind) = @{$$arr};
   is_same_sql_bind(
-    $query, \@bind,
-    "SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me",
+    $rs->as_query,
+    "( SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me )",
     [],
   );
 }
@@ -86,12 +75,12 @@ my $cdrs = $schema->resultset('CD');
     },
   );
 
-  my $arr = $rs->as_query;
-  my ($query, @bind) = @{$$arr};
   is_same_sql_bind(
-    $query, \@bind,
-    "SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE id > 20) cd2",
-    [],
+    $rs->as_query,
+    "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE id > ?) cd2 )",
+    [
+      [ 'id', 20 ]
+    ],
   );
 }
 
@@ -104,11 +93,9 @@ my $cdrs = $schema->resultset('CD');
       { 'me.artistid' => 'cds_artist' } ] ]
   });
 
-  my $arr = $art_rs2->as_query;
-  my ($query, @bind) = @{$$arr};
   is_same_sql_bind(
-    $query, \@bind,
-    "SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me JOIN (SELECT me.artist as cds_artist FROM cd me) cds ON me.artistid = cds_artist", []
+    $art_rs2->as_query,
+    "( SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me JOIN (SELECT me.artist as cds_artist FROM cd me) cds ON me.artistid = cds_artist )", []
   );
 
 
@@ -133,18 +120,20 @@ my $cdrs = $schema->resultset('CD');
     },
   );
 
-  my $arr = $rs->as_query;
-  my ($query, @bind) = @{$$arr};
   is_same_sql_bind(
-    $query, \@bind,
-    "SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track 
+    $rs->as_query,
+    "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track 
       FROM 
         (SELECT cd3.cdid,cd3.artist,cd3.title,cd3.year,cd3.genreid,cd3.single_track 
           FROM 
             (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track 
-              FROM cd me WHERE id < 40) cd3
-          WHERE id > 20) cd2",
-    [],
+              FROM cd me WHERE id < ?) cd3
+          WHERE id > ?) cd2
+    )",
+    [
+      [ 'id', 40 ], 
+      [ 'id', 20 ]
+    ],
   );
 
 }
@@ -158,13 +147,32 @@ my $cdrs = $schema->resultset('CD');
       )->get_column('year')->max_rs->as_query,
     },
   });
-  my $arr = $rs->as_query;
-  my ($query, @bind) = @{$$arr};
+
   is_same_sql_bind(
-    $query, \@bind,
-    "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE year = (SELECT MAX(inner.year) FROM cd inner WHERE artistid = me.artistid)",
+    $rs->as_query,
+    "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE year = (SELECT MAX(inner.year) FROM cd inner WHERE artistid = me.artistid) )",
     [],
   );
 }
 
-__END__
+{
+  my $rs = $cdrs->search(
+    {},
+    {
+      alias => 'cd2',
+      from => [
+        { cd2 => $cdrs->search({ title => 'Thriller' })->as_query },
+      ],
+    },
+  );
+
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE title = ?) cd2)",
+    [
+      [ 'title',
+        'Thriller'
+      ]
+    ],
+  );
+}
similarity index 98%
rename from t/99rh_perl_perf_bug.t
rename to t/zzzzzzz_perl_perf_bug.t
index 8bed2d7..3ccd4a7 100644 (file)
@@ -1,8 +1,8 @@
-#!/usr/bin/perl
 use strict;
 use warnings;
 use Test::More;
 use lib qw(t/lib);
+use DBICTest; # do not remove even though it is not used
 
 # This is a rather unusual test.
 # It does not test any aspect of DBIx::Class, but instead tests the