comparison src/luan/modules/lucene/PostgresBackup.java @ 1562:b89212fd04b5

remove table.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 08 Nov 2020 16:50:59 -0700
parents 26c51acf00f3
children 8fbcc4747091
comparison
equal deleted inserted replaced
1561:e1a13e707bf3 1562:b89212fd04b5
32 private final Connection con; 32 private final Connection con;
33 private final PreparedStatement insertStmt; 33 private final PreparedStatement insertStmt;
34 private final PreparedStatement updateStmt; 34 private final PreparedStatement updateStmt;
35 private final PreparedStatement deleteStmt; 35 private final PreparedStatement deleteStmt;
36 private int trans = 0; 36 private int trans = 0;
37 private final LuanToString luanToString = new LuanToString(null,null); 37 private final LuanToString luanToString;
38 38
39 PostgresBackup(Luan luan,LuanTable spec) 39 PostgresBackup(Luan luan,LuanTable spec)
40 throws ClassNotFoundException, SQLException, LuanException 40 throws ClassNotFoundException, SQLException, LuanException
41 { 41 {
42 spec = new LuanTable(spec); 42 spec = new LuanTable(spec);
80 ); 80 );
81 deleteStmt = con.prepareStatement( 81 deleteStmt = con.prepareStatement(
82 "delete from lucene where id=?" 82 "delete from lucene where id=?"
83 ); 83 );
84 84
85 luanToString = new LuanToString(luan,null,null);
85 luanToString.settingsInit.strict = true; 86 luanToString.settingsInit.strict = true;
86 luanToString.settingsInit.numberTypes = true; 87 luanToString.settingsInit.numberTypes = true;
87 } 88 }
88 89
89 Connection newConnection() throws SQLException { 90 Connection newConnection() throws SQLException {
100 protected void finalize() throws Throwable { 101 protected void finalize() throws Throwable {
101 close(); 102 close();
102 super.finalize(); 103 super.finalize();
103 } 104 }
104 105
105 void add(LuanTable doc) throws LuanException, SQLException { 106 void add(Luan luan,LuanTable doc) throws LuanException, SQLException {
106 Long id = (Long)doc.get("id"); 107 Long id = (Long)doc.get(luan,"id");
107 String data = luanToString.toString(doc); 108 String data = luanToString.toString(doc);
108 insertStmt.setLong(1,id); 109 insertStmt.setLong(1,id);
109 insertStmt.setString(2,data); 110 insertStmt.setString(2,data);
110 insertStmt.executeUpdate(); 111 insertStmt.executeUpdate();
111 } 112 }
112 113
113 void update(LuanTable doc) throws LuanException, SQLException { 114 void update(Luan luan,LuanTable doc) throws LuanException, SQLException {
114 Long id = (Long)doc.get("id"); 115 Long id = (Long)doc.get(luan,"id");
115 String data = luanToString.toString(doc); 116 String data = luanToString.toString(doc);
116 updateStmt.setString(1,data); 117 updateStmt.setString(1,data);
117 updateStmt.setLong(2,id); 118 updateStmt.setLong(2,id);
118 int n = updateStmt.executeUpdate(); 119 int n = updateStmt.executeUpdate();
119 if( n==0 ) { 120 if( n==0 ) {
120 logger.error("update not found for id="+id+", trying add"); 121 logger.error("update not found for id="+id+", trying add");
121 add(doc); 122 add(luan,doc);
122 } else if( n!=1 ) 123 } else if( n!=1 )
123 throw new RuntimeException(); 124 throw new RuntimeException();
124 } 125 }
125 126
126 void deleteAll() throws SQLException { 127 void deleteAll() throws SQLException {
156 } 157 }
157 158
158 void restoreLucene(LuceneIndex li) 159 void restoreLucene(LuceneIndex li)
159 throws LuanException, IOException, SQLException, ParseException 160 throws LuanException, IOException, SQLException, ParseException
160 { 161 {
161 Luan luan = new Luan();
162 Statement stmt = con.createStatement(); 162 Statement stmt = con.createStatement();
163 ResultSet rs = stmt.executeQuery("select data from lucene"); 163 ResultSet rs = stmt.executeQuery("select data from lucene");
164 while( rs.next() ) { 164 while( rs.next() ) {
165 String data = rs.getString("data"); 165 String data = rs.getString("data");
166 LuanTable doc = (LuanTable)LuanParser.parse(luan,data); 166 LuanTable doc = (LuanTable)LuanParser.parse(data);
167 li.restore(doc); 167 li.restore(doc);
168 } 168 }
169 stmt.close(); 169 stmt.close();
170 } 170 }
171 171
172 final class Checker { 172 final class Checker {
173 private final Connection con; 173 private final Connection con;
174 private final PreparedStatement pstmt; 174 private final PreparedStatement pstmt;
175 private final Luan luan = new Luan();
176 175
177 Checker() throws SQLException { 176 Checker() throws SQLException {
178 con = newConnection(); 177 con = newConnection();
179 con.setReadOnly(true); 178 con.setReadOnly(true);
180 pstmt = con.prepareStatement( 179 pstmt = con.prepareStatement(
203 pstmt.setLong(1,id); 202 pstmt.setLong(1,id);
204 ResultSet rs = pstmt.executeQuery(); 203 ResultSet rs = pstmt.executeQuery();
205 if( !rs.next() ) 204 if( !rs.next() )
206 return null; 205 return null;
207 String data = rs.getString("data"); 206 String data = rs.getString("data");
208 LuanTable doc = (LuanTable)LuanParser.parse(luan,data); 207 LuanTable doc = (LuanTable)LuanParser.parse(data);
209 return doc; 208 return doc;
210 } 209 }
211 } 210 }
212 211
213 Checker newChecker() throws SQLException { 212 Checker newChecker() throws SQLException {