| 1 |
# My::Builder |
| 2 |
# A local Module::Build subclass for building the database |
| 3 |
# |
| 4 |
# $Id: Builder.pm 8633 2009-08-18 16:00:58Z [email protected] $ |
| 5 |
|
| 6 |
package My::Builder; |
| 7 |
|
| 8 |
use strict; |
| 9 |
use warnings; |
| 10 |
|
| 11 |
use base 'Module::Build'; |
| 12 |
|
| 13 |
use Cwd (); |
| 14 |
use File::Spec (); |
| 15 |
use Carp (); |
| 16 |
|
| 17 |
use DBI (); |
| 18 |
use DBD::SQLite (); |
| 19 |
|
| 20 |
my $ORIG_DIR = Cwd::cwd(); |
| 21 |
|
| 22 |
# These are utility commands for getting into and out of our build directory |
| 23 |
sub _chdir_or_die { |
| 24 |
my $dir = File::Spec->catfile(@_); |
| 25 |
chdir $dir or Carp::croak("Failed to chdir to $dir: $!"); |
| 26 |
} |
| 27 |
sub _chdir_back { |
| 28 |
chdir $ORIG_DIR or Carp::croak("Failed to chdir to $ORIG_DIR: $!"); |
| 29 |
} |
| 30 |
|
| 31 |
sub ACTION_code { |
| 32 |
my ($self) = @_; |
| 33 |
|
| 34 |
_chdir_or_die('lib', 'Video', 'FourCC'); |
| 35 |
|
| 36 |
unless (-f 'codecs.dat') { |
| 37 |
print "Opening database source file\n"; |
| 38 |
open(my $fh, '<', 'source-data.sql') or die("Failed: $!\n"); |
| 39 |
|
| 40 |
my $dbh = DBI->connect( |
| 41 |
'dbi:SQLite:dbname=codecs.dat', |
| 42 |
'notused', # cannot be null, or DBI complains |
| 43 |
'notused', |
| 44 |
{ |
| 45 |
RaiseError => 1, |
| 46 |
PrintError => 0, |
| 47 |
} |
| 48 |
); |
| 49 |
|
| 50 |
# Use a transaction for faster insertion |
| 51 |
$dbh->begin_work; |
| 52 |
|
| 53 |
print "Creating database structure\n"; |
| 54 |
$dbh->do(q{ |
| 55 |
CREATE TABLE fourcc |
| 56 |
( |
| 57 |
fourcc char(4) NOT NULL, |
| 58 |
"owner" varchar(100), |
| 59 |
registered date, |
| 60 |
description varchar(200) NOT NULL, |
| 61 |
CONSTRAINT fourcc_pkey PRIMARY KEY (fourcc) |
| 62 |
); |
| 63 |
CREATE UNIQUE INDEX idx_fourcc ON fourcc (fourcc); |
| 64 |
}); |
| 65 |
|
| 66 |
# This slurps the entire source file into memory -- this is less than |
| 67 |
# efficient, but builds don't take long and ~60KB isn't bad. |
| 68 |
print "Inserting database data\n"; |
| 69 |
while (<$fh>) { |
| 70 |
chomp $_; |
| 71 |
$dbh->do($_); |
| 72 |
} |
| 73 |
$dbh->commit; |
| 74 |
|
| 75 |
# Vacuum the resulting database |
| 76 |
print "Optimizing database\n"; |
| 77 |
$dbh->do('VACUUM fourcc'); |
| 78 |
|
| 79 |
print "Writing database\n"; |
| 80 |
$dbh->disconnect; |
| 81 |
} |
| 82 |
|
| 83 |
_chdir_back(); |
| 84 |
|
| 85 |
return $self->SUPER::ACTION_code; |
| 86 |
} |
| 87 |
|
| 88 |
sub ACTION_clean { |
| 89 |
my ($self) = @_; |
| 90 |
|
| 91 |
my $rc = $self->SUPER::ACTION_clean; |
| 92 |
_chdir_or_die('lib', 'Video', 'FourCC'); |
| 93 |
|
| 94 |
if (-f 'codecs.dat') { |
| 95 |
print "Deleting generated database\n"; |
| 96 |
unlink('codecs.dat') or die "Failed: $!\n"; |
| 97 |
} |
| 98 |
|
| 99 |
_chdir_back(); |
| 100 |
|
| 101 |
|
| 102 |
return $rc; |
| 103 |
} |
| 104 |
|
| 105 |
1; |