Mercurial Hosting > luan
annotate src/luan/modules/sql/Database.java @ 1268:725e52076f03
remove broken pooling
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 12 Nov 2018 03:54:04 -0700 |
parents | 9fa8b8389578 |
children | 25746915a241 |
rev | line source |
---|---|
1267 | 1 package luan.modules.sql; |
2 | |
3 import java.sql.Connection; | |
4 import java.sql.DriverManager; | |
5 import java.sql.Statement; | |
6 import java.sql.PreparedStatement; | |
7 import java.sql.ResultSet; | |
8 import java.sql.SQLException; | |
9 import java.util.Map; | |
10 import java.util.HashMap; | |
11 import java.util.Properties; | |
12 import org.slf4j.Logger; | |
13 import org.slf4j.LoggerFactory; | |
14 import luan.LuanState; | |
15 import luan.LuanTable; | |
16 import luan.LuanException; | |
17 | |
18 | |
1268
725e52076f03
remove broken pooling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1267
diff
changeset
|
19 public final class Database { |
1267 | 20 private static final Logger logger = LoggerFactory.getLogger(Database.class); |
21 | |
22 public final Connection con; | |
23 private final Map<String,PreparedStatement> pstmts = new HashMap<String,PreparedStatement>(); | |
24 | |
25 public Database(Connection con) { | |
26 this.con = con; | |
27 } | |
28 | |
1268
725e52076f03
remove broken pooling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1267
diff
changeset
|
29 public Database(LuanTable specTbl) |
1267 | 30 throws LuanException, ClassNotFoundException, SQLException |
31 { | |
1268
725e52076f03
remove broken pooling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1267
diff
changeset
|
32 Map<Object,Object> spec = specTbl.asMap(); |
1267 | 33 String cls = getString(spec,"class"); |
34 Class.forName(cls); | |
35 String url = getString(spec,"url"); | |
36 Properties props = new Properties(); | |
37 props.putAll(spec); | |
38 this.con = DriverManager.getConnection(url,props); | |
39 } | |
40 | |
41 private static String getString(Map spec,String key) throws LuanException { | |
42 Object val = spec.remove(key); | |
43 if( val==null ) | |
44 throw new LuanException( "parameter '"+key+"' is required" ); | |
45 if( !(val instanceof String) ) | |
46 throw new LuanException( "parameter '"+key+"' must be a string" ); | |
47 return (String)val; | |
48 } | |
49 | |
50 private PreparedStatement prepareStatement(String sql,Object[] args) throws SQLException { | |
51 PreparedStatement pstmt = pstmts.get(sql); | |
52 if( pstmt==null ) { | |
53 pstmt = con.prepareStatement(sql); | |
54 pstmts.put(sql,pstmt); | |
55 } | |
56 for( int i=0; i<args.length; i++ ) { | |
57 pstmt.setObject(i+1,args[i]); | |
58 } | |
59 return pstmt; | |
60 } | |
61 | |
62 public ResultSet query(String sql,Object... args) throws SQLException { | |
63 if( args.length == 0 ) { | |
64 Statement stmt = con.createStatement(); | |
65 return stmt.executeQuery(sql); | |
66 } else { | |
67 PreparedStatement pstmt = prepareStatement(sql,args); | |
68 return pstmt.executeQuery(); | |
69 } | |
70 } | |
71 | |
72 public int update(String sql,Object... args) throws SQLException { | |
73 if( args.length == 0 ) { | |
74 Statement stmt = con.createStatement(); | |
75 int n = stmt.executeUpdate(sql); | |
76 stmt.close(); | |
77 return n; | |
78 } else { | |
79 PreparedStatement pstmt = prepareStatement(sql,args); | |
80 return pstmt.executeUpdate(); | |
81 } | |
82 } | |
83 | |
84 } |