comparison src/luan/modules/sql/Database.java @ 1267:9fa8b8389578

add LuanTable.luan; support metatable __gc(); add luan.sql;
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Nov 2018 02:10:41 -0700
parents
children 725e52076f03
comparison
equal deleted inserted replaced
1266:05934fbf635a 1267:9fa8b8389578
1 package luan.modules.sql;
2
3 import java.io.Closeable;
4 import java.sql.Connection;
5 import java.sql.DriverManager;
6 import java.sql.Statement;
7 import java.sql.PreparedStatement;
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10 import java.util.Map;
11 import java.util.HashMap;
12 import java.util.Properties;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import luan.LuanState;
16 import luan.LuanTable;
17 import luan.LuanException;
18
19
20 public final class Database implements Closeable {
21 private static final Logger logger = LoggerFactory.getLogger(Database.class);
22
23 private static Map<Map,Database> pool = new HashMap<Map,Database>();
24
25 public static Database get(Connection con) {
26 return new Database(con);
27 }
28
29 public static synchronized Database get(LuanTable specTbl)
30 throws LuanException, ClassNotFoundException, SQLException
31 {
32 Map<Object,Object> spec = specTbl.asMap();
33 Database db = pool.get(spec);
34 if( db==null ) {
35 db = new Database(spec);
36 pool.put(spec,db);
37 }
38 return db;
39 }
40
41 public int uses = 0;
42 public final Connection con;
43 private final Map<String,PreparedStatement> pstmts = new HashMap<String,PreparedStatement>();
44
45 public Database(Connection con) {
46 this.con = con;
47 }
48
49 private Database(Map<Object,Object> spec)
50 throws LuanException, ClassNotFoundException, SQLException
51 {
52 spec = new HashMap<Object,Object>(spec);
53 String cls = getString(spec,"class");
54 Class.forName(cls);
55 String url = getString(spec,"url");
56 Properties props = new Properties();
57 props.putAll(spec);
58 this.con = DriverManager.getConnection(url,props);
59 }
60
61 private static String getString(Map spec,String key) throws LuanException {
62 Object val = spec.remove(key);
63 if( val==null )
64 throw new LuanException( "parameter '"+key+"' is required" );
65 if( !(val instanceof String) )
66 throw new LuanException( "parameter '"+key+"' must be a string" );
67 return (String)val;
68 }
69
70 private PreparedStatement prepareStatement(String sql,Object[] args) throws SQLException {
71 PreparedStatement pstmt = pstmts.get(sql);
72 if( pstmt==null ) {
73 pstmt = con.prepareStatement(sql);
74 pstmts.put(sql,pstmt);
75 }
76 for( int i=0; i<args.length; i++ ) {
77 pstmt.setObject(i+1,args[i]);
78 }
79 return pstmt;
80 }
81
82 public ResultSet query(String sql,Object... args) throws SQLException {
83 if( args.length == 0 ) {
84 Statement stmt = con.createStatement();
85 return stmt.executeQuery(sql);
86 } else {
87 PreparedStatement pstmt = prepareStatement(sql,args);
88 return pstmt.executeQuery();
89 }
90 }
91
92 public int update(String sql,Object... args) throws SQLException {
93 if( args.length == 0 ) {
94 Statement stmt = con.createStatement();
95 int n = stmt.executeUpdate(sql);
96 stmt.close();
97 return n;
98 } else {
99 PreparedStatement pstmt = prepareStatement(sql,args);
100 return pstmt.executeUpdate();
101 }
102 }
103
104 public void close() {
105 try {
106 con.close();
107 } catch(SQLException e) {
108 throw new RuntimeException(e);
109 }
110 }
111
112 protected void finalize() throws Throwable {
113 if( !con.isClosed() ) {
114 logger.error("not closed");
115 close();
116 }
117 super.finalize();
118 }
119
120 }