Database-Abstraction
view release on metacpan or search on metacpan
lib/Database/Abstraction.pm view on Meta::CPAN
my $params = Params::Get::get_params('logger', @_);
if(my $logger = $params->{'logger'}) {
if(Scalar::Util::blessed($logger)) {
$self->{'logger'} = $logger;
} else {
$self->{'logger'} = Log::Abstraction->new($logger);
}
return $self;
}
Carp::croak('Usage: set_logger(logger => $logger)')
}
# Open the database connection based on the specified type (e.g., SQLite, CSV).
# Read the data into memory or establish a connection to the database file.
# column_names allows the column names to be overridden on CSV files
sub _open
{
if(!UNIVERSAL::isa((caller)[0], __PACKAGE__)) {
Carp::croak('Illegal Operation: This method can only be called by a subclass');
}
my $self = shift;
my $params = Params::Get::get_params(undef, @_);
$params->{'sep_char'} ||= $self->{'sep_char'} ? $self->{'sep_char'} : '!';
my $max_slurp_size = $params->{'max_slurp_size'} || $self->{'max_slurp_size'};
my $table = $self->{'table'} || ref($self);
$table =~ s/.*:://;
$self->_trace(ref($self), ": _open $table");
return if($self->{$table});
# Read in the database
my $dbh;
my $dir = $self->{'directory'} || $defaults{'directory'};
my $dbname = $self->{'dbname'} || $defaults{'dbname'} || $table;
my $slurp_file = File::Spec->catfile($dir, "$dbname.sql");
$self->_debug("_open: try to open $slurp_file");
# Look at various places to find the file and derive the file type from the file's name
if(-r $slurp_file) {
# SQLite file
require DBI && DBI->import() unless DBI->can('connect');
$dbh = DBI->connect("dbi:SQLite:dbname=$slurp_file", undef, undef, {
sqlite_open_flags => SQLITE_OPEN_READONLY,
});
}
if($dbh) {
$dbh->do('PRAGMA synchronous = OFF');
$dbh->do('PRAGMA cache_size = -4096'); # Use 4MB cache - negative = KB)
$dbh->do('PRAGMA journal_mode = OFF'); # Read-only, no journal needed
$dbh->do('PRAGMA temp_store = MEMORY'); # Store temp data in RAM
$dbh->do('PRAGMA mmap_size = 1048576'); # Use 1MB memory-mapped I/O
$dbh->sqlite_busy_timeout(100000); # 10s
$self->_debug("read in $table from SQLite $slurp_file");
$self->{'type'} = 'DBI';
} elsif($self->_is_berkeley_db(File::Spec->catfile($dir, "$dbname.db"))) {
$self->_debug("$table is a BerkeleyDB file");
$self->{'type'} = 'BerkeleyDB';
} else {
my $fin;
($fin, $slurp_file) = File::pfopen::pfopen($dir, $dbname, 'csv.gz:db.gz', '<');
if(defined($slurp_file) && (-r $slurp_file)) {
require Gzip::Faster;
Gzip::Faster->import();
close($fin);
$fin = File::Temp->new(SUFFIX => '.csv', UNLINK => 0);
print $fin gunzip_file($slurp_file);
$slurp_file = $fin->filename();
$self->{'temp'} = $slurp_file;
} else {
($fin, $slurp_file) = File::pfopen::pfopen($dir, $dbname, 'psv', '<');
if(defined($fin)) {
# Pipe separated file
$params->{'sep_char'} = '|';
} else {
# CSV file
($fin, $slurp_file) = File::pfopen::pfopen($dir, $dbname, 'csv:db', '<');
}
}
if(my $filename = $self->{'filename'} || $defaults{'filename'}) {
$self->_debug("Looking for $filename in $dir");
$slurp_file = File::Spec->catfile($dir, $filename);
}
if(defined($slurp_file) && (-r $slurp_file)) {
close($fin) if(defined($fin));
my $sep_char = $params->{'sep_char'};
$self->_debug(__LINE__, ' of ', __PACKAGE__, ": slurp_file = $slurp_file, sep_char = $sep_char");
if($params->{'column_names'}) {
$dbh = DBI->connect("dbi:CSV:db_name=$slurp_file", undef, undef,
{
csv_sep_char => $sep_char,
csv_tables => {
$table => {
col_names => $params->{'column_names'},
},
},
}
);
} else {
$dbh = DBI->connect("dbi:CSV:db_name=$slurp_file", undef, undef, { csv_sep_char => $sep_char});
}
$dbh->{'RaiseError'} = 1;
$self->_debug("read in $table from CSV $slurp_file");
$dbh->{csv_tables}->{$table} = {
allow_loose_quotes => 1,
blank_is_undef => 1,
empty_is_undef => 1,
binary => 1,
( run in 2.959 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )