Revision b354800b
Von Sven Schöling vor mehr als 8 Jahren hinzugefügt
SL/Helper/UserPreferences.pm | ||
---|---|---|
5 | 5 |
use version; |
6 | 6 |
|
7 | 7 |
use SL::DBUtils qw(selectall_hashref_query selectfirst_hashref_query do_query selectall_ids); |
8 |
use SL::DB; |
|
8 | 9 |
|
9 | 10 |
use Rose::Object::MakeMethods::Generic ( |
10 | 11 |
'scalar --get_set_init' => [ qw(login namespace upgrade_callbacks current_version auto_store_back) ], |
... | ... | |
13 | 14 |
sub store { |
14 | 15 |
my ($self, $key, $value) = @_; |
15 | 16 |
|
16 |
my $tuple = $self->get_tuple($key); |
|
17 |
SL::DB->client->with_transaction(sub { |
|
18 |
my $tuple = $self->get_tuple($key); |
|
17 | 19 |
|
18 |
if ($tuple && $tuple->{id}) { |
|
19 |
$tuple->{value} = $value; |
|
20 |
$self->_update($tuple); |
|
21 |
} else { |
|
22 |
my $query = 'INSERT INTO user_preferences (login, namespace, version, key, value) VALUES (?, ?, ?, ?, ?)'; |
|
23 |
do_query($::form, $::form->get_standard_dbh, $query, $self->login, $self->namespace, $self->current_version, $key, $value); |
|
24 |
} |
|
20 |
if ($tuple && $tuple->{id}) { |
|
21 |
$tuple->{value} = $value; |
|
22 |
$self->_update($tuple); |
|
23 |
} else { |
|
24 |
my $query = 'INSERT INTO user_preferences (login, namespace, version, key, value) VALUES (?, ?, ?, ?, ?)'; |
|
25 |
do_query($::form, $::form->get_standard_dbh, $query, $self->login, $self->namespace, $self->current_version, $key, $value); |
|
26 |
} |
|
27 |
1; |
|
28 |
}) or do { die SL::DB->client->error }; |
|
25 | 29 |
} |
26 | 30 |
|
27 | 31 |
sub get { |
... | ... | |
35 | 39 |
sub get_tuple { |
36 | 40 |
my ($self, $key) = @_; |
37 | 41 |
|
38 |
my $tuple = selectfirst_hashref_query($::form, $::form->get_standard_dbh, <<"", $self->login, $self->namespace, $key); |
|
39 |
SELECT * FROM user_preferences WHERE login = ? AND namespace = ? AND key = ? |
|
42 |
my $tuple; |
|
40 | 43 |
|
41 |
if ($tuple && $tuple->{version} < $self->current_version) {
|
|
42 |
$self->_upgrade($tuple);
|
|
43 |
}
|
|
44 |
SL::DB->client->with_transaction(sub {
|
|
45 |
$tuple = selectfirst_hashref_query($::form, $::form->get_standard_dbh, <<"", $self->login, $self->namespace, $key);
|
|
46 |
SELECT * FROM user_preferences WHERE login = ? AND namespace = ? AND key = ?
|
|
44 | 47 |
|
45 |
if ($tuple && $tuple->{version} > $self->current_version) { |
|
46 |
die "Future version $tuple->{version} for user preference @{ $self->namespace }/$key. Expected @{ $self->current_version } or less."; |
|
47 |
} |
|
48 |
if ($tuple && $tuple->{version} < $self->current_version) { |
|
49 |
$self->_upgrade($tuple); |
|
50 |
} |
|
51 |
|
|
52 |
if ($tuple && $tuple->{version} > $self->current_version) { |
|
53 |
die "Future version $tuple->{version} for user preference @{ $self->namespace }/$key. Expected @{ $self->current_version } or less."; |
|
54 |
} |
|
55 |
1; |
|
56 |
}) or do { die SL::DB->client->error }; |
|
48 | 57 |
|
49 | 58 |
return $tuple; |
50 | 59 |
} |
... | ... | |
52 | 61 |
sub get_all { |
53 | 62 |
my ($self) = @_; |
54 | 63 |
|
55 |
my $data = selectall_hashref_query($::form, $::form->get_standard_dbh, <<"", $self->login, $self->namespace); |
|
56 |
SELECT * FROM user_preferences WHERE login = ? AND namespace = ? |
|
64 |
my $data; |
|
57 | 65 |
|
58 |
for my $tuple (@$data) { |
|
59 |
if ($tuple->{version} < $self->current_version) { |
|
60 |
$self->_upgrade($tuple); |
|
61 |
} |
|
66 |
SL::DB->client->with_transaction(sub { |
|
67 |
$data = selectall_hashref_query($::form, $::form->get_standard_dbh, <<"", $self->login, $self->namespace); |
|
68 |
SELECT * FROM user_preferences WHERE login = ? AND namespace = ? |
|
62 | 69 |
|
63 |
if ($tuple->{version} > $self->current_version) { |
|
64 |
die "Future version $tuple->{version} for user preference @{ $self->namespace }/$tuple->{key}. Expected @{ $self->current_version } or less."; |
|
70 |
for my $tuple (@$data) { |
|
71 |
if ($tuple->{version} < $self->current_version) { |
|
72 |
$self->_upgrade($tuple); |
|
73 |
} |
|
74 |
|
|
75 |
if ($tuple->{version} > $self->current_version) { |
|
76 |
die "Future version $tuple->{version} for user preference @{ $self->namespace }/$tuple->{key}. Expected @{ $self->current_version } or less."; |
|
77 |
} |
|
65 | 78 |
} |
66 |
} |
|
79 |
1; |
|
80 |
}) or do { die SL::DB->client->error }; |
|
67 | 81 |
|
68 | 82 |
return $data; |
69 | 83 |
} |
... | ... | |
82 | 96 |
|
83 | 97 |
die 'delete without key is not allowed, use delete_all instead' unless $key; |
84 | 98 |
|
85 |
my @keys = do_query($::form, $::form->get_standard_dbh, <<"", $self->login, $self->namespace, $key); |
|
86 |
DELETE FROM user_preferences WHERE login = ? AND namespace = ? AND key = ? |
|
87 |
|
|
99 |
SL::DB->client->with_transaction(sub { |
|
100 |
my $query = 'DELETE FROM user_preferences WHERE login = ? AND namespace = ? AND key = ?'; |
|
101 |
do_query($::form, $::form->get_standard_dbh, $query, $self->login, $self->namespace, $key); |
|
102 |
1; |
|
103 |
}) or do { die SL::DB->client->error }; |
|
88 | 104 |
} |
89 | 105 |
|
90 | 106 |
sub delete_all { |
91 | 107 |
my ($self, $key) = @_; |
92 | 108 |
|
93 |
my @keys = do_query($::form, $::form->get_standard_dbh, <<"", $self->login, $self->namespace); |
|
94 |
DELETE FROM user_preferences WHERE login = ? AND namespace = ? |
|
109 |
my @keys; |
|
95 | 110 |
|
111 |
SL::DB->client->with_transaction(sub { |
|
112 |
my $query = 'DELETE FROM user_preferences WHERE login = ? AND namespace = ?'; |
|
113 |
do_query($::form, $::form->get_standard_dbh, $query, $self->login, $self->namespace); |
|
114 |
1; |
|
115 |
}) or do { die SL::DB->client->error }; |
|
96 | 116 |
} |
97 | 117 |
|
98 | 118 |
### internal stuff |
Auch abrufbar als: Unified diff
UserPrefs: Transaktionen für alle