... | ... |
@@ -36,7 +36,7 @@ sub AUTOLOAD { |
36 | 36 |
} |
37 | 37 |
|
38 | 38 |
my @methods = qw/insert insert_at update update_at update_all |
39 |
- delete delete_at delete_all select select_at count update_or_insert/; |
|
39 |
+ delete delete_at delete_all select select_at count/; |
|
40 | 40 |
for my $method (@methods) { |
41 | 41 |
|
42 | 42 |
my $code = |
... | ... |
@@ -49,12 +49,8 @@ for my $method (@methods) { |
49 | 49 |
my @attrs = qw/table type primary_key bind_type/; |
50 | 50 |
my @insert_attrs = qw/created_at updated_at/; |
51 | 51 |
my @update_attrs = qw/updated_at/; |
52 |
- my @update_or_insert_attrs = qw/created_at updated_at/; |
|
53 | 52 |
my @select_attrs = qw/join/; |
54 | 53 |
if ($method eq 'insert') { push @attrs, @insert_attrs } |
55 |
- elsif ($method eq 'update_or_insert') { |
|
56 |
- push @attrs, @update_or_insert_attrs; |
|
57 |
- } |
|
58 | 54 |
elsif ($method eq 'update') { push @attrs, @update_attrs } |
59 | 55 |
elsif (index($method, 'select') != -1) { push @attrs, @select_attrs } |
60 | 56 |
|
... | ... |
@@ -70,6 +66,20 @@ for my $method (@methods) { |
70 | 66 |
croak $code if $@; |
71 | 67 |
} |
72 | 68 |
|
69 |
+sub update_or_insert { |
|
70 |
+ my ($self, $param, %opt) = @_; |
|
71 |
+ |
|
72 |
+ croak "update_or_insert method need primary_key and id option " |
|
73 |
+ unless (defined $opt{id} || defined $self->{id}) |
|
74 |
+ && (defined $opt{primary_key} || defined $self->{primary_key}); |
|
75 |
+ |
|
76 |
+ my $statement_opt = $opt{option} || {}; |
|
77 |
+ my $row = $self->select(%opt, %{$statement_opt->{select} || {}})->one; |
|
78 |
+ return $row ? $self->update($param, %opt, %{$statement_opt->{update} || {}}) |
|
79 |
+ : $self->insert($param, %opt, %{$statement_opt->{insert} || {}}); |
|
80 |
+} |
|
81 |
+ |
|
82 |
+ |
|
73 | 83 |
sub execute { |
74 | 84 |
my $self = shift; |
75 | 85 |
return $self->dbi->execute( |
... | ... |
@@ -32,6 +32,7 @@ my $dbname; |
32 | 32 |
my $row; |
33 | 33 |
my $rows; |
34 | 34 |
my $result; |
35 |
+my $model; |
|
35 | 36 |
|
36 | 37 |
test 'connect'; |
37 | 38 |
eval { |
... | ... |
@@ -78,6 +79,34 @@ $dbi->update_or_insert( |
78 | 79 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
79 | 80 |
is_deeply($row, {key1 => 1, key2 => 3}, "basic"); |
80 | 81 |
|
82 |
+$dbi->delete_all(table => 'table1'); |
|
83 |
+$model = $dbi->create_model( |
|
84 |
+ table => 'table1', |
|
85 |
+ primary_key => 'key1', |
|
86 |
+); |
|
87 |
+$model->update_or_insert( |
|
88 |
+ {key2 => 2}, |
|
89 |
+ id => 1, |
|
90 |
+ option => { |
|
91 |
+ select => {append => 'for update'}, |
|
92 |
+ insert => {append => ' #'}, |
|
93 |
+ update => {append => ' #'} |
|
94 |
+ } |
|
95 |
+); |
|
96 |
+$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
|
97 |
+is_deeply($row, {key1 => 1, key2 => 2}, "basic"); |
|
98 |
+$model->update_or_insert( |
|
99 |
+ {key2 => 3}, |
|
100 |
+ id => 1, |
|
101 |
+ option => { |
|
102 |
+ select => {append => 'for update'}, |
|
103 |
+ insert => {append => ' #'}, |
|
104 |
+ update => {append => ' #'} |
|
105 |
+ } |
|
106 |
+); |
|
107 |
+$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
|
108 |
+is_deeply($row, {key1 => 1, key2 => 3}, "basic"); |
|
109 |
+ |
|
81 | 110 |
# Test memory leaks |
82 | 111 |
for (1 .. 200) { |
83 | 112 |
$dbi = DBIx::Custom->connect( |