diff src/luan/modules/sql/Database.java @ 1392:002152af497a

hosted postgres
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 Sep 2019 00:19:47 -0600
parents a015a0b5c388
children 27efb1fcbcb5
line wrap: on
line diff
--- a/src/luan/modules/sql/Database.java	Thu Sep 05 01:29:57 2019 -0600
+++ b/src/luan/modules/sql/Database.java	Fri Sep 06 00:19:47 2019 -0600
@@ -14,6 +14,7 @@
 import luan.Luan;
 import luan.LuanTable;
 import luan.LuanException;
+import luan.modules.Utils;
 
 
 public final class Database {
@@ -31,12 +32,14 @@
 		throws LuanException, ClassNotFoundException, SQLException
 	{
 		Map<Object,Object> spec = specTbl.asMap();
-		String cls = getString(spec,"class");
+		String cls = Utils.removeRequiredString(spec,"class");
 		Class.forName(cls);
-		String url = getString(spec,"url");
+		String url = Utils.removeRequiredString(spec,"url");
 		Properties props = new Properties();
 		props.putAll(spec);
 		this.con = DriverManager.getConnection(url,props);
+		spec.remove("user");
+		spec.remove("password");
 		set(spec);
 	}
 
@@ -45,31 +48,13 @@
 	}
 
 	private void set(Map<Object,Object> options) throws LuanException, SQLException {
-		Object obj;
-		obj = options.remove("auto_commit");
-		if( obj != null ) {
-			if( !(obj instanceof Boolean) )
-				throw new LuanException( "parameter 'auto_commit' must be a boolean" );
-			con.setAutoCommit((Boolean)obj);
-		}
-		obj = options.remove("fetch_size");
-		if( obj != null ) {
-			Integer n = Luan.asInteger(obj);
-			if( n == null )
-				throw new LuanException( "parameter 'fetch_size' must be an integer" );
+		Boolean autoCommit = Utils.removeBoolean(options,"auto_commit");
+		if( autoCommit != null )
+			con.setAutoCommit(autoCommit);
+		Integer n = Utils.removeInteger(options,"fetch_size");
+		if( n != null )
 			fetchSize = n;
-		}
-		if( !options.isEmpty() )
-			throw new LuanException( "unrecognized parameters: "+options );
-	}
-
-	private static String getString(Map spec,String key) throws LuanException {
-		Object val = spec.remove(key);
-		if( val==null )
-			throw new LuanException( "parameter '"+key+"' is required" );
-		if( !(val instanceof String) )
-			throw new LuanException( "parameter '"+key+"' must be a string" );
-		return (String)val;
+		Utils.checkEmpty(options);
 	}
 
 	private void fix(Statement stmt) throws SQLException {