root / trunk / org.gvsig.mysql / org.gvsig.mysql.provider / src / main / java / org / gvsig / mysql / dal / MySQLHelper.java @ 120
History | View | Annotate | Download (7.14 KB)
1 | 22 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2016 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | package org.gvsig.mysql.dal; |
||
23 | |||
24 | import java.sql.Connection; |
||
25 | import java.sql.SQLException; |
||
26 | import java.text.MessageFormat; |
||
27 | import org.apache.commons.dbcp.BasicDataSource; |
||
28 | import org.cresques.cts.IProjection; |
||
29 | import org.gvsig.fmap.dal.SQLBuilder; |
||
30 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
31 | import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
||
32 | import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
||
33 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
||
34 | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
||
35 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
||
36 | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
||
37 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase; |
||
38 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
39 | import org.gvsig.mysql.dal.operations.MySQLOperationsFactory; |
||
40 | |||
41 | import org.slf4j.Logger; |
||
42 | import org.slf4j.LoggerFactory; |
||
43 | |||
44 | public class MySQLHelper extends JDBCHelperBase { |
||
45 | |||
46 | static final Logger logger = LoggerFactory.getLogger(MySQLHelper.class); |
||
47 | |||
48 | public static final String NAME = "MySQL"; |
||
49 | public static final String MySQLJDBCDriver = "com.mysql.jdbc.Driver"; |
||
50 | |||
51 | public static String getConnectionURL(MySQLConnectionParameters params) { |
||
52 | String connectionURL = MessageFormat.format( |
||
53 | "jdbc:mysql://{0}:{1,number,#}/{2}",
|
||
54 | params.getHost(), |
||
55 | params.getPort(), |
||
56 | params.getDBName() |
||
57 | ); |
||
58 | logger.debug("connectionURL: {}", connectionURL);
|
||
59 | return connectionURL;
|
||
60 | } |
||
61 | |||
62 | private static class ConnectionProvider { |
||
63 | |||
64 | private static boolean needRegisterDriver = true; |
||
65 | |||
66 | private BasicDataSource dataSource = null; |
||
67 | |||
68 | private final MySQLConnectionParameters connectionParameters; |
||
69 | |||
70 | public ConnectionProvider(MySQLConnectionParameters connectionParameters) {
|
||
71 | this.connectionParameters = connectionParameters;
|
||
72 | } |
||
73 | |||
74 | public Connection getConnection() throws SQLException { |
||
75 | if (this.dataSource == null) { |
||
76 | this.dataSource = this.createDataSource(); |
||
77 | } |
||
78 | Connection conn = this.dataSource.getConnection(); |
||
79 | return conn;
|
||
80 | } |
||
81 | |||
82 | private BasicDataSource createDataSource() throws SQLException { |
||
83 | if (!this.isRegistered()) { |
||
84 | this.registerDriver();
|
||
85 | } |
||
86 | JDBCConnectionParameters params = connectionParameters; |
||
87 | |||
88 | BasicDataSource dataSource = new BasicDataSource();
|
||
89 | dataSource.setDriverClassName(params.getJDBCDriverClassName()); |
||
90 | |||
91 | dataSource.setUsername(params.getUser()); |
||
92 | dataSource.setPassword(params.getPassword()); |
||
93 | dataSource.setUrl(params.getUrl()); |
||
94 | |||
95 | dataSource.setMaxWait(60L * 1000); |
||
96 | return dataSource;
|
||
97 | } |
||
98 | |||
99 | private boolean isRegistered() { |
||
100 | return needRegisterDriver;
|
||
101 | } |
||
102 | |||
103 | public void registerDriver() throws SQLException { |
||
104 | String className = this.connectionParameters.getJDBCDriverClassName(); |
||
105 | if (className == null) { |
||
106 | return;
|
||
107 | } |
||
108 | try {
|
||
109 | Class theClass = Class.forName(className); |
||
110 | if (theClass == null) { |
||
111 | throw new JDBCDriverClassNotFoundException(MySQLLibrary.NAME, className); |
||
112 | } |
||
113 | } catch (Exception e) { |
||
114 | throw new SQLException("Can't register JDBC driver '" + className + "'.", e); |
||
115 | } |
||
116 | needRegisterDriver = false;
|
||
117 | } |
||
118 | |||
119 | } |
||
120 | |||
121 | private ConnectionProvider connectionProvider = null; |
||
122 | |||
123 | public MySQLHelper(JDBCConnectionParameters connectionParameters) {
|
||
124 | super(connectionParameters);
|
||
125 | } |
||
126 | |||
127 | @Override
|
||
128 | public Connection getConnection() throws AccessResourceException { |
||
129 | try {
|
||
130 | if (this.connectionProvider == null) { |
||
131 | this.connectionProvider = new ConnectionProvider(this.getConnectionParameters()); |
||
132 | } |
||
133 | return this.connectionProvider.getConnection(); |
||
134 | } catch (SQLException ex) { |
||
135 | throw new AccessResourceException(MySQLLibrary.NAME, ex); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | @Override
|
||
140 | public MySQLConnectionParameters getConnectionParameters() {
|
||
141 | return (MySQLConnectionParameters) super.getConnectionParameters(); |
||
142 | } |
||
143 | |||
144 | @Override
|
||
145 | public String getConnectionURL() { |
||
146 | return getConnectionURL(this.getConnectionParameters()); |
||
147 | } |
||
148 | |||
149 | @Override
|
||
150 | protected String getResourceType() { |
||
151 | return MySQLLibrary.NAME;
|
||
152 | } |
||
153 | |||
154 | @Override
|
||
155 | public String getProviderName() { |
||
156 | return MySQLLibrary.NAME;
|
||
157 | } |
||
158 | |||
159 | @Override
|
||
160 | public JDBCSQLBuilderBase createSQLBuilder() {
|
||
161 | return new MySQLSQLBuilder(this); |
||
162 | } |
||
163 | |||
164 | @Override
|
||
165 | public OperationsFactory getOperations() {
|
||
166 | if (this.operationsFactory == null) { |
||
167 | this.operationsFactory = new MySQLOperationsFactory(this); |
||
168 | } |
||
169 | return operationsFactory;
|
||
170 | } |
||
171 | |||
172 | @Override
|
||
173 | public SQLBuilder.GeometrySupportType getGeometrySupportType() {
|
||
174 | return SQLBuilder.GeometrySupportType.WKB;
|
||
175 | } |
||
176 | |||
177 | @Override
|
||
178 | public boolean hasSpatialFunctions() { |
||
179 | return true; |
||
180 | } |
||
181 | |||
182 | @Override
|
||
183 | public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
||
184 | return true; |
||
185 | } |
||
186 | |||
187 | @Override
|
||
188 | public String getQuoteForIdentifiers() { |
||
189 | return "\""; |
||
190 | } |
||
191 | |||
192 | @Override
|
||
193 | public boolean allowAutomaticValues() { |
||
194 | return true; |
||
195 | } |
||
196 | |||
197 | @Override
|
||
198 | public boolean supportOffsetInSelect() { |
||
199 | return true; |
||
200 | } |
||
201 | |||
202 | @Override
|
||
203 | public String getQuoteForStrings() { |
||
204 | return "'"; |
||
205 | } |
||
206 | |||
207 | @Override
|
||
208 | public String getSourceId(JDBCStoreParameters parameters) { |
||
209 | return parameters.getDBName() + "." + |
||
210 | parameters.getSchema()+ "." +
|
||
211 | parameters.getTable(); |
||
212 | } |
||
213 | |||
214 | @Override
|
||
215 | public JDBCNewStoreParameters createNewStoreParameters() {
|
||
216 | return new MySQLNewStoreParameters(); |
||
217 | } |
||
218 | |||
219 | @Override
|
||
220 | public JDBCStoreParameters createOpenStoreParameters() {
|
||
221 | return new MySQLStoreParameters(); |
||
222 | } |
||
223 | |||
224 | @Override
|
||
225 | public JDBCServerExplorerParameters createServerExplorerParameters() {
|
||
226 | return new MySQLExplorerParameters(); |
||
227 | } |
||
228 | |||
229 | } |