@ISA
=
qw(DBIx::ORM::Declarative::Table)
;
sub
delete
{
my
(
$self
) =
@_
;
carp
"delete: not a class method"
and
return
unless
ref
$self
;
$self
->{__deleted} = 1;
return
$self
;
}
sub
__set_data
{
my
(
$self
,
@data
) =
@_
;
carp
"not a class method"
and
return
unless
ref
$self
;
@{
$self
->{__data}}{
$self
->_column_sql_names} =
@data
;
return
$self
;
}
sub
__create_where
{
my
(
$self
) =
@_
;
carp
"not a class method"
and
return
unless
ref
$self
;
if
(not
exists
$self
->{__whereinfo})
{
my
@cols
=
$self
->_columns;
my
%name2sql
=
$self
->_column_map;
my
@binds
;
my
@wclause
;
my
@un
=
$self
->_unique_keys;
my
@colv
;
@colv
=
map
{
$name2sql
{
$_
} } @{
$un
[0]}
if
@un
;
@colv
=
map
{
$_
->{sql_name} }
@cols
unless
@colv
;
for
my
$k
(
@colv
)
{
if
(
defined
(
$self
->{__data}{
$k
}))
{
push
@wclause
,
"$k=?"
;
push
@binds
,
$self
->{__data}{
$k
};
}
else
{
push
@wclause
,
"$k IS NULL"
;
}
}
@{
$self
->{__whereinfo}} = (
join
(
' AND '
,
@wclause
),
@binds
);
}
return
@{
$self
->{__whereinfo}};
}
sub
commit
{
my
(
$self
) =
@_
;
carp
"commit: not a class method"
and
return
unless
ref
$self
;
my
$handle
=
$self
->handle;
carp
"can't commit without a database handle"
and
return
unless
$handle
;
return
$self
unless
$self
->{__deleted} or
$self
->{__dirty};
my
(
$where
,
@binds
) =
$self
->__create_where;
my
$sql
;
if
(
$self
->{__deleted})
{
$sql
=
"DELETE FROM "
.
$self
->_sql_name .
" WHERE $where"
;
}
else
{
my
@cols
=
$self
->_columns;
$sql
=
"UPDATE "
.
$self
->_sql_name .
" SET "
;
$sql
.=
join
(
','
,
map
{
$_
->{sql_name} .
'=?'
}
@cols
);
unshift
@binds
,
map
{
$self
->{__data}{
$_
->{sql_name}} }
@cols
;
$sql
.=
" WHERE $where"
;
}
unshift
@binds
,
undef
if
@binds
;
if
(not
$handle
->
do
(
$sql
,
@binds
))
{
carp
"Database error: "
,
$handle
->errstr;
$self
->__do_rollback;
return
;
}
delete
$self
->{__dirty};
delete
$self
->{__whereinfo};
if
(
$self
->{__deleted})
{
delete
$self
->{__data};
delete
$self
->{__deleted};
bless
$self
,
$self
->_class;
}
else
{
$self
->__create_where
}
local
(
$SIG
{__WARN__}) =
$self
->w__noop;
$handle
->commit;
return
$self
;
}
1;