Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 743f51fc

Von Sven Schöling vor mehr als 17 Jahren hinzugefügt

  • ID 743f51fcaf0e84fd9a9543dae3247ad2b00d57d0
  • Vorgänger 43c22d1c
  • Nachfolger dba493ac

Mehr perldoc

Unterschiede anzeigen:

SL/DBUtils.pm
199 199

  
200 200
  use DBUtils;
201 201
  
202
  conv_i
203
  conv_date
204
  conv_dateq
205
  quote_db_date($str)
202
  conv_i($str, $default)
203
  conv_date($str)
204
  conv_dateq($str)
205
  quote_db_date($date)
206 206

  
207 207
  do_query($form, $dbh, $query)
208 208
  do_statement($form, $sth, $query)
......
218 218
  
219 219
    
220 220
=head1 DESCRIPTION
221

  
222
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.
223

  
224
Using DBUtils most database procedures can be reduced to defining the query, executing it, and retrieving the result. Let DBUtils handle the rest. Whenever there is a database operation not covered in DBUtils, add it here, rather than working around it in the backend code.
225

  
226
DBUtils relies heavily on two parameters which have to be passed to almost every function: $form and $dbh.
227
  - $form is used for error handling only. It can be omitted in theory, but should not.
228
  - $dbh is a handle to the databe, as returned by the DBI::connect routine. If you don't have an active connectiong, you can query $form->get_standard_dbh() to get a generic no_auto connection. Don't forget to commit in this case!
229

  
230

  
231
Every function here should accomplish the follwing things:
232
  - Easy debugging. Every handled query gets dumped via LXDebug, if specified there.
233
  - Safe value binding. Although DBI is far from perfect in terms of binding, the rest of the bindings should happen here.
234
  - Error handling. Should a query fail, an error message will be generated here instead of in the backend code invoking DBUtils.
235

  
236
Note that binding is not perfect here either... 
221 237
  
222
=head1 FUNCTIONS
223
  
238
=head2 QUOTING FUNCTIONS
239

  
224 240
=over 4
225
  
226
=item conv_i
227 241

  
228
=item conv_date
242
=item conv_i STR
229 243

  
230
=item conv_dateq
244
=item conv_i STR,DEFAULT
231 245

  
232
=item quote_db_date($str)
246
Converts STR to an integer. If STR is empty, returns DEFAULT. If no DEFAULT is given, returns undef.
233 247

  
234
=item do_query($form, $dbh, $query)
248
=item conv_date STR
235 249

  
236
=item do_statement($form, $sth, $query)
250
Converts STR to a date string. If STR is emptry, returns undef.
237 251

  
238
=item dump_query($level, $msg, $query)
252
=item conv_dateq STR
239 253

  
240
=item prepare_execute_query($form, $dbh, $query)
254
Database version of conv_date. Quotes STR before returning. Returns 'NULL' if STR is empty.
241 255

  
242
=item selectall_hashref_query($form, $dbh, $query)
256
=item quote_db_date STR
243 257

  
244
=item selectfirst_hashref_query($form, $dbh, $query);
258
Treats STR as a database date, quoting it. If STR equals current_date returns an escaped version which is treated as the current date by Postgres.
259
Returns 'NULL' if STR is empty.
245 260

  
246
=item selectfirst_array_query($form, $dbh, $query);  # ==
261
=back
262

  
263
=head2 QUERY FUNCTIONS
264

  
265
=over 4
266

  
267
=item do_query FORM,DBH,QUERY,ARRAY
268

  
269
Uses DBI::do to execute QUERY on DBH using ARRAY for binding values. FORM is only needed for error handling, but should always be passed nevertheless. Use this for insertions or updates that don't need to be prepared.
270

  
271
=item do_statement FORM,STH,QUERY,ARRAY
272

  
273
Uses DBI::execute to execute QUERY on DBH using ARRAY for binding values. As with do_query, FORM is only used for error handling. If you are unsure what to use, refer to the documentation of DBI::do and DBI::execute.
274

  
275
=item prepare_execute_query FORM,DBH,QUERY,ARRAY
276

  
277
Prepares and executes QUERY on DBH using DBI::prepare and DBI::execute. ARRAY is passed as binding values to execute.
247 278

  
248
=item selectrow_query($form, $dbh, $query);
249
  
250 279
=back
280

  
281
=head2 RETRIEVAL FUNCTIONS
282

  
283
=over 4
284

  
285
=item selectfirst_array_query FORM,DBH,QUERY,ARRAY
286

  
287
=item selectrow_query FORM,DBH,QUERY,ARRAY
288

  
289
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. 
290

  
291
=item selectfirst_hashref_query FORM,DBH,QUERY,ARRAY
292

  
293
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. 
294

  
295
=item selectall_hashref_query FORM,DBH,QUERY,ARRAY
296

  
297
Prepares and executes a query using DBUtils functions, retireves all data from the database, and returns it in hashref mode. This is slightly confusing, as the data structure will actually be a reference to an array, containing hashrefs for each row.
298

  
299
=back
300

  
301
=head2 DEBUG FUNCTIONS
302

  
303
=over 4
304

  
305
=item dump_query LEVEL,MSG,QUERY,ARRAY
306

  
307
Dumps a query using LXDebug->message, using LEVEL for the debug-level of LXDebug. If MSG is given, it preceeds the QUERY dump in the logfiles. ARRAY is used to interpolate the '?' placeholders in QUERY, the resulting QUERY can be copy-pasted into a database frontend for debugging. Note that this method is also automatically called by each of the other QUERY FUNCTIONS, so there is in general little need to invoke it manually.
308

  
309
=back
310

  
311
=head1 EXAMPLES
312

  
313
=over 4
314

  
315
=item Retrieving a whole table:
316

  
317
  $query = qq|SELECT id, pricegroup FROM pricegroup|;
318
  $form->{PRICEGROUPS} = selectall_hashref_query($form, $dbh, $query);
319

  
320
=item Retrieving a single value:
321

  
322
  $query = qq|SELECT nextval('glid')|;
323
  ($new_id) = selectrow_query($form, $dbh, $query);
324

  
325
=item Using binding values:
326

  
327
  $query = qq|UPDATE ar SET paid = amount + paid, storno = 't' WHERE id = ?|;
328
  do_query($form, $dbh, $query, $id);
329

  
330
=item A more complicated example, using dynamic binding values:
331

  
332
  my @values;
333
    
334
  if ($form->{language_values} ne "") {
335
    $query = qq|SELECT l.id, l.description, tr.translation, tr.longdescription
336
                  FROM language l
337
                  LEFT OUTER JOIN translation tr ON (tr.language_id = l.id) AND (tr.parts_id = ?)|;
338
    @values = (conv_i($form->{id}));
339
  } else {
340
    $query = qq|SELECT id, description FROM language|;
341
  }
251 342
  
252
=head1 EXAMPLE
343
  my $languages = selectall_hashref_query($form, $dbh, $query, @values);
344

  
345
=back
253 346

  
254 347
=head1 SEE ALSO
255 348

  
256 349
=head1 MODULE AUTHORS
257 350

  
258
Moritz Bunkus E<lt>m.bunkus@linet-services.de<gt>
259
Sven Schoeling E<lt>s.schoeling@linet-services.de<gt>
351
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
352
Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
260 353
 
261 354
=head1 DOCUMENTATION AUTHORS
262 355

  
263
Udo Spallek  E<lt>udono@gmx.netE<gt>
356
Udo Spallek E<lt>udono@gmx.netE<gt>
357
Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
264 358

  
265 359
=head1 COPYRIGHT AND LICENSE
266 360

  

Auch abrufbar als: Unified diff