summaryrefslogtreecommitdiff
path: root/src/database/BrandAnalyzerInserter.java
blob: f7ed62cbb1dd7f7a1db32bc16754efe3c119aacf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
    }
}