@ISA
=
qw(DBIx::ORM::Declarative::Row)
;
sub
delete
{
carp
"You must delete each component of a join individually"
;
return
;
}
sub
__create_where
{
my
(
$self
) =
@_
;
carp
"not a class method"
and
return
unless
ref
$self
;
if
(not
exists
$self
->{__whereinfo})
{
for
my
$table
(
$self
->_primary,
map
{
$_
->{table} }
$self
->_join_info)
{
my
$tab_obj
=
$self
->table(
$table
);
my
(
$un
) =
$tab_obj
->_unique_keys;
$un
||= [
map
{
$_
->{name} }
$tab_obj
->_columns ];
my
%name2sql_map
=
$tab_obj
->_column_map;
my
(
@wclause
,
@binds
);
for
my
$key
(
@$un
)
{
my
$sql_name
=
$name2sql_map
{
$key
};
my
$value
=
$self
->{__data}{
"$table.$sql_name"
};
if
(
defined
$value
)
{
push
@wclause
,
"$sql_name=?"
;
push
@binds
,
$value
;
}
else
{
push
@wclause
,
"$sql_name IS NULL"
;
}
}
@{
$self
->{__whereinfo}{
$table
}} =
(
join
(
' AND '
,
@wclause
),
@binds
);
}
}
return
%{
$self
->{__whereinfo}};
}
sub
commit
{
my
(
$self
) =
@_
;
carp
"commit: not a class method"
and
return
unless
ref
$self
;
return
$self
unless
$self
->{__dirty};
my
$handle
=
$self
->handle;
carp
"Can't commit without a handle"
and
return
unless
$handle
;
my
%whereinfo
=
$self
->__create_where;
for
my
$table
(
keys
%whereinfo
)
{
my
$tab_obj
=
$self
->table(
$table
);
my
@cols
=
map
{
$_
->{sql_name} }
$tab_obj
->_columns;
carp
"encountered a table with no columns"
and
next
unless
@cols
;
my
(
$wclause
,
@binds
) = @{
$whereinfo
{
$table
}};
unshift
@binds
, @{
$self
->{__data}}{
map
{
"$table.$_"
}
@cols
};
my
$table_name
=
$tab_obj
->_sql_name;
my
$sql
=
"UPDATE $table_name SET "
.
join
(
','
,
map
{
"$_=?"
}
@cols
) .
" WHERE $wclause"
;
if
(not
$handle
->
do
(
$sql
,
undef
,
@binds
))
{
carp
"Database error: "
,
$handle
->errstr;
$self
->__do_rollback;
return
;
}
}
delete
$self
->{__dirty};
return
$self
;
}
1;