diff options
Diffstat (limited to 'src/database/BrandAnalyzerInserter.java')
-rw-r--r-- | src/database/BrandAnalyzerInserter.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/database/BrandAnalyzerInserter.java b/src/database/BrandAnalyzerInserter.java new file mode 100644 index 0000000..f7ed62c --- /dev/null +++ b/src/database/BrandAnalyzerInserter.java @@ -0,0 +1,48 @@ +package database; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.concurrent.Callable; +import main.Watcher; + +/** + * Inserts queued brand analyses into the database. + * + * @author Peter Wu + */ +public class BrandAnalyzerInserter implements Callable<Integer>, + Watcher.Progressable { + + private final BrandAnalyzerQueue analyzer; + private final NamedPreparedStatement insertBrand; + private volatile int count; + + public BrandAnalyzerInserter(Connection connection, + BrandAnalyzerQueue analyzer) throws SQLException { + this.analyzer = analyzer; + this.insertBrand = new NamedPreparedStatement(connection, + "INSERT INTO mentionsbrand" + + " SELECT :tweetid AS tweetid, :brand AS brand" + + " WHERE NOT EXISTS (SELECT 1 FROM mentionsbrand WHERE" + + " tweetid=:tweetid AND brand=:brand)"); + } + + @Override + public Integer call() throws SQLException { + BrandAnalyzerQueue.Result result; + while ((result = analyzer.next()) != null) { + for (String brand : result.brands) { + QueryUtils.setInsertBrandParams(insertBrand, result.tweetid, brand); + insertBrand.executeUpdate(); + } + + count++; + } + return count; + } + + @Override + public int getCount() { + return count; + } +} |