CPAN-MetaCurator
view release on metacpan or search on metacpan
lib/CPAN/MetaCurator/Database.pm view on Meta::CPAN
$$pad{module_names}{$$_{name} } = $$_{id} for (@{$$pad{modules} });
# Topics.
# There is a db table called topics so we need another name for the hash
# where the keys are the names of the topics and the values are db ids.
$$pad{topic_names} = {};
$$pad{topic_html_ids} = {};
for (@{$$pad{topics} })
{
$$pad{count}{topic}++;
$$pad{topic_html_ids}{$$_{title} } = $$pad{html_id_offset} * $$_{id};
$$pad{topic_names}{$$_{title} } = $$_{id};
}
# Dates.
# DateTime::Tiny does not handle time_zone.
$_ = DateTime::Tiny -> now;
$$pad{now} = $_ -> as_string;
$$pad{current_year} = substr($$pad{now}, 0, 4);
return $self -> pad($pad);
} # End of build_pad.
# -----------------------------------------------
sub get_table_column_names
{
my($self, $discard_id, $table_name) = @_;
my($sth) = $self -> dbh -> prepare("PRAGMA table_info($table_name)");
$sth -> execute;
my($row);
my(@column_names);
while ($row = $sth -> fetchrow_hashref)
{
push @column_names, $$row{name} if (! $discard_id);
}
$sth -> finish;
$self -> column_names(\@column_names);
} # End of get_table_column_names;
# -----------------------------------------------
sub init_db
{
my($self) = @_;
my($config) = $self -> config;
my(%attributes) =
(
AutoCommit => $$config{AutoCommit},
mysql_enable_utf8 => $$config{mysql_enable_utf8}, # Ignored if not using MySQL.
mysql_enable_utf8mb4 => $$config{mysql_enable_utf8mb4}, # Ignored if not using MySQL.
pg_enable_utf8 => $$config{pg_enable_utf8}, # Ignored if not using Pg.
RaiseError => $$config{RaiseError},
sqlite_unicode => $$config{sqlite_unicode}, # Ignored if not using SQLite.
);
my(@dsn) = split('=', $$config{dsn});
$dsn[1] = File::Spec -> catfile($self -> home_path, $dsn[1]);
$dsn[0] = "$dsn[0]=$dsn[1]";
$self -> dbh(DBI -> connect($dsn[0], $$config{username}, $$config{password}, \%attributes) );
$self -> dbh -> do('PRAGMA foreign_keys = ON') if ($$config{dsn} =~ /SQLite/i);
$self -> db(DBIx::Simple -> new($self -> dbh) );
$self -> creator(DBIx::Admin::CreateTable -> new(dbh => $self -> dbh, verbose => 0) );
$self -> engine($self -> creator -> db_vendor =~ /(?:Mysql)/i ? 'engine=innodb' : '');
$self -> time_option($self -> creator -> db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : '');
$self -> logger -> info("Connected to $dsn[0]");
$self -> logger -> info($self -> separator);
} # End of init_db.
# -----------------------------------------------
sub insert_hashref
{
my($self, $table_name, $hashref) = @_;
$self -> db -> insert($table_name, {map{($_ => $$hashref{$_})} keys %$hashref})
|| die $self -> db -> error;
return $self -> db -> last_insert_id(undef, undef, $table_name, undef);
} # End of insert_hashref.
# -----------------------------------------------
sub init_metapackager_db
{
my($self) = @_;
my($config) = $self -> metapackager_config;
my(%attributes) =
(
AutoCommit => $$config{AutoCommit},
mysql_enable_utf8 => $$config{mysql_enable_utf8}, # Ignored if not using MySQL.
mysql_enable_utf8mb4 => $$config{mysql_enable_utf8mb4}, # Ignored if not using MySQL.
pg_enable_utf8 => $$config{pg_enable_utf8}, # Ignored if not using Pg.
RaiseError => $$config{RaiseError},
sqlite_unicode => $$config{sqlite_unicode}, # Ignored if not using SQLite.
);
my(@dsn) = split('=', $$config{dsn});
$dsn[1] = File::Spec -> catfile($dsn[1]);
$dsn[0] = "$dsn[0]=$dsn[1]";
$self -> metapackager_dbh(DBI -> connect($dsn[0], $$config{username}, $$config{password}, \%attributes) );
$self -> metapackager_dbh -> do('PRAGMA foreign_keys = ON') if ($$config{dsn} =~ /SQLite/i);
$self -> metapackager_db(DBIx::Simple -> new($self -> metapackager_dbh) );
$self -> logger -> info("Connected to $dsn[0]");
$self -> logger -> info($self -> separator);
} # End of init_metapackager_db.
# --------------------------------------------------
sub read_1_record
{
my($self, $table_name, $id) = @_;
my($sql) = "select * from $table_name where id = $id";
my($set) = $self -> db -> query($sql) || die $self -> db -> error;
# Return a hashref.
return ${$set -> hashes}[0];
} # End of read_1_record.
# --------------------------------------------------
sub read_table
{
my($self, $table_name) = @_;
my($sql) = "select * from $table_name";
my($set) = $self -> db -> query($sql) || die $self -> db -> error;
# Return an arrayref of hashrefs.
return [$set -> hashes];
} # End of read_table.
# --------------------------------------------------
sub read_metapackager_table
{
my($self, $pad) = @_;
$self -> init_metapackager_config;
$self -> init_metapackager_db;
# Return an arrayref of hashrefs.
my($table_name) = 'packages';
my($sql) = "select * from $table_name";
my($set) = $self -> metapackager_db -> query($sql) || die $self -> metapackager_db -> error;
$set = [$set -> hashes];
( run in 1.298 second using v1.01-cache-2.11-cpan-39bf76dae61 )