From 2f213967683b8da261334f3e75133f4b0b2a6549 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jorge=20Rodr=C3=ADguez?= Date: Mon, 15 Oct 2007 12:50:33 +0200 Subject: [PATCH] Add debug option to dump query. --- tools/jabber2db.pl | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/tools/jabber2db.pl b/tools/jabber2db.pl index 450118f..d5bc46c 100755 --- a/tools/jabber2db.pl +++ b/tools/jabber2db.pl @@ -7,6 +7,8 @@ my $dbuser = "jabber"; my $dbpass = "secret"; my $dbname = "jabberd2"; +my $DUMP_QUERY = 0; + ########################## use strict; use DBI; @@ -87,6 +89,7 @@ set_all_vcards (); add_user ($ARGV[0]) if $option_add_user; populate_all_rosters () if $option_populate; +$dbh->disconnect; exit; ### @@ -154,26 +157,24 @@ sub db_open { sub execute_query { my $query = shift; - my $sth; + my $rows; + + print $query if ($DUMP_QUERY); + + $rows = $dbh->do ($query); - $sth = $dbh->prepare ($query) or die "jabber2db: cannot prepare query! ($!)"; - $sth->execute or die "jabber2db: cannot execute query! ($!)"; - $sth->finish; + die "jabber2db: cannot execute query! ($!)" if (undef $rows); + + return $rows; } sub db_delete_vcards { - my $query = "DELETE FROM \"vcard\""; - - $dbh->do ($query) or die "jabber2db: cannot delete vcards!"; + execute_query ("DELETE FROM \"vcard\";"); } sub db_delete_rosters { - my $query; - - $query = "DELETE FROM \"roster-items\""; - $dbh->do ($query) or die "jabber2d: cannot delete roster-items!"; - $query = "DELETE FROM \"roster-groups\""; - $dbh->do ($query) or die "jabberd2d: cannot delete roster-groups!"; + execute_query ("DELETE FROM \"roster-items\";\n" . + "DELETE FROM \"roster-groups\";"); } sub db_insert_vcard { @@ -226,6 +227,16 @@ sub db_update_vcard { return $query; } +sub db_vcard_exists { + my $pers = shift; + my $query; + + $query = "SELECT COUNT(*) FROM vcard WHERE \"collection-owner\" = " . + $dbh->quote (Vcard2Ldap::jabber_jid ($pers)); + + return execute_query ($query) > 0; +} + sub db_set_vcard { my $pers = shift; my $is_update = 0; @@ -235,16 +246,7 @@ sub db_set_vcard { if (!Vcard2Ldap::jabber_jid ($pers) || !Vcard2Ldap::jabber_group ($pers)); - if (!$option_populate) { - my $sth; - - $query = "SELECT * FROM vcard WHERE \"collection-owner\" = " . - $dbh->quote (Vcard2Ldap::jabber_jid ($pers)); - $sth = $dbh->prepare ($query) or die "Cannot prepare query! ($!)"; - $sth->execute or die "Cannot execute query! ($!) \n $query"; - $is_update = $sth->rows > 0; - $sth->finish; - } + $is_update = db_vcard_exists ($pers) if (!$option_populate); if ($is_update) { db_update_vcard ($pers); -- 2.11.4.GIT