Hide Show 6 lines of Pod
use
vars (
qw($VERSION @ISA)
);
@ISA
=
qw(Perlbug::Object)
;
$VERSION
=
do
{
my
@r
= (
q$Revision: 1.37 $
=~ /\d+/g);
sprintf
"%d."
.
"%02d"
x
$#r
,
@r
};
$|=1;
Hide Show 10 lines of Pod
Hide Show 37 lines of Pod
sub
new {
my
$proto
=
shift
;
my
$class
=
ref
(
$proto
) ||
$proto
;
my
$o_Perlbug_Base
=
(
ref
(
$_
[0]) =~ /^Perlbug::(Base|Fix|(Interface::(Cmd|Email|Web)))$/o)
?
shift
: Perlbug::Base->new;
my
$src
=
shift
;
my
$tgt
=
shift
;
my
$type
=
shift
||
'to'
;
(
$src
,
$tgt
) = (
$type
eq
'from'
) ? (
$tgt
,
$src
) : (
$src
,
$tgt
);
my
$o_src
= (
ref
(
$src
)) ?
$src
:
$o_Perlbug_Base
->object(
$src
);
my
$s_key
=
$o_src
->attr(
'key'
);
my
$o_tgt
= (
ref
(
$tgt
)) ?
$tgt
:
$o_Perlbug_Base
->object(
$tgt
);
my
$hint
=
$src
.
'_x_'
.
$tgt
;
my
$t_key
=
lc
((
$hint
=~ /(parent|child)/io) ? $1 :
$o_tgt
->attr(
'key'
));
my
$table
=
'pb_'
.
join
(
'_'
,
sort
(
$s_key
,
$t_key
));
my
$self
= Perlbug::Object->new(
$o_Perlbug_Base
,
'hint'
=>
$hint
,
'key'
=>
$s_key
.
'->'
.
$t_key
,
'match_oid'
=>
$o_src
->attr(
'match_oid'
),
'name'
=>
ucfirst
(
$s_key
),
'table'
=>
$table
,
'type'
=>
$type
,
);
$self
->{
'_attr'
}{
'source'
} =
$s_key
;
$self
->{
'_attr'
}{
'target'
} =
$t_key
;
bless
(
$self
,
$class
);
$table
=
$self
->attr(
'table'
);
$self
->debug(3,
"rjsf: Relation::new($type) src($src)\t-> "
.
sprintf
(
'%-15s'
,
$o_src
).
") & tgt($tgt)\t-> "
.
sprintf
(
'%-15s'
,
$o_tgt
).
" table($table)"
)
if
$Perlbug::DEBUG
;
$self
->source(
$o_src
);
$self
->target(
$o_tgt
);
$self
->check();
return
$self
;
}
Hide Show 8 lines of Pod
sub
source {
my
$self
=
shift
;
my
$o_src
=
shift
;
my
$key
=
lc
(
shift
||
$self
->attr(
'source'
));
my
$o_obj
=
$self
->object(
$key
,
$o_src
);
return
$o_obj
;
}
Hide Show 8 lines of Pod
sub
target {
my
$self
=
shift
;
my
$o_tgt
=
shift
;
my
$key
=
lc
(
shift
||
$self
->attr(
'target'
));
my
$o_obj
=
$self
->object(
$key
,
$o_tgt
);
return
$o_obj
;
}
Hide Show 8 lines of Pod
sub
check {
my
$self
=
shift
;
my
$o_src
=
shift
||
$self
->source;
my
$o_tgt
=
shift
||
$self
->target;
my
$i_ok
= 0;
my
$hint
=
$self
->attr(
'hint'
);
my
$name
=
$self
->attr(
'name'
);
my
$table
=
$self
->attr(
'table'
);
my
$src
=
$o_src
->key;
my
@src_from
=
$o_src
->attr(
'from'
);
my
@src_to
=
$o_src
->attr(
'to'
);
my
$tgt
=
$o_tgt
->key;
my
@tgt_from
=
$o_tgt
->attr(
'from'
);
my
@tgt_to
=
$o_tgt
->attr(
'to'
);
my
$err
=
''
;
unless
(
$hint
=~ /(parent|child)/io) {
if
(
lc
(
$src
) eq
lc
(
$tgt
)) {
$err
=
"Source("
.
ref
(
$o_src
).
") is the same as target("
.
ref
(
$o_tgt
).
")!"
;
}
if
(!(
grep
(/^
$tgt
$/,
@src_to
,
@src_from
))) {
$err
=
"Source("
.
ref
(
$o_src
).
") doesn't recognise target("
.
ref
(
$o_tgt
).
")!"
;
}
if
(!(
grep
(/^
$src
$/,
@tgt_to
,
@tgt_from
))) {
$err
=
"Target("
.
ref
(
$o_tgt
).
") doesn't recognise source("
.
ref
(
$o_src
).
")!"
;
}
}
if
(
$err
!~ /\w+/) {
$i_ok
++;
}
else
{
$self
->error(
qq|$self $hint $name $table
Src($src) $o_src
from(@src_from)
to(@src_to)
Tgt($tgt) $o_tgt
from(@tgt_from)
to(@tgt_to)
$err
|
);
}
return
$i_ok
;
}
Hide Show 20 lines of Pod
sub
set_source {
my
$self
=
shift
;
my
$key
=
lc
(
shift
);
my
$oid
=
shift
||
''
;
my
(
$o_src
,
$o_tgt
) = (
$self
->source,
$self
->target);
my
(
$s_key
,
$t_key
) = (
$o_src
->key,
$o_tgt
->key);
my
$type
= (
$self
->attr(
'type'
) eq
'from'
) ?
'to'
:
'from'
;
if
(
$key
=~ /\w+/o &&
$key
eq
$t_key
) {
$self
->attr({
'type'
=>
$type
,
'source'
=>
$t_key
,
'target'
=>
$s_key
});
$self
->source(
$o_tgt
,
$t_key
);
$self
->target(
$o_src
,
$s_key
);
$o_src
=
$self
->source;
$o_tgt
=
$self
->target;
}
$o_src
->oid(
$oid
)
if
$oid
;
$self
->check();
$self
->debug(3,
qq|key($key) oid($oid) type($type)...
src($s_key)\t-> o_src($o_src)
tgt($t_key)\t-> o_tgt($o_tgt)
|
)
if
$Perlbug::DEBUG
;
return
$self
;
}
Hide Show 10 lines of Pod
sub
key {
my
$self
=
shift
;
my
$type
=
shift
||
'source'
;
my
$res
=
lc
(
$self
->
$type
()->key).
'id'
;
return
$res
;
}
Hide Show 8 lines of Pod
sub
oid {
my
$self
=
shift
;
my
$in
=
shift
||
''
;
my
(
$o_src
,
$s_key
) = (
$self
->source,
$self
->key(
'source'
));
my
(
$o_tgt
,
$t_key
) = (
$self
->target,
$self
->key(
'target'
));
if
(
defined
(
$in
)) {
my
$src
=
$self
->source->oid(
$in
);
$self
->attr({
'objectid'
=>
$in
});
$self
->data({
$s_key
=>
$in
});
}
my
$oid
=
$self
->attr(
'objectid'
);
$self
->debug(3,
"oid: src($s_key) tgt($t_key) -> in($in) oid($oid)"
)
if
$Perlbug::DEBUG
;
return
$oid
;
}
Hide Show 12 lines of Pod
sub
ids {
my
$self
=
shift
;
my
$input
=
shift
||
''
;
my
$extra
=
shift
||
''
;
my
(
$o_src
,
$s_key
) = (
$self
->source,
$self
->key(
'source'
));
my
(
$o_tgt
,
$t_key
) = (
$self
->target,
$self
->key(
'target'
));
my
$sql
=
"SELECT DISTINCT $t_key FROM "
.
$self
->attr(
'table'
);
if
(
ref
(
$input
)) {
$sql
.=
' WHERE '
.
$input
->key.
"id = '"
.
$input
->oid().
"'"
;
$sql
.=
" AND $extra"
if
$extra
;
}
elsif
(
$input
=~ /\w+/o) {
$input
=~ s/^\s
*WHERE
\s*//io;
$sql
.=
" WHERE $input"
;
}
my
@ids
=
$self
->base->get_list(
$sql
);
$self
->debug(3,
"input($input) extra($extra) -> ids(@ids)"
)
if
$Perlbug::DEBUG
;
return
@ids
;
}
Hide Show 8 lines of Pod
sub
reinit {
my
$self
=
shift
;
my
$oid
=
shift
||
$self
->oid;
$self
->SUPER::reinit(
$oid
);
$self
->ASSIGNED(0);
$self
;
}
sub
prep {
my
$self
=
shift
;
my
$sql
=
$self
->SUPER::prep(
@_
);
$self
->error(
"NULL's not allowed in relations: "
.
$sql
)
if
$sql
=~ /NULL/o;
return
$sql
;
}
sub
track {
my
$self
=
shift
;
$self
->TRACKED(1);
return
$self
;
}
Hide Show 20 lines of Pod
sub
assign {
my
$self
=
shift
;
my
$a_input
=
shift
;
if
(!
ref
(
$a_input
)) {
$self
->error(
"no input ids given to assign($a_input)"
);
}
else
{
my
@given
= @{
$a_input
};
my
(
$o_src
,
$s_key
) = (
$self
->source,
$self
->key(
'source'
));
my
(
$o_tgt
,
$t_key
) = (
$self
->target,
$self
->key(
'target'
));
my
@ids
=
$o_tgt
->
exists
(
$a_input
);
my
$oid
=
$o_src
->oid;
if
(!
$o_src
->
exists
([
$oid
])) {
$self
->error(
"has no source valid objectid($oid) to assign from!"
);
}
else
{
my
$table
=
$self
->attr(
'table'
);
my
$ids
=
join
(
"', '"
,
@ids
);
my
$sql
=
"DELETE FROM $table WHERE $s_key = '"
.
$o_src
->oid().
"'"
;
my
$sth
=
$self
->base->
exec
(
$sql
);
$self
->debug(0,
"non-prejudicial $sql -> sth($sth)"
)
if
$Perlbug::DEBUG
;
if
(!
defined
(
$sth
)) {
$self
->error(
ref
(
$self
).
" assign trim failed: sql($sql) -> sth($sth)"
);
}
else
{
foreach
my
$id
(
@ids
) {
$self
->oid(
$oid
);
$self
->data({
$t_key
=>
$id
, });
$self
->create(
$self
->_oref(
'data'
),
'relation'
);
if
(
$self
->CREATED) {
$self
->ASSIGNED(1);
$self
->debug(2,
"assigned: $s_key($oid) $t_key($id)"
)
if
$Perlbug::DEBUG
;
}
}
}
}
}
return
$self
;
}
Hide Show 8 lines of Pod
sub
ASSIGNED {
my
$self
=
shift
;
my
$i_flag
=
shift
||
''
;
$self
->flag({
'assigned'
, $1})
if
$i_flag
=~ /^(1|0)$/o;
$i_flag
=
$self
->flag(
'assigned'
);
return
$i_flag
;
}
Hide Show 8 lines of Pod
sub
_assign {
my
$self
=
shift
;
my
$a_input
=
shift
;
if
(!
ref
(
$a_input
)) {
$self
->error(
"no input names given to _assign($a_input)"
);
}
else
{
my
$rel
=
ref
(
$self
);
$self
->create_target(
$a_input
);
my
@ids
=
$self
->target->name2id(
$a_input
);
$self
->assign(\
@ids
);
}
return
$self
;
}
Hide Show 12 lines of Pod
sub
store {
my
$self
=
shift
;
my
$a_input
=
shift
||
''
;
if
(!
ref
(
$a_input
)) {
$self
->error(
"no input ids given to store($a_input)"
);
}
else
{
my
@orig
= @{
$a_input
};
my
@IDS
= ();
my
(
$o_src
,
$s_key
) = (
$self
->source,
$self
->key(
'source'
));
my
(
$o_tgt
,
$t_key
) = (
$self
->target,
$self
->key(
'target'
));
my
@ids
=
$o_tgt
->
exists
(
$a_input
);
my
$ids
=
join
(
"', '"
,
@ids
);
my
$oid
=
$o_src
->oid();
if
(
scalar
(
$o_src
->
exists
([
$oid
])) == 0) {
$self
->error(
"has no source objectid($oid) to store against!"
);
}
else
{
if
(!(
scalar
(
@ids
) >= 1)) {
$self
->debug(0,
"not trashing($oid) ${s_key}_$t_key records unless supplied(@orig) with valid objectids(@ids)!"
);
}
else
{
my
$table
=
$self
->attr(
'table'
);
my
$sql
=
"DELETE FROM $table WHERE $s_key = '"
.
$o_src
->oid().
"'"
;
my
$sth
=
$self
->base->
exec
(
$sql
);
$self
->debug(0,
"prejudicial $sql -> sth($sth)"
)
if
$Perlbug::DEBUG
;
if
(!
defined
(
$sth
)) {
$self
->error(
ref
(
$self
).
" store trim failed: sql($sql) -> sth($sth)"
);
}
else
{
$self
->assign(\
@ids
);
if
(
$self
->ASSIGNED) {
$self
->STORED(1);
$self
->debug(0,
"assigned("
.
$self
->ASSIGNED.
") ids(@ids)"
)
if
$Perlbug::DEBUG
;
}
}
$self
->base->clean_cache(
'sql'
);
}
}
}
return
$self
;
}
Hide Show 8 lines of Pod
sub
_store {
my
$self
=
shift
;
my
$a_input
=
shift
||
''
;
if
(!
ref
(
$a_input
)) {
$self
->error(
"no input names given to _store($a_input)"
);
}
else
{
$self
->create_target(
$a_input
);
my
@ids
=
$self
->target->name2id(
$a_input
);
$self
->store(\
@ids
);
}
return
$self
;
}
Hide Show 8 lines of Pod
sub
delete
{
my
$self
=
shift
;
my
$a_input
=
shift
||
''
;
if
(!
ref
(
$a_input
)) {
$self
->error(
"no input ids given to delete($a_input)"
);
}
else
{
my
@orig
= @{
$a_input
};
my
@IDS
= ();
my
(
$o_src
,
$s_key
) = (
$self
->source,
$self
->key(
'source'
));
my
(
$o_tgt
,
$t_key
) = (
$self
->target,
$self
->key(
'target'
));
my
@ids
=
$o_tgt
->
exists
(
$a_input
);
$self
->debug(2,
"working with ids(@ids)"
)
if
$Perlbug::DEBUG
;
my
$ids
=
join
(
"', '"
,
@ids
);
my
$oid
=
$o_src
->oid();
if
(
scalar
(
$o_src
->
exists
([
$oid
])) == 0) {
$self
->error(
"has no source objectid($oid) to delete!"
);
}
else
{
my
$where
=
" WHERE $s_key = '$oid'"
;
my
$sql
=
"DELETE FROM "
.
$self
->attr(
'table'
).
" $where AND $t_key IN ('$ids')"
;
my
$sth
=
$self
->base->
exec
(
$sql
);
if
(!
$sth
) {
$self
->error(
ref
(
$self
).
" delete failed: sql($sql) -> sth($sth)"
);
}
else
{
$self
->DELETED(1);
$self
->base->clean_cache(
'sql'
);
}
}
}
return
$self
;
}
Hide Show 8 lines of Pod
sub
_delete {
my
$self
=
shift
;
my
$a_input
=
shift
||
''
;
if
(!
ref
(
$a_input
)) {
$self
->error(
"no input names given to _delete($a_input)"
);
}
else
{
my
@ids
=
$self
->target->name2id(
$a_input
);
$self
->
delete
(\
@ids
);
}
return
$self
;
}
Hide Show 12 lines of Pod
sub
create_target {
my
$self
=
shift
;
my
$a_input
=
shift
;
my
(
$table
,
$pri
) = (
$self
->attr(
'table'
),
$self
->attr(
'primary_key'
));
if
(!
ref
(
$a_input
) eq
'ARRAY'
) {
$self
->error(
"no ids given to create_target($a_input) from"
);
}
else
{
my
@given
=
$self
->trim(
$a_input
);
my
(
$o_src
,
$s_key
) = (
$self
->source,
$self
->key(
'source'
));
my
(
$o_tgt
,
$t_key
) = (
$self
->target,
$self
->key(
'target'
));
my
$t_pri
=
$o_tgt
->primary_key;
if
(
scalar
(
@given
) == 0) {
$self
->error(
"has no target objectids(@given) to create_target for!"
);
}
else
{
my
@exist
=
$o_tgt
->_exists(\
@given
);
my
@extantids
=
$o_tgt
->name2id(\
@exist
);
$self
->debug(1,
"pri($t_pri) given(@given) exist(@exist) extant(@extantids)"
)
if
$Perlbug::DEBUG
;
IDENT:
foreach
my
$ident
(
@given
) {
next
IDENT
unless
$ident
=~ /\w+/o;
$self
->debug(1,
"does $ident exist(@exist)?"
)
if
$Perlbug::DEBUG
;
next
IDENT
if
grep
(/^
$ident
$/,
@exist
);
$self
->debug(1,
"NOPE($ident) -> creating!"
)
if
$Perlbug::DEBUG
;
$o_tgt
->reinit->oid(
$ident
);
my
$h_data
= {
$t_key
=>
$self
->new_id,
$o_tgt
->
identifier
=>
$ident
,
};
$self
->debug(0,
ref
(
$o_tgt
).
' data: '
.Dumper(
$h_data
));
$o_tgt
->create(
$h_data
);
}
}
}
return
$self
;
}
Hide Show 10 lines of Pod
1;