use
5.016;
$Geoffrey::Action::Table::VERSION
=
'0.000206'
;
sub
_hr_merge_templates {
my
(
$self
,
$s_template
,
$s_table_name
) =
@_
;
if
((
$self
->{template} && !
$self
->{template}->template(
$s_template
)) || !
$self
->{template}) {
Geoffrey::Exception::Template::throw_template_not_found(
$s_template
);
}
my
$ar_template_columns
= [];
my
$ar_template_constraints
= [];
$self
->column_action->for_table(1);
$self
->constraint_action->for_table(1);
for
(@{
$self
->{template}->template(
$s_template
)}) {
$_
->{table} =
$s_table_name
;
push
@{
$ar_template_columns
},
$self
->column_action->add(
$_
,
$self
->constraint_action->add(
$s_table_name
,
$_
,
$ar_template_constraints
));
}
$self
->column_action->for_table(0);
$self
->constraint_action->for_table(0);
return
{
columns
=>
$ar_template_columns
,
constraints
=>
$ar_template_constraints
};
}
sub
postfix {
return
$_
[0]->{postfix} //
q~~
if
!
defined
$_
[1];
$_
[0]->{postfix} =
$_
[1];
return
$_
[0]->{postfix};
}
sub
prefix {
return
$_
[0]->{prefix} //
q~~
if
!
defined
$_
[1];
$_
[0]->{prefix} =
$_
[1];
return
$_
[0]->{prefix};
}
sub
constraint_action {
my
$self
=
shift
;
return
$self
->{constraint_action}
if
(
$self
->{constraint_action});
$self
->{constraint_action}
= Geoffrey::Action::Constraint->new(
converter
=>
$self
->converter,
dbh
=>
$self
->dbh,);
return
$self
->{constraint_action};
}
sub
column_action {
my
$self
=
shift
;
return
$self
->{column_action}
if
(
$self
->{column_action});
$self
->{column_action}
= Geoffrey::Action::Column->new(
converter
=>
$self
->converter,
dbh
=>
$self
->dbh,);
return
$self
->{column_action};
}
sub
action {
my
(
$self
,
$s_action
) =
@_
;
$s_action
=
join
q//
,
map
{
ucfirst
}
split
/_/,
$s_action
;
return
Geoffrey::Utils::action_obj_from_name(
$s_action
,
dbh
=>
$self
->dbh,
converter
=>
$self
->converter,
dryrun
=>
$self
->dryrun
);
}
sub
add {
my
(
$self
,
$hr_params
) =
@_
;
if
(!
$hr_params
|| !
$hr_params
->{name}) {
Geoffrey::Exception::RequiredValue::throw_table_name(__PACKAGE__);
}
my
@columns
= ();
my
$ar_constraints
= [];
my
$constraint_action
=
$self
->constraint_action;
my
$column_action
=
$self
->column_action;
if
(
$hr_params
->{template}) {
my
$templates
=
$self
->_hr_merge_templates(
$hr_params
->{template},
$hr_params
->{name});
push
@columns
, @{
$templates
->{columns}};
push
@{
$ar_constraints
}, @{
$templates
->{constraints}};
}
$constraint_action
->for_table(1);
$column_action
->for_table(1);
for
my
$hr_column
(@{
$hr_params
->{columns}}) {
$hr_column
->{schema} =
$hr_params
->{schema}
if
exists
$hr_params
->{schema};
$hr_column
->{table} =
$hr_params
->{name};
my
$const
=
$constraint_action
->add(
$hr_params
->{name},
$hr_column
,
$ar_constraints
);
push
@columns
,
$column_action
->add(
$hr_column
,
$const
);
}
for
(@{
$hr_params
->{constraints}}) {
$_
->{schema} =
$hr_params
->{schema}
if
exists
$hr_params
->{schema};
$constraint_action
->add(
$hr_params
->{name},
$_
,
$ar_constraints
);
}
push
@columns
, @{
$ar_constraints
};
if
(
scalar
@columns
== 0 && !
$self
->converter->can_create_empty_table) {
Geoffrey::Exception::NotSupportedException::throw_empty_table(
$self
->converter,
$hr_params
);
}
$constraint_action
->for_table(0);
$column_action
->for_table(0);
my
$sql
= Geoffrey::Utils::replace_spare(
$self
->converter->table->add, [
(
$hr_params
->{schema} ?
$hr_params
->{schema} .
q/./
:
q//
) .
$self
->prefix .
$hr_params
->{name} .
$self
->postfix,
join
(
q/,/
,
@columns
),
$hr_params
->{engine},
$hr_params
->{charset}
]);
return
$self
->
do
(
$sql
);
}
sub
alter {
my
(
$self
,
$hr_params
) =
@_
;
if
(!Ref::Util::is_hashref(
$hr_params
)) {
Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ .
'::alter'
,
'hash'
);
}
if
(!
$hr_params
->{name}) {
Geoffrey::Exception::General::throw_no_table_name(
'to alter'
);
}
my
@ar_result
= ();
for
(@{
$hr_params
->{alter}}) {
my
(
$s_sub
,
$s_action
) = Geoffrey::Utils::parse_package_sub(
$_
->{action});
my
$obj_action
=
$self
->action(
$s_action
);
if
(!
$s_sub
|| !
$obj_action
->can(
$s_sub
)) {
Geoffrey::Exception::RequiredValue::throw_action_sub(
$s_action
);
}
$_
->{table} =
$hr_params
->{name};
push
@ar_result
,
$obj_action
->
$s_sub
(
$_
);
}
return
\
@ar_result
;
}
sub
drop {
my
(
$self
,
$hr_params
) =
@_
;
my
$s_name
= Ref::Util::is_hashref(
$hr_params
) ?
$hr_params
->{name} :
undef
;
if
(!
$s_name
) {
Geoffrey::Exception::General::throw_no_table_name(
'to drop'
);
}
return
$self
->
do
(Geoffrey::Utils::replace_spare(
$self
->converter->table->drop, [
$s_name
]));
}
sub
list_from_schema {
my
(
$self
,
$schema
) =
@_
;
return
[
map
{
$_
->{name} } @{
$self
->do_arrayref(
$self
->converter->table->list(
$schema
), [])}];
}
1;