Biblio-Thesaurus-SQLite

 view release on metacpan or  search on metacpan

lib/Biblio/Thesaurus/SQLite.pm  view on Meta::CPAN

		},
	}
	);
	$db->commit;

}

##
# This method convert a SQLite database to a ISO thesaurus text file
# @param The SQLite database
# @param The output ISO Thesaurus file
# @note This method is VERY VERY slow! I tried to know why, run a profiller
# 	and saw that most of the time we are consuming CPU in the ->hashes
# 	function of the DBIx::Simple module.... TODO: get this think faster :D
sub TheSql2ISOthe {
	my $dbfile = shift or die;
	my $file = shift or die;

	# ok so this is easy :D	
	# connect to the database
	my $db  = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
		or die DBIx::Simple->error;

	# process meta-data
	open(F, ">$file");
	for my $row ($db->query('SELECT DISTINCT term FROM meta')->flat) {
		print F '%' . $row . ' ' . 
		      join(' ',
		        $db->query(
		      	  'SELECT val FROM META WHERE term = ?',
			  $row
		        )->flat
		      ), "\n";
	}

	# process translations
	$db->query('SELECT val FROM meta WHERE term = ?', 'baselang')->into(my $baselang);
	if(defined($baselang)) {
		$db->query('SELECT lang FROM lang LIMIT 1')->into(my $lang);
		print F $baselang, '==', $lang, "\n\n";
		for my $row ($db->query('SELECT * FROM lang')->hashes) {
			print F $row->{ori}, '==', $row->{dest}, "\n";
		}
	}

	# process the main data
	for my $row ($db->query('SELECT DISTINCT term FROM rel')->flat) {
		print F "\n\n$row\n";
		for my $row2 ($db->query('SELECT rel, def FROM rel WHERE term = ?', $row)->hashes) {
			print F $row2->{rel}, ' ', $row2->{def}, "\n";
		}
	}
	close(F);
}

##
# this method tries to output the result of a term as a xhtml table
# 	maybe to use with a cgi module
# @param the term to find data
# @param the sqlite database file
sub getTermAsXHTML {
	my $termo = shift or die;
	my $dbfile = shift or die;

	# connect to the database
	my $db  = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
		or die DBIx::Simple->error;

	# try to see if we got any results avaiable
	my $count;
	$db->query(
		'SELECT COUNT(term) FROM rel WHERE term = ?'
		, $termo
	)->into($count);

	return ( '<h3>Termo <emph>' . $termo .
	         '</emph> nao encontrado</h3>' )
	         if $count == 0;

	# now starting the output of the table
	my $res = '<b1>' . $termo . '<b1><table><th><td>Relacao</td>' .
	          '<td>Definição</td></th>' . "\n";	
	# this is ugly....
	for my $row ($db->query('SELECT * FROM rel WHERE term = ?',
	                        $termo)->hashes) {
		$res .= '<tr><td>' . $row->{rel} .
		        '</td><td>' . $row->{def} .
			'</td></tr>' . "\n";
	}
	$res .= '</table>';

	return $res;
}

##
# Does the same thing as the previous method, but outputs the data in an
# 	ISO Thesaurus format
# @param .....
# @param guess w00t ?
sub getTermAsISOthe {
	my $termo = shift or die;
	my $dbfile = shift or die;

	# connect to the database
	my $db  = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
		or die DBIx::Simple->error;

	my $count;
	$db->query(
		'SELECT COUNT(term) FROM rel WHERE term = ?'
		, $termo
	)->into($count);

	return '' if $count == 0;
	
	my $res = $termo . "\n";	
	for my $row ($db->query('SELECT * FROM rel WHERE term = ?',
	                        $termo)->hashes) {
		$res .= '- ' . $row->{rel} . ' -> ' . 
		        $row->{def} . "\n";
	}



( run in 1.158 second using v1.01-cache-2.11-cpan-e1769b4cff6 )