Return the internal sequence name for the given Column @param conn @param dbType @param catalog_col @return
(Connection conn, DatabaseType dbType, Column catalog_col)
| 202 | * @return |
| 203 | */ |
| 204 | public static String getSequenceName(Connection conn, DatabaseType dbType, Column catalog_col) |
| 205 | throws SQLException { |
| 206 | Table catalog_tbl = catalog_col.getTable(); |
| 207 | |
| 208 | String seqName = null; |
| 209 | String sql = null; |
| 210 | if (dbType == DatabaseType.POSTGRES) { |
| 211 | sql = |
| 212 | String.format( |
| 213 | "SELECT pg_get_serial_sequence('%s', '%s')", |
| 214 | catalog_tbl.getName(), catalog_col.getName()); |
| 215 | } else if (dbType == DatabaseType.SQLSERVER || dbType == DatabaseType.SQLAZURE) { |
| 216 | // NOTE: This likely only handles certain syntaxes for defaults. |
| 217 | sql = |
| 218 | String.format( |
| 219 | """ |
| 220 | SELECT REPLACE(REPLACE([definition], '(NEXT VALUE FOR [', ''), '])', '') AS seq |
| 221 | FROM sys.default_constraints dc |
| 222 | JOIN sys.columns c ON c.default_object_id=dc.object_id |
| 223 | JOIN sys.tables t ON c.object_id=t.object_id |
| 224 | WHERE t.name='%s' AND c.name='%s' |
| 225 | """, |
| 226 | catalog_tbl.getName(), catalog_col.getName()); |
| 227 | } else { |
| 228 | if (LOG.isDebugEnabled()) { |
| 229 | LOG.debug("Unexpected request for sequence name on {} using {}", catalog_col, dbType); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (sql != null) { |
| 234 | try (Statement stmt = conn.createStatement()) { |
| 235 | try (ResultSet result = stmt.executeQuery(sql)) { |
| 236 | if (result.next()) { |
| 237 | seqName = result.getString(1); |
| 238 | } |
| 239 | } |
| 240 | if (LOG.isDebugEnabled()) { |
| 241 | LOG.debug(String.format("%s => [%s]", sql, seqName)); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return seqName; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Mark the given table which has an identity column to be able to be inserted into with explicit |
no test coverage detected