comparison src/jdbcpgbackup/View.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 View extends DbBackupObject {
13
14 static class ViewFactory implements DBOFactory<View> {
15
16 @Override
17 public Iterable<View> getDbBackupObjects(Connection con, Schema schema) throws SQLException {
18 List<View> views = new ArrayList<View>();
19 PreparedStatement stmt = null;
20 try {
21 stmt = con.prepareStatement(
22 "SELECT * FROM pg_views WHERE schemaname = ?");
23 stmt.setString(1, schema.getName());
24 ResultSet rs = stmt.executeQuery();
25 while (rs.next()) {
26 views.add(new View(rs.getString("viewname"), schema, rs.getString("viewowner"), rs.getString("definition")));
27 }
28 rs.close();
29 } finally {
30 if (stmt != null) stmt.close();
31 }
32 return views;
33 }
34
35 @Override
36 public View getDbBackupObject(Connection con, String viewName, Schema schema) throws SQLException {
37 View view = null;
38 PreparedStatement stmt = null;
39 try {
40 stmt = con.prepareStatement(
41 "SELECT * FROM pg_views WHERE schemaname = ? AND viewname = ?");
42 stmt.setString(1, schema.getName());
43 stmt.setString(2, viewName);
44 ResultSet rs = stmt.executeQuery();
45 if (rs.next())
46 view = new View(viewName, schema, rs.getString("viewowner"), rs.getString("definition"));
47 else
48 throw new RuntimeException("no such view: " + viewName);
49 rs.close();
50 } finally {
51 if (stmt != null) stmt.close();
52 }
53 return view;
54 }
55 }
56
57 static class CachingViewFactory extends CachingDBOFactory<View> {
58
59 protected CachingViewFactory(Schema.CachingSchemaFactory schemaFactory) {
60 super(schemaFactory);
61 }
62
63 @Override
64 protected final PreparedStatement getAllStatement(Connection con) throws SQLException {
65 return con.prepareStatement(
66 "SELECT c.relnamespace AS schema_oid, c.relname AS viewname, pg_get_userbyid(c.relowner) AS viewowner, " +
67 "pg_get_viewdef(c.oid) AS definition " +
68 "FROM pg_class c " +
69 "WHERE c.relkind = 'v'::\"char\"");
70 /*
71 "SELECT * FROM pg_views " +
72 "WHERE schemaname NOT LIKE 'pg_%' " +
73 "AND schemaname <> 'information_schema'");
74 */
75 }
76
77 @Override
78 protected final View newDbBackupObject(Connection con, ResultSet rs, Schema schema) throws SQLException {
79 return new View(rs.getString("viewname"), schema,
80 rs.getString("viewowner"), rs.getString("definition"));
81 }
82
83 }
84
85
86 private final String definition;
87
88 private View(String name, Schema schema, String owner, String definition) {
89 super(name, schema, owner);
90 this.definition = definition;
91 }
92
93 @Override
94 protected StringBuilder appendCreateSql(StringBuilder buf) {
95 buf.append("CREATE VIEW ");
96 buf.append(getName());
97 buf.append(" AS ");
98 buf.append(definition);
99 buf.append(" ;\n");
100 return buf;
101 }
102
103 }