summaryrefslogtreecommitdiff
path: root/src/main/java/com/mesasoft/cn/util/MariaDbBase.java
blob: 777ec44337f8868010dfcf644e9d5da7e7497f8d (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.mesasoft.cn.util;

import com.mesasoft.cn.sketch.config.ApplicationConfig;
import org.apache.log4j.Logger;

import java.sql.*;
import java.util.Date;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: joy
 * Date: 2021/12/28
 * Time: 2:56 PM
 * Description: No Description
 */
public class MariaDbBase {

    private static final Logger LOG = Logger.getLogger(MariaDbBase.class);
    private static final Properties props = new Properties();

    private final Statement statement;

    public MariaDbBase(Connection conn, Statement stat) {
        statement = stat;
    }

    /**
     * 执行写入sql
     */
    public void writeSqlExecute(String sql){
        try {
            statement.executeUpdate(sql);
        } catch (SQLIntegrityConstraintViolationException e){
            LOG.error("Duplicated entry for key 'PRIMARY'");
        } catch (SQLException exception) {
            LOG.error("Sql : " + sql);
            exception.printStackTrace();
        }
    }

    /**
     * 执行查询sql
     */
    public ResultSet querySqlExecute(String sql){
        ResultSet set = null;
        try {
            set = statement.executeQuery(sql);
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
        return set;
    }


    /**
     * 获得指定表格、按指定时间字段的过期记录
     * @param tableName 库表名称
     * @param timeColumnName 时间列名
     * @return 查询结果
     */
    public ResultSet getExpiredRecord(String tableName, String timeColumnName){
        Date lastUpdateTime = new Timestamp(getExpiredTime(ApplicationConfig.UPDATE_EXPIRED_DAY).getTime());

        String resSql = "SELECT *"
                + " FROM " + ApplicationConfig.DATABASE + "." + tableName
                + " WHERE " + timeColumnName + " < '" + lastUpdateTime + '\'';

        LOG.debug("Update task: expired query sql" + resSql);

        return querySqlExecute(resSql);
    }

    /**
     * TODO: getUnlabeledRecord() 考虑多个来源的情况
     */

    /**
     * 获得过期时间, 当前时间的expiredRangeDays天之前的日期为过期日期
     */
    public static Date getExpiredTime(int expiredRangeDays){
        return new Timestamp(TimeUtils.getStartOfDay(-expiredRangeDays).getTime());
    }
}