Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 451843cf

Von Sven Schöling vor etwa 15 Jahren hinzugefügt

  • ID 451843cf750b655eb2678de12c369af74bca9867
  • Vorgänger 90daea72
  • Nachfolger 5f783ffe

Löschen von RecordLinks

Unterschiede anzeigen:

SL/DBUtils.pm
9 9
             selectall_hashref_query selectall_array_query
10 10
             selectall_as_map
11 11
             prepare_execute_query prepare_query
12
             create_sort_spec does_table_exist);
12
             create_sort_spec does_table_exist
13
             add_token);
13 14

  
14 15
sub conv_i {
15 16
  my ($value, $default) = @_;
......
280 281
  return $result;
281 282
}
282 283

  
284
# add token to values.
285
# usage:
286
#  add_token(
287
#    \@where_tokens,
288
#    \@where_values,
289
#    col => 'id',
290
#    val => [ 23, 34, 17 ]
291
#    esc => \&conf_i
292
#  )
293
#  will append to the given arrays:
294
#   -> 'id IN (?, ?, ?)'
295
#   -> (conv_i(23), conv_i(34), conv_i(17))
296
#
297
#  features:
298
#   - don't care if one or multiple values are given. singlewill result in 'col = ?'
299
#   - pass escape routines
300
#   - expand for future method
301
#   - no need to type "push @where_tokens, 'id = ?'" over and over again
302
sub add_token {
303
  my $tokens = shift() || [];
304
  my $values = shift() || [];
305
  my %params = @_;
306
  my $col    = $params{col};
307
  my $val    = $params{val};
308
  my $method = $params{method} || '=';
309
  my $escape = $params{esc} || sub { $_ };
310

  
311
  $val = [ $val ] unless ref $val eq 'ARRAY';
312

  
313
  my %escapes = (
314
    id     => \&conv_i,
315
    date   => \&conv_date,
316
  );
317

  
318
  my %methods = (
319
    '=' => sub {
320
      my $col = shift;
321
      return scalar @_ >  1 ? sprintf '%s IN (%s)', $col, join ', ', ("?") x scalar @_
322
           : scalar @_ == 1 ? sprintf '%s = ?',     $col
323
           :                  undef;
324
    },
325
  );
326

  
327
  $method = $methods{$method} || $method;
328
  $escape = $escapes{$escape} || $escape;
329

  
330
  my $token = $method->($col, @{ $val });
331
  my @vals  = map { $escape->($_) } @{ $val };
332

  
333
  return unless $token;
334

  
335
  push @{ $tokens }, $token;
336
  push @{ $values }, @vals;
337

  
338
  return ($token, @vals);
339
}
283 340

  
284 341
1;
285 342

  
......
293 350
=head1 SYNOPSIS
294 351

  
295 352
  use DBUtils;
296
  
353

  
297 354
  conv_i($str, $default)
298 355
  conv_date($str)
299 356
  conv_dateq($str)
......
307 364

  
308 365
  my $all_results_ref       = selectall_hashref_query($form, $dbh, $query)
309 366
  my $first_result_hash_ref = selectfirst_hashref_query($form, $dbh, $query);
310
  
367

  
311 368
  my @first_result =  selectfirst_array_query($form, $dbh, $query);  # ==
312 369
  my @first_result =  selectrow_query($form, $dbh, $query);
313
  
370

  
314 371
  my %sort_spec = create_sort_spec(%params);
315 372

  
316 373
=head1 DESCRIPTION
......
329 386
  - Safe value binding. Although DBI is far from perfect in terms of binding, the rest of the bindings should happen here.
330 387
  - Error handling. Should a query fail, an error message will be generated here instead of in the backend code invoking DBUtils.
331 388

  
332
Note that binding is not perfect here either... 
333
  
389
Note that binding is not perfect here either...
390

  
334 391
=head2 QUOTING FUNCTIONS
335 392

  
336 393
=over 4
......
386 443

  
387 444
=item selectrow_query FORM,DBH,QUERY,ARRAY
388 445

  
389
Prepares and executes a query using DBUtils functions, retireves the first row from the database, and returns it as an arrayref of the first row. 
446
Prepares and executes a query using DBUtils functions, retireves the first row from the database, and returns it as an arrayref of the first row.
390 447

  
391 448
=item selectfirst_hashref_query FORM,DBH,QUERY,ARRAY
392 449

  
393
Prepares and executes a query using DBUtils functions, retireves the first row from the database, and returns it as a hashref of the first row. 
450
Prepares and executes a query using DBUtils functions, retireves the first row from the database, and returns it as a hashref of the first row.
394 451

  
395 452
=item selectall_hashref_query FORM,DBH,QUERY,ARRAY
396 453

  
......
484 541
=item A more complicated example, using dynamic binding values:
485 542

  
486 543
  my @values;
487
    
544

  
488 545
  if ($form->{language_values} ne "") {
489 546
    $query = qq|SELECT l.id, l.description, tr.translation, tr.longdescription
490 547
                  FROM language l
......
493 550
  } else {
494 551
    $query = qq|SELECT id, description FROM language|;
495 552
  }
496
  
553

  
497 554
  my $languages = selectall_hashref_query($form, $dbh, $query, @values);
498 555

  
499 556
=back
......
504 561

  
505 562
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
506 563
Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
507
 
564

  
508 565
=head1 DOCUMENTATION AUTHORS
509 566

  
510 567
Udo Spallek E<lt>udono@gmx.netE<gt>
......
526 583
You should have received a copy of the GNU General Public License
527 584
along with this program; if not, write to the Free Software
528 585
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
529
=cut    
586
=cut
SL/IC.pm
811 811
     lastcost     => ' ',
812 812
     factor       => 'pfac.',
813 813
     'SUM(ioi.qty)' => ' ',
814
     description => 'p.',
814 815
  );
815 816

  
816 817
  my %renamed_columns = (
......
818 819
    'SUM(ioi.qty)' => 'soldtotal',
819 820
  );
820 821

  
821
  my %joins_needed;
822

  
823 822
  if (($form->{searchitems} eq 'assembly') && $form->{l_lastcost}) {
824 823
    @simple_l_switches = grep { $_ ne 'lastcost' } @simple_l_switches;
825 824
  }
......
830 829
  my @where_tokens  = qw(1=1);
831 830
  my @group_tokens  = ();
832 831
  my @bind_vars     = ();
832
  my %joins_needed  = ();
833 833

  
834 834
  # special case transdate
835 835
  if (grep { $form->{$_} } qw(transdatefrom transdateto)) {
......
842 842
    }
843 843
  }
844 844

  
845
  my %simple_filter_table_prefix = (
846
     description  => 'p.',
847
  );
848

  
849 845
  foreach (@simple_filters, @makemodel_filters, @invoice_oi_filters) {
850 846
    next unless $form->{$_};
851 847
    $form->{"l_$_"} = '1'; # show the column
852
    push @where_tokens, "$simple_filter_table_prefix{$_}$_ ILIKE ?";
848
    push @where_tokens, "$table_prefix{$_}$_ ILIKE ?";
853 849
    push @bind_vars,    "%$form->{$_}%";
854 850
  }
855 851

  
SL/RecordLinks.pm
170 170
  return wantarray ? @{ $result } : $result;
171 171
}
172 172

  
173
sub delete {
174
  $main::lxdebug->enter_sub();
175

  
176
  my $self     = shift;
177
  my %params   = @_;
178

  
179
  Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
180

  
181
  my $myconfig   = \%main::myconfig;
182
  my $form       = $main::form;
183

  
184
  my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
185

  
186
  # content
187
  my (@where_tokens, @where_values);
188

  
189
  for my $col (qw(from_table from_id to_table to_id)) {
190
    add_token(\@where_tokens, \@where_values, col => $col, val => $params{$col}) if $params{$col};
191
  }
192

  
193
  my $where = "WHERE ". join ' AND ', map { "($_)" } @where_tokens if scalar @where_tokens;
194
  my $query = "DELETE FROM record_links $where";
195

  
196
  do_query($form, $dbh, $query, @where_values);
197

  
198
  $dbh->commit() unless ($params{dbh});
199

  
200
  $main::lxdebug->leave_sub();
201
}
202

  
173 203
1;

Auch abrufbar als: Unified diff