[Bps-public-commit] dbix-searchbuilder branch, cud-from-select, updated. 1.59-12-gb0ee073

Ruslan Zakirov ruz at bestpractical.com
Wed Aug 3 22:58:34 EDT 2011


The branch, cud-from-select has been updated
       via  b0ee073d8eef8d2c8bdca16031ca1ca519a74710 (commit)
       via  40f9e3d5e75085ffe15274c2d124188cf21e5420 (commit)
      from  18e008c206899d2092f5a73b4e0337304f02ffda (commit)

Summary of changes:
 lib/DBIx/SearchBuilder/Handle.pm       |    2 +-
 lib/DBIx/SearchBuilder/Handle/mysql.pm |    2 +-
 t/03cud_from_select.t                  |  293 ++++++++++++++++++++++++++++++++
 3 files changed, 295 insertions(+), 2 deletions(-)
 create mode 100644 t/03cud_from_select.t

- Log -----------------------------------------------------------------
commit 40f9e3d5e75085ffe15274c2d124188cf21e5420
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Aug 4 05:18:46 2011 +0400

    things we set in UPDATE should be joined with ', '
    
    joining with AND is bad idea even if it's correct SQL

diff --git a/lib/DBIx/SearchBuilder/Handle.pm b/lib/DBIx/SearchBuilder/Handle.pm
index e53d10c..b5f1c99 100755
--- a/lib/DBIx/SearchBuilder/Handle.pm
+++ b/lib/DBIx/SearchBuilder/Handle.pm
@@ -502,7 +502,7 @@ sub SimpleUpdateFromSelect {
     }
 
     my $full_query = "UPDATE $table SET ";
-    $full_query .= join ' AND ', map "$_ = ?", @columns;
+    $full_query .= join ', ', map "$_ = ?", @columns;
     $full_query .= ' WHERE id IN ('. $query .')';
     my $sth = $self->SimpleQuery( $full_query, @binds );
     return $sth unless $sth;
diff --git a/lib/DBIx/SearchBuilder/Handle/mysql.pm b/lib/DBIx/SearchBuilder/Handle/mysql.pm
index e4eb015..f3aabaf 100755
--- a/lib/DBIx/SearchBuilder/Handle/mysql.pm
+++ b/lib/DBIx/SearchBuilder/Handle/mysql.pm
@@ -75,7 +75,7 @@ sub SimpleUpdateFromSelect {
     }
 
     my $update_query = "UPDATE $table SET "
-        . join( ' AND ', map "$_ = ?", @columns )
+        . join( ', ', map "$_ = ?", @columns )
         .' WHERE ID IN ';
 
     return $self->SimpleMassChangeFromSelect(

commit b0ee073d8eef8d2c8bdca16031ca1ca519a74710
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Aug 4 06:57:47 2011 +0400

    test {Insert,SimpleUpdate,Delete}FromSelect methods

diff --git a/t/03cud_from_select.t b/t/03cud_from_select.t
new file mode 100644
index 0000000..0cfddbd
--- /dev/null
+++ b/t/03cud_from_select.t
@@ -0,0 +1,293 @@
+#!/usr/bin/perl -w
+
+use strict;
+use Test::More;
+
+BEGIN { require "t/utils.pl" }
+our (@AvailableDrivers);
+
+use constant TESTS_PER_DRIVER => 1;
+
+my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+foreach my $d ( @AvailableDrivers ) {
+SKIP: {
+    unless( has_schema( 'TestApp', $d ) ) {
+        skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+    }
+    unless( should_test( $d ) ) {
+        skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+    }
+
+    my $handle = get_handle( $d );
+    connect_handle( $handle );
+    isa_ok($handle->dbh, 'DBI::db');
+
+    my $ret = init_schema( 'TestApp', $handle );
+    isa_ok($ret, 'DBI::st', "Inserted the schema. got a statement handle back");
+
+    init_data( $_, $handle ) foreach qw(
+        TestApp::User
+        TestApp::Group
+        TestApp::UsersToGroup
+    );
+
+diag "insert into table from other tables only" if $ENV{'TEST_VERBOSE'};
+{
+    my $res = $handle->InsertFromSelect(
+        'UsersToGroups' => ['UserId', 'GroupId'],
+        'SELECT id, 1 FROM Users WHERE Login LIKE ?', '%o%'
+    );
+    is( $res, 2 );
+    my $users = TestApp::Users->new( $handle );
+    my $alias = $users->Join( FIELD1 => 'id', TABLE2 => 'UsersToGroups', FIELD2 => 'UserId' );
+    $users->Limit( ALIAS => $alias, FIELD => 'GroupId', VALUE => 1 );
+    is_deeply( [ sort map $_->Login, @{ $users->ItemsArrayRef } ], ['bob', 'john'] );
+}
+
+diag "insert into table we select from" if $ENV{'TEST_VERBOSE'};
+{
+    my $res = $handle->InsertFromSelect(
+        'UsersToGroups' => ['UserId', 'GroupId'],
+        'SELECT u.id, g.id FROM Users u, Groups g WHERE u.Login LIKE ? AND g.Name = ?',
+        '%a%', 'Support'
+    );
+    is( $res, 2 );
+    my $users = TestApp::Users->new( $handle );
+    my $u2g_alias = $users->Join( FIELD1 => 'id', TABLE2 => 'UsersToGroups', FIELD2 => 'UserId' );
+    my $g_alias = $users->Join(
+        ALIAS1 => $u2g_alias, FIELD1 => 'GroupId', TABLE2 => 'Groups', FIELD2 => 'id',
+    );
+    $users->Limit( ALIAS => $g_alias, FIELD => 'Name', VALUE => 'Support' );
+    is_deeply( [ sort map $_->Login, @{ $users->ItemsArrayRef } ], ['aurelia', 'ivan'] );
+}
+
+{
+    my $res = $handle->DeleteFromSelect(
+        'UsersToGroups' => 'SELECT id FROM UsersToGroups WHERE GroupId = 1'
+    );
+    is( $res, 2 );
+
+    my $users = TestApp::Users->new( $handle );
+    my $alias = $users->Join( FIELD1 => 'id', TABLE2 => 'UsersToGroups', FIELD2 => 'UserId' );
+    $users->Limit( ALIAS => $alias, FIELD => 'GroupId', VALUE => 1 );
+    is( $users->Count, 0 );
+}
+
+{
+    my $res = $handle->SimpleUpdateFromSelect(
+        'UsersToGroups',
+        { UserId => 2, GroupId => 2 },
+        'SELECT id FROM UsersToGroups WHERE UserId = 1 AND GroupId = 3'
+    );
+    is( $res, 1 );
+
+    my $u2gs = TestApp::UsersToGroups->new( $handle );
+    $u2gs->Limit( FIELD => 'UserId', VALUE => 1 );
+    $u2gs->Limit( FIELD => 'GroupId', VALUE => 3 );
+    is( $u2gs->Count, 0 );
+
+    $u2gs = TestApp::UsersToGroups->new( $handle );
+    $u2gs->Limit( FIELD => 'UserId', VALUE => 2 );
+    $u2gs->Limit( FIELD => 'GroupId', VALUE => 2 );
+    is( $u2gs->Count, 1 );
+}
+
+    cleanup_schema( 'TestApp', $handle );
+
+}} # SKIP, foreach blocks
+
+1;
+
+
+package TestApp;
+sub schema_sqlite {
+[
+q{
+CREATE TABLE Users (
+    id integer primary key,
+    Login varchar(36)
+) },
+q{
+CREATE TABLE UsersToGroups (
+    id integer primary key,
+    UserId  integer,
+    GroupId integer
+) },
+q{
+CREATE TABLE Groups (
+    id integer primary key,
+    Name varchar(36)
+) },
+]
+}
+
+sub schema_mysql {
+[
+q{
+CREATE TEMPORARY TABLE Users (
+    id integer primary key AUTO_INCREMENT,
+    Login varchar(36)
+) },
+q{
+CREATE TEMPORARY TABLE UsersToGroups (
+    id integer primary key AUTO_INCREMENT,
+    UserId  integer,
+    GroupId integer
+) },
+q{
+CREATE TEMPORARY TABLE Groups (
+    id integer primary key AUTO_INCREMENT,
+    Name varchar(36)
+) },
+]
+}
+
+sub schema_pg {
+[
+q{
+CREATE TEMPORARY TABLE Users (
+    id serial primary key,
+    Login varchar(36)
+) },
+q{
+CREATE TEMPORARY TABLE UsersToGroups (
+    id serial primary key,
+    UserId integer,
+    GroupId integer
+) },
+q{
+CREATE TEMPORARY TABLE Groups (
+    id serial primary key,
+    Name varchar(36)
+) },
+]
+}
+
+sub schema_oracle { [
+    "CREATE SEQUENCE Users_seq",
+    "CREATE TABLE Users (
+        id integer CONSTRAINT Users_Key PRIMARY KEY,
+        Login varchar(36)
+    )",
+    "CREATE SEQUENCE UsersToGroups_seq",
+    "CREATE TABLE UsersToGroups (
+        id integer CONSTRAINT UsersToGroups_Key PRIMARY KEY,
+        UserId integer,
+        GroupId integer
+    )",
+    "CREATE SEQUENCE Groups_seq",
+    "CREATE TABLE Groups (
+        id integer CONSTRAINT Groups_Key PRIMARY KEY,
+        Name varchar(36)
+    )",
+] }
+
+sub cleanup_schema_oracle { [
+    "DROP SEQUENCE Users_seq",
+    "DROP TABLE Users", 
+    "DROP SEQUENCE Groups_seq",
+    "DROP TABLE Groups", 
+    "DROP SEQUENCE UsersToGroups_seq",
+    "DROP TABLE UsersToGroups", 
+] }
+
+package TestApp::Record;
+
+use base $ENV{SB_TEST_CACHABLE}?
+    qw/DBIx::SearchBuilder::Record::Cachable/:
+    qw/DBIx::SearchBuilder::Record/;
+
+sub _Init {
+    my $self = shift;
+    my $handle = shift;
+    $self->_Handle($handle);
+
+    my $table = ref $self || $self;
+    $table =~ s/.*:://;
+    $table .= 's';
+    $self->Table( $table );
+}
+
+package TestApp::Col;
+use base 'DBIx::SearchBuilder';
+
+sub _Init {
+    my $self = shift;
+    $self->SUPER::_Init( Handle => shift );
+
+    my $table = ref $self || $self;
+    $table =~ s/.*:://;
+    $self->Table( $table );
+}
+
+sub NewItem {
+    my $self = shift;
+    my $record_class = (ref($self) || $self);
+    $record_class =~ s/s$//;
+    return $record_class->new( $self->_Handle );
+}
+
+package TestApp::User;
+use base 'TestApp::Record';
+
+sub _ClassAccessible { return {
+    id => {read => 1, type => 'int(11)'}, 
+    Login => {read => 1, write => 1, type => 'varchar(36)'},
+} }
+
+sub init_data {
+    return (
+    [ 'Login' ],
+
+    [ 'ivan' ],
+    [ 'john' ],
+    [ 'bob' ],
+    [ 'aurelia' ],
+    );
+}
+
+package TestApp::Group;
+use base 'TestApp::Record';
+
+sub _ClassAccessible {
+    {
+        id => {read => 1, type => 'int(11)'}, 
+        Name => {read => 1, write => 1, type => 'varchar(36)'},
+    }
+}
+
+sub init_data {
+    return (
+    [ 'Name' ],
+
+    [ 'Developers' ],
+    [ 'Sales' ],
+    [ 'Support' ],
+    );
+}
+
+package TestApp::UsersToGroup;
+use base 'TestApp::Record';
+
+sub _ClassAccessible {
+    return {
+        id => {read => 1, type => 'int(11)'}, 
+        UserId => {read => 1, type => 'int(11)'}, 
+        GroupId => {read => 1, type => 'int(11)'}, 
+    }
+}
+
+sub init_data {
+    return ([ 'GroupId',    'UserId' ]);
+}
+
+package TestApp::Users;
+use base 'TestApp::Col';
+
+package TestApp::Groups;
+use base 'TestApp::Col';
+
+package TestApp::UsersToGroups;
+use base 'TestApp::Col';

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list