author | František Kučera <franta-hg@frantovo.cz> |
Sun, 22 Dec 2013 23:31:55 +0100 | |
branch | v_0 |
changeset 34 | 9335cf31c0f2 |
parent 29 | d66858b4b563 |
child 35 | b2ff3b2d58b2 |
permissions | -rw-r--r-- |
26 | 1 |
/** |
2 |
* SQL-DK |
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
27
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
18 |
package info.globalcode.sql.dk; |
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
19 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
20 |
import info.globalcode.sql.dk.batch.Batch; |
27
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
21 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
22 |
import info.globalcode.sql.dk.formatting.ColumnsHeader; |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
23 |
import info.globalcode.sql.dk.formatting.Formatter; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
24 |
import java.sql.Connection; |
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
25 |
import java.sql.DriverManager; |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
26 |
import java.sql.PreparedStatement; |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
27 |
import java.sql.ResultSet; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
28 |
import java.sql.SQLException; |
26 | 29 |
|
30 |
/** |
|
31 |
* |
|
32 |
* @author Ing. František Kučera (frantovo.cz) |
|
33 |
*/ |
|
34 |
public class DatabaseConnection { |
|
35 |
||
36 |
private DatabaseDefinition databaseDefinition; |
|
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
37 |
private Connection connection; |
26 | 38 |
|
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
39 |
public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException { |
26 | 40 |
this.databaseDefinition = databaseDefinition; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
41 |
|
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
42 |
connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword()); |
26 | 43 |
} |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
44 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
45 |
public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
46 |
formatter.writeStartDatabase(databaseDefinition); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
47 |
processCommand(sqlCommand, formatter); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
48 |
formatter.writeEndDatabase(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
49 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
50 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
51 |
public void executeBatch(Batch batch, Formatter formatter) throws SQLException { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
52 |
formatter.writeStartDatabase(databaseDefinition); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
53 |
while (batch.hasNext()) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
54 |
processCommand(batch.next(), formatter); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
55 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
56 |
formatter.writeEndDatabase(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
57 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
58 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
59 |
private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
60 |
SQLCommand.COMMAND_TYPE commandType = sqlCommand.getCommandType(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
61 |
switch (commandType) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
62 |
case QUERY: |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
63 |
processQueryCommand(sqlCommand, formatter); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
64 |
break; |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
65 |
case UPDATE: |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
66 |
processUpdateCommand(sqlCommand, formatter); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
67 |
break; |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
68 |
default: |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
69 |
throw new IllegalArgumentException("Unexpected command type: " + commandType); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
70 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
71 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
72 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
73 |
private void processQueryCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
74 |
formatter.writeStartResultSet(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
75 |
formatter.writeQuery(sqlCommand.getQuery()); |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
76 |
formatter.writeParameters(sqlCommand.getParameters()); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
77 |
try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
78 |
sqlCommand.parametrize(ps); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
79 |
try (ResultSet rs = ps.executeQuery()) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
80 |
processResultSet(rs, formatter); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
81 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
82 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
83 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
84 |
formatter.writeEndResultSet(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
85 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
86 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
87 |
private void processUpdateCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
88 |
formatter.writeStartUpdatesResult(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
89 |
formatter.writeQuery(sqlCommand.getQuery()); |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
90 |
formatter.writeParameters(sqlCommand.getParameters()); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
91 |
try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
92 |
sqlCommand.parametrize(ps); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
93 |
int updatedRowsCount = ps.executeUpdate(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
94 |
formatter.writeUpdatedRowsCount(updatedRowsCount); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
95 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
96 |
formatter.writeStartGeneratedKeys(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
97 |
try (ResultSet rs = ps.getGeneratedKeys()) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
98 |
processResultSet(rs, formatter); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
99 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
100 |
formatter.writeEndGeneratedKeys(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
101 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
102 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
103 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
104 |
formatter.writeEndUpdatesResult(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
105 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
106 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
107 |
private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException { |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
108 |
formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData())); |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
109 |
int columnCount = rs.getMetaData().getColumnCount(); |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
110 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
111 |
while (rs.next()) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
112 |
formatter.writeStartRow(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
113 |
|
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
114 |
for (int i = 1; i <= columnCount; i++) { |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
115 |
formatter.writeColumnValue(rs.getObject(i)); |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
116 |
} |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
117 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
118 |
formatter.writeEndRow(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
119 |
} |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
120 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
121 |
} |
26 | 122 |
} |