DatabaseDriver.pm 11.9 KB
Newer Older
1
# -*- indent-tabs-mode: nil; -*-
2
# vim:ft=perl:et:sw=4
3
4
# $Id$

5
# Sympa - SYsteme de Multi-Postage Automatique
6
7
8
9
#
# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
10
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER
11
12
# Copyright 2018, 2021 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
13
# <https://github.com/sympa-community/sympa.git>.
14
15
16
17
18
19
20
21
22
23
24
25
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
26
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
27

28
package Sympa::DatabaseDriver;
29
30

use strict;
31
use warnings;
32

33
use base qw(Sympa::Database);
34

35
use constant required_modules    => [];
36
use constant required_parameters => [qw(db_name db_user)];
37
use constant optional_modules    => [];
38
use constant optional_parameters =>
39
    [qw(db_host db_port db_passwd db_options db_env)];
40

41
42
43
44
sub translate_type {
    return $_[1];
}

45
46
47
48
49
50
# For DOUBLE type.
sub AS_DOUBLE {
    return $_[1] if scalar @_ > 1;
    return ();
}

sikeda's avatar
sikeda committed
51
52
53
54
55
56
57
# For BLOB types.
sub AS_BLOB {
    return $_[1] if scalar @_ > 1;
    return ();
}

1;
58
59
60
61
62
63
__END__

=encoding utf-8

=head1 NAME

64
Sympa::DatabaseDriver - Base class of database drivers for Sympa
65
66
67

=head1 SYNOPSIS

68
69
  package Sympa::DatabaseDriver::FOO;
  use base qw(Sympa::DatabaseDriver);
70
71
72

=head1 DESCRIPTION

73
L<Sympa::DatabaseDriver> is the base class of driver classes for
74
75
Sympa Database Manager (SDM).

sikeda's avatar
sikeda committed
76
=head2 Instance methods subclasses should implement
77
78
79

=over

80
81
82
83
84
85
86
87
88
89
=item required_modules ( )

I<Overridable>.
Returns an arrayref including package name(s) this driver requires.
By default, no packages are required.

=item required_parameters ( )

I<Overridable>.
Returns an arrayref including names of required (not optional) parameters.
90
91
92
93
94
By default, returns C<['db_name', 'db_user']>.

I<Note>:
On Sympa prior to 6.2.37b.2, it by default returned
C<['db_host', 'db_name', 'db_user']>.
95

96
97
98
99
100
101
102
103
=item optional_modules ( )

I<Overridable>.
Returns an arrayref including all name(s) of optional packages.
By default, there are no optional packages.

This method was introduced by Sympa 6.2.4.

104
105
106
107
=item optional_parameters ( )

I<Overridable>.
Returns an arrayref including all names of optional parameters.
sikeda's avatar
sikeda committed
108
By default, returns C<'db_passwd'>, C<'db_port'>, C<'db_options'> and so on.
109

110
111
=item build_connect_string ( )

sikeda's avatar
sikeda committed
112
I<Mandatory for SQL driver>.
113
114
115
116
117
118
119
120
121
122
Builds the string to be used by the DBI to connect to the database.

Parameter:

None.

Returns:

String representing data source name (DSN).

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
=item connect ( )

I<Overridable>.
Connects to database calling L</_connect>() and sets database handle.

Parameter:

None.

Returns:

True value or, if connection failed, false value.

=item _connect ( )

I<Overridable>.
Connects to database and returns native database handle.

The default implementation is for L<DBI> database handle.

143
144
145
=item get_substring_clause ( { source_field => $source_field,
separator => $separator, substring_length => $substring_length } )

146
This method was deprecated by Sympa 6.2.4.
147
148
149
150
151
152
153

=item get_limit_clause ( )

This method was deprecated.

=item get_formatted_date ( { mode => $mode, target => $target } )

154
155
B<Deprecated> as of Sympa 6.2.25b.3.

sikeda's avatar
sikeda committed
156
I<Mandatory for SQL driver>.
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
Returns a character string corresponding to the expression to use in a query
involving a date.

Parameters:

=over

=item $mode

authorized values:

=over

=item C<'write'>

The sub returns the expression to use in 'INSERT' or 'UPDATE' queries.

=item C<'read'>

The sub returns the expression to use in 'SELECT' queries.

=back

=item $target

The name of the field or the value to be used in the query.

=back

Returns:

The formatted date or C<undef> if the date format mode is unknown.

=item is_autoinc ( { table => $table, field => $field } )

I<Required to probe database structure>.
Checks whether a field is an auto-increment field or not.

Parameters:

=over

=item $field

The name of the field to test

=item $table

The name of the table to add

=back

Returns:

True if the field is an auto-increment field, false otherwise

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
=item is_sufficient_field_type ( $required, $actual )

I<Overridable>, I<only for SQL driver>.
Checks if database field type is sufficient.

Parameters:

=over

=item $required

Required field type.

=item $actual

Actual field type.

=back

Returns:

The true value if actual field type is appropriate AND size is equal to or
greater than required size.

This method was added on Sympa 6.2.67b.1.

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
=item set_autoinc ( { table => $table, field => $field } )

I<Required to update database structure>.
Defines the field as an auto-increment field.

Parameters:

=over

=item $field

The name of the field to set.

=item $table

The name of the table to add.

=back

Returns:

C<1> if the auto-increment could be set, C<undef> otherwise.

=item get_tables ( )

I<Required to probe database structure>.
Returns the list of the tables in the database.

Parameters:

None.

Returns:

sikeda's avatar
sikeda committed
273
A ref to an array containing the list of the table names in the
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
database, C<undef> if something went wrong.

=item add_table ( { table => $table } )

I<Required to update database structure>.
Adds a table to the database.

Parameter:

=over

=item $table

The name of the table to add

=back

Returns:

A character string report of the operation done or C<undef> if something
went wrong.

=item get_fields ( { table => $table } )

I<Required to probe database structure>.
Returns a ref to an hash containing the description of the fields in a table
from the database.

Parameters:

=over

=item $table

The name of the table whose fields are requested.

=back

Returns:

A hash in which the keys are the field names and the values are the field type.

Returns C<undef> if something went wrong.

=item update_field ( { table => $table, field => $field, type => $type, ... } )

I<Required to update database structure>.
Changes the type of a field in a table from the database.

Parameters:

=over

=item $field

The name of the field to update.

=item $table

The name of the table whose fields will be updated.

=item $type

The type of the field to add.

=item $notnull

Specifies that the field must not be null

=back

Returns:

A character string report of the operation done or C<undef> if something
went wrong.

=item add_field ( { table => $table, field => $field, type => $type, ... } )

I<Required to update database structure>.
Adds a field in a table from the database.

Parameters:

=over

=item $field

The name of the field to add.

=item $table

The name of the table where the field will be added.

=item $type

The type of the field to add.

=item $notnull

Specifies that the field must not be null.

=item $autoinc

Specifies that the field must be auto-incremental.

=item $primary

Specifies that the field is a key.

=back

Returns:

A character string report of the operation done or C<undef> if something
went wrong.

=item delete_field ( { table => $table, field => $field } )

I<Required to update database structure>.
Deletes a field from a table in the database.

Parameters:

=over

=item $field

The name of the field to delete

=item $table

The name of the table where the field will be deleted.

=back

Returns:

A character string report of the operation done or C<undef> if something
went wrong.

=item get_primary_key ( { table => $table } )

I<Required to probe database structure>.
Returns the list fields being part of a table's primary key.

=over

=item $table

The name of the table for which the primary keys are requested.

=back

Returns:

A ref to a hash in which each key is the name of a primary key or C<undef>
if something went wrong.

=item unset_primary_key ( { table => $table } )

I<Required to update database structure>.
Drops the primary key of a table.

Parameter:

=over

=item $table

The name of the table for which the primary keys must be
dropped.

=back

Returns:

A character string report of the operation done or C<undef> if something
went wrong.

=item set_primary_key ( { table => $table, fields => $fields } )

I<Required to update database structure>.
Sets the primary key of a table.

Parameters:

=over

=item $table

The name of the table for which the primary keys must be
defined.

=item $fields

A ref to an array containing the names of the fields used
in the key.

=back

Returns:

A character string report of the operation done or C<undef> if something
went wrong.

=item get_indexes ( { table => $table } )

I<Required to probe database structure>.
Returns a ref to a hash in which each key is the name of an index.

Parameter:

=over

=item $table

The name of the table for which the indexes are requested.

=back

Returns:

A ref to a hash in which each key is the name of an index.  These key
point to a second level hash in which each key is the name of the field
indexed.  Returns C<undef> if something went wrong.

=item unset_index ( { table => $table, index => $index } )

I<Required to update database structure>.
Drops an index of a table.

Parameters:

=over

=item $table

The name of the table for which the index must be dropped.

=item $index

The name of the index to be dropped.

=back

Returns:

A character string report of the operation done or C<undef> if something
went wrong.

=item set_index ( { table => $table, index_name => $index_name,
fields => $fields } )

I<Required to update database structure>.
Sets an index in a table.

Parameters:

=over

=item $table

The name of the table for which the index must be defined.

=item $fields

A ref to an array containing the names of the fields used
in the index.

=item $index_name

The name of the index to be defined.

=back

Returns:

A character string report of the operation done or C<undef> if something
went wrong.

554
555
556
557
558
559
560
=item translate_type ( $generic_type )

I<Required to probe and update database structure>.
Get native field type corresponds to generic type.
The generic type is based on MySQL:
See L<Sympa::DatabaseDescription/full_db_struct>.

sikeda's avatar
sikeda committed
561
=back
sikeda's avatar
sikeda committed
562

sikeda's avatar
sikeda committed
563
564
Subclasses of L<Sympa::DatabaseDriver> class also can override methods
provided by L<Sympa::Database> class:
sikeda's avatar
sikeda committed
565

sikeda's avatar
sikeda committed
566
=over
sikeda's avatar
sikeda committed
567

568
569
570
571
572
573
574
575
=item begin ( )

I<Overridable>, I<only for SQL driver>.

=item commit ( )

I<Overridable>, I<only for SQL driver>.

sikeda's avatar
sikeda committed
576
=item do_operation ( $operation, $parameters, ...)
sikeda's avatar
sikeda committed
577

sikeda's avatar
sikeda committed
578
I<Overridable>, I<only for LDAP driver>.
sikeda's avatar
sikeda committed
579

580
581
=item do_query ( $query, $parameters, ... )

sikeda's avatar
sikeda committed
582
I<Overridable>, I<only for SQL driver>.
583
584
585

=item do_prepared_query ( $query, $parameters, ... )

sikeda's avatar
sikeda committed
586
I<Overridable>, I<only for SQL driver>.
587

588
589
590
591
=item rollback ( )

I<Overridable>, I<only for SQL driver>.

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
=item AS_DOUBLE ( $value )

I<Overridable>.
Helper functions to return the DOUBLE binding type and value used by
L</do_prepared_query>().
Overridden by inherited classes.

Parameter:

=over

=item $value

=back

Parameter value

Returns:

One of:

=over

=item *

An array C<( { sql_type =E<gt> SQL_type }, value )>.

=item *

Single value (i.e. an array with single item), if special
treatment won't be needed.

=item *

Empty array C<()> if arguments were not given.

=back

=item AS_BLOB ( $value )

I<Overridable>.
Helper functions to return the BLOB (binary large object) binding type and
value used by L</do_prepared_query>().
Overridden by inherited classes.

See L</AS_DOUBLE> for more details.

=back

641
642
643
644
645
646
647
648
649
650
651
652
=head2 Utility method

=over

=item __dbh ( )

I<Instance method>, I<protected>.
Returns native database handle which L<_connect>() returned.
This may be used at inside of each driver class.

=back

653
654
=head1 SEE ALSO

sikeda's avatar
sikeda committed
655
L<Sympa::Database>, L<Sympa::DatabaseManager>.
656
657
658

=head1 HISTORY

sikeda's avatar
sikeda committed
659
Sympa Database Manager (SDM) appeared on Sympa 6.2.
660
661

=cut