Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision d3f20faa

Von Moritz Bunkus vor mehr als 15 Jahren hinzugefügt

  • ID d3f20faab4293434dccf854f39cbe1691960f0dd
  • Vorgänger 6078d376
  • Nachfolger e9135fb6

Eine Funktion implementiert, die SQL-Code für Sortierbedingungen unter Berücksichtigung von Standardwerten, gültigen Spaltennamen und Benutzereingaben erstellt.

Unterschiede anzeigen:

SL/DBUtils.pm
8 8
             selectfirst_hashref_query selectfirst_array_query
9 9
             selectall_hashref_query selectall_array_query
10 10
             selectall_as_map
11
             prepare_execute_query prepare_query);
11
             prepare_execute_query prepare_query
12
             create_sort_spec);
12 13

  
13 14
sub conv_i {
14 15
  my ($value, $default) = @_;
......
220 221
  return %hash;
221 222
}
222 223

  
224
sub create_sort_spec {
225
  $main::lxdebug->enter_sub(2);
226

  
227
  my %params = @_;
228

  
229
  # Safety check:
230
  $params{defs}    || die;
231
  $params{default} || die;
232

  
233
  # The definition of valid columns to sort by.
234
  my $defs        = $params{defs};
235

  
236
  # The column name to sort by. Use the default column name if none was given.
237
  my %result      = ( 'column' => $params{column} || $params{default} );
238

  
239
  # Overwrite the column name with the default column name if the other one is not valid.
240
  $result{column} = $params{default} unless ($defs->{ $result{column} });
241

  
242
  # The sort direction. true means 'sort ascending', false means 'sort descending'.
243
  $result{dir}    = defined $params{dir}         ? $params{dir}
244
                  : defined $params{default_dir} ? $params{default_dir}
245
                  :                                1;
246
  $result{dir}    = $result{dir} ?     1 :      0;
247
  my $asc_desc    = $result{dir} ? 'ASC' : 'DESC';
248

  
249
  # Create the SQL code.
250
  my $cols        = $defs->{ $result{column} };
251
  $result{sql}    = join ', ', map { "${_} ${asc_desc}" } @{ ref $cols eq 'ARRAY' ? $cols : [ $cols ] };
252

  
253
  $main::lxdebug->leave_sub(2);
254

  
255
  return %result;
256
}
257

  
223 258
1;
224 259

  
225 260

  
......
250 285
  my @first_result =  selectfirst_array_query($form, $dbh, $query);  # ==
251 286
  my @first_result =  selectrow_query($form, $dbh, $query);
252 287
  
253
    
288
  my %sort_spec = create_sort_spec(%params);
289

  
254 290
=head1 DESCRIPTION
255 291

  
256 292
DBUtils is the attempt to reduce the amount of overhead it takes to retrieve information from the database in Lx-Office. Previously it would take about 15 lines of code just to get one single integer out of the database, including failure procedures and importing the necessary packages. Debugging would take even more.
......
336 372

  
337 373
=back
338 374

  
375
=head2 UTILITY FUNCTIONS
376

  
377
=over 4
378

  
379
=item create_sort_spec
380

  
381
  params:
382
    defs        => { },         # mandatory
383
    default     => 'name',      # mandatory
384
    column      => 'name',
385
    default_dir => 0|1,
386
    dir         => 0|1,
387

  
388
  returns hash:
389
    column      => 'name',
390
    dir         => 0|1,
391
    sql         => 'SQL code',
392

  
393
This function simplifies the creation of SQL code for sorting
394
columns. It uses a hashref of valid column names, the column name and
395
direction requested by the user, the application defaults for the
396
column name and the direction and returns the actual column name,
397
direction and SQL code that can be used directly in a query.
398

  
399
The parameter 'defs' is a hash reference. The keys are the column
400
names as they may come from the application. The values are either
401
scalars with SQL code or array references of SQL code. Example:
402

  
403
'defs' => { 'customername' => 'lower(customer.name)',
404
            'address'      => [ 'lower(customer.city)', 'lower(customer.street)' ], }
405

  
406
'default' is the default column name to sort by. It must be a key of
407
'defs' and should not be come from user input.
408

  
409
The 'column' parameter is the column name as requested by the
410
application (e.g. if the user clicked on a column header in a
411
report). If it is invalid then the 'default' parameter will be used
412
instead.
413

  
414
'default_dir' is the default sort direction. A true value means 'sort
415
ascending', a false one 'sort descending'. 'default_dir' defaults to
416
'1' if undefined.
417

  
418
The 'dir' parameter is the sort direction as requested by the
419
application (e.g. if the user clicked on a column header in a
420
report). If it is undefined then the 'default_dir' parameter will be
421
used instead.
422

  
423
=back
424

  
339 425
=head2 DEBUG FUNCTIONS
340 426

  
341 427
=over 4

Auch abrufbar als: Unified diff