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) { } } } }