CPAN-MetaCurator

 view release on metacpan or  search on metacpan

lib/CPAN/MetaCurator/Database.pm  view on Meta::CPAN

	# For useage, see Export.pm line 54.

	$$pad{topic_names}		= {};
	$$pad{topic_html_ids}	= {};

	for (@{$$pad{topics} })
	{
		$$pad{count}{topic}++;

		$$pad{topic_html_ids}{$$_{title} }	= $$pad{html_id_offset} * $$_{id}; # $leaf_id in Export.pm.
		$$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]");

} # 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]");

} # End of init_metapackager_db.

# -----------------------------------------------

sub read_csv_file
{
	my($self, $path) = @_;

	my($csv) = Text::CSV::Encoded -> new
	({
		allow_whitespace	=> 1,
		encoding_in			=> 'utf-8',
		strict				=> 1,
	});

	open(my $io, '<', $path) || die "Can't open($path): $!\n";

	my(@column_names)	= $csv -> column_names($csv -> getline($io) );
	my($count)			= 0;

	my(@records);

	for my $item (@{$csv -> getline_hr_all($io) })
	{
		$count++;

		push @records, $$item{$column_names[0]};
	}

	close $io;

	$self -> logger -> info("Read 1 heading + $count records from '$path'");

	return [@records];

} # End of read_csv_file.

# --------------------------------------------------

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];



( run in 0.679 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )