Skip to content

Commit

Permalink
Merge 9091967 into b57788e
Browse files Browse the repository at this point in the history
  • Loading branch information
jonny64 committed Oct 26, 2022
2 parents b57788e + 9091967 commit 05aba75
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Eludia/SQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,12 @@ sub assert {

wish (table_keys => [map {{name => $_, parts => $table -> {keys} -> {$_}}} (keys %{$table -> {keys}})], {table => $table -> {name}, table_def => $table}) if exists $table -> {keys};

if (exists $table -> {triggers}) {

wish (table_triggers => [map {{name => $_, body => $table -> {triggers} -> {$_}}} (keys %{$table -> {triggers}})], {table => $table -> {name}});

}

if (exists $table -> {data} && ref $table -> {data} eq ARRAY && @{$table -> {data}} > 0) {

wish (table_data => $table -> {data}, {
Expand Down
72 changes: 72 additions & 0 deletions Eludia/SQL/Dialect/MySQL/Wish/table_triggers.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#############################################################################

sub wish_to_clarify_demands_for_table_triggers {

my ($i, $options) = @_;

my ($phase, @events) = split /_/, $i -> {name};

$i -> {phase} = uc $phase;

$i -> {events} = [sort map {uc} @events];

my $tail = lc (join '_', (
$i -> {phase},
@{$i -> {events}},
$options -> {table}
));

length $tail < 61 or $tail = Digest::MD5::md5_hex ($tail);

$i -> {global_name} = 'on_' . $tail;

}

#############################################################################

sub wish_to_actually_create_table_triggers {

my ($items, $options) = @_;

foreach my $i (@$items) {

my $events = join ' OR ', @{$i -> {events}};

foreach my $sql (

qq {
DROP TRIGGER IF EXISTS
$i->{global_name}
;
},

qq {
CREATE TRIGGER
$i->{global_name}
$i->{phase} $events ON
$options->{table}
FOR EACH ROW
BEGIN
$i->{body}
END;
},

) {

$sql =~ s{\s+}{ }gsm;
$sql =~ s{^ }{};
$sql =~ s{ $}{};

sql_do ($sql);
}


}

}

1;
86 changes: 86 additions & 0 deletions Eludia/SQL/Wish/t/wish_table_triggers.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use Test::More tests => 2;

my $table = 'testtesttesttable1';

cleanup ();

my @tables = ({

name => $table,

columns => {

id => {TYPE_NAME => 'int', _EXTRA => 'auto_increment', _PK => 1},
fake => {TYPE_NAME => 'bigint'},
label => {TYPE_NAME => 'varchar', COLUMN_SIZE => 255},
id_status => {TYPE_NAME => 'int', label => 'Статус'},
dt_status => {TYPE_NAME => 'datetime', label => 'Дата изменения статуса'},

},

triggers => {
before_update => q {
IF NEW.id_status <> OLD.id_status THEN
SET NEW.dt_status = '2022-10-26 15:00:00';
END IF;
},
}

});

wish (tables => Storable::dclone \@tables, {});

foreach my $table (@tables) {

wish (table_columns => [map {{name => $_, %{$table -> {columns} -> {$_}}}} (keys %{$table -> {columns}})], {table => $table -> {name}});
wish (table_triggers => [map {{name => $_, body => $table -> {triggers} -> {$_}}} (keys %{$table -> {triggers}})], {table => $table -> {name}});
}

################################################################################

wish (table_data => [

{id => 1, fake => 0, label => 'The One', id_status => undef, dt_status => undef},
{id => 2, fake => 0, label => 'The One', id_status => 10, dt_status => undef},

], {table => $table, key => 'id'});

is_stored ('Initial fill in by id', [

{id => 1, fake => 0, label => 'The One', id_status => undef, dt_status => undef},
{id => 2, fake => 0, label => 'The One', id_status => 10, dt_status => undef},

]);

################################################################################

sql_do ("UPDATE $table SET id_status = 20 WHERE id <= 2");

is_stored ('Update status dt by trigger', [

{id => 1, fake => 0, label => 'The One', id_status => 20, dt_status => undef},
{id => 2, fake => 0, label => 'The One', id_status => 20, dt_status => '2022-10-26 15:00:00'},

]);

################################################################################

sub is_stored {

my ($name, $data) = @_;

my $sql_data = sql_select_all ("SELECT * FROM $table ORDER BY id");

my $result = is_deeply ($sql_data, $data, $name) or darn [$sql_data, $data];

return $result;

}

sub cleanup {

eval {sql_do ("DROP TABLE $table")};

}

END { cleanup () }
13 changes: 13 additions & 0 deletions Eludia/SQL/Wish/table_triggers.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#############################################################################

sub wish_to_adjust_options_for_table_triggers {}

#############################################################################

sub wish_to_explore_existing_table_triggers {{}}

#############################################################################

sub wish_to_schedule_cleanup_for_table_triggers {}

1;

0 comments on commit 05aba75

Please sign in to comment.