comparison src/jdbcpgbackup/Schema.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.Collection;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16
17 final class Schema extends DbBackupObject {
18
19 static class SchemaFactory implements DBOFactory<Schema> {
20
21 @Override
22 public List<Schema> getDbBackupObjects(Connection con, Schema ignored) throws SQLException {
23 List<Schema> schemas = new ArrayList<Schema>();
24 PreparedStatement stmt = null;
25 try {
26 stmt = con.prepareStatement(
27 "SELECT nspname, pg_get_userbyid(nspowner) AS owner, oid FROM pg_namespace " +
28 "WHERE nspname NOT LIKE 'pg_%' " +
29 "AND nspname <> 'information_schema'");
30 ResultSet rs = stmt.executeQuery();
31 while (rs.next()) {
32 schemas.add(new Schema(rs.getString("nspname"), rs.getString("owner"), rs.getInt("oid")));
33 }
34 rs.close();
35 } finally {
36 if (stmt != null) stmt.close();
37 }
38 return schemas;
39 }
40
41 @Override
42 public Schema getDbBackupObject(Connection con, String schemaName, Schema ignored) throws SQLException {
43 PreparedStatement stmt = null;
44 Schema schema = null;
45 try {
46 stmt = con.prepareStatement(
47 "SELECT pg_get_userbyid(nspowner) AS owner, oid FROM pg_namespace WHERE nspname = ?");
48 stmt.setString(1, schemaName);
49 ResultSet rs = stmt.executeQuery();
50 if (rs.next())
51 schema = new Schema(schemaName, rs.getString("owner"), rs.getInt("oid"));
52 rs.close();
53 } finally {
54 if (stmt != null) stmt.close();
55 }
56 return schema;
57 }
58
59 }
60
61 static class CachingSchemaFactory implements DBOFactory<Schema> { // does not extend CachingDBOFactory
62
63 protected Map<String,Schema> map = null;
64 private final Map<Integer,Schema> batch = new HashMap<Integer,Schema>();
65 private Iterator<Schema> itr;
66
67 @Override
68 public Collection<Schema> getDbBackupObjects(Connection con, Schema ignored) throws SQLException {
69 if (map == null) {
70 loadMap(con);
71 }
72 return Collections.unmodifiableCollection(map.values());
73 }
74
75 @Override
76 public Schema getDbBackupObject(Connection con, String name, Schema ignored) throws SQLException {
77 if (map == null) {
78 loadMap(con);
79 }
80 return map.get(name);
81 }
82
83 public Collection<Schema> nextBatch(Connection con, int batchSize) throws SQLException {
84 if (itr == null) {
85 itr = getDbBackupObjects(con, null).iterator();
86 }
87 batch.clear();
88 while (itr.hasNext() && batch.size() < batchSize) {
89 Schema schema = itr.next();
90 batch.put(schema.getOid(), schema);
91 }
92 return Collections.unmodifiableCollection(batch.values());
93 }
94
95 public Schema getFromCurrentBatch(int oid) {
96 return batch.get(oid);
97 }
98
99 private void loadMap(Connection con) throws SQLException {
100 map = new HashMap<String,Schema>();
101 PreparedStatement stmt = null;
102 try {
103 stmt = con.prepareStatement(
104 "SELECT nspname AS schemaname, pg_get_userbyid(nspowner) AS owner, oid FROM pg_namespace " +
105 "WHERE nspname NOT LIKE 'pg_%' " +
106 "AND nspname <> 'information_schema'");
107 ResultSet rs = stmt.executeQuery();
108 while (rs.next()) {
109 String schemaName = rs.getString("schemaname");
110 map.put(schemaName, new Schema(rs.getString("schemaname"), rs.getString("owner"), rs.getInt("oid")));
111 }
112 rs.close();
113 } finally {
114 if (stmt != null) stmt.close();
115 }
116 }
117
118 }
119
120 static Schema createSchema(Connection con, String schemaName, String owner, DBOFactory<Schema> schemaFactory) throws SQLException {
121 PreparedStatement stmt = null;
122 try {
123 stmt = con.prepareStatement(
124 "CREATE SCHEMA " + schemaName + " AUTHORIZATION " + owner);
125 stmt.executeUpdate();
126 } finally {
127 if (stmt != null) stmt.close();
128 }
129 return schemaFactory.getDbBackupObject(con, schemaName, null);
130 }
131
132 private final int oid;
133
134 private Schema(String name, String owner, int oid) {
135 super(name, null, owner);
136 this.oid = oid;
137 }
138
139 @Override
140 String getSql(DataFilter dataFilter) {
141 return "CREATE SCHEMA " + name + " AUTHORIZATION " + owner + " ;\n";
142 }
143
144 @Override
145 protected StringBuilder appendCreateSql(StringBuilder buf) {
146 throw new UnsupportedOperationException();
147 }
148
149 @Override
150 String getFullname() {
151 return name;
152 }
153
154 int getOid() {
155 return oid;
156 }
157 }