-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |