comparison 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
comparison
equal deleted inserted replaced
1391:94f48cc76de8 1392:002152af497a
12 import luan.lib.logging.Logger; 12 import luan.lib.logging.Logger;
13 import luan.lib.logging.LoggerFactory; 13 import luan.lib.logging.LoggerFactory;
14 import luan.Luan; 14 import luan.Luan;
15 import luan.LuanTable; 15 import luan.LuanTable;
16 import luan.LuanException; 16 import luan.LuanException;
17 import luan.modules.Utils;
17 18
18 19
19 public final class Database { 20 public final class Database {
20 private static final Logger logger = LoggerFactory.getLogger(Database.class); 21 private static final Logger logger = LoggerFactory.getLogger(Database.class);
21 22
29 30
30 public Database(LuanTable specTbl) 31 public Database(LuanTable specTbl)
31 throws LuanException, ClassNotFoundException, SQLException 32 throws LuanException, ClassNotFoundException, SQLException
32 { 33 {
33 Map<Object,Object> spec = specTbl.asMap(); 34 Map<Object,Object> spec = specTbl.asMap();
34 String cls = getString(spec,"class"); 35 String cls = Utils.removeRequiredString(spec,"class");
35 Class.forName(cls); 36 Class.forName(cls);
36 String url = getString(spec,"url"); 37 String url = Utils.removeRequiredString(spec,"url");
37 Properties props = new Properties(); 38 Properties props = new Properties();
38 props.putAll(spec); 39 props.putAll(spec);
39 this.con = DriverManager.getConnection(url,props); 40 this.con = DriverManager.getConnection(url,props);
41 spec.remove("user");
42 spec.remove("password");
40 set(spec); 43 set(spec);
41 } 44 }
42 45
43 public void set(LuanTable options) throws LuanException, SQLException { 46 public void set(LuanTable options) throws LuanException, SQLException {
44 set(options.asMap()); 47 set(options.asMap());
45 } 48 }
46 49
47 private void set(Map<Object,Object> options) throws LuanException, SQLException { 50 private void set(Map<Object,Object> options) throws LuanException, SQLException {
48 Object obj; 51 Boolean autoCommit = Utils.removeBoolean(options,"auto_commit");
49 obj = options.remove("auto_commit"); 52 if( autoCommit != null )
50 if( obj != null ) { 53 con.setAutoCommit(autoCommit);
51 if( !(obj instanceof Boolean) ) 54 Integer n = Utils.removeInteger(options,"fetch_size");
52 throw new LuanException( "parameter 'auto_commit' must be a boolean" ); 55 if( n != null )
53 con.setAutoCommit((Boolean)obj);
54 }
55 obj = options.remove("fetch_size");
56 if( obj != null ) {
57 Integer n = Luan.asInteger(obj);
58 if( n == null )
59 throw new LuanException( "parameter 'fetch_size' must be an integer" );
60 fetchSize = n; 56 fetchSize = n;
61 } 57 Utils.checkEmpty(options);
62 if( !options.isEmpty() )
63 throw new LuanException( "unrecognized parameters: "+options );
64 }
65
66 private static String getString(Map spec,String key) throws LuanException {
67 Object val = spec.remove(key);
68 if( val==null )
69 throw new LuanException( "parameter '"+key+"' is required" );
70 if( !(val instanceof String) )
71 throw new LuanException( "parameter '"+key+"' must be a string" );
72 return (String)val;
73 } 58 }
74 59
75 private void fix(Statement stmt) throws SQLException { 60 private void fix(Statement stmt) throws SQLException {
76 if( fetchSize > 0 ) 61 if( fetchSize > 0 )
77 stmt.setFetchSize(fetchSize); 62 stmt.setFetchSize(fetchSize);