Mercurial Hosting > luan
comparison src/luan/modules/sql/Database.java @ 1341:a015a0b5c388
add Html.decode(), Lucene.count_tokens(), lucene boosts, Sql.database.set()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 19 Feb 2019 08:14:40 -0700 |
parents | 8b61c8c4e07a |
children | 002152af497a |
comparison
equal
deleted
inserted
replaced
1340:b3c4fcf29a53 | 1341:a015a0b5c388 |
---|---|
9 import java.util.Map; | 9 import java.util.Map; |
10 import java.util.HashMap; | 10 import java.util.HashMap; |
11 import java.util.Properties; | 11 import java.util.Properties; |
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.LuanTable; | 15 import luan.LuanTable; |
15 import luan.LuanException; | 16 import luan.LuanException; |
16 | 17 |
17 | 18 |
18 public final class Database { | 19 public final class Database { |
19 private static final Logger logger = LoggerFactory.getLogger(Database.class); | 20 private static final Logger logger = LoggerFactory.getLogger(Database.class); |
20 | 21 |
21 public final Connection con; | 22 public final Connection con; |
22 private final Map<String,PreparedStatement> pstmts = new HashMap<String,PreparedStatement>(); | 23 private final Map<String,PreparedStatement> pstmts = new HashMap<String,PreparedStatement>(); |
24 private int fetchSize = 0; | |
23 | 25 |
24 public Database(Connection con) { | 26 public Database(Connection con) { |
25 this.con = con; | 27 this.con = con; |
26 } | 28 } |
27 | 29 |
33 Class.forName(cls); | 35 Class.forName(cls); |
34 String url = getString(spec,"url"); | 36 String url = getString(spec,"url"); |
35 Properties props = new Properties(); | 37 Properties props = new Properties(); |
36 props.putAll(spec); | 38 props.putAll(spec); |
37 this.con = DriverManager.getConnection(url,props); | 39 this.con = DriverManager.getConnection(url,props); |
40 set(spec); | |
41 } | |
42 | |
43 public void set(LuanTable options) throws LuanException, SQLException { | |
44 set(options.asMap()); | |
45 } | |
46 | |
47 private void set(Map<Object,Object> options) throws LuanException, SQLException { | |
48 Object obj; | |
49 obj = options.remove("auto_commit"); | |
50 if( obj != null ) { | |
51 if( !(obj instanceof Boolean) ) | |
52 throw new LuanException( "parameter 'auto_commit' must be a boolean" ); | |
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; | |
61 } | |
62 if( !options.isEmpty() ) | |
63 throw new LuanException( "unrecognized parameters: "+options ); | |
38 } | 64 } |
39 | 65 |
40 private static String getString(Map spec,String key) throws LuanException { | 66 private static String getString(Map spec,String key) throws LuanException { |
41 Object val = spec.remove(key); | 67 Object val = spec.remove(key); |
42 if( val==null ) | 68 if( val==null ) |
44 if( !(val instanceof String) ) | 70 if( !(val instanceof String) ) |
45 throw new LuanException( "parameter '"+key+"' must be a string" ); | 71 throw new LuanException( "parameter '"+key+"' must be a string" ); |
46 return (String)val; | 72 return (String)val; |
47 } | 73 } |
48 | 74 |
75 private void fix(Statement stmt) throws SQLException { | |
76 if( fetchSize > 0 ) | |
77 stmt.setFetchSize(fetchSize); | |
78 } | |
79 | |
49 private PreparedStatement prepareStatement(String sql,Object[] args) throws SQLException { | 80 private PreparedStatement prepareStatement(String sql,Object[] args) throws SQLException { |
50 PreparedStatement pstmt = pstmts.get(sql); | 81 PreparedStatement pstmt = pstmts.get(sql); |
51 if( pstmt==null ) { | 82 if( pstmt==null ) { |
52 pstmt = con.prepareStatement(sql); | 83 pstmt = con.prepareStatement(sql); |
84 fix(pstmt); | |
53 pstmts.put(sql,pstmt); | 85 pstmts.put(sql,pstmt); |
54 } | 86 } |
55 for( int i=0; i<args.length; i++ ) { | 87 for( int i=0; i<args.length; i++ ) { |
56 pstmt.setObject(i+1,args[i]); | 88 pstmt.setObject(i+1,args[i]); |
57 } | 89 } |
59 } | 91 } |
60 | 92 |
61 public ResultSet query(String sql,Object... args) throws SQLException { | 93 public ResultSet query(String sql,Object... args) throws SQLException { |
62 if( args.length == 0 ) { | 94 if( args.length == 0 ) { |
63 Statement stmt = con.createStatement(); | 95 Statement stmt = con.createStatement(); |
96 fix(stmt); | |
64 return stmt.executeQuery(sql); | 97 return stmt.executeQuery(sql); |
65 } else { | 98 } else { |
66 PreparedStatement pstmt = prepareStatement(sql,args); | 99 PreparedStatement pstmt = prepareStatement(sql,args); |
67 return pstmt.executeQuery(); | 100 return pstmt.executeQuery(); |
68 } | 101 } |
69 } | 102 } |
70 | 103 |
71 public int update(String sql,Object... args) throws SQLException { | 104 public int update(String sql,Object... args) throws SQLException { |
72 if( args.length == 0 ) { | 105 if( args.length == 0 ) { |
73 Statement stmt = con.createStatement(); | 106 Statement stmt = con.createStatement(); |
107 fix(stmt); | |
74 int n = stmt.executeUpdate(sql); | 108 int n = stmt.executeUpdate(sql); |
75 stmt.close(); | 109 stmt.close(); |
76 return n; | 110 return n; |
77 } else { | 111 } else { |
78 PreparedStatement pstmt = prepareStatement(sql,args); | 112 PreparedStatement pstmt = prepareStatement(sql,args); |