yangys
2024-05-18 040976de6f9934b99f30268a28e2ecf42260e217
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.qianwen.core.sequence.range.db;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import javax.sql.DataSource;
import com.qianwen.core.sequence.exception.SeqException;
import com.qianwen.core.sequence.range.db.provider.SqlProviderFactory;
import com.qianwen.core.tool.utils.SpringUtil;
 
 
public class BaseDbHelper {
    private static final long DELTA = 100000000;
    private static final SqlProviderFactory SQL_PROVIDER_FACTORY = (SqlProviderFactory) SpringUtil.getBean(SqlProviderFactory.class);
    private static final DefaultIdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator();
 
    /* JADX INFO: Access modifiers changed from: package-private */
    public static void createTable(DataSource dataSource, String tableName) {
        Connection conn = null;
        Statement stmt = null;
        try {
            try {
                conn = dataSource.getConnection();
                stmt = conn.createStatement();
                stmt.executeUpdate(SQL_PROVIDER_FACTORY.getCreateTableSql().replace("#tableName", tableName));
                closeQuietly(stmt);
                closeQuietly(conn);
            } catch (SQLException e) {
                throw new SeqException(e);
            }
        } catch (Throwable th) {
            closeQuietly(stmt);
            closeQuietly(conn);
            throw th;
        }
    }
 
    private static void insertRange(DataSource dataSource, String tableName, String name, long stepStart) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            try {
                conn = dataSource.getConnection();
                stmt = conn.prepareStatement(SQL_PROVIDER_FACTORY.getInsertRangeSql().replace("#tableName", tableName));
                stmt.setLong(1, identifierGenerator.nextId((Object) null).longValue());
                stmt.setString(2, name);
                stmt.setLong(3, stepStart);
                stmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
                stmt.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
                stmt.executeUpdate();
                closeQuietly(stmt);
                closeQuietly(conn);
            } catch (SQLException e) {
                throw new SeqException(e);
            }
        } catch (Throwable th) {
            closeQuietly(stmt);
            closeQuietly(conn);
            throw th;
        }
    }
 
    /* JADX INFO: Access modifiers changed from: package-private */
    public static boolean updateRange(DataSource dataSource, String tableName, Long newValue, Long oldValue, String name) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            try {
                conn = dataSource.getConnection();
                stmt = conn.prepareStatement(SQL_PROVIDER_FACTORY.getUpdateRangeSql().replace("#tableName", tableName));
                stmt.setLong(1, newValue.longValue());
                stmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
                stmt.setString(3, name);
                stmt.setLong(4, oldValue.longValue());
                int affectedRows = stmt.executeUpdate();
                boolean z = affectedRows > 0;
                closeQuietly(stmt);
                closeQuietly(conn);
                return z;
            } catch (SQLException e) {
                throw new SeqException(e);
            }
        } catch (Throwable th) {
            closeQuietly(stmt);
            closeQuietly(conn);
            throw th;
        }
    }
 
    /* JADX INFO: Access modifiers changed from: package-private */
    public static Long selectRange(DataSource dataSource, String tableName, String name, long stepStart) {
        try {
            try {
                Connection conn = dataSource.getConnection();
                PreparedStatement stmt = conn.prepareStatement(SQL_PROVIDER_FACTORY.getSelectRangeSql().replace("#tableName", tableName));
                stmt.setString(1, name);
                ResultSet rs = stmt.executeQuery();
                if (!rs.next()) {
                    insertRange(dataSource, tableName, name, stepStart);
                    closeQuietly(rs);
                    closeQuietly(stmt);
                    closeQuietly(conn);
                    return null;
                }
                long oldValue = rs.getLong(1);
                if (oldValue < 0) {
                    String msg = "Sequence value cannot be less than zero, value = " + oldValue + ", please check table sequence" + tableName;
                    throw new SeqException(msg);
                } else if (oldValue > 9223372036754775807L) {
                    String msg2 = "Sequence value overflow, value = " + oldValue + ", please check table sequence" + tableName;
                    throw new SeqException(msg2);
                } else {
                    Long valueOf = Long.valueOf(oldValue);
                    closeQuietly(rs);
                    closeQuietly(stmt);
                    closeQuietly(conn);
                    return valueOf;
                }
            } catch (SQLException e) {
                throw new SeqException(e);
            }
        } catch (Throwable th) {
            closeQuietly(null);
            closeQuietly(null);
            closeQuietly(null);
            throw th;
        }
    }
 
    public static Boolean existTable(DataSource dataSource, String tableName) {
        String existTableSql = SQL_PROVIDER_FACTORY.getExistTableSql();
        if (StrUtil.isBlank(existTableSql)) {
            return true;
        }
        try {
            try {
                Connection conn = dataSource.getConnection();
                Statement stmt = conn.createStatement();
                ResultSet resultSet = stmt.executeQuery(SQL_PROVIDER_FACTORY.getExistTableSql().replace("#tableName", tableName));
                if (!resultSet.next()) {
                    closeQuietly(stmt);
                    closeQuietly(conn);
                    return false;
                }
                int count = resultSet.getInt(1);
                if (0 == count) {
                    closeQuietly(stmt);
                    closeQuietly(conn);
                    return true;
                }
                closeQuietly(stmt);
                closeQuietly(conn);
                return false;
            } catch (SQLException e) {
                throw new SeqException(e);
            }
        } catch (Throwable th) {
            closeQuietly(null);
            closeQuietly(null);
            throw th;
        }
    }
 
    private static void closeQuietly(AutoCloseable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (Throwable th) {
            }
        }
    }
}