• R/O
  • SSH
  • HTTPS

estocdb: Commit


Commit MetaInfo

Revisão30 (tree)
Hora2010-08-26 17:22:31
Autorshun_m

Mensagem de Log

chenge parameter
from Connection to ConnectionProvider interface

Mudança Sumário

Diff

--- trunk/test/TestAnnotateDerby.java (revision 29)
+++ trunk/test/TestAnnotateDerby.java (revision 30)
@@ -8,16 +8,16 @@
88 import org.junit.Test;
99
1010 import estoc.dbm.AccessManager;
11+import estoc.dbm.ConnectionProvider;
1112 import estoc.dbm.DataAccess;
1213 import estoc.dbm.DbType;
1314 import estoc.dbm.TableAccess;
1415
1516 public class TestAnnotateDerby extends TestAnnotate {
16- private Connection conn;
17+ private ConnectionProvider provider;
1718 private AccessManager am;
1819
19- @BeforeClass
20- public static void preStart() throws Exception {
20+ @BeforeClass public static void preStart() throws Exception {
2121 File dir = new File("../estocdb/testdb");
2222 if (dir.exists()) {
2323 delDir(dir.listFiles());
@@ -34,58 +34,51 @@
3434 }
3535 }
3636
37- @Override
38- @Before
39- public void setup() throws Exception {
37+ @Override @Before public void setup() throws Exception {
4038 Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
41- conn = DriverManager.getConnection("jdbc:derby:testdb;create=true");
39+ provider = new ConnectionProvider() {
40+ @Override public Connection getConnection() throws SQLException {
41+ return DriverManager.getConnection("jdbc:derby:testdb;create=true");
42+ }
43+ };
4244 am = new AccessManager(DbType.Derby);
4345 }
4446
45- @Override
46- protected DataAccess getDataAccess() {
47- return am.getDataAccess(conn);
47+ @Override protected DataAccess getDataAccess() {
48+ return am.getDataAccess(provider);
4849 }
4950
50- @Override
51- protected TableAccess getTableAccess() {
52- return am.getTableAccess(conn);
51+ @Override protected TableAccess getTableAccess() {
52+ try {
53+ return am.getTableAccess(provider);
54+ } catch (Exception e) {
55+ e.printStackTrace();
56+ return null;
57+ }
5358 }
5459
5560 // ------------------------------------------------------
56- @Override
57- @Test
58- public void testAutoInc() throws SQLException {
61+ @Override @Test public void testAutoInc() throws SQLException {
5962 super.testAutoInc();
6063 }
6164
62- @Override
63- @Test
64- public void testIndex() throws SQLException {
65+ @Override @Test public void testIndex() throws SQLException {
6566 super.testIndex();
6667 }
6768
68- @Override
69- @Test
70- public void testNotNull() throws SQLException {
69+ @Override @Test public void testNotNull() throws SQLException {
7170 super.testNotNull();
7271 }
7372
74- @Override
75- @Test
76- public void testPk() throws SQLException {
73+ @Override @Test public void testPk() throws SQLException {
7774 super.testPk();
7875 }
7976
80- @Override
81- @Test
82- public void testTimeStamp() throws SQLException {
77+ @Override @Test public void testTimeStamp() throws SQLException {
8378 super.testTimeStamp();
8479 }
8580
86- @Override
87- @Test
88- public void testUnique() throws SQLException {
81+ @Override @Test public void testUnique() throws SQLException {
8982 super.testUnique();
9083 }
9184 }
--- trunk/test/TestAnnotateMysql.java (revision 29)
+++ trunk/test/TestAnnotateMysql.java (revision 30)
@@ -6,60 +6,56 @@
66 import org.junit.Test;
77
88 import estoc.dbm.AccessManager;
9+import estoc.dbm.ConnectionProvider;
910 import estoc.dbm.DataAccess;
1011 import estoc.dbm.DbType;
1112 import estoc.dbm.TableAccess;
1213
1314 public class TestAnnotateMysql extends TestAnnotate {
14- private Connection conn;
15+ private ConnectionProvider provider;
1516 private AccessManager am;
1617
17- @Override @Before
18- public void setup() throws Exception {
18+ @Override @Before public void setup() throws Exception {
1919 Class.forName("org.gjt.mm.mysql.Driver");
20- conn = DriverManager.getConnection("jdbc:mysql:///testdb");
20+ provider = new ConnectionProvider() {
21+ @Override public Connection getConnection() throws SQLException {
22+ return DriverManager.getConnection("jdbc:mysql:///testdb");
23+ }
24+ };
2125 am = new AccessManager(DbType.Mysql);
2226 dropBeforeCreate = true;
2327 }
2428
25- @Override
26- protected DataAccess getDataAccess() {
27- return am.getDataAccess(conn);
29+ @Override protected DataAccess getDataAccess() {
30+ return am.getDataAccess(provider);
2831 }
2932
30- @Override
31- protected TableAccess getTableAccess() {
32- return am.getTableAccess(conn);
33+ @Override protected TableAccess getTableAccess() {
34+ return am.getTableAccess(provider);
3335 }
34- //------------------------------------------------------
35- @Override @Test
36- public void testAutoInc() throws SQLException {
36+
37+ // ------------------------------------------------------
38+ @Override @Test public void testAutoInc() throws SQLException {
3739 super.testAutoInc();
3840 }
3941
40- @Override @Test
41- public void testIndex() throws SQLException {
42+ @Override @Test public void testIndex() throws SQLException {
4243 super.testIndex();
4344 }
4445
45- @Override @Test
46- public void testNotNull() throws SQLException {
46+ @Override @Test public void testNotNull() throws SQLException {
4747 super.testNotNull();
4848 }
4949
50- @Override @Test
51- public void testPk() throws SQLException {
50+ @Override @Test public void testPk() throws SQLException {
5251 super.testPk();
5352 }
5453
55- @Override @Test
56- public void testTimeStamp() throws SQLException {
54+ @Override @Test public void testTimeStamp() throws SQLException {
5755 super.testTimeStamp();
5856 }
5957
60- @Override @Test
61- public void testUnique() throws SQLException {
58+ @Override @Test public void testUnique() throws SQLException {
6259 super.testUnique();
6360 }
64-
6561 }
--- trunk/test/TestAnnotateOracle.java (revision 29)
+++ trunk/test/TestAnnotateOracle.java (revision 30)
@@ -6,60 +6,56 @@
66 import org.junit.Test;
77
88 import estoc.dbm.AccessManager;
9+import estoc.dbm.ConnectionProvider;
910 import estoc.dbm.DataAccess;
1011 import estoc.dbm.DbType;
1112 import estoc.dbm.TableAccess;
1213
1314 public class TestAnnotateOracle extends TestAnnotate {
14- private Connection conn;
15+ private ConnectionProvider provider;
1516 private AccessManager am;
1617
17- @Override @Before
18- public void setup() throws Exception {
18+ @Override @Before public void setup() throws Exception {
1919 dropBeforeCreate = true;
2020 Class.forName("oracle.jdbc.driver.OracleDriver");
21- conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","test","test");
21+ provider = new ConnectionProvider() {
22+ @Override public Connection getConnection() throws SQLException {
23+ return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "test", "test");
24+ }
25+ };
2226 am = new AccessManager(DbType.Oracle);
2327 }
2428
25- @Override
26- protected DataAccess getDataAccess() {
27- return am.getDataAccess(conn);
29+ @Override protected DataAccess getDataAccess() {
30+ return am.getDataAccess(provider);
2831 }
2932
30- @Override
31- protected TableAccess getTableAccess() {
32- return am.getTableAccess(conn);
33+ @Override protected TableAccess getTableAccess() {
34+ return am.getTableAccess(provider);
3335 }
34- //------------------------------------------------------
35- @Override @Test
36- public void testAutoInc() throws SQLException {
36+
37+ // ------------------------------------------------------
38+ @Override @Test public void testAutoInc() throws SQLException {
3739 super.testAutoInc();
3840 }
3941
40- @Override @Test
41- public void testIndex() throws SQLException {
42+ @Override @Test public void testIndex() throws SQLException {
4243 super.testIndex();
4344 }
4445
45- @Override @Test
46- public void testNotNull() throws SQLException {
46+ @Override @Test public void testNotNull() throws SQLException {
4747 super.testNotNull();
4848 }
4949
50- @Override @Test
51- public void testPk() throws SQLException {
50+ @Override @Test public void testPk() throws SQLException {
5251 super.testPk();
5352 }
5453
55- @Override @Test
56- public void testTimeStamp() throws SQLException {
54+ @Override @Test public void testTimeStamp() throws SQLException {
5755 super.testTimeStamp();
5856 }
5957
60- @Override @Test
61- public void testUnique() throws SQLException {
58+ @Override @Test public void testUnique() throws SQLException {
6259 super.testUnique();
6360 }
64-
6561 }
--- trunk/test/TestAnnotatePgsql.java (revision 29)
+++ trunk/test/TestAnnotatePgsql.java (revision 30)
@@ -6,60 +6,56 @@
66 import org.junit.Test;
77
88 import estoc.dbm.AccessManager;
9+import estoc.dbm.ConnectionProvider;
910 import estoc.dbm.DataAccess;
1011 import estoc.dbm.DbType;
1112 import estoc.dbm.TableAccess;
1213
1314 public class TestAnnotatePgsql extends TestAnnotate {
14- private Connection conn;
15+ private ConnectionProvider provider;
1516 private AccessManager am;
1617
17- @Override @Before
18- public void setup() throws Exception {
18+ @Override @Before public void setup() throws Exception {
1919 Class.forName("org.postgresql.Driver").newInstance();
20- conn = DriverManager.getConnection("jdbc:postgresql://localhost/testdb", "postgres", "1111");
20+ provider = new ConnectionProvider() {
21+ @Override public Connection getConnection() throws SQLException {
22+ return DriverManager.getConnection("jdbc:postgresql://localhost/testdb", "postgres", "1111");
23+ }
24+ };
2125 am = new AccessManager(DbType.Postgresql);
2226 dropBeforeCreate = true;
2327 }
2428
25- @Override
26- protected DataAccess getDataAccess() {
27- return am.getDataAccess(conn);
29+ @Override protected DataAccess getDataAccess() {
30+ return am.getDataAccess(provider);
2831 }
2932
30- @Override
31- protected TableAccess getTableAccess() {
32- return am.getTableAccess(conn);
33+ @Override protected TableAccess getTableAccess() {
34+ return am.getTableAccess(provider);
3335 }
34- //------------------------------------------------------
35- @Override @Test
36- public void testAutoInc() throws SQLException {
36+
37+ // ------------------------------------------------------
38+ @Override @Test public void testAutoInc() throws SQLException {
3739 super.testAutoInc();
3840 }
3941
40- @Override @Test
41- public void testIndex() throws SQLException {
42+ @Override @Test public void testIndex() throws SQLException {
4243 super.testIndex();
4344 }
4445
45- @Override @Test
46- public void testNotNull() throws SQLException {
46+ @Override @Test public void testNotNull() throws SQLException {
4747 super.testNotNull();
4848 }
4949
50- @Override @Test
51- public void testPk() throws SQLException {
50+ @Override @Test public void testPk() throws SQLException {
5251 super.testPk();
5352 }
5453
55- @Override @Test
56- public void testTimeStamp() throws SQLException {
54+ @Override @Test public void testTimeStamp() throws SQLException {
5755 super.testTimeStamp();
5856 }
5957
60- @Override @Test
61- public void testUnique() throws SQLException {
58+ @Override @Test public void testUnique() throws SQLException {
6259 super.testUnique();
6360 }
64-
6561 }
--- trunk/test/TestDbTypeH2.java (revision 29)
+++ trunk/test/TestDbTypeH2.java (revision 30)
@@ -6,67 +6,61 @@
66 import org.junit.Test;
77
88 import estoc.dbm.AccessManager;
9+import estoc.dbm.ConnectionProvider;
910 import estoc.dbm.DataAccess;
1011 import estoc.dbm.DbType;
1112 import estoc.dbm.TableAccess;
1213
13-
1414 public class TestDbTypeH2 extends TestDbType {
15- private Connection conn;
15+ private ConnectionProvider provider;
1616 private AccessManager am;
1717
18- @Override @Before
19- public void setup() throws Exception {
18+ @Override @Before public void setup() throws Exception {
2019 // メモリ上にH2のデータベース testdb を作成します
2120 Class.forName("org.h2.Driver").newInstance();
22- conn = DriverManager.getConnection("jdbc:h2:mem:testdb;create=true");
21+ provider = new ConnectionProvider() {
22+ @Override public Connection getConnection() throws SQLException {
23+ return DriverManager.getConnection("jdbc:h2:mem:testdb;create=true");
24+ }
25+ };
2326 am = new AccessManager(DbType.H2);
2427 }
2528
26- @Override
27- protected DataAccess getDataAccess() {
28- return am.getDataAccess(conn);
29+ @Override protected DataAccess getDataAccess() {
30+ return am.getDataAccess(provider);
2931 }
3032
31- @Override
32- protected TableAccess getTableAccess() {
33- return am.getTableAccess(conn);
33+ @Override protected TableAccess getTableAccess() {
34+ return am.getTableAccess(provider);
3435 }
35- //------------------------------------------------------
3636
37- @Override @Test
38- public void testTypeBigDecimal() throws SQLException {
37+ // ------------------------------------------------------
38+ @Override @Test public void testTypeBigDecimal() throws SQLException {
3939 super.testTypeBigDecimal();
4040 }
4141
42- @Override @Test
43- public void testTypeBoolean() throws SQLException {
42+ @Override @Test public void testTypeBoolean() throws SQLException {
4443 super.testTypeBoolean();
4544 }
4645
47- @Override @Test
48- public void testTypeBytes() throws SQLException {
46+ @Override @Test public void testTypeBytes() throws SQLException {
4947 super.testTypeBytes();
5048 }
5149
52- @Override @Test
53- public void testTypeDate() throws SQLException {
50+ @Override @Test public void testTypeDate() throws SQLException {
5451 super.testTypeDate();
5552 }
5653
57- @Override
58- public void testTypeDouble() throws SQLException {
54+ @Override public void testTypeDouble() throws SQLException {
5955 // TODO Auto-generated method stub
6056 super.testTypeDouble();
6157 }
6258
63- @Override @Test
64- public void testTypeInteger() throws SQLException {
59+ @Override @Test public void testTypeInteger() throws SQLException {
6560 super.testTypeInteger();
6661 }
6762
68- @Override @Test
69- public void testTypeString() throws SQLException {
63+ @Override @Test public void testTypeString() throws SQLException {
7064 super.testTypeString();
7165 }
7266 }
--- trunk/test/TestAnnotateH2.java (revision 29)
+++ trunk/test/TestAnnotateH2.java (revision 30)
@@ -6,60 +6,56 @@
66 import org.junit.Test;
77
88 import estoc.dbm.AccessManager;
9+import estoc.dbm.ConnectionProvider;
910 import estoc.dbm.DataAccess;
1011 import estoc.dbm.DbType;
1112 import estoc.dbm.TableAccess;
1213
1314 public class TestAnnotateH2 extends TestAnnotate {
14- private Connection conn;
15+ private ConnectionProvider provider;
1516 private AccessManager am;
1617
17- @Override @Before
18- public void setup() throws Exception {
18+ @Override @Before public void setup() throws Exception {
1919 // メモリ上にH2のデータベース testdb を作成します
2020 Class.forName("org.h2.Driver").newInstance();
21- conn = DriverManager.getConnection("jdbc:h2:mem:testdb;create=true");
21+ provider = new ConnectionProvider() {
22+ @Override public Connection getConnection() throws SQLException {
23+ return DriverManager.getConnection("jdbc:h2:mem:testdb;create=true");
24+ }
25+ };
2226 am = new AccessManager(DbType.H2);
2327 }
2428
25- @Override
26- protected DataAccess getDataAccess() {
27- return am.getDataAccess(conn);
29+ @Override protected DataAccess getDataAccess() {
30+ return am.getDataAccess(provider);
2831 }
2932
30- @Override
31- protected TableAccess getTableAccess() {
32- return am.getTableAccess(conn);
33+ @Override protected TableAccess getTableAccess() {
34+ return am.getTableAccess(provider);
3335 }
34- //------------------------------------------------------
35- @Override @Test
36- public void testAutoInc() throws SQLException {
36+
37+ // ------------------------------------------------------
38+ @Override @Test public void testAutoInc() throws SQLException {
3739 super.testAutoInc();
3840 }
3941
40- @Override @Test
41- public void testIndex() throws SQLException {
42+ @Override @Test public void testIndex() throws SQLException {
4243 super.testIndex();
4344 }
4445
45- @Override @Test
46- public void testNotNull() throws SQLException {
46+ @Override @Test public void testNotNull() throws SQLException {
4747 super.testNotNull();
4848 }
4949
50- @Override @Test
51- public void testPk() throws SQLException {
50+ @Override @Test public void testPk() throws SQLException {
5251 super.testPk();
5352 }
5453
55- @Override @Test
56- public void testTimeStamp() throws SQLException {
54+ @Override @Test public void testTimeStamp() throws SQLException {
5755 super.testTimeStamp();
5856 }
5957
60- @Override @Test
61- public void testUnique() throws SQLException {
58+ @Override @Test public void testUnique() throws SQLException {
6259 super.testUnique();
6360 }
64-
6561 }
--- trunk/test/TestDbTypeDerby.java (revision 29)
+++ trunk/test/TestDbTypeDerby.java (revision 30)
@@ -9,16 +9,16 @@
99 import org.junit.Test;
1010
1111 import estoc.dbm.AccessManager;
12+import estoc.dbm.ConnectionProvider;
1213 import estoc.dbm.DataAccess;
1314 import estoc.dbm.DbType;
1415 import estoc.dbm.TableAccess;
1516
1617 public class TestDbTypeDerby extends TestDbType {
17- private Connection conn;
18+ private ConnectionProvider provider;
1819 private AccessManager am;
1920
20- @BeforeClass
21- public static void preStart() throws Exception {
21+ @BeforeClass public static void preStart() throws Exception {
2222 File dir = new File("../estocdb/testdb");
2323 if (dir.exists()) {
2424 delDir(dir.listFiles());
@@ -35,34 +35,30 @@
3535 }
3636 }
3737
38- @Override
39- @Before
40- public void setup() throws Exception {
38+ @Override @Before public void setup() throws Exception {
4139 Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
42- conn = DriverManager.getConnection("jdbc:derby:testdb;create=true");
40+ provider = new ConnectionProvider() {
41+ @Override public Connection getConnection() throws SQLException {
42+ return DriverManager.getConnection("jdbc:derby:testdb;create=true");
43+ }
44+ };
4345 am = new AccessManager(DbType.Derby);
4446 }
4547
46- @Override
47- protected DataAccess getDataAccess() {
48- return am.getDataAccess(conn);
48+ @Override protected DataAccess getDataAccess() {
49+ return am.getDataAccess(provider);
4950 }
5051
51- @Override
52- protected TableAccess getTableAccess() {
53- return am.getTableAccess(conn);
52+ @Override protected TableAccess getTableAccess() {
53+ return am.getTableAccess(provider);
5454 }
5555
5656 // ------------------------------------------------------
57- @Override
58- @Test
59- public void testTypeBigDecimal() throws SQLException {
57+ @Override @Test public void testTypeBigDecimal() throws SQLException {
6058 super.testTypeBigDecimal();
6159 }
6260
63- @Override
64- @Test
65- public void testTypeBoolean() throws SQLException {
61+ @Override @Test public void testTypeBoolean() throws SQLException {
6662 try {
6763 super.testTypeBoolean();
6864 } catch (UnsupportedOperationException e) {
@@ -71,32 +67,23 @@
7167 Assert.fail();
7268 }
7369
74- @Override
75- @Test
76- public void testTypeBytes() throws SQLException {
70+ @Override @Test public void testTypeBytes() throws SQLException {
7771 super.testTypeBytes();
7872 }
7973
80- @Override
81- @Test
82- public void testTypeDate() throws SQLException {
74+ @Override @Test public void testTypeDate() throws SQLException {
8375 super.testTypeDate();
8476 }
8577
86- @Override
87- public void testTypeDouble() throws SQLException {
78+ @Override public void testTypeDouble() throws SQLException {
8879 super.testTypeDouble();
8980 }
9081
91- @Override
92- @Test
93- public void testTypeInteger() throws SQLException {
82+ @Override @Test public void testTypeInteger() throws SQLException {
9483 super.testTypeInteger();
9584 }
9685
97- @Override
98- @Test
99- public void testTypeString() throws SQLException {
86+ @Override @Test public void testTypeString() throws SQLException {
10087 super.testTypeString();
10188 }
10289 }
--- trunk/test/TestDbTypeMysql.java (revision 29)
+++ trunk/test/TestDbTypeMysql.java (revision 30)
@@ -6,68 +6,60 @@
66 import org.junit.Test;
77
88 import estoc.dbm.AccessManager;
9+import estoc.dbm.ConnectionProvider;
910 import estoc.dbm.DataAccess;
1011 import estoc.dbm.DbType;
1112 import estoc.dbm.TableAccess;
1213
13-
1414 public class TestDbTypeMysql extends TestDbType {
15- private Connection conn;
15+ private ConnectionProvider provider;
1616 private AccessManager am;
1717
18- @Override @Before
19- public void setup() throws Exception {
18+ @Override @Before public void setup() throws Exception {
2019 Class.forName("org.gjt.mm.mysql.Driver");
21- conn = DriverManager.getConnection("jdbc:mysql:///testdb");
20+ provider = new ConnectionProvider() {
21+ @Override public Connection getConnection() throws SQLException {
22+ return DriverManager.getConnection("jdbc:mysql:///testdb");
23+ }
24+ };
2225 am = new AccessManager(DbType.Mysql);
2326 dropBeforeCreate = true;
2427 }
2528
26- @Override
27- protected DataAccess getDataAccess() {
28- return am.getDataAccess(conn);
29+ @Override protected DataAccess getDataAccess() {
30+ return am.getDataAccess(provider);
2931 }
3032
31- @Override
32- protected TableAccess getTableAccess() {
33- return am.getTableAccess(conn);
33+ @Override protected TableAccess getTableAccess() {
34+ return am.getTableAccess(provider);
3435 }
35- //------------------------------------------------------
3636
37- @Override
38- @Test
39- public void testTypeBigDecimal() throws SQLException {
37+ // ------------------------------------------------------
38+ @Override @Test public void testTypeBigDecimal() throws SQLException {
4039 super.testTypeBigDecimal();
4140 }
4241
43- @Override
44- @Test
45- public void testTypeBoolean() throws SQLException {
42+ @Override @Test public void testTypeBoolean() throws SQLException {
4643 super.testTypeBoolean();
4744 }
4845
49- @Override @Test
50- public void testTypeBytes() throws SQLException {
46+ @Override @Test public void testTypeBytes() throws SQLException {
5147 super.testTypeBytes();
5248 }
5349
54- @Override @Test
55- public void testTypeDate() throws SQLException {
50+ @Override @Test public void testTypeDate() throws SQLException {
5651 super.testTypeDate();
5752 }
5853
59- @Override
60- public void testTypeDouble() throws SQLException {
54+ @Override public void testTypeDouble() throws SQLException {
6155 super.testTypeDouble();
6256 }
6357
64- @Override @Test
65- public void testTypeInteger() throws SQLException {
58+ @Override @Test public void testTypeInteger() throws SQLException {
6659 super.testTypeInteger();
6760 }
6861
69- @Override @Test
70- public void testTypeString() throws SQLException {
62+ @Override @Test public void testTypeString() throws SQLException {
7163 super.testTypeString();
7264 }
7365 }
--- trunk/test/TestDbTypeOracle.java (revision 29)
+++ trunk/test/TestDbTypeOracle.java (revision 30)
@@ -7,76 +7,63 @@
77 import org.junit.Test;
88
99 import estoc.dbm.AccessManager;
10+import estoc.dbm.ConnectionProvider;
1011 import estoc.dbm.DataAccess;
1112 import estoc.dbm.DbType;
1213 import estoc.dbm.TableAccess;
1314
1415 public class TestDbTypeOracle extends TestDbType {
15- private Connection conn;
16+ private ConnectionProvider provider;
1617 private AccessManager am;
1718
18- @BeforeClass
19- public static void preStart() throws Exception {
19+ @BeforeClass public static void preStart() throws Exception {
2020 }
2121
22- @Override
23- @Before
24- public void setup() throws Exception {
22+ @Override @Before public void setup() throws Exception {
2523 dropBeforeCreate = true;
2624 Class.forName("oracle.jdbc.driver.OracleDriver");
27- conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","test","test");
25+ provider = new ConnectionProvider() {
26+ @Override public Connection getConnection() throws SQLException {
27+ return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "test", "test");
28+ }
29+ };
2830 am = new AccessManager(DbType.Oracle);
2931 }
3032
31- @Override
32- protected DataAccess getDataAccess() {
33- return am.getDataAccess(conn);
33+ @Override protected DataAccess getDataAccess() {
34+ return am.getDataAccess(provider);
3435 }
3536
36- @Override
37- protected TableAccess getTableAccess() {
38- return am.getTableAccess(conn);
37+ @Override protected TableAccess getTableAccess() {
38+ return am.getTableAccess(provider);
3939 }
4040
4141 // ------------------------------------------------------
42- @Override
43- @Test
44- public void testTypeBigDecimal() throws SQLException {
42+ @Override @Test public void testTypeBigDecimal() throws SQLException {
4543 super.testTypeBigDecimal();
4644 }
4745
48- @Override
49- @Test
50- public void testTypeBoolean() throws SQLException {
46+ @Override @Test public void testTypeBoolean() throws SQLException {
5147 super.testTypeBoolean();
5248 }
5349
54- @Override
55- @Test
56- public void testTypeBytes() throws SQLException {
50+ @Override @Test public void testTypeBytes() throws SQLException {
5751 super.testTypeBytes();
5852 }
5953
60- @Override
61- @Test
62- public void testTypeDate() throws SQLException {
54+ @Override @Test public void testTypeDate() throws SQLException {
6355 super.testTypeDate();
6456 }
6557
66- @Override
67- public void testTypeDouble() throws SQLException {
58+ @Override public void testTypeDouble() throws SQLException {
6859 super.testTypeDouble();
6960 }
7061
71- @Override
72- @Test
73- public void testTypeInteger() throws SQLException {
62+ @Override @Test public void testTypeInteger() throws SQLException {
7463 super.testTypeInteger();
7564 }
7665
77- @Override
78- @Test
79- public void testTypeString() throws SQLException {
66+ @Override @Test public void testTypeString() throws SQLException {
8067 super.testTypeString();
8168 }
8269 }
--- trunk/test/TestDbTypePgsql.java (revision 29)
+++ trunk/test/TestDbTypePgsql.java (revision 30)
@@ -7,76 +7,63 @@
77 import org.junit.Test;
88
99 import estoc.dbm.AccessManager;
10+import estoc.dbm.ConnectionProvider;
1011 import estoc.dbm.DataAccess;
1112 import estoc.dbm.DbType;
1213 import estoc.dbm.TableAccess;
1314
1415 public class TestDbTypePgsql extends TestDbType {
15- private Connection conn;
16+ private ConnectionProvider provider;
1617 private AccessManager am;
1718
18- @BeforeClass
19- public static void preStart() throws Exception {
19+ @BeforeClass public static void preStart() throws Exception {
2020 }
2121
22- @Override
23- @Before
24- public void setup() throws Exception {
22+ @Override @Before public void setup() throws Exception {
2523 Class.forName("org.postgresql.Driver").newInstance();
26- conn = DriverManager.getConnection("jdbc:postgresql://localhost/testdb", "postgres", "1111");
24+ provider = new ConnectionProvider() {
25+ @Override public Connection getConnection() throws SQLException {
26+ return DriverManager.getConnection("jdbc:postgresql://localhost/testdb", "postgres", "1111");
27+ }
28+ };
2729 am = new AccessManager(DbType.Postgresql);
2830 dropBeforeCreate = true;
2931 }
3032
31- @Override
32- protected DataAccess getDataAccess() {
33- return am.getDataAccess(conn);
33+ @Override protected DataAccess getDataAccess() {
34+ return am.getDataAccess(provider);
3435 }
3536
36- @Override
37- protected TableAccess getTableAccess() {
38- return am.getTableAccess(conn);
37+ @Override protected TableAccess getTableAccess() {
38+ return am.getTableAccess(provider);
3939 }
4040
4141 // ------------------------------------------------------
42- @Override
43- @Test
44- public void testTypeBigDecimal() throws SQLException {
42+ @Override @Test public void testTypeBigDecimal() throws SQLException {
4543 super.testTypeBigDecimal();
4644 }
4745
48- @Override
49- @Test
50- public void testTypeBoolean() throws SQLException {
46+ @Override @Test public void testTypeBoolean() throws SQLException {
5147 super.testTypeBoolean();
5248 }
5349
54- @Override
55- @Test
56- public void testTypeBytes() throws SQLException {
50+ @Override @Test public void testTypeBytes() throws SQLException {
5751 super.testTypeBytes();
5852 }
5953
60- @Override
61- @Test
62- public void testTypeDate() throws SQLException {
54+ @Override @Test public void testTypeDate() throws SQLException {
6355 super.testTypeDate();
6456 }
6557
66- @Override
67- public void testTypeDouble() throws SQLException {
58+ @Override public void testTypeDouble() throws SQLException {
6859 super.testTypeDouble();
6960 }
7061
71- @Override
72- @Test
73- public void testTypeInteger() throws SQLException {
62+ @Override @Test public void testTypeInteger() throws SQLException {
7463 super.testTypeInteger();
7564 }
7665
77- @Override
78- @Test
79- public void testTypeString() throws SQLException {
66+ @Override @Test public void testTypeString() throws SQLException {
8067 super.testTypeString();
8168 }
8269 }
--- trunk/src/estoc/dbm/TableAccessDerby.java (revision 29)
+++ trunk/src/estoc/dbm/TableAccessDerby.java (revision 30)
@@ -1,13 +1,12 @@
11 package estoc.dbm;
22
3-import java.sql.Connection;
43 import java.sql.SQLException;
54 import java.util.ArrayList;
65 import java.util.List;
76
87 class TableAccessDerby extends TableAccess {
9- public TableAccessDerby(Connection connection) {
10- super(connection);
8+ public TableAccessDerby(ConnectionProvider provider) {
9+ super(provider);
1110 }
1211
1312 @Override
--- trunk/src/estoc/dbm/TableAccessMysql.java (revision 29)
+++ trunk/src/estoc/dbm/TableAccessMysql.java (revision 30)
@@ -1,12 +1,11 @@
11 package estoc.dbm;
22
3-import java.sql.Connection;
43 import java.sql.SQLException;
54 import java.util.List;
65
76 class TableAccessMysql extends TableAccess {
8- public TableAccessMysql(Connection connection) {
9- super(connection);
7+ public TableAccessMysql(ConnectionProvider provider) {
8+ super(provider);
109 }
1110
1211 @Override
--- trunk/src/estoc/dbm/TableAccessOracle.java (revision 29)
+++ trunk/src/estoc/dbm/TableAccessOracle.java (revision 30)
@@ -1,13 +1,12 @@
11 package estoc.dbm;
22
3-import java.sql.Connection;
43 import java.sql.SQLException;
54 import java.util.ArrayList;
65 import java.util.List;
76
87 class TableAccessOracle extends TableAccess {
9- public TableAccessOracle(Connection connection) {
10- super(connection);
8+ public TableAccessOracle(ConnectionProvider provider) {
9+ super(provider);
1110 }
1211
1312 @Override
--- trunk/src/estoc/dbm/DbmUtil.java (revision 29)
+++ trunk/src/estoc/dbm/DbmUtil.java (revision 30)
@@ -2,10 +2,14 @@
22
33 import java.lang.reflect.Field;
44 import java.lang.reflect.Method;
5+import java.sql.Connection;
56 import java.sql.ResultSet;
67 import java.sql.SQLException;
8+import java.sql.Statement;
79 import java.util.Date;
810
11+import estoc.dbm.annotate.Table;
12+
913 /**
1014 * Util for estoc db
1115 */
@@ -34,7 +38,7 @@
3438 return sb.toString();
3539 }
3640
37-
41+
3842 /**
3943 * fill to TableObject from ResultSet
4044 * @param <T> Class
@@ -65,7 +69,7 @@
6569 }
6670 }
6771
68-
72+
6973 /**
7074 * Set value to Field
7175 * @param obj target object
@@ -87,6 +91,11 @@
8791 }
8892 }
8993
94+ public static String getTableName(Class<?> clz) {
95+ Table table = clz.getAnnotation(Table.class);
96+ return table.value();
97+ }
98+
9099 /**
91100 * Get value from Filed
92101 * @param obj target object
@@ -127,4 +136,31 @@
127136 sb.append("get").append(first.toUpperCase()).append(name.substring(1));
128137 return sb.toString();
129138 }
139+
140+
141+ public static void close(Statement statement) {
142+ if (statement != null) {
143+ try {
144+ statement.close();
145+ } catch (Exception e) {
146+ }
147+ try {
148+ Connection connection = statement.getConnection();
149+ if (connection != null) {
150+ connection.close();
151+ }
152+ } catch (Exception e) {
153+ }
154+ }
155+ }
156+
157+ public static void close(ResultSet result) {
158+ if (result != null) {
159+ try {
160+ result.close();
161+ } catch (Exception e) {
162+ }
163+ }
164+ }
165+
130166 }
--- trunk/src/estoc/dbm/DataAccessOracleImpl.java (revision 29)
+++ trunk/src/estoc/dbm/DataAccessOracleImpl.java (revision 30)
@@ -4,10 +4,10 @@
44 import java.io.InputStream;
55 import java.lang.reflect.Field;
66 import java.math.BigDecimal;
7-import java.sql.Connection;
87 import java.sql.PreparedStatement;
98 import java.sql.ResultSet;
109 import java.sql.SQLException;
10+import java.sql.Statement;
1111 import java.sql.Types;
1212 import java.util.ArrayList;
1313 import java.util.Date;
@@ -18,24 +18,17 @@
1818 import java.util.Set;
1919 import java.util.logging.Logger;
2020
21-import estoc.dbm.annotate.Table;
22-
2321 class DataAccessOracleImpl implements DataAccess {
2422 private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
25- private final Connection connection;
2623 private final SqlCreator creator;
24+ private final ConnectionProvider connectionProvider;
2725
28- public DataAccessOracleImpl(Connection connection, SqlCreator creator) {
29- this.connection = connection;
26+ public DataAccessOracleImpl(ConnectionProvider connectionProvider, SqlCreator creator) {
27+ this.connectionProvider = connectionProvider;
3028 this.creator = creator;
3129 }
3230
3331 Map<Class<?>, List<ColumnInfo>> fieldMap = new HashMap<Class<?>, List<ColumnInfo>>();
34- Map<Class<?>, PreparedStatement> selectStmts = new HashMap<Class<?>, PreparedStatement>();
35- Map<Class<?>, PreparedStatement> insertStmts = new HashMap<Class<?>, PreparedStatement>();
36- Map<Class<?>, PreparedStatement> autoKeyStmts = new HashMap<Class<?>, PreparedStatement>();
37- Map<Class<?>, PreparedStatement> updateStmts = new HashMap<Class<?>, PreparedStatement>();
38- Map<Class<?>, PreparedStatement> deleteStmts = new HashMap<Class<?>, PreparedStatement>();
3932
4033 private List<ColumnInfo> getDeclaredFields(Class<?> clz) {
4134 List<ColumnInfo> fields = fieldMap.get(clz);
@@ -52,24 +45,70 @@
5245
5346 /*
5447 * (non-Javadoc)
55- *
48+ *
5649 * @see dbm.DataAccess#select(java.lang.Class, java.lang.Object)
5750 */
5851 public <T> T select(Class<T> clz, Object... params) throws SQLException {
59- PreparedStatement stmt = selectStmts.get(clz);
60- if (stmt == null || stmt.getConnection() != connection) {
61- stmt = createSelectStmt(clz);
52+ PreparedStatement stmt = createSelectStmt(clz);
53+ try {
54+ setParams(stmt, params);
55+ ResultSet result = stmt.executeQuery();
56+ try {
57+ if (result != null && result.next()) {
58+ return DbmUtil.fillData(clz, result);
59+ }
60+ return null;
61+ } finally {
62+ DbmUtil.close(result);
63+ }
64+ } finally {
65+ DbmUtil.close(stmt);
6266 }
63- setParams(stmt, params);
64- ResultSet result = stmt.executeQuery();
65- if (result != null && result.next()) {
66- return DbmUtil.fillData(clz, result);
67+ }
68+
69+ /*
70+ * (非 Javadoc)
71+ *
72+ * @see estoc.dbm.DataAccess#fillter(java.lang.Class, java.lang.String,
73+ * java.lang.String)
74+ */
75+ public <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString) throws SQLException {
76+ return fillter(clz, whereString, orderString, -1);
77+ }
78+
79+ @Override
80+ public <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString, long maxCount)
81+ throws SQLException {
82+ Statement stmt = connectionProvider.getConnection().createStatement();
83+ try {
84+ String tblName = DbmUtil.getTableName(clz);
85+ StringBuilder sql = new StringBuilder("SELECT * FROM ").append(tblName);
86+ if (whereString != null && whereString.length() > 0) {
87+ sql.append(" WHERE ").append(whereString);
88+ }
89+ if (orderString != null && orderString.length() > 0) {
90+ sql.append(" ORDER BY ").append(orderString);
91+ }
92+ stmt.execute(sql.toString());
93+ ResultSet rs = stmt.getResultSet();
94+ try {
95+ ArrayList<T> rtn = new ArrayList<T>();
96+ if (rs == null) {
97+ return rtn;
98+ }
99+ for (long i = 0; rs.next() && (maxCount < 0 || i < maxCount); i++) {
100+ rtn.add(DbmUtil.fillData(clz, rs));
101+ }
102+ return rtn;
103+ } finally {
104+ DbmUtil.close(rs);
105+ }
106+ } finally {
107+ DbmUtil.close(stmt);
67108 }
68- return null;
69109 }
70110
71111 private <T> PreparedStatement createSelectStmt(Class<T> clz) throws SQLException {
72- PreparedStatement stmt;
73112 Set<String> cols = new LinkedHashSet<String>();
74113 Set<String> pks = new LinkedHashSet<String>();
75114 for (Field field : clz.getDeclaredFields()) {
@@ -84,36 +123,35 @@
84123 if (pks.size() == 0) {
85124 throw new UnsupportedOperationException("When if you use SELECT, table have to be defined PK");
86125 }
87- String tableName = getTableName(clz);
126+ String tableName = DbmUtil.getTableName(clz);
88127 String selectSql = creator.getSelectSql(tableName, pks, cols);
89- stmt = connection.prepareStatement(selectSql);
90- selectStmts.put(clz, stmt);
128+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(selectSql);
91129 return stmt;
92130 }
93131
94132 /*
95133 * (non-Javadoc)
96- *
134+ *
97135 * @see dbm.DataAccess#delete(java.lang.Object)
98136 */
99137 public boolean delete(Object obj) throws SQLException {
100138 Class<?> clz = obj.getClass();
101- PreparedStatement stmt = deleteStmts.get(clz);
102- if (stmt == null || stmt.getConnection() != connection) {
103- stmt = createDeleteStmt(clz);
104- }
105- List<Object> params = new ArrayList<Object>();
106- for (ColumnInfo info : getDeclaredFields(clz)) {
107- if (info.isPk()) {
108- params.add(DbmUtil.getVlaueFromField(obj, info));
139+ PreparedStatement stmt = createDeleteStmt(clz);
140+ try {
141+ List<Object> params = new ArrayList<Object>();
142+ for (ColumnInfo info : getDeclaredFields(clz)) {
143+ if (info.isPk()) {
144+ params.add(DbmUtil.getVlaueFromField(obj, info));
145+ }
109146 }
147+ setParams(stmt, params.toArray());
148+ return stmt.executeUpdate() == 1;
149+ } finally {
150+ DbmUtil.close(stmt);
110151 }
111- setParams(stmt, params.toArray());
112- return stmt.executeUpdate() == 1;
113152 }
114153
115154 private PreparedStatement createDeleteStmt(Class<?> clz) throws SQLException {
116- PreparedStatement stmt;
117155 Set<String> pks = new LinkedHashSet<String>();
118156 for (ColumnInfo info : getDeclaredFields(clz)) {
119157 if (info.isPk()) {
@@ -123,94 +161,91 @@
123161 if (pks.size() == 0) {
124162 throw new UnsupportedOperationException("When if you use DELETE, table have to be defined PK");
125163 }
126- String tableName = getTableName(clz);
164+ String tableName = DbmUtil.getTableName(clz);
127165 String sql = creator.getDeleteSql(tableName, pks);
128166 LOG.info("sql = " + sql);
129- stmt = connection.prepareStatement(sql);
130- deleteStmts.put(clz, stmt);
167+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(sql);
131168 return stmt;
132169 }
133170
134171 /*
135172 * (non-Javadoc)
136- *
173+ *
137174 * @see dbm.DataAccess#insert(java.lang.Object)
138175 */
139176 public <T> T insert(T obj) throws SQLException {
140177 Class<?> clz = obj.getClass();
141-
142- PreparedStatement getKeyStmt;
143- if (autoKeyStmts.containsKey(clz)) {
144- getKeyStmt = autoKeyStmts.get(clz);
145- } else {
146- getKeyStmt = createGetKeyStmt(clz);
147- }
148-
149178 int nextVal = -1;
150- if (getKeyStmt != null) {
151- ResultSet rs = getKeyStmt.executeQuery();
152- if (rs.next()) {
153- nextVal = rs.getInt(1);
154- } else {
155- throw new RuntimeException("Faild to get nextval for auto inc.");
179+ PreparedStatement getKeyStmt = createGetKeyStmt(clz);
180+ try {
181+ if (getKeyStmt != null) {
182+ ResultSet rs = getKeyStmt.executeQuery();
183+ try {
184+ if (rs.next()) {
185+ nextVal = rs.getInt(1);
186+ } else {
187+ throw new RuntimeException("Faild to get nextval for auto inc.");
188+ }
189+ } finally {
190+ DbmUtil.close(rs);
191+ }
156192 }
157-
193+ } finally {
194+ DbmUtil.close(getKeyStmt);
158195 }
159-
160- PreparedStatement stmt = insertStmts.get(clz);
161- // create statement
162- if (stmt == null || stmt.getConnection() != connection) {
163- stmt = createInsertStmt(clz);
164- }
165- // set value
166- List<Object> params = new ArrayList<Object>();
167- Date now = new Date();
168- ColumnInfo autoIncField = null;
169- List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
170- for (ColumnInfo info : getDeclaredFields(clz)) {
171- if (info.isColumn()) {
172- if (info.isAutoInc()) {
173- autoIncField = info;
174- params.add(nextVal);
175- } else if (info.isTimeStamp()) {
176- timeFields.add(info);
177- params.add(now);
178- } else {
179- params.add(DbmUtil.getVlaueFromField(obj, info));
196+ PreparedStatement stmt = createInsertStmt(clz);
197+ try {
198+ // set value
199+ List<Object> params = new ArrayList<Object>();
200+ Date now = new Date();
201+ ColumnInfo autoIncField = null;
202+ List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
203+ for (ColumnInfo info : getDeclaredFields(clz)) {
204+ if (info.isColumn()) {
205+ if (info.isAutoInc()) {
206+ autoIncField = info;
207+ params.add(nextVal);
208+ } else if (info.isTimeStamp()) {
209+ timeFields.add(info);
210+ params.add(now);
211+ } else {
212+ params.add(DbmUtil.getVlaueFromField(obj, info));
213+ }
180214 }
181215 }
182- }
183- setParams(stmt, params.toArray());
184- // Do insert
185- if (stmt.executeUpdate() == 1) {
186- // AutoInc, TimeStamp の値埋め込み
187- for (ColumnInfo info : timeFields) {
188- DbmUtil.setVlaueToField(obj, info, now);
189- }
190- if (autoIncField != null) {
191- if (autoIncField.getType() == Integer.class) {
192- DbmUtil.setVlaueToField(obj, autoIncField, new Integer(nextVal));
193- } else if (autoIncField.getType() == Long.class) {
194- DbmUtil.setVlaueToField(obj, autoIncField, new Long(nextVal));
195- } else {
196- throw new UnsupportedOperationException("Unsupport type:" + autoIncField.getType());
216+ setParams(stmt, params.toArray());
217+ // Do insert
218+ if (stmt.executeUpdate() == 1) {
219+ // AutoInc, TimeStamp の値埋め込み
220+ for (ColumnInfo info : timeFields) {
221+ DbmUtil.setVlaueToField(obj, info, now);
197222 }
223+ if (autoIncField != null) {
224+ if (autoIncField.getType() == Integer.class) {
225+ DbmUtil.setVlaueToField(obj, autoIncField, new Integer(nextVal));
226+ } else if (autoIncField.getType() == Long.class) {
227+ DbmUtil.setVlaueToField(obj, autoIncField, new Long(nextVal));
228+ } else {
229+ throw new UnsupportedOperationException("Unsupport type:" + autoIncField.getType());
230+ }
231+ }
232+ return obj;
233+ } else {
234+ return null;
198235 }
199- return obj;
200- } else {
201- return null;
236+ } finally {
237+ DbmUtil.close(stmt);
202238 }
203239 }
204240
205241 /**
206242 * serialの次番を取得するStatementを作成します AutoInc列が存在しない場合nullを返します
207- *
243+ *
208244 * @param clz
209245 * @return
210246 * @throws SQLException
211247 */
212248 private PreparedStatement createGetKeyStmt(Class<?> clz) throws SQLException {
213- PreparedStatement stmt;
214249 String colName = null;
215250 for (ColumnInfo info : getDeclaredFields(clz)) {
216251 if (info.isColumn()) {
@@ -221,27 +256,17 @@
221256 }
222257 }
223258 if (colName == null) {
224- autoKeyStmts.put(clz, null);
225259 return null;
226260 }
227-
228261 StringBuilder sb = new StringBuilder();
229-
230- // select seq_01.nextval as nextval from dual
231-
232-
233- sb.append("SELECT ")
234- .append("seq_")
235- .append(getTableName(clz))
236- .append("_").append(colName)
237- .append(".nextval FROM dual");
238- stmt = connection.prepareStatement(sb.toString());
239- autoKeyStmts.put(clz, stmt);
262+ //  select seq_01.nextval as nextval from dual
263+ sb.append("SELECT ").append("seq_").append(DbmUtil.getTableName(clz)).append("_").append(colName)
264+ .append(".nextval FROM dual");
265+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(sb.toString());
240266 return stmt;
241267 }
242268
243269 private PreparedStatement createInsertStmt(Class<?> clz) throws SQLException {
244- PreparedStatement stmt;
245270 Set<String> columns = new LinkedHashSet<String>();
246271 for (ColumnInfo info : getDeclaredFields(clz)) {
247272 if (info.isColumn()) {
@@ -249,62 +274,60 @@
249274 columns.add(info.getName());
250275 }
251276 }
252- String tableName = getTableName(clz);
277+ String tableName = DbmUtil.getTableName(clz);
253278 String sql = creator.getInsertSql(tableName, columns);
254279 LOG.info("sql= " + sql);
255-
256- stmt = connection.prepareStatement(sql);
257- insertStmts.put(clz, stmt);
280+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(sql);
258281 return stmt;
259282 }
260283
261284 /*
262285 * (non-Javadoc)
263- *
286+ *
264287 * @see dbm.DataAccess#update(java.lang.Object, boolean)
265288 */
266289 public <T> T update(T obj) throws SQLException {
267290 Class<?> clz = obj.getClass();
268- PreparedStatement stmt = updateStmts.get(clz);
269- if (stmt == null || stmt.getConnection() != connection) {
270- stmt = createUpdateStmt(clz);
271- }
272- // set value
273- List<Object> params = new ArrayList<Object>();
274- List<Object> pkParams = new ArrayList<Object>();
275- List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
276- Date now = new Date();
277- for (ColumnInfo info : getDeclaredFields(clz)) {
278- if (info.isPk()) {
279- pkParams.add(DbmUtil.getVlaueFromField(obj, info));
280- } else {
281- if (info.isColumn()) {
282- if (info.isTimeStamp()) {
283- timeFields.add(info);
284- params.add(now);
285- } else {
286- params.add(DbmUtil.getVlaueFromField(obj, info));
291+ PreparedStatement stmt = createUpdateStmt(clz);
292+ try {
293+ // set value
294+ List<Object> params = new ArrayList<Object>();
295+ List<Object> pkParams = new ArrayList<Object>();
296+ List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
297+ Date now = new Date();
298+ for (ColumnInfo info : getDeclaredFields(clz)) {
299+ if (info.isPk()) {
300+ pkParams.add(DbmUtil.getVlaueFromField(obj, info));
301+ } else {
302+ if (info.isColumn()) {
303+ if (info.isTimeStamp()) {
304+ timeFields.add(info);
305+ params.add(now);
306+ } else {
307+ params.add(DbmUtil.getVlaueFromField(obj, info));
308+ }
287309 }
288310 }
289311 }
290- }
291- List<Object> tmp = new ArrayList<Object>();
292- tmp.addAll(params);
293- tmp.addAll(pkParams);
294- setParams(stmt, tmp.toArray());
295- // do update
296- if (stmt.executeUpdate() == 1) {
297- for (ColumnInfo info : timeFields) {
298- DbmUtil.setVlaueToField(obj, info, now);
312+ List<Object> tmp = new ArrayList<Object>();
313+ tmp.addAll(params);
314+ tmp.addAll(pkParams);
315+ setParams(stmt, tmp.toArray());
316+ // do update
317+ if (stmt.executeUpdate() == 1) {
318+ for (ColumnInfo info : timeFields) {
319+ DbmUtil.setVlaueToField(obj, info, now);
320+ }
321+ return obj;
322+ } else {
323+ return null;
299324 }
300- return obj;
301- } else {
302- return null;
325+ } finally {
326+ DbmUtil.close(stmt);
303327 }
304328 }
305329
306330 private PreparedStatement createUpdateStmt(Class<?> clz) throws SQLException {
307- PreparedStatement stmt;
308331 Set<String> pks = new LinkedHashSet<String>();
309332 Set<String> columns = new LinkedHashSet<String>();
310333 for (ColumnInfo info : getDeclaredFields(clz)) {
@@ -319,19 +342,13 @@
319342 if (pks.size() == 0) {
320343 throw new UnsupportedOperationException("When if you use UPDATE, table have to be defined PK");
321344 }
322- String tableName = getTableName(clz);
345+ String tableName = DbmUtil.getTableName(clz);
323346 String sql = creator.getUpdateSql(tableName, pks, columns);
324347 LOG.info("sql= " + sql);
325- stmt = connection.prepareStatement(sql);
326- updateStmts.put(clz, stmt);
348+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(sql);
327349 return stmt;
328350 }
329351
330- private String getTableName(Class<?> clz) {
331- Table table = clz.getAnnotation(Table.class);
332- return table.value();
333- }
334-
335352 private int getSqlType(Class<?> type) {
336353 if (type == String.class) {
337354 return Types.VARCHAR;
--- trunk/src/estoc/dbm/TableAccessH2.java (revision 29)
+++ trunk/src/estoc/dbm/TableAccessH2.java (revision 30)
@@ -1,12 +1,11 @@
11 package estoc.dbm;
22
3-import java.sql.Connection;
43 import java.sql.SQLException;
54 import java.util.List;
65
76 class TableAccessH2 extends TableAccess {
8- public TableAccessH2(Connection connection) {
9- super(connection);
7+ public TableAccessH2(ConnectionProvider provider) {
8+ super(provider);
109 }
1110
1211 @Override
--- trunk/src/estoc/dbm/TableAccess.java (revision 29)
+++ trunk/src/estoc/dbm/TableAccess.java (revision 30)
@@ -2,7 +2,6 @@
22
33 import java.lang.reflect.Field;
44 import java.math.BigDecimal;
5-import java.sql.Connection;
65 import java.sql.ResultSet;
76 import java.sql.SQLException;
87 import java.util.ArrayList;
@@ -34,21 +33,17 @@
3433 */
3534 public abstract void create(Class<?> clz) throws SQLException;
3635
37- private final Connection connection;
36+ private final ConnectionProvider provider;
3837
3938 /**
4039 * constructor
4140 * @param connection JDBC connection
4241 */
43- public TableAccess(Connection connection) {
44- try {
45- if (connection == null || connection.isClosed()) {
42+ public TableAccess(ConnectionProvider provider) {
43+ if (provider == null ) {
4644 throw new IllegalArgumentException();
4745 }
48- this.connection = connection;
49- } catch (SQLException e) {
50- throw new RuntimeException(e);
51- }
46+ this.provider = provider;
5247 }
5348
5449 protected String getTableName(Class<?> clz) {
@@ -108,11 +103,11 @@
108103
109104 protected void executeDiretory(String sql) throws SQLException {
110105 LOG.info("sql= " + sql);
111- connection.createStatement().executeUpdate(sql);
106+ provider.getConnection().createStatement().executeUpdate(sql);
112107 }
113-
108+
114109 protected ResultSet executeQuery(String sql) throws SQLException {
115110 LOG.info("sql= " + sql);
116- return connection.createStatement().executeQuery(sql);
111+ return provider.getConnection().createStatement().executeQuery(sql);
117112 }
118113 }
--- trunk/src/estoc/dbm/ConnectionProvider.java (nonexistent)
+++ trunk/src/estoc/dbm/ConnectionProvider.java (revision 30)
@@ -0,0 +1,9 @@
1+package estoc.dbm;
2+
3+import java.sql.Connection;
4+import java.sql.SQLException;
5+
6+
7+public interface ConnectionProvider {
8+ Connection getConnection() throws SQLException;
9+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- trunk/src/estoc/dbm/DataAccessImpl.java (revision 29)
+++ trunk/src/estoc/dbm/DataAccessImpl.java (revision 30)
@@ -19,23 +19,17 @@
1919 import java.util.Set;
2020 import java.util.logging.Logger;
2121
22-import estoc.dbm.annotate.Table;
23-
2422 class DataAccessImpl implements DataAccess {
2523 private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
26- private final Connection connection;
2724 private final SqlCreator creator;
25+ private final ConnectionProvider connectionProvider;
2826
29- public DataAccessImpl(Connection connection, SqlCreator creator) {
30- this.connection = connection;
27+ public DataAccessImpl(ConnectionProvider connectionProvider, SqlCreator creator) {
28+ this.connectionProvider = connectionProvider;
3129 this.creator = creator;
3230 }
3331
3432 Map<Class<?>, List<ColumnInfo>> fieldMap = new HashMap<Class<?>, List<ColumnInfo>>();
35- Map<Class<?>, PreparedStatement> selectStmts = new HashMap<Class<?>, PreparedStatement>();
36- Map<Class<?>, PreparedStatement> insertStmts = new HashMap<Class<?>, PreparedStatement>();
37- Map<Class<?>, PreparedStatement> updateStmts = new HashMap<Class<?>, PreparedStatement>();
38- Map<Class<?>, PreparedStatement> deleteStmts = new HashMap<Class<?>, PreparedStatement>();
3933
4034 private List<ColumnInfo> getDeclaredFields(Class<?> clz) {
4135 List<ColumnInfo> fields = fieldMap.get(clz);
@@ -52,20 +46,66 @@
5246
5347 /*
5448 * (non-Javadoc)
55- *
49+ *
5650 * @see dbm.DataAccess#select(java.lang.Class, java.lang.Object)
5751 */
5852 public <T> T select(Class<T> clz, Object... params) throws SQLException {
59- PreparedStatement stmt = selectStmts.get(clz);
60- if (stmt == null || stmt.getConnection() != connection) {
61- stmt = createSelectStmt(clz);
53+ PreparedStatement stmt = createSelectStmt(clz);
54+ try {
55+ setParams(stmt, params);
56+ ResultSet result = stmt.executeQuery();
57+ try {
58+ if (result != null && result.next()) {
59+ return DbmUtil.fillData(clz, result);
60+ }
61+ return null;
62+ } finally {
63+ DbmUtil.close(result);
64+ }
65+ } finally {
66+ DbmUtil.close(stmt);
6267 }
63- setParams(stmt, params);
64- ResultSet result = stmt.executeQuery();
65- if (result != null && result.next()) {
66- return DbmUtil.fillData(clz, result);
68+ }
69+
70+ /*
71+ * (非 Javadoc)
72+ *
73+ * @see estoc.dbm.DataAccess#fillter(java.lang.Class, java.lang.String,
74+ * java.lang.String)
75+ */
76+ public <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString) throws SQLException {
77+ return fillter(clz, whereString, orderString, -1);
78+ }
79+
80+ @Override public <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString, long maxCount)
81+ throws SQLException {
82+ Statement stmt = connectionProvider.getConnection().createStatement();
83+ try {
84+ String tblName = DbmUtil.getTableName(clz);
85+ StringBuilder sql = new StringBuilder("SELECT * FROM ").append(tblName);
86+ if (whereString != null && whereString.length() > 0) {
87+ sql.append(" WHERE ").append(whereString);
88+ }
89+ if (orderString != null && orderString.length() > 0) {
90+ sql.append(" ORDER BY ").append(orderString);
91+ }
92+ stmt.execute(sql.toString());
93+ ResultSet rs = stmt.getResultSet();
94+ try {
95+ ArrayList<T> rtn = new ArrayList<T>();
96+ if (rs == null) {
97+ return rtn;
98+ }
99+ for (long i = 0; rs.next() && (maxCount < 0 || i < maxCount); i++) {
100+ rtn.add(DbmUtil.fillData(clz, rs));
101+ }
102+ return rtn;
103+ } finally {
104+ DbmUtil.close(rs);
105+ }
106+ } finally {
107+ stmt.close();
67108 }
68- return null;
69109 }
70110
71111 private <T> PreparedStatement createSelectStmt(Class<T> clz) throws SQLException {
@@ -84,37 +124,42 @@
84124 if (pks.size() == 0) {
85125 throw new UnsupportedOperationException("When if you use SELECT, table have to be defined PK");
86126 }
87- String tableName = getTableName(clz);
127+ String tableName = DbmUtil.getTableName(clz);
88128 String selectSql = creator.getSelectSql(tableName, pks, cols);
89- stmt = connection.prepareStatement(selectSql);
90- selectStmts.put(clz, stmt);
129+ stmt = connectionProvider.getConnection().prepareStatement(selectSql);
91130 return stmt;
92131 }
93132
94133 /*
95134 * (non-Javadoc)
96- *
135+ *
97136 * @see dbm.DataAccess#delete(java.lang.Object)
98137 */
99138 public boolean delete(Object obj) throws SQLException {
100139 if (obj instanceof Class<?>) {
101- String tableName = getTableName((Class<?>) obj);
102- connection.createStatement().executeUpdate("DELETE FROM " + tableName);
140+ String tableName = DbmUtil.getTableName((Class<?>) obj);
141+ Connection connection = connectionProvider.getConnection();
142+ try {
143+ connection.createStatement().executeUpdate("DELETE FROM " + tableName);
144+ } finally {
145+ connection.close();
146+ }
103147 return true;
104148 }
105149 Class<?> clz = obj.getClass();
106- PreparedStatement stmt = deleteStmts.get(clz);
107- if (stmt == null || stmt.getConnection() != connection) {
108- stmt = createDeleteStmt(clz);
109- }
110- List<Object> params = new ArrayList<Object>();
111- for (ColumnInfo info : getDeclaredFields(clz)) {
112- if (info.isPk()) {
113- params.add(DbmUtil.getVlaueFromField(obj, info));
150+ PreparedStatement stmt = createDeleteStmt(clz);
151+ try {
152+ List<Object> params = new ArrayList<Object>();
153+ for (ColumnInfo info : getDeclaredFields(clz)) {
154+ if (info.isPk()) {
155+ params.add(DbmUtil.getVlaueFromField(obj, info));
156+ }
114157 }
158+ setParams(stmt, params.toArray());
159+ return stmt.executeUpdate() == 1;
160+ } finally {
161+ DbmUtil.close(stmt);
115162 }
116- setParams(stmt, params.toArray());
117- return stmt.executeUpdate() == 1;
118163 }
119164
120165 private PreparedStatement createDeleteStmt(Class<?> clz) throws SQLException {
@@ -128,94 +173,97 @@
128173 if (pks.size() == 0) {
129174 throw new UnsupportedOperationException("When if you use DELETE, table have to be defined PK");
130175 }
131- String tableName = getTableName(clz);
176+ String tableName = DbmUtil.getTableName(clz);
132177 String sql = creator.getDeleteSql(tableName, pks);
133178 LOG.info("sql = " + sql);
134- stmt = connection.prepareStatement(sql);
135- deleteStmts.put(clz, stmt);
179+ stmt = connectionProvider.getConnection().prepareStatement(sql);
136180 return stmt;
137181 }
138182
139183 /*
140184 * (non-Javadoc)
141- *
185+ *
142186 * @see dbm.DataAccess#insert(java.lang.Object)
143187 */
144188 public <T> T insert(T obj) throws SQLException {
145189 Class<?> clz = obj.getClass();
146- PreparedStatement stmt = insertStmts.get(clz);
147- // create statement
148- if (stmt == null || stmt.getConnection() != connection) {
149- stmt = createInsertStmt(clz);
150- }
151- // set value
152- List<Object> params = new ArrayList<Object>();
153- Date now = new Date();
154- List<ColumnInfo> autoIncFields = new ArrayList<ColumnInfo>();
155- List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
156- for (ColumnInfo info : getDeclaredFields(clz)) {
157- if (info.isColumn()) {
158- if (info.isAutoInc()) {
159- autoIncFields.add(info);
160- continue;
190+ PreparedStatement stmt = createInsertStmt(clz);
191+ try {
192+ // set value
193+ List<Object> params = new ArrayList<Object>();
194+ Date now = new Date();
195+ List<ColumnInfo> autoIncFields = new ArrayList<ColumnInfo>();
196+ List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
197+ for (ColumnInfo info : getDeclaredFields(clz)) {
198+ if (info.isColumn()) {
199+ if (info.isAutoInc()) {
200+ autoIncFields.add(info);
201+ continue;
202+ }
203+ if (info.isTimeStamp()) {
204+ timeFields.add(info);
205+ params.add(now);
206+ } else {
207+ params.add(DbmUtil.getVlaueFromField(obj, info));
208+ }
161209 }
162- if (info.isTimeStamp()) {
163- timeFields.add(info);
164- params.add(now);
165- } else {
166- params.add(DbmUtil.getVlaueFromField(obj, info));
210+ }
211+ setParams(stmt, params.toArray());
212+ // Do insert
213+ if (stmt.executeUpdate() == 1) {
214+ // AutoInc, TimeStamp の値埋め込み
215+ for (ColumnInfo info : timeFields) {
216+ DbmUtil.setVlaueToField(obj, info, now);
167217 }
168- }
169- }
170- setParams(stmt, params.toArray());
171- // Do insert
172- if (stmt.executeUpdate() == 1) {
173- // AutoInc, TimeStamp の値埋め込み
174- for (ColumnInfo info : timeFields) {
175- DbmUtil.setVlaueToField(obj, info, now);
176- }
177- ResultSet keys = stmt.getGeneratedKeys();
178- for (ColumnInfo info : autoIncFields) {
179- if (keys.next()) {
180- Object object = keys.getObject(1);
181- if (object instanceof Integer) {
182- if (info.getType() == Integer.class) {
183- DbmUtil.setVlaueToField(obj, info, object);
184- } else if (info.getType() == Long.class) {
185- Long val = ((Integer) object).longValue();
186- DbmUtil.setVlaueToField(obj, info, val);
218+ ResultSet keys = stmt.getGeneratedKeys();
219+ try {
220+ for (ColumnInfo info : autoIncFields) {
221+ if (keys.next()) {
222+ Object object = keys.getObject(1);
223+ if (object instanceof Integer) {
224+ if (info.getType() == Integer.class) {
225+ DbmUtil.setVlaueToField(obj, info, object);
226+ } else if (info.getType() == Long.class) {
227+ Long val = ((Integer) object).longValue();
228+ DbmUtil.setVlaueToField(obj, info, val);
229+ } else {
230+ throw new UnsupportedOperationException("Unsupport type:" + info.getType());
231+ }
232+ } else if (object instanceof Long) {
233+ if (info.getType() == Integer.class) {
234+ Integer val = ((Long) object).intValue();
235+ DbmUtil.setVlaueToField(obj, info, val);
236+ } else if (info.getType() == Long.class) {
237+ DbmUtil.setVlaueToField(obj, info, object);
238+ } else {
239+ throw new UnsupportedOperationException("Unsupport type:" + info.getType());
240+ }
241+ } else if (object instanceof BigDecimal) {
242+ if (info.getType() == Integer.class) {
243+ Integer val = ((BigDecimal) object).intValue();
244+ DbmUtil.setVlaueToField(obj, info, val);
245+ } else if (info.getType() == Long.class) {
246+ Long val = ((BigDecimal) object).longValue();
247+ DbmUtil.setVlaueToField(obj, info, val);
248+ } else {
249+ throw new UnsupportedOperationException("Unsupport type:" + info.getType());
250+ }
251+ } else {
252+ throw new UnsupportedOperationException("Unsupport result type:" + object.getClass());
253+ }
187254 } else {
188- throw new UnsupportedOperationException("Unsupport type:" + info.getType());
255+ break;
189256 }
190- } else if (object instanceof Long) {
191- if (info.getType() == Integer.class) {
192- Integer val = ((Long) object).intValue();
193- DbmUtil.setVlaueToField(obj, info, val);
194- } else if (info.getType() == Long.class) {
195- DbmUtil.setVlaueToField(obj, info, object);
196- } else {
197- throw new UnsupportedOperationException("Unsupport type:" + info.getType());
198- }
199- } else if (object instanceof BigDecimal) {
200- if (info.getType() == Integer.class) {
201- Integer val = ((BigDecimal) object).intValue();
202- DbmUtil.setVlaueToField(obj, info, val);
203- } else if (info.getType() == Long.class) {
204- Long val = ((BigDecimal) object).longValue();
205- DbmUtil.setVlaueToField(obj, info, val);
206- } else {
207- throw new UnsupportedOperationException("Unsupport type:" + info.getType());
208- }
209- } else {
210- throw new UnsupportedOperationException("Unsupport result type:" + object.getClass());
211257 }
212- } else {
213- break;
258+ } finally {
259+ DbmUtil.close(keys);
214260 }
261+ return obj;
262+ } else {
263+ return null;
215264 }
216- return obj;
217- } else {
218- return null;
265+ } finally {
266+ DbmUtil.close(stmt);
219267 }
220268 }
221269
@@ -229,56 +277,56 @@
229277 }
230278 }
231279 }
232- String tableName = getTableName(clz);
280+ String tableName = DbmUtil.getTableName(clz);
233281 String sql = creator.getInsertSql(tableName, columns);
234282 LOG.info("sql= " + sql);
235- stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
236- insertStmts.put(clz, stmt);
283+ stmt = connectionProvider.getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
237284 return stmt;
238285 }
239286
240287 /*
241288 * (non-Javadoc)
242- *
289+ *
243290 * @see dbm.DataAccess#update(java.lang.Object, boolean)
244291 */
245292 public <T> T update(T obj) throws SQLException {
246293 Class<?> clz = obj.getClass();
247- PreparedStatement stmt = updateStmts.get(clz);
248- if (stmt == null || stmt.getConnection() != connection) {
249- stmt = createUpdateStmt(clz);
250- }
251- // set value
252- List<Object> params = new ArrayList<Object>();
253- List<Object> pkParams = new ArrayList<Object>();
254- List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
255- Date now = new Date();
256- for (ColumnInfo info : getDeclaredFields(clz)) {
257- if (info.isPk()) {
258- pkParams.add(DbmUtil.getVlaueFromField(obj, info));
259- } else {
260- if (info.isColumn()) {
261- if (info.isTimeStamp()) {
262- timeFields.add(info);
263- params.add(now);
264- } else {
265- params.add(DbmUtil.getVlaueFromField(obj, info));
294+ PreparedStatement stmt = createUpdateStmt(clz);
295+ try {
296+ // set value
297+ List<Object> params = new ArrayList<Object>();
298+ List<Object> pkParams = new ArrayList<Object>();
299+ List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
300+ Date now = new Date();
301+ for (ColumnInfo info : getDeclaredFields(clz)) {
302+ if (info.isPk()) {
303+ pkParams.add(DbmUtil.getVlaueFromField(obj, info));
304+ } else {
305+ if (info.isColumn()) {
306+ if (info.isTimeStamp()) {
307+ timeFields.add(info);
308+ params.add(now);
309+ } else {
310+ params.add(DbmUtil.getVlaueFromField(obj, info));
311+ }
266312 }
267313 }
268314 }
269- }
270- List<Object> tmp = new ArrayList<Object>();
271- tmp.addAll(params);
272- tmp.addAll(pkParams);
273- setParams(stmt, tmp.toArray());
274- // do update
275- if (stmt.executeUpdate() == 1) {
276- for (ColumnInfo info : timeFields) {
277- DbmUtil.setVlaueToField(obj, info, now);
315+ List<Object> tmp = new ArrayList<Object>();
316+ tmp.addAll(params);
317+ tmp.addAll(pkParams);
318+ setParams(stmt, tmp.toArray());
319+ // do update
320+ if (stmt.executeUpdate() == 1) {
321+ for (ColumnInfo info : timeFields) {
322+ DbmUtil.setVlaueToField(obj, info, now);
323+ }
324+ return obj;
325+ } else {
326+ return null;
278327 }
279- return obj;
280- } else {
281- return null;
328+ } finally {
329+ DbmUtil.close(stmt);
282330 }
283331 }
284332
@@ -298,19 +346,13 @@
298346 if (pks.size() == 0) {
299347 throw new UnsupportedOperationException("When if you use UPDATE, table have to be defined PK");
300348 }
301- String tableName = getTableName(clz);
349+ String tableName = DbmUtil.getTableName(clz);
302350 String sql = creator.getUpdateSql(tableName, pks, columns);
303351 LOG.info("sql= " + sql);
304- stmt = connection.prepareStatement(sql);
305- updateStmts.put(clz, stmt);
352+ stmt = connectionProvider.getConnection().prepareStatement(sql);
306353 return stmt;
307354 }
308355
309- private String getTableName(Class<?> clz) {
310- Table table = clz.getAnnotation(Table.class);
311- return table.value();
312- }
313-
314356 private int getSqlType(Class<?> type) {
315357 if (type == String.class) {
316358 return Types.VARCHAR;
--- trunk/src/estoc/dbm/TableAccessPgsql.java (revision 29)
+++ trunk/src/estoc/dbm/TableAccessPgsql.java (revision 30)
@@ -1,12 +1,11 @@
11 package estoc.dbm;
22
3-import java.sql.Connection;
43 import java.sql.SQLException;
54 import java.util.List;
65
76 class TableAccessPgsql extends TableAccess {
8- public TableAccessPgsql(Connection connection) {
9- super(connection);
7+ public TableAccessPgsql(ConnectionProvider provider) {
8+ super(provider);
109 }
1110
1211 @Override
--- trunk/src/estoc/dbm/DataAccessPgsqlImpl.java (revision 29)
+++ trunk/src/estoc/dbm/DataAccessPgsqlImpl.java (revision 30)
@@ -4,10 +4,10 @@
44 import java.io.InputStream;
55 import java.lang.reflect.Field;
66 import java.math.BigDecimal;
7-import java.sql.Connection;
87 import java.sql.PreparedStatement;
98 import java.sql.ResultSet;
109 import java.sql.SQLException;
10+import java.sql.Statement;
1111 import java.sql.Types;
1212 import java.util.ArrayList;
1313 import java.util.Date;
@@ -18,24 +18,17 @@
1818 import java.util.Set;
1919 import java.util.logging.Logger;
2020
21-import estoc.dbm.annotate.Table;
22-
2321 class DataAccessPgsqlImpl implements DataAccess {
2422 private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
25- private final Connection connection;
2623 private final SqlCreator creator;
24+ private final ConnectionProvider connectionProvider;
2725
28- public DataAccessPgsqlImpl(Connection connection, SqlCreator creator) {
29- this.connection = connection;
26+ public DataAccessPgsqlImpl(ConnectionProvider connectionProvider, SqlCreator creator) {
27+ this.connectionProvider = connectionProvider;
3028 this.creator = creator;
3129 }
3230
3331 Map<Class<?>, List<ColumnInfo>> fieldMap = new HashMap<Class<?>, List<ColumnInfo>>();
34- Map<Class<?>, PreparedStatement> selectStmts = new HashMap<Class<?>, PreparedStatement>();
35- Map<Class<?>, PreparedStatement> insertStmts = new HashMap<Class<?>, PreparedStatement>();
36- Map<Class<?>, PreparedStatement> autoKeyStmts = new HashMap<Class<?>, PreparedStatement>();
37- Map<Class<?>, PreparedStatement> updateStmts = new HashMap<Class<?>, PreparedStatement>();
38- Map<Class<?>, PreparedStatement> deleteStmts = new HashMap<Class<?>, PreparedStatement>();
3932
4033 private List<ColumnInfo> getDeclaredFields(Class<?> clz) {
4134 List<ColumnInfo> fields = fieldMap.get(clz);
@@ -52,24 +45,69 @@
5245
5346 /*
5447 * (non-Javadoc)
55- *
48+ *
5649 * @see dbm.DataAccess#select(java.lang.Class, java.lang.Object)
5750 */
5851 public <T> T select(Class<T> clz, Object... params) throws SQLException {
59- PreparedStatement stmt = selectStmts.get(clz);
60- if (stmt == null || stmt.getConnection() != connection) {
61- stmt = createSelectStmt(clz);
52+ PreparedStatement stmt = createSelectStmt(clz);
53+ try {
54+ setParams(stmt, params);
55+ ResultSet result = stmt.executeQuery();
56+ try {
57+ if (result != null && result.next()) {
58+ return DbmUtil.fillData(clz, result);
59+ }
60+ return null;
61+ } finally {
62+ DbmUtil.close(result);
63+ }
64+ } finally {
65+ DbmUtil.close(stmt);
6266 }
63- setParams(stmt, params);
64- ResultSet result = stmt.executeQuery();
65- if (result != null && result.next()) {
66- return DbmUtil.fillData(clz, result);
67+ }
68+
69+ /*
70+ * (非 Javadoc)
71+ *
72+ * @see estoc.dbm.DataAccess#fillter(java.lang.Class, java.lang.String,
73+ * java.lang.String)
74+ */
75+ public <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString) throws SQLException {
76+ return fillter(clz, whereString, orderString, -1);
77+ }
78+
79+ @Override public <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString, long maxCount)
80+ throws SQLException {
81+ Statement stmt = connectionProvider.getConnection().createStatement();
82+ try {
83+ String tblName = DbmUtil.getTableName(clz);
84+ StringBuilder sql = new StringBuilder("SELECT * FROM ").append(tblName);
85+ if (whereString != null && whereString.length() > 0) {
86+ sql.append(" WHERE ").append(whereString);
87+ }
88+ if (orderString != null && orderString.length() > 0) {
89+ sql.append(" ORDER BY ").append(orderString);
90+ }
91+ stmt.execute(sql.toString());
92+ ResultSet rs = stmt.getResultSet();
93+ try {
94+ ArrayList<T> rtn = new ArrayList<T>();
95+ if (rs == null) {
96+ return rtn;
97+ }
98+ for (long i = 0; rs.next() && (maxCount < 0 || i < maxCount); i++) {
99+ rtn.add(DbmUtil.fillData(clz, rs));
100+ }
101+ return rtn;
102+ } finally {
103+ DbmUtil.close(rs);
104+ }
105+ } finally {
106+ DbmUtil.close(stmt);
67107 }
68- return null;
69108 }
70109
71110 private <T> PreparedStatement createSelectStmt(Class<T> clz) throws SQLException {
72- PreparedStatement stmt;
73111 Set<String> cols = new LinkedHashSet<String>();
74112 Set<String> pks = new LinkedHashSet<String>();
75113 for (Field field : clz.getDeclaredFields()) {
@@ -84,36 +122,35 @@
84122 if (pks.size() == 0) {
85123 throw new UnsupportedOperationException("When if you use SELECT, table have to be defined PK");
86124 }
87- String tableName = getTableName(clz);
125+ String tableName = DbmUtil.getTableName(clz);
88126 String selectSql = creator.getSelectSql(tableName, pks, cols);
89- stmt = connection.prepareStatement(selectSql);
90- selectStmts.put(clz, stmt);
127+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(selectSql);
91128 return stmt;
92129 }
93130
94131 /*
95132 * (non-Javadoc)
96- *
133+ *
97134 * @see dbm.DataAccess#delete(java.lang.Object)
98135 */
99136 public boolean delete(Object obj) throws SQLException {
100137 Class<?> clz = obj.getClass();
101- PreparedStatement stmt = deleteStmts.get(clz);
102- if (stmt == null || stmt.getConnection() != connection) {
103- stmt = createDeleteStmt(clz);
104- }
105- List<Object> params = new ArrayList<Object>();
106- for (ColumnInfo info : getDeclaredFields(clz)) {
107- if (info.isPk()) {
108- params.add(DbmUtil.getVlaueFromField(obj, info));
138+ PreparedStatement stmt = createDeleteStmt(clz);
139+ try {
140+ List<Object> params = new ArrayList<Object>();
141+ for (ColumnInfo info : getDeclaredFields(clz)) {
142+ if (info.isPk()) {
143+ params.add(DbmUtil.getVlaueFromField(obj, info));
144+ }
109145 }
146+ setParams(stmt, params.toArray());
147+ return stmt.executeUpdate() == 1;
148+ } finally {
149+ DbmUtil.close(stmt);
110150 }
111- setParams(stmt, params.toArray());
112- return stmt.executeUpdate() == 1;
113151 }
114152
115153 private PreparedStatement createDeleteStmt(Class<?> clz) throws SQLException {
116- PreparedStatement stmt;
117154 Set<String> pks = new LinkedHashSet<String>();
118155 for (ColumnInfo info : getDeclaredFields(clz)) {
119156 if (info.isPk()) {
@@ -123,94 +160,91 @@
123160 if (pks.size() == 0) {
124161 throw new UnsupportedOperationException("When if you use DELETE, table have to be defined PK");
125162 }
126- String tableName = getTableName(clz);
163+ String tableName = DbmUtil.getTableName(clz);
127164 String sql = creator.getDeleteSql(tableName, pks);
128165 LOG.info("sql = " + sql);
129- stmt = connection.prepareStatement(sql);
130- deleteStmts.put(clz, stmt);
166+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(sql);
131167 return stmt;
132168 }
133169
134170 /*
135171 * (non-Javadoc)
136- *
172+ *
137173 * @see dbm.DataAccess#insert(java.lang.Object)
138174 */
139175 public <T> T insert(T obj) throws SQLException {
140176 Class<?> clz = obj.getClass();
141-
142- PreparedStatement getKeyStmt;
143- if (autoKeyStmts.containsKey(clz)) {
144- getKeyStmt = autoKeyStmts.get(clz);
145- } else {
146- getKeyStmt = createGetKeyStmt(clz);
147- }
148-
149177 int nextVal = -1;
150- if (getKeyStmt != null) {
151- ResultSet rs = getKeyStmt.executeQuery();
152- if (rs.next()) {
153- nextVal = rs.getInt(1);
154- } else {
155- throw new RuntimeException("Faild to get nextval for auto inc.");
178+ PreparedStatement getKeyStmt = createGetKeyStmt(clz);
179+ try {
180+ if (getKeyStmt != null) {
181+ ResultSet rs = getKeyStmt.executeQuery();
182+ try {
183+ if (rs.next()) {
184+ nextVal = rs.getInt(1);
185+ } else {
186+ throw new RuntimeException("Faild to get nextval for auto inc.");
187+ }
188+ } finally {
189+ DbmUtil.close(rs);
190+ }
156191 }
157-
192+ } finally {
193+ DbmUtil.close(getKeyStmt);
158194 }
159-
160- PreparedStatement stmt = insertStmts.get(clz);
161- // create statement
162- if (stmt == null || stmt.getConnection() != connection) {
163- stmt = createInsertStmt(clz);
164- }
165- // set value
166- List<Object> params = new ArrayList<Object>();
167- Date now = new Date();
168- ColumnInfo autoIncField = null;
169- List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
170- for (ColumnInfo info : getDeclaredFields(clz)) {
171- if (info.isColumn()) {
172- if (info.isAutoInc()) {
173- autoIncField = info;
174- params.add(nextVal);
175- } else if (info.isTimeStamp()) {
176- timeFields.add(info);
177- params.add(now);
178- } else {
179- params.add(DbmUtil.getVlaueFromField(obj, info));
195+ PreparedStatement stmt = createInsertStmt(clz);
196+ try {
197+ // set value
198+ List<Object> params = new ArrayList<Object>();
199+ Date now = new Date();
200+ ColumnInfo autoIncField = null;
201+ List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
202+ for (ColumnInfo info : getDeclaredFields(clz)) {
203+ if (info.isColumn()) {
204+ if (info.isAutoInc()) {
205+ autoIncField = info;
206+ params.add(nextVal);
207+ } else if (info.isTimeStamp()) {
208+ timeFields.add(info);
209+ params.add(now);
210+ } else {
211+ params.add(DbmUtil.getVlaueFromField(obj, info));
212+ }
180213 }
181214 }
182- }
183- setParams(stmt, params.toArray());
184- // Do insert
185- if (stmt.executeUpdate() == 1) {
186- // AutoInc, TimeStamp の値埋め込み
187- for (ColumnInfo info : timeFields) {
188- DbmUtil.setVlaueToField(obj, info, now);
189- }
190- if (autoIncField != null) {
191- if (autoIncField.getType() == Integer.class) {
192- DbmUtil.setVlaueToField(obj, autoIncField, new Integer(nextVal));
193- } else if (autoIncField.getType() == Long.class) {
194- DbmUtil.setVlaueToField(obj, autoIncField, new Long(nextVal));
195- } else {
196- throw new UnsupportedOperationException("Unsupport type:" + autoIncField.getType());
215+ setParams(stmt, params.toArray());
216+ // Do insert
217+ if (stmt.executeUpdate() == 1) {
218+ // AutoInc, TimeStamp の値埋め込み
219+ for (ColumnInfo info : timeFields) {
220+ DbmUtil.setVlaueToField(obj, info, now);
197221 }
222+ if (autoIncField != null) {
223+ if (autoIncField.getType() == Integer.class) {
224+ DbmUtil.setVlaueToField(obj, autoIncField, new Integer(nextVal));
225+ } else if (autoIncField.getType() == Long.class) {
226+ DbmUtil.setVlaueToField(obj, autoIncField, new Long(nextVal));
227+ } else {
228+ throw new UnsupportedOperationException("Unsupport type:" + autoIncField.getType());
229+ }
230+ }
231+ return obj;
232+ } else {
233+ return null;
198234 }
199- return obj;
200- } else {
201- return null;
235+ } finally {
236+ DbmUtil.close(stmt);
202237 }
203238 }
204239
205240 /**
206241 * serialの次番を取得するStatementを作成します AutoInc列が存在しない場合nullを返します
207- *
242+ *
208243 * @param clz
209244 * @return
210245 * @throws SQLException
211246 */
212247 private PreparedStatement createGetKeyStmt(Class<?> clz) throws SQLException {
213- PreparedStatement stmt;
214248 String colName = null;
215249 for (ColumnInfo info : getDeclaredFields(clz)) {
216250 if (info.isColumn()) {
@@ -221,24 +255,19 @@
221255 }
222256 }
223257 if (colName == null) {
224- autoKeyStmts.put(clz, null);
225258 return null;
226259 }
227-
228260 StringBuilder sb = new StringBuilder();
229-
230261 sb.append("SELECT nextval('");
231- sb.append(getTableName(clz));
262+ sb.append(DbmUtil.getTableName(clz));
232263 sb.append("_").append(colName);
233264 sb.append("_seq");
234265 sb.append("')");
235- stmt = connection.prepareStatement(sb.toString());
236- autoKeyStmts.put(clz, stmt);
266+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(sb.toString());
237267 return stmt;
238268 }
239269
240270 private PreparedStatement createInsertStmt(Class<?> clz) throws SQLException {
241- PreparedStatement stmt;
242271 Set<String> columns = new LinkedHashSet<String>();
243272 for (ColumnInfo info : getDeclaredFields(clz)) {
244273 if (info.isColumn()) {
@@ -246,62 +275,60 @@
246275 columns.add(info.getName());
247276 }
248277 }
249- String tableName = getTableName(clz);
278+ String tableName = DbmUtil.getTableName(clz);
250279 String sql = creator.getInsertSql(tableName, columns);
251280 LOG.info("sql= " + sql);
252-
253- stmt = connection.prepareStatement(sql);
254- insertStmts.put(clz, stmt);
281+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(sql);
255282 return stmt;
256283 }
257284
258285 /*
259286 * (non-Javadoc)
260- *
287+ *
261288 * @see dbm.DataAccess#update(java.lang.Object, boolean)
262289 */
263290 public <T> T update(T obj) throws SQLException {
264291 Class<?> clz = obj.getClass();
265- PreparedStatement stmt = updateStmts.get(clz);
266- if (stmt == null || stmt.getConnection() != connection) {
267- stmt = createUpdateStmt(clz);
268- }
269- // set value
270- List<Object> params = new ArrayList<Object>();
271- List<Object> pkParams = new ArrayList<Object>();
272- List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
273- Date now = new Date();
274- for (ColumnInfo info : getDeclaredFields(clz)) {
275- if (info.isPk()) {
276- pkParams.add(DbmUtil.getVlaueFromField(obj, info));
277- } else {
278- if (info.isColumn()) {
279- if (info.isTimeStamp()) {
280- timeFields.add(info);
281- params.add(now);
282- } else {
283- params.add(DbmUtil.getVlaueFromField(obj, info));
292+ PreparedStatement stmt = createUpdateStmt(clz);
293+ try {
294+ // set value
295+ List<Object> params = new ArrayList<Object>();
296+ List<Object> pkParams = new ArrayList<Object>();
297+ List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
298+ Date now = new Date();
299+ for (ColumnInfo info : getDeclaredFields(clz)) {
300+ if (info.isPk()) {
301+ pkParams.add(DbmUtil.getVlaueFromField(obj, info));
302+ } else {
303+ if (info.isColumn()) {
304+ if (info.isTimeStamp()) {
305+ timeFields.add(info);
306+ params.add(now);
307+ } else {
308+ params.add(DbmUtil.getVlaueFromField(obj, info));
309+ }
284310 }
285311 }
286312 }
287- }
288- List<Object> tmp = new ArrayList<Object>();
289- tmp.addAll(params);
290- tmp.addAll(pkParams);
291- setParams(stmt, tmp.toArray());
292- // do update
293- if (stmt.executeUpdate() == 1) {
294- for (ColumnInfo info : timeFields) {
295- DbmUtil.setVlaueToField(obj, info, now);
313+ List<Object> tmp = new ArrayList<Object>();
314+ tmp.addAll(params);
315+ tmp.addAll(pkParams);
316+ setParams(stmt, tmp.toArray());
317+ // do update
318+ if (stmt.executeUpdate() == 1) {
319+ for (ColumnInfo info : timeFields) {
320+ DbmUtil.setVlaueToField(obj, info, now);
321+ }
322+ return obj;
323+ } else {
324+ return null;
296325 }
297- return obj;
298- } else {
299- return null;
326+ } finally {
327+ DbmUtil.close(stmt);
300328 }
301329 }
302330
303331 private PreparedStatement createUpdateStmt(Class<?> clz) throws SQLException {
304- PreparedStatement stmt;
305332 Set<String> pks = new LinkedHashSet<String>();
306333 Set<String> columns = new LinkedHashSet<String>();
307334 for (ColumnInfo info : getDeclaredFields(clz)) {
@@ -316,19 +343,13 @@
316343 if (pks.size() == 0) {
317344 throw new UnsupportedOperationException("When if you use UPDATE, table have to be defined PK");
318345 }
319- String tableName = getTableName(clz);
346+ String tableName = DbmUtil.getTableName(clz);
320347 String sql = creator.getUpdateSql(tableName, pks, columns);
321348 LOG.info("sql= " + sql);
322- stmt = connection.prepareStatement(sql);
323- updateStmts.put(clz, stmt);
349+ PreparedStatement stmt = connectionProvider.getConnection().prepareStatement(sql);
324350 return stmt;
325351 }
326352
327- private String getTableName(Class<?> clz) {
328- Table table = clz.getAnnotation(Table.class);
329- return table.value();
330- }
331-
332353 private int getSqlType(Class<?> type) {
333354 if (type == String.class) {
334355 return Types.VARCHAR;
--- trunk/src/estoc/dbm/DataAccess.java (revision 29)
+++ trunk/src/estoc/dbm/DataAccess.java (revision 30)
@@ -1,6 +1,7 @@
11 package estoc.dbm;
22
33 import java.sql.SQLException;
4+import java.util.ArrayList;
45
56 /**
67 * Data access class <br>
@@ -7,10 +8,10 @@
78 * ex. select, delete, insert, update
89 */
910 public interface DataAccess {
10-
11-
11+
12+
1213 /**
13- * Select the record
14+ * Select a record
1415 * @param <T> Class
1516 * @param clz Table object
1617 * @param pk conditions
@@ -19,7 +20,31 @@
1920 */
2021 public abstract <T> T select(Class<T> clz, Object... params) throws SQLException;
2122
23+
2224 /**
25+ * Select sum records
26+ * @param clz
27+ * @param whereString ex. id > 100 or empty
28+ * @param orderString ex. id DESC or empty
29+ * @return selected records or emptyList if record not found
30+ * @throws SQLException
31+ */
32+ public abstract <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString) throws SQLException;
33+
34+
35+
36+ /**
37+ * Select sum records
38+ * @param clz
39+ * @param whereString ex. id > 100 or empty
40+ * @param orderString ex. id DESC or empty
41+ * @param maxCount max record count
42+ * @return selected records or emptyList if record not found
43+ * @throws SQLException
44+ */
45+ public abstract <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString, long maxCount) throws SQLException;
46+
47+ /**
2348 * Delete the record
2449 * @param obj TableClass
2550 * @return true: if success, false:faild
@@ -29,7 +54,7 @@
2954
3055 /**
3156 * Insert the record
32- * @param <T> Class
57+ * @param <T> Class
3358 * @param obj Table object
3459 * @return inserted record
3560 * @throws SQLException
--- trunk/src/estoc/dbm/AccessManager.java (revision 29)
+++ trunk/src/estoc/dbm/AccessManager.java (revision 30)
@@ -1,6 +1,5 @@
11 package estoc.dbm;
22
3-import java.sql.Connection;
43
54 /**
65 * Database accesser factory
@@ -10,7 +9,9 @@
109
1110 /**
1211 * constructor
13- * @param dbType the type of Database
12+ *
13+ * @param dbType
14+ * the type of Database
1415 */
1516 public AccessManager(DbType dbType) {
1617 if (dbType == null) {
@@ -21,50 +22,55 @@
2122
2223 /**
2324 * get DataAccess object
24- * @param connection JDBC connection
25+ *
26+ * @param connection
27+ * JDBC connection
2528 * @return
2629 */
27- public DataAccess getDataAccess(Connection connection) {
30+ public DataAccess getDataAccess(ConnectionProvider connectionProvider) {
31+ if (connectionProvider == null) {
32+ throw new IllegalArgumentException("connectionProvider is null");
33+ }
2834 SqlCreatorImpl creator = new SqlCreatorImpl();
2935 switch (dbType) {
3036 case H2:
3137 case Derby:
32- return new DataAccessImpl(connection, creator);
38+ return new DataAccessImpl(connectionProvider, creator);
3339 case Mysql:
3440 creator.setColumnQuoter("`");
35- return new DataAccessImpl(connection, creator);
41+ return new DataAccessImpl(connectionProvider, creator);
3642 case Postgresql:
37- return new DataAccessPgsqlImpl(connection, creator);
43+ return new DataAccessPgsqlImpl(connectionProvider, creator);
3844 case Oracle:
39- return new DataAccessOracleImpl(connection, creator);
45+ return new DataAccessOracleImpl(connectionProvider, creator);
4046 default:
4147 throw new UnsupportedOperationException("DbType=" + dbType);
42-
4348 }
4449 }
4550
4651 /**
4752 * get TableAccess object
53+ *
4854 * @param connection
4955 * @return
5056 */
51- public TableAccess getTableAccess(Connection connection) {
57+ public TableAccess getTableAccess(ConnectionProvider provider) {
5258 TableAccess tableAccess = null;
5359 switch (dbType) {
5460 case H2:
55- tableAccess = new TableAccessH2(connection);
61+ tableAccess = new TableAccessH2(provider);
5662 break;
5763 case Derby:
58- tableAccess = new TableAccessDerby(connection);
64+ tableAccess = new TableAccessDerby(provider);
5965 break;
6066 case Postgresql:
61- tableAccess = new TableAccessPgsql(connection);
67+ tableAccess = new TableAccessPgsql(provider);
6268 break;
6369 case Mysql:
64- tableAccess = new TableAccessMysql(connection);
70+ tableAccess = new TableAccessMysql(provider);
6571 break;
6672 case Oracle:
67- tableAccess = new TableAccessOracle(connection);
73+ tableAccess = new TableAccessOracle(provider);
6874 break;
6975 default:
7076 throw new UnsupportedOperationException("DbType=" + dbType);
--- trunk/build.xml (revision 29)
+++ trunk/build.xml (revision 30)
@@ -2,8 +2,8 @@
22
33 <project name="estocdb" default="jar" basedir=".">
44
5- <property name="release.version" value="0.4"/>
6-
5+ <property name="release.version" value="1.0"/>
6+
77 <!-- property -->
88 <property name="src.dir" value="src"/>
99 <property name="classes.dir" value="classes"/>
@@ -30,7 +30,7 @@
3030 <fileset dir="${src.dir}"/>
3131 </javadoc>
3232 </target>
33-
33+
3434 <!-- target clean-->
3535 <target name="clean">
3636 <delete dir="${classes.dir}" />
Show on old repository browser