Mercurial Hosting > nabble
comparison src/jdbcpgbackup/Sequence.java @ 0:7ecd1a4ef557
add content
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 21 Mar 2019 19:15:52 -0600 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:7ecd1a4ef557 |
|---|---|
| 1 /* Copyright (c) 2012 Tomislav Gountchev <tomi@gountchev.net> */ | |
| 2 | |
| 3 package jdbcpgbackup; | |
| 4 | |
| 5 import java.sql.Connection; | |
| 6 import java.sql.PreparedStatement; | |
| 7 import java.sql.ResultSet; | |
| 8 import java.sql.SQLException; | |
| 9 import java.util.ArrayList; | |
| 10 import java.util.List; | |
| 11 | |
| 12 final class Sequence extends DbBackupObject { | |
| 13 | |
| 14 static class SequenceFactory implements DBOFactory<Sequence> { | |
| 15 | |
| 16 @Override | |
| 17 public Iterable<Sequence> getDbBackupObjects(Connection con, Schema schema) throws SQLException { | |
| 18 List<Sequence> sequences = new ArrayList<Sequence>(); | |
| 19 PreparedStatement stmt = null; | |
| 20 try { | |
| 21 stmt = con.prepareStatement( | |
| 22 "SELECT c.relname AS sequencename, pg_get_userbyid(c.relowner) AS owner FROM pg_class c " + | |
| 23 "WHERE c.relkind='S' AND c.relnamespace = ?"); | |
| 24 stmt.setInt(1, schema.getOid()); | |
| 25 ResultSet rs = stmt.executeQuery(); | |
| 26 while (rs.next()) { | |
| 27 Sequence sequence = getSequence(con, schema, rs.getString("sequencename"), rs.getString("owner")); | |
| 28 sequences.add(sequence); | |
| 29 } | |
| 30 rs.close(); | |
| 31 } finally { | |
| 32 if (stmt != null) stmt.close(); | |
| 33 } | |
| 34 return sequences; | |
| 35 } | |
| 36 | |
| 37 @Override | |
| 38 public Sequence getDbBackupObject(Connection con, String sequenceName, Schema schema) throws SQLException { | |
| 39 PreparedStatement stmt = null; | |
| 40 Sequence sequence = null; | |
| 41 try { | |
| 42 stmt = con.prepareStatement( | |
| 43 "SELECT c.relname AS sequencename, pg_get_userbyid(c.relowner) AS owner FROM pg_class c " + | |
| 44 "WHERE c.relkind='S' AND c.relnamespace = ? AND c.relname = ?"); | |
| 45 stmt.setInt(1, schema.getOid()); | |
| 46 stmt.setString(2, sequenceName); | |
| 47 ResultSet rs = stmt.executeQuery(); | |
| 48 if (rs.next()) { | |
| 49 sequence = getSequence(con, schema, rs.getString("sequencename"), rs.getString("owner")); | |
| 50 } else { | |
| 51 throw new RuntimeException("no such sequence " + sequenceName); | |
| 52 } | |
| 53 rs.close(); | |
| 54 } finally { | |
| 55 if (stmt != null) stmt.close(); | |
| 56 } | |
| 57 return sequence; | |
| 58 } | |
| 59 | |
| 60 } | |
| 61 | |
| 62 static class CachingSequenceFactory extends CachingDBOFactory<Sequence> { | |
| 63 | |
| 64 protected CachingSequenceFactory(Schema.CachingSchemaFactory schemaFactory) { | |
| 65 super(schemaFactory); | |
| 66 } | |
| 67 | |
| 68 @Override | |
| 69 protected PreparedStatement getAllStatement(Connection con) throws SQLException { | |
| 70 return con.prepareStatement( | |
| 71 "SELECT c.relname AS sequencename, pg_get_userbyid(c.relowner) AS owner, " + | |
| 72 "c.relnamespace AS schema_oid FROM pg_class c " + | |
| 73 "WHERE c.relkind='S'"); | |
| 74 } | |
| 75 | |
| 76 @Override | |
| 77 protected Sequence newDbBackupObject(Connection con, ResultSet rs, Schema schema) throws SQLException { | |
| 78 return getSequence(con, schema, rs.getString("sequencename"), rs.getString("owner")); | |
| 79 } | |
| 80 | |
| 81 | |
| 82 } | |
| 83 | |
| 84 private static Sequence getSequence(Connection con, Schema schema, String sequenceName, String owner) throws SQLException { | |
| 85 PreparedStatement stmt = null; | |
| 86 Sequence sequence = null; | |
| 87 try { | |
| 88 stmt = con.prepareStatement( | |
| 89 "SELECT * FROM " + schema.getName() + "." + sequenceName); | |
| 90 ResultSet rs = stmt.executeQuery(); | |
| 91 if (rs.next()) | |
| 92 sequence = new Sequence(sequenceName, rs, schema, owner); | |
| 93 else | |
| 94 throw new RuntimeException("no such sequence: " + sequenceName); | |
| 95 rs.close(); | |
| 96 } finally { | |
| 97 if (stmt != null) stmt.close(); | |
| 98 } | |
| 99 return sequence; | |
| 100 } | |
| 101 | |
| 102 private final long last_value; | |
| 103 private final long start_value; | |
| 104 private final long increment_by; | |
| 105 private final long max_value; | |
| 106 private final long min_value; | |
| 107 private final long cache_value; | |
| 108 private final boolean is_cycled; | |
| 109 | |
| 110 private Sequence(String sequenceName, ResultSet rs, Schema schema, String owner) throws SQLException { | |
| 111 //super(rs.getString("sequence_name"), schema, owner); // postgresql bug? not always consistent with pg_class.relname | |
| 112 super(sequenceName, schema, owner); | |
| 113 this.last_value = rs.getLong("last_value"); | |
| 114 this.start_value = rs.getLong("start_value"); | |
| 115 this.increment_by = rs.getLong("increment_by"); | |
| 116 this.max_value = rs.getLong("max_value"); | |
| 117 this.min_value = rs.getLong("min_value"); | |
| 118 this.is_cycled = rs.getBoolean("is_cycled"); | |
| 119 this.cache_value = rs.getLong("cache_value"); | |
| 120 } | |
| 121 | |
| 122 @Override | |
| 123 protected StringBuilder appendCreateSql(StringBuilder buf, DataFilter dataFilter) { | |
| 124 buf.append("CREATE SEQUENCE "); | |
| 125 buf.append(getName()); | |
| 126 if (increment_by != 1) { | |
| 127 buf.append(" INCREMENT BY "); | |
| 128 buf.append(increment_by); | |
| 129 } | |
| 130 buf.append(" MINVALUE "); | |
| 131 buf.append(min_value); | |
| 132 buf.append(" MAXVALUE "); | |
| 133 buf.append(max_value); | |
| 134 if (is_cycled) | |
| 135 buf.append(" CYCLE"); | |
| 136 if (cache_value > 1) { | |
| 137 buf.append(" CACHE "); | |
| 138 buf.append(cache_value); | |
| 139 } | |
| 140 buf.append(" START "); | |
| 141 buf.append(start_value); | |
| 142 buf.append(";\n"); | |
| 143 if (dataFilter.dumpData(schema.getName(), name)) { | |
| 144 buf.append("SELECT setval('"); | |
| 145 buf.append(getName()); | |
| 146 buf.append("',"); | |
| 147 buf.append(last_value); | |
| 148 buf.append(") ;\n"); | |
| 149 } | |
| 150 return buf; | |
| 151 } | |
| 152 | |
| 153 @Override | |
| 154 protected StringBuilder appendCreateSql(StringBuilder buf) { | |
| 155 throw new UnsupportedOperationException(); | |
| 156 } | |
| 157 } |
