[r176]: trunk / cgi-bin / yabb3 / YaBB3 / DataSource / File.pm Maximize Restore History

Download this file

File.pm    586 lines (447 with data), 15.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
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
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
273
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
###############################################################################
# YaBB3/DataSource/File.pm
# YaBB3::DataSource::File
# YaBB3::DataSource::File::st
# YaBB3::DataSource::File::Statement
# YaBB3::DataSource::File::Table
###############################################################################
# YaBB: Yet another Bulletin Board
# Open-Source Community Software for Webmasters
# Version: YaBB 2.4
# Packaged: April 12, 2009
# Distributed by: http://www.yabbforum.com
# ===========================================================================
# Copyright (c) 2000-2009 YaBB (www.yabbforum.com) - All Rights Reserved.
# Software by: The YaBB Development Team
# with assistance from the YaBB community.
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com
# Your source for web hosting, web design, and domains.
###############################################################################
#
# $Id$
#
# Beware, ye who enter. There be dragons here.
use strict;
use SQL::Parser;
use YaBB3::Paths qw( $vardir );
$YaBB3::Paths::DatabaseDir = "$vardir/database";
# check whether flock works, technique comes from DBD::File
$YaBB3::DataSource::File::can_lock = eval { flock STDOUT, 0; 1 };
###############################################################################
# I used the doc SQL::Statement::Embed and the code for DBD::File as
# references when building this to make sure I don't forget something
# important. This is kind of an important module, and screwing it up would be
# bad.
###############################################################################
package YaBB3::DataSource::File;
use strict;
# only need one parser object, ever
my $parser = undef;
(our $VERSION = '$Revision$') =~ s~^\$ R e v i s i o n: \s (.*) \s \$$~$1~x;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
die "YaBB3::DataSource object not properly initialized: odd number of args"
if @_ % 2 == 1;
my %args = @_;
die "Database must be specified" unless defined $args{database};
$self->{config} = \%args;
if (not defined $parser) {
$parser = SQL::Parser->new();
$parser->{RaiseError} = 1;
$parser->{PrintError} = 0;
$parser->feature('valid_data_types', 'TEXT', 1);
$parser->feature('valid_data_types', 'BLOB', 1);
}
# subclasses should override initialize
$self->initialize(\%args);
return $self;
}
sub initialize { }
# do_query( $sql, [ bind_values, ... ])
sub do_query {
my ($self, $sql, $bind) = @_;
my $sth = $self->prepare( $sql );
$sth->execute( @$bind );
return $sth;
}
# prepare( $sql )
sub prepare {
my ($self, $sql) = @_;
# add a couple things that SQL::Parser/Statement don't seem to support
$sql =~ s/(--.*)$/$2/mg; # /m makes $ match any EOL
my $stmt = YaBB3::DataSource::File::Statement->new($sql, $parser);
my $sth = {
database => $self->{config}{database},
stmt => $stmt,
params => [],
NUM_OF_PARAMS => scalar( $stmt->params() ),
};
bless $sth, 'YaBB3::DataSource::File::st';
return $sth;
}
package YaBB3::DataSource::File::st;
use strict;
sub execute {
my $sth = shift;
my $params = [ @_ ];
$sth->finish;
my $stmt = $sth->{stmt};
# according to DBD::File, there is a bug in SQL::Statement if these are
# ->params() is called more than once....
if (not $sth->{params_checked}) {
$sth->{params_checked} = 1;
my $required = $stmt->params();
my $given = @$params;
if ($required != $given) {
die "$given parameters passed when $required required.";
}
}
return $stmt->execute($sth, $params) || "0E0";
}
sub fetch {
my $sth = shift;
my $data = $sth->{stmt}->{data};
if (!$data or ref $data ne "ARRAY") {
die "->fetch() called without ->execute() or with non-SELECT query.";
}
my $results = shift @$data;
if (not $results) {
$sth->finish;
return;
}
return $results;
}
sub finish {
delete $_[0]->{stmt}->{data};
return 1;
}
package YaBB3::DataSource::File::Statement;
use strict;
use base 'SQL::Statement';
use Fcntl qw( :flock );
sub get_file {
my ($self, $data, $table_name) = @_;
die "No table given" if not defined $table_name;
#name can be quoted
$table_name =~ s/^\"//;
$table_name =~ s/\"$//;
die "No table given" if $table_name eq "";
return "$YaBB3::Paths::DatabaseDir/$data->{database}/$table_name";
}
# instead of calling YaBB3::DataSource::File::Table->new, we call this one
sub open_table {
my ($self, $data, $table_name, $create_table, $lock_mode) = @_;
my $file_base = $self->get_file($data, $table_name);
my $fh;
if ($create_table) {
if( -e $file_base ) {
die "'$table_name' already exists.";
}
open $fh, '+>>', "$file_base.tbl"
or die "Cannot create $file_base.tbl: $!";
seek $fh, 0, 0
or die "Seek Error: $!";
}
else {
open $fh, ($lock_mode ? "+<" : "<"), "$file_base.tbl"
or die "Error opening $file_base.tbl: $!";
}
die "Could not open table: $table_name" if not $fh;
binmode $fh;
if ($YaBB3::DataSource::File::can_lock) {
if ($lock_mode) {
flock $fh, LOCK_EX
or die "Can't lock $file_base.tbl: $!";
}
else {
flock $fh, LOCK_SH
or die "Can't lock $file_base.tbl: $!";
}
}
my $table = {
file_base => $file_base,
fh => $fh,
col_nums => {}, # { n1 => 0, n2 => 1, ... }
col_names => [], # ['n1', 'n2', ...]
first_row_pos => tell($fh),
};
bless $table, 'YaBB3::DataSource::File::Table';
$table->_get_cols if not $create_table;
return $table;
}
###############################################################################
package YaBB3::DataSource::File::Table;
use strict;
use Fcntl qw( :flock );
sub col_names { return $_[0]->{col_names}; }
sub col_nums { return $_[0]->{col_nums}; }
sub column_num {
return $_[0]->{col_nums}->{uc($_[1])};
}
sub _get_cols {
my ($self) = @_;
my ($col_names, $col_types, $col_nums, $pk_name);
open my $col_fh, '<', "$self->{file_base}.cols"
or die "Could not read columns $self->{file_base}.cols: $!";
flock $col_fh, LOCK_SH
or die "Can't lock $self->{file_base}.cols: $!";
my @lines = <$col_fh>;
close $col_fh;
for (@lines) { chomp; }
@$col_names = split /\|/, uc($lines[0]);
@$col_types = split /\|/, uc($lines[1]);
$pk_name = uc($lines[2]);
my $num_cols = scalar @$col_names;
for (my $i = 0; $i < $num_cols; $i++) {
$col_nums->{$col_names->[$i]} = $i;
}
$self->{col_nums} = $col_nums;
$self->{col_names} = $col_names;
$self->{col_datatypes} = $col_types;
$self->{primary_key} = $pk_name;
return;
}
#TODO
sub _check_pk {
}
=oops, i got auto_increment and primary_key confused
sub _get_next_pk {
my ($self) = @_;
open my $col_fh, '+<', "$self->{file_base}.cols"
or die "Could not read columns $self->{file_base}.cols: $!";
flock $col_fh, LOCK_EX
or die "Can't lock $self->{file_base}.cols: $!";
my @lines = <$col_fh>;
if (not defined $lines[3]) { $lines[3] = 0; }
chomp $lines[3];
$lines[3]++;
my $pk_val = $lines[3];
$lines[3] .= "\n";
# rewrite the file
seek $col_fh, 0, 0
or die "Seek error for $self->{file_base}.tbl: $!";
truncate $col_fh, 0
or die "Truncate error for $self->{file_base}.tbl: $!";
print $col_fh @lines;
close $col_fh;
return $pk_val;
}
sub _update_next_pk {
my ($self, $pk_req) = @_;
open my $col_fh, '+<', "$self->{file_base}.cols"
or die "Could not read columns $self->{file_base}.cols: $!";
flock $col_fh, LOCK_EX
or die "Can't lock $self->{file_base}.cols: $!";
my @lines = <$col_fh>;
chomp $lines[3];
my $pk_val = $lines[3];
if ($pk_req > $pk_val) {
$lines[3] = "$pk_req\n";
# rewrite the file
seek $col_fh, 0, 0
or die "Seek error for $self->{file_base}.tbl: $!";
truncate $col_fh, 0
or die "Truncate error for $self->{file_base}.tbl: $!";
print $col_fh @lines;
}
close $col_fh;
return;
}
=cut
#TODO
#sub delete_one_row {
# my ($self, $data, $fields) = @_;
#}
#sub update_one_row {
# my ($self, $data, $new_fields) = @_;
#}
#sub update_specific_row {
# my ($self, $data, $new_fields, $orig_fields) = @_;
#}
# not sure how this one works yet
#sub fetch_one_row { }
# not sure what this one does
#sub display_name { }
# corresponds to a SQL command to drop the table
sub drop {
my ($self) = (@_);
if ($self->{fh}) {
close $self->{fh};
}
unlink "$self->{file_base}.tbl";
unlink "$self->{file_base}.cols";
return 1;
}
sub fetch_row {
my ($self, $data) = @_;
my $fh = $self->{fh};
my $line = <$fh>;
return undef if not $line;
chomp $line;
my @fields = split /\|/, $line, scalar($self->{col_names});
# put them back to |s to maintain integrity
for (@fields) {
$_ =~ s/&#124;/\|/g;
}
$self->{row} = ( @fields ? \@fields : undef );
return $self->{row};
}
sub push_row {
my ($self, $data, $fields) = @_;
# make sure undefined fields still get put in & write.
# added bonus of stopped a bunch of undef warnings
@$fields = map { defined $_ ? $_ : '' } @$fields;
# we have to nuke all |s, because that's our delimeter...
for (@$fields) { $_ =~ s/\|/&#124;/g; }
# handle primary key
if (defined $self->{primary_key} and $self->{primary_key} ne "") {
my $col = $self->{col_nums}{uc($self->{primary_key})};
if ($fields->[$col] eq "") {
die "Primary Key field ($self->{primary_key}) cannot be empty!";
# $fields->[$col] = $self->_get_next_pk();
}
else {
# $self->_check_pk();
}
#else {
# $self->_update_next_pk($fields->[$col]);
#}
}
my $fh = $self->{fh};
print $fh join('|', @$fields);
print $fh "\n";
return 1;
}
sub push_names {
my ($self, $data, $row) = @_;
my $col_fh;
my $created = 0;
if (not -e "$self->{file_base}.cols") {
$created = 1;
open $col_fh, '>', "$self->{file_base}.cols"
or die "Could not create columns $self->{file_base}.cols: $!";
}
else {
open $col_fh, '+<', "$self->{file_base}.cols"
or die "Could not read/write columns $self->{file_base}.cols: $!";
}
if ($YaBB3::DataSource::File::can_lock) {
flock $col_fh, LOCK_EX
or die "Can't lock $self->{file_base}.cols: $!";
# we don't want other processes getting confused with weird table data
if ($data->{fh}) {
my $tbl_fh = $data->{fh};
flock $tbl_fh, LOCK_EX
or die "Can't lock $self->{file_base}.tbl: $!";
}
}
# if we didn't just create this table, get the PK value
my $pk_val = 0;
if (not $created) {
my @lines = <$col_fh>;
$pk_val = $lines[3];
chomp $pk_val;
# rewind & nuke current contents
seek $col_fh, 0, 0
or die "Seek error for $self->{file_base}.tbl: $!";
truncate $col_fh, 0
or die "Truncate error for $self->{file_base}.tbl: $!";
}
# store fields in object
push @{$self->{col_names}}, @$row;
$self->{col_nums}{$row} = scalar @{$self->{col_names}} - 1;
# store data types and PK
my $cols = $self->{col_names};
my @types;
my $pk = undef;
for my $col (@$row) {
my $cd = $data->{stmt}{column_defs}{$col};
push @types, $cd->{data_type};
if (not defined $pk and $cd->{constraints}) {
if (scalar grep {$_ eq "PRIMARY KEY"} @{$cd->{constraints}}) {
$pk = $col;
}
}
}
$self->{col_datatypes} = \@types;
if (defined $pk) { $self->{primary_key} = uc($pk); }
print $col_fh uc(join('|', @{$self->{col_names}})), "\n";
print $col_fh uc(join('|', @types)), "\n";
print $col_fh ( defined $pk ? $pk : '' ), "\n";
#print $col_fh ( defined $pk_val ? $pk_val : 0 );
close $col_fh;
}
sub seek {
my ($self, $data, $pos, $whence) = @_;
if ($whence == 0 and $pos == 0) {
$pos = $self->{first_row_pos};
}
elsif ($whence != 2 or $pos != 0) {
die "Illegal seek: pos = $pos, whence = $whence";
}
seek $self->{fh}, $pos, $whence
or die "Seek error for $self->{file_base}.tbl: $!";
return 1;
}
sub truncate {
my ($self, $data) = @_;
truncate $self->{fh}, tell($self->{fh})
or die "Truncate error for $self->{file_base}.tbl: $!";
return 1;
}
1;
__END__
=pod
=head1 NAME
YaBB3::DataSource::File - Provides a file base data source for YaBB 3
=head1 SYNOPSIS
This is a driver for YaBB3::DataSource. Do not call it directly.
=head1 DESCRIPTION
This is the file backend driver for YaBB3. It uses L<SQL::Statement> to parse
the SQL queries. Because we don't want to depend on DBI, I can't use
L<DBD::File> in this module. I did use the DBD::File source as a guide on how
I should properly do things, so that I wouldn't forget important things.
The guys who wrote C<SQL::Statement> and C<DBD::File> are awesome--despite
their code being a bit frightening. Without their documentation and code this
would have been 100 times harder.
=head1 METHODS
=head2 new(option => 'value')
This functions is usually called by the YaBB3::DataSource module. It will
supply the user, password and database arguments based on the current board's
configuration.
=head3 Options
=over
=item database
This is the name of the database to connect to. It should be set by the user
of the module. This will be a subdirectory in the database directory defined
by the Paths modules. (C<YaBB3::Paths::DatabaseDir>)
=back
=head3 Return Value
Returns an object of the class that new was called in.
=head2 do_query( $sql, [ bind_values, ... ])
Behaves in accordance with the API defined in L<YaBB3::DataSource>
=head2 prepare( $sql )
Behaves in accordance with the API defined in L<YaBB3::DataSource>
=head1 STATEMENT HANDLERS
=head2 execute( bind_values, ... )
Behaves in accordance with the API defined in L<YaBB3::DataSource>
=head2 fetch( )
Behaves in accordance with the API defined in L<YaBB3::DataSource>
=head1 ACKNOWLEDGMENTS
The team would like to thank the following people for their help. Whether they
know it or not, they have helped YaBB in a big way.
=over
=item * Jeff Zucker for SQL::Statement and contributions to DBD::File.
=item * H.Merijn Brand for contributions to DBD::File
=item * Jehns Rehsack for contributions to DBD::File
=item * Jochen Wiedmann for creating DBD::File
=back
=head1 SEE ALSO
L<YaBB3::DataSourcE>, L<SQL::Statement>, L<DBD::File>
=head1 COPYRIGHT
This module was written by Matt Siegman as part of the YaBB 3 project.
Copyright (C) 2002-2008, YaBB 3 Development Team. All Rights Reserved. You may
distribute this module under the terms of YaBB 3.
=cut