diff src/luan/modules/lucene/PostgresBackup.java @ 1392:002152af497a

hosted postgres
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 Sep 2019 00:19:47 -0600
parents 94f48cc76de8
children cc0dbca576dc
line wrap: on
line diff
--- a/src/luan/modules/lucene/PostgresBackup.java	Thu Sep 05 01:29:57 2019 -0600
+++ b/src/luan/modules/lucene/PostgresBackup.java	Fri Sep 06 00:19:47 2019 -0600
@@ -10,11 +10,14 @@
 import java.util.Properties;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Map;
 import luan.Luan;
 import luan.LuanTable;
 import luan.LuanFunction;
 import luan.LuanException;
+import luan.modules.Utils;
 import luan.modules.parsers.LuanToString;
+import luan.modules.logging.LuanLogger;
 import luan.lib.logging.Logger;
 import luan.lib.logging.LoggerFactory;
 
@@ -22,18 +25,6 @@
 final class PostgresBackup {
 	private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class);
 
-	static PostgresBackup newInstance() {
-		try {
-			return new PostgresBackup();
-		} catch(ClassNotFoundException e) {
-			logger.error("creation failed",e);
-			return null;
-		} catch(SQLException e) {
-			logger.error("creation failed",e);
-			return null;
-		}
-	}
-
 	final boolean wasCreated;
 	private final String url;
 	private final Properties props = new Properties();
@@ -44,14 +35,23 @@
 	private int trans = 0;
 	private final LuanToString luanToString = new LuanToString();
 
-	private PostgresBackup()
-		throws ClassNotFoundException, SQLException
+	PostgresBackup(Map spec)
+		throws ClassNotFoundException, SQLException, LuanException
 	{
+/*
 		Class.forName("org.postgresql.Driver");
-
 		url = "jdbc:postgresql://localhost:5432/luan";
 		props.setProperty("user","postgres");
 		props.setProperty("password","");
+*/
+		String cls = "org.postgresql.Driver";
+		if( !Utils.removeRequiredString(spec,"class").equals(cls) )
+			throw new LuanException( "parameter 'class' must be '"+cls+"'" );
+		Class.forName(cls);
+		url = Utils.removeRequiredString(spec,"url");
+		props.setProperty( "user", Utils.removeRequiredString(spec,"user") );
+		props.setProperty( "password", Utils.removeRequiredString(spec,"password") );
+		Utils.checkEmpty(spec);
 
 		con = newConnection();
 
@@ -88,15 +88,11 @@
 		return DriverManager.getConnection(url,props);
 	}
 
-	void close() {
-		try {
-			insertStmt.close();
-			updateStmt.close();
-			deleteStmt.close();
-			con.close();
-		} catch(SQLException e) {
-			logger.error("close failed",e);
-		}
+	void close() throws SQLException {
+		insertStmt.close();
+		updateStmt.close();
+		deleteStmt.close();
+		con.close();
 	}
 
 	protected void finalize() throws Throwable {
@@ -107,90 +103,58 @@
 		}
 	}
 
-	void add(LuanTable doc) throws LuanException {
-		try {
-			Long id = (Long)doc.get("id");
-			String data = luanToString.toString(doc);
-			insertStmt.setLong(1,id);
-			insertStmt.setString(2,data);
-			insertStmt.executeUpdate();
-		} catch(SQLException e) {
-			logger.error("add failed",e);
-		}
+	void add(LuanTable doc) throws LuanException, SQLException {
+		Long id = (Long)doc.get("id");
+		String data = luanToString.toString(doc);
+		insertStmt.setLong(1,id);
+		insertStmt.setString(2,data);
+		insertStmt.executeUpdate();
 	}
 
-	void update(LuanTable doc) throws LuanException {
-		try {
-			Long id = (Long)doc.get("id");
-			String data = luanToString.toString(doc);
-			updateStmt.setString(1,data);
-			updateStmt.setLong(2,id);
-			int n = updateStmt.executeUpdate();
-			if( n==0 ) {
-				logger.error("update not found for id="+id+", trying add");
-				add(doc);
-			} else if( n!=1 )
-				throw new RuntimeException();
-		} catch(SQLException e) {
-			logger.error("update failed",e);
-		}
-	}
-
-	void deleteAll() {
-		try {
-			Statement stmt = con.createStatement();
-			stmt.executeUpdate("delete from lucene");
-			stmt.close();
-		} catch(SQLException e) {
-			logger.error("update failed",e);
-		}
+	void update(LuanTable doc) throws LuanException, SQLException {
+		Long id = (Long)doc.get("id");
+		String data = luanToString.toString(doc);
+		updateStmt.setString(1,data);
+		updateStmt.setLong(2,id);
+		int n = updateStmt.executeUpdate();
+		if( n==0 ) {
+			Logger logger = LuanLogger.getLogger(doc.luan(),PostgresBackup.class);
+			logger.error("update not found for id="+id+", trying add");
+			add(doc);
+		} else if( n!=1 )
+			throw new RuntimeException();
 	}
 
-	void delete(long id) {
-		try {
-			deleteStmt.setLong(1,id);
-			int n = deleteStmt.executeUpdate();
-			if( n==0 ) {
-				logger.error("delete not found for id="+id);
-			}
-		} catch(SQLException e) {
-			logger.error("update failed",e);
-		}
+	void deleteAll() throws SQLException {
+		Statement stmt = con.createStatement();
+		stmt.executeUpdate("delete from lucene");
+		stmt.close();
 	}
 
-	void begin() {
-		try {
-			if( trans++ == 0 )
-				con.setAutoCommit(false);
-		} catch(SQLException e) {
-			logger.error("begin failed",e);
-		}
+	void delete(long id) throws SQLException, LuanException {
+		deleteStmt.setLong(1,id);
+		int n = deleteStmt.executeUpdate();
+		if( n==0 )
+			throw new LuanException("delete not found for id="+id);
 	}
 
-	void commit() {
-		try {
-			if( trans <= 0 ) {
-				logger.error("commit not in transaction");
-				return;
-			}
-			if( --trans == 0 )
-				con.setAutoCommit(true);
-		} catch(SQLException e) {
-			logger.error("begin failed",e);
-		}
+	void begin() throws SQLException {
+		if( trans++ == 0 )
+			con.setAutoCommit(false);
 	}
 
-	void rollback() {
-		try {
-			if( --trans != 0 ) {
-				logger.error("rollback failed trans="+trans);
-				return;
-			}
-			con.rollback();
+	void commit() throws SQLException, LuanException {
+		if( trans <= 0 )
+			throw new LuanException("commit not in transaction");
+		if( --trans == 0 )
 			con.setAutoCommit(true);
-		} catch(SQLException e) {
-			logger.error("begin failed",e);
-		}
+	}
+
+	void rollback() throws SQLException, LuanException {
+		if( --trans != 0 )
+			throw new LuanException("rollback failed trans="+trans);
+		con.rollback();
+		con.setAutoCommit(true);
 	}
 
 	private static LuanTable newEnv() {
@@ -205,38 +169,28 @@
 	}
 
 	void restoreLucene(LuceneIndex li)
-		throws LuanException, IOException
+		throws LuanException, IOException, SQLException
 	{
-		try {
-			LuanTable env = newEnv();
-			Statement stmt = con.createStatement();
-			ResultSet rs = stmt.executeQuery("select data from lucene");
-			while( rs.next() ) {
-				String data = rs.getString("data");
-				LuanTable doc = (LuanTable)eval(data,env);
-				li.restore(doc);
-			}
-			stmt.close();
-		} catch(SQLException e) {
-			logger.error("restoreLucene failed",e);
-			throw new RuntimeException(e);
+		LuanTable env = newEnv();
+		Statement stmt = con.createStatement();
+		ResultSet rs = stmt.executeQuery("select data from lucene");
+		while( rs.next() ) {
+			String data = rs.getString("data");
+			LuanTable doc = (LuanTable)eval(data,env);
+			li.restore(doc);
 		}
+		stmt.close();
 	}
 
 	long maxId()
-		throws LuanException, IOException
+		throws LuanException, IOException, SQLException
 	{
-		try {
-			Statement stmt = con.createStatement();
-			ResultSet rs = stmt.executeQuery("select max(id) as m from lucene");
-			rs.next();
-			long m = rs.getLong("m");
-			stmt.close();
-			return m;
-		} catch(SQLException e) {
-			logger.error("maxId failed",e);
-			throw new RuntimeException(e);
-		}
+		Statement stmt = con.createStatement();
+		ResultSet rs = stmt.executeQuery("select max(id) as m from lucene");
+		rs.next();
+		long m = rs.getLong("m");
+		stmt.close();
+		return m;
 	}
 
 	final class Checker {