From 90f45290bbedeb2b19f4a8751cb1a145c4d50ba0 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Thu, 5 Feb 2015 18:25:39 +0100
Subject: [PATCH] trying to do a less shit with the db

---
 lib/sessions/irc.pm | 11 +----------
 lib/sites/parser.pm | 34 +++++++++++++++++++++++-----------
 2 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/lib/sessions/irc.pm b/lib/sessions/irc.pm
index 82ebbb4..827e697 100644
--- a/lib/sessions/irc.pm
+++ b/lib/sessions/irc.pm
@@ -144,7 +144,6 @@ sub setConf
 {
     commands::parser::setConf($irc, $dbh, $log, \%lastID);
 
-    $sites::parser::dbh = $dbh;
     $sites::parser::irc = $irc;
     $sites::parser::log = $log;
 }
@@ -288,15 +287,7 @@ sub on_speak
     # first we test if it's a command
     if (!commands::parser::exec(@args)) {
         # if not, maybe there is an url we can parse
-	    my $id = sites::parser::parse(@args);
-
-        # if there is a new content, there is a new id to save
-        if ($id) {
-            $lastID{$chan->[0]} = $id;
-	    
-            # we insert the potiential tags
-            commands::parser::tag($msg, $chan);
-        }
+	    sites::parser::parse(@args);
     }	
 }
 
diff --git a/lib/sites/parser.pm b/lib/sites/parser.pm
index 6ad7912..6b60f06 100644
--- a/lib/sites/parser.pm
+++ b/lib/sites/parser.pm
@@ -8,10 +8,11 @@ use zippy;
 use dailymotion;
 
 use lib "$FindBin::Bin/lib/";
+use utils::db;
 use utils::print;
+use commands::parser;
 
 our $irc;
-our $dbh;
 our $log;
 
 sub parse {
@@ -20,6 +21,7 @@ sub parse {
 
     my %content;
     my $id;
+    my $dbh = utils::db::main_session();
 
     # parsing
     if ($msg =~ m#(?:^|[^!])https?://(?:www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)#) {
@@ -73,11 +75,14 @@ sub parse {
 			    or $log->error("Couldn't finish transaction: " . $dbh->errstr);
 	    }
 
-        my @tags;
 	    # sélection de l'id de la vidéo insérée
         $id = $sth->{mysql_insertid};
 	    if (!$id) {
             # la vido avait déjà été insérée
+            # L'état de la bdd est stable (puisqu'on a en fait rien fait),
+            # on peut commiter.
+            $dbh->commit;
+
 		    my $sth = $dbh->prepare_cached('SELECT id FROM playbot WHERE url = ?');
 		    $log->error("Couldn't prepare querie; aborting") unless (defined $sth);
 
@@ -87,6 +92,21 @@ sub parse {
 		    $id = $sth->fetch->[0];
 	    }
 
+        # insertion du chan
+        my $sth = $dbh->prepare_cached('
+            INSERT INTO playbot_chan (content, chan, sender_irc)
+            VALUES (?,?,?)');
+		$log->error("Couldn't prepare querie; aborting") unless (defined $sth);
+
+        $sth->execute($id, $chan->[0], $nick)
+            or $log->error("Couldn't finish transaction: " . $dbh->errstr);
+
+        # in all cases, we commit now
+        $dbh->commit;
+
+        commands::parser::tag($msg, $chan);
+
+        my @tags;
         # get tags
         $sth = $dbh->prepare("select tag
             from playbot_tags
@@ -99,15 +119,7 @@ sub parse {
             $tag =~ s/([a-zA-Z0-9_-]+)/#$1/;
             push @tags, $tag;
         }
-
-        # insertion du chan
-        my $sth = $dbh->prepare_cached('
-            INSERT INTO playbot_chan (content, chan, sender_irc)
-            VALUES (?,?,?)');
-		$log->error("Couldn't prepare querie; aborting") unless (defined $sth);
-
-        $sth->execute($id, $chan->[0], $nick)
-            or $log->error("Couldn't finish transaction: " . $dbh->errstr);
+        $dbh->commit;
 
         # message sur irc
         $content{'id'} = $id;
-- 
GitLab